From fe0126b4935995967b4fa79844a9cc6662add462 Mon Sep 17 00:00:00 2001 From: em Date: Mon, 24 Apr 2017 21:21:34 +0200 Subject: [PATCH] implemented precondition os based strategy, implemented brew being mandatory, prepared 0.3.0 release, fixed upgrade check being run on first installation. Install osx libs using brew only --- Gemfile.lock | 8 +- VERSION | 2 +- docker-sync.gemspec | 1 + docker_sync.iml | 3 +- lib/docker-sync.rb | 2 +- lib/docker-sync/config/project_config.rb | 7 + lib/docker-sync/preconditions.rb | 112 ------------- .../preconditions/preconditions_linux.rb | 24 +++ .../preconditions/preconditions_osx.rb | 148 ++++++++++++++++++ lib/docker-sync/preconditions/strategy.rb | 57 +++++++ lib/docker-sync/sync_strategy/rsync.rb | 6 +- lib/docker-sync/sync_strategy/unison.rb | 6 +- lib/docker-sync/update_check.rb | 22 ++- lib/docker-sync/upgrade_check.rb | 21 ++- lib/docker-sync/watch_strategy/dummy.rb | 1 - lib/docker-sync/watch_strategy/fswatch.rb | 4 +- lib/docker-sync/watch_strategy/unison.rb | 1 - spec/lib/docker-sync/preconditions_spec.rb | 139 ++++++++++++++-- tasks/daemon/daemon.thor | 2 +- tasks/stack/stack.thor | 6 +- tasks/sync/sync.thor | 8 +- 21 files changed, 418 insertions(+), 162 deletions(-) delete mode 100644 lib/docker-sync/preconditions.rb create mode 100644 lib/docker-sync/preconditions/preconditions_linux.rb create mode 100644 lib/docker-sync/preconditions/preconditions_osx.rb create mode 100644 lib/docker-sync/preconditions/strategy.rb diff --git a/Gemfile.lock b/Gemfile.lock index 49c8eed1..c339a4bc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,11 +1,12 @@ PATH remote: . specs: - docker-sync (0.2.3) + docker-sync (0.3.0) daemons (~> 1.2, >= 1.2.3) docker-compose (~> 1.0, >= 1.0.2) dotenv (~> 2.1, >= 2.1.1) gem_update_checker (~> 0.2.0, >= 0.2.0) + os terminal-notifier (= 1.6.3) thor (~> 0.19, >= 0.19.0) @@ -15,10 +16,11 @@ GEM backticks (1.0.0) daemons (1.2.4) diff-lcs (1.3) - docker-compose (1.1.4) + docker-compose (1.1.5) backticks (~> 1.0) dotenv (2.2.0) gem_update_checker (0.2.0) + os (1.0.0) rspec (3.5.0) rspec-core (~> 3.5.0) rspec-expectations (~> 3.5.0) @@ -43,4 +45,4 @@ DEPENDENCIES rspec BUNDLED WITH - 1.13.7 + 1.14.6 diff --git a/VERSION b/VERSION index 71790396..0d91a54c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.3 +0.3.0 diff --git a/docker-sync.gemspec b/docker-sync.gemspec index e9478fab..8f2a4e0b 100644 --- a/docker-sync.gemspec +++ b/docker-sync.gemspec @@ -16,4 +16,5 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'terminal-notifier', '1.6.3' s.add_runtime_dependency 'dotenv', '~> 2.1', '>= 2.1.1' s.add_runtime_dependency 'daemons', '~> 1.2', '>= 1.2.3' + s.add_runtime_dependency 'os' end diff --git a/docker_sync.iml b/docker_sync.iml index 9bbaa4b4..082d1fd6 100644 --- a/docker_sync.iml +++ b/docker_sync.iml @@ -49,9 +49,10 @@ - + + diff --git a/lib/docker-sync.rb b/lib/docker-sync.rb index b1166f43..40d73694 100644 --- a/lib/docker-sync.rb +++ b/lib/docker-sync.rb @@ -1,4 +1,4 @@ require 'docker-sync/config/config_locator' require 'docker-sync/config/global_config' require 'docker-sync/config/project_config' -require 'docker-sync/preconditions' +require 'docker-sync/preconditions/strategy' diff --git a/lib/docker-sync/config/project_config.rb b/lib/docker-sync/config/project_config.rb index e59c786a..d3694674 100644 --- a/lib/docker-sync/config/project_config.rb +++ b/lib/docker-sync/config/project_config.rb @@ -48,6 +48,13 @@ def unison_required? } end + def rsync_required? + config['syncs'].any? { |name, sync_config| + sync_config['sync_strategy'] == 'rsync' + } + end + + private def validate_config! diff --git a/lib/docker-sync/preconditions.rb b/lib/docker-sync/preconditions.rb deleted file mode 100644 index c401caa5..00000000 --- a/lib/docker-sync/preconditions.rb +++ /dev/null @@ -1,112 +0,0 @@ -require 'mkmf' - -module Preconditions - def self.check_all_preconditions(config) - docker_available - docker_running - - if config.unison_required? - unison_available - unox_available - macfsevents_available - watchdog_available - end - end - - def self.docker_available - if (find_executable0 'docker').nil? - raise('Could not find docker binary in path. Please install it, e.g. using "brew install docker" or install docker-for-mac') - end - end - - def self.docker_running - `docker ps` - if $?.exitstatus > 0 - raise('No docker daemon seems to be running. Did you start your docker-for-mac / docker-machine?') - end - end - - def self.fswatch_available - if (find_executable0 'fswatch').nil? - raise('No fswatch available. Install it by "brew install fswatch"') - end - end - - def self.docker_sync_available - if (find_executable0 'docker-sync').nil? - raise('No docker-sync available. Install it by "gem install docker-sync"') - end - end - - def self.rsync_available - if (find_executable0 'rsync').nil? - raise('Could not find rsync binary in path. Please install it, e.g. using "brew install rsync"') - end - end - - def self.unison_available - if (find_executable0 'unison').nil? - raise('Could not find unison binary in path. Please install it, e.g. using "brew install unison"') - end - end - - def self.unox_available - if (find_executable0 'unison-fsmonitor').nil? - cmd1 = 'curl "https://raw.githubusercontent.com/hnsl/unox/master/unox.py" -o "/usr/local/bin/unison-fsmonitor" \ - && chmod +x /usr/local/bin/unison-fsmonitor' - - Thor::Shell::Basic.new.say_status 'warning', 'Could not find unison-fsmonitor (for file watching) binary in $PATH. We try to install unox now (for manual instracutions see https://github.com/hnsl/unox.)', :red - if Thor::Shell::Basic.new.yes?('Shall I install unison-fsmonitor for you? (y/N)') - system cmd1 - else - raise("Please install it, see https://github.com/hnsl/unox, or simply run :\n #{cmd1}") - end - end - - end - - def self.macfsevents_available - install_pip 'macfsevents', 'fsevents' - end - - def self.watchdog_available - install_pip 'watchdog' - end - - def self.install_pip(package, test = nil) - test ? `python -c 'import #{test}'` : `python -c 'import #{package}'` - - unless $?.success? - Thor::Shell::Basic.new.say_status 'warning', "Could not find #{package}. Will try to install it using pip", :red - if find_executable0('python') == '/usr/bin/python' - Thor::Shell::Basic.new.say_status 'ok', 'You seem to use the system python, we will need sudo below' - sudo = true - cmd2 = "sudo easy_install pip && sudo pip install #{package}" - else - Thor::Shell::Basic.new.say_status 'ok', 'You seem to have a custom python, using non-sudo commands' - sudo = false - cmd2 = "easy_install pip && pip install #{package}" - end - if sudo - question = "I will ask you for you root password to install #{package} by running (This will ask for sudo, since we use the system python)" - else - question = "I will now install #{package} for you by running" - end - - Thor::Shell::Basic.new.say_status 'info', "#{question}: `#{cmd2}\n\n" - if Thor::Shell::Basic.new.yes?('Shall I continue? (y/N)') - system cmd2 - if $?.exitstatus > 0 - raise("Failed to install #{package}, please file an issue with the output of the error") - end - test ? `python -c 'import #{test}'` : `python -c 'import #{package}'` - unless $?.success? - raise("Somehow I could not successfully install #{package} even though I tried. Please report this issue.") - end - else - raise("Please install #{package} manually, see https://github.com/EugenMayer/docker-sync/wiki/1.-Installation") - end - end - end - -end diff --git a/lib/docker-sync/preconditions/preconditions_linux.rb b/lib/docker-sync/preconditions/preconditions_linux.rb new file mode 100644 index 00000000..4167146c --- /dev/null +++ b/lib/docker-sync/preconditions/preconditions_linux.rb @@ -0,0 +1,24 @@ +module DockerSync + module Preconditions + class Linux + def check_all_preconditions(config) + end + + def docker_available + end + + def docker_running + end + + def fswatch_available + end + + def rsync_available + end + + def unison_available + end + end + + end +end diff --git a/lib/docker-sync/preconditions/preconditions_osx.rb b/lib/docker-sync/preconditions/preconditions_osx.rb new file mode 100644 index 00000000..0e3b194a --- /dev/null +++ b/lib/docker-sync/preconditions/preconditions_osx.rb @@ -0,0 +1,148 @@ +require 'mkmf' +module DockerSync + module Preconditions + class Osx + def check_all_preconditions(config) + return unless should_run_precondition? + + docker_available + docker_running + + if config.unison_required? + unison_available + end + + if config.rsync_required? + rsync_available + fswatch_available + end + end + + def docker_available + if (find_executable0 'docker').nil? + raise('Could not find docker binary in path. Please install it, e.g. using "brew install docker" or install docker-for-mac') + end + end + + def docker_running + `docker ps` + if $?.exitstatus > 0 + raise('No docker daemon seems to be running. Did you start your docker-for-mac / docker-machine?') + end + end + + def rsync_available + if should_run_precondition? + if (find_executable0 'rsync').nil? + raise('Could not find rsync binary in path. Please install it, e.g. using "brew install rsync"') + end + end + end + + def unison_available + if should_run_precondition? + if (find_executable0 'unison').nil? + cmd1 = 'brew install unison"' + + Thor::Shell::Basic.new.say_status 'warning', 'Could not find unison binary in $PATH. Trying to install now', :red + if Thor::Shell::Basic.new.yes?('I will install unison using brew for you? (y/N)') + system cmd1 + else + raise('Please install it yourself using: brew install unison') + end + end + + unox_available + end + end + + def fswatch_available + if should_run_precondition? + if (find_executable0 'fswatch').nil? + cmd1 = 'brew install fswatch"' + + Thor::Shell::Basic.new.say_status 'warning', 'No fswatch available. Install it by "brew install fswatch Trying to install now', :red + if Thor::Shell::Basic.new.yes?('I will install fswatch using brew for you? (y/N)') + system cmd1 + else + raise('Please install it yourself using: brew install fswatch') + end + end + end + + end + + private + + def should_run_precondition?(silent = false) + unless has_brew? + Thor::Shell::Basic.new.say_status 'inf', 'Not running any precondition checks since you have no brew and that is unsupported. Is all up to you know.', :white unless silent + return false + end + return true + end + + def has_brew? + return find_executable0 'brew' + end + + + def unox_available + if should_run_precondition? + `brew list unox` + if $?.exitstatus > 0 + unless (find_executable0 'unison-fsmonitor').nil? + # unox installed, but not using brew, we do not allow that anymore + Thor::Shell::Basic.new.say_status 'error', 'You install unison-fsmonitor (unox) not using brew. Please uninstall it and run docker-sync again, so we can install it for you', :red + exit 1 + end + cmd1 = 'brew tap eugenmayer/dockersync && brew install eugenmayer/dockersync/unox' + + Thor::Shell::Basic.new.say_status 'warning', 'Could not find unison-fsmonitor (unox) binary in $PATH. Trying to install now', :red + if Thor::Shell::Basic.new.yes?('I will install unox through brew for you? (y/N)') + system cmd1 + else + raise('Please install it yourself using: brew tap eugenmayer/dockersync && brew install unox') + end + end + end + end + + def install_pip(package, test = nil) + test ? `python -c 'import #{test}'` : `python -c 'import #{package}'` + + unless $?.success? + Thor::Shell::Basic.new.say_status 'warning', "Could not find #{package}. Will try to install it using pip", :red + if find_executable0('python') == '/usr/bin/python' + Thor::Shell::Basic.new.say_status 'ok', 'You seem to use the system python, we will need sudo below' + sudo = true + cmd2 = "sudo easy_install pip && sudo pip install #{package}" + else + Thor::Shell::Basic.new.say_status 'ok', 'You seem to have a custom python, using non-sudo commands' + sudo = false + cmd2 = "easy_install pip && pip install #{package}" + end + if sudo + question = "I will ask you for you root password to install #{package} by running (This will ask for sudo, since we use the system python)" + else + question = "I will now install #{package} for you by running" + end + + Thor::Shell::Basic.new.say_status 'info', "#{question}: `#{cmd2}\n\n" + if Thor::Shell::Basic.new.yes?('Shall I continue? (y/N)') + system cmd2 + if $?.exitstatus > 0 + raise("Failed to install #{package}, please file an issue with the output of the error") + end + test ? `python -c 'import #{test}'` : `python -c 'import #{package}'` + unless $?.success? + raise("Somehow I could not successfully install #{package} even though I tried. Please report this issue.") + end + else + raise("Please install #{package} manually, see https://github.com/EugenMayer/docker-sync/wiki/1.-Installation") + end + end + end + end + end +end diff --git a/lib/docker-sync/preconditions/strategy.rb b/lib/docker-sync/preconditions/strategy.rb new file mode 100644 index 00000000..9dfff6da --- /dev/null +++ b/lib/docker-sync/preconditions/strategy.rb @@ -0,0 +1,57 @@ +require 'docker-sync/preconditions/preconditions_osx' +require 'docker-sync/preconditions/preconditions_linux' + +require 'singleton' +require 'os' + +module DockerSync + module Preconditions + class Strategy + include Singleton + + attr_accessor :strategy + + def initialize + if DockerSync::Preconditions::Strategy.is_osx + @strategy = DockerSync::Preconditions::Osx.new + elsif DockerSync::Preconditions::Strategy.is_linux + @strategy = DockerSync::Preconditions::Linux.new + end + end + + def self.is_osx + return OS.mac? + end + + def self.is_linux + return OS.linux? + end + + def check_all_preconditions(config) + strategy.check_all_preconditions(config) + end + + def docker_available + strategy.docker_available + end + + def docker_running + strategy.docker_running + end + + def rsync_available + strategy.rsync_available + end + + + def fswatch_available + strategy.fswatch_available + end + + def unison_available + strategy.unison_available + end + end + end +end + diff --git a/lib/docker-sync/sync_strategy/rsync.rb b/lib/docker-sync/sync_strategy/rsync.rb index b6c63260..26584fbd 100644 --- a/lib/docker-sync/sync_strategy/rsync.rb +++ b/lib/docker-sync/sync_strategy/rsync.rb @@ -1,5 +1,5 @@ require 'thor/shell' -require 'docker-sync/preconditions' +require 'docker-sync/preconditions/strategy' require 'terminal-notifier' module Docker_Sync @@ -22,9 +22,9 @@ def initialize(sync_name, options) end begin - Preconditions::rsync_available + DockerSync::Preconditions::Strategy.instance.rsync_available rescue Exception => e - say_status 'error', "#{@sync_name} has been configured to sync with rsync, but no rsync binary available", :red + say_status 'error', "#{@sync_name} has been configured to sync with rsync, but no rsync or fswatch binary available", :red say_status 'error', e.message, :red exit 1 end diff --git a/lib/docker-sync/sync_strategy/unison.rb b/lib/docker-sync/sync_strategy/unison.rb index 8a4d9ef1..a45af05d 100644 --- a/lib/docker-sync/sync_strategy/unison.rb +++ b/lib/docker-sync/sync_strategy/unison.rb @@ -1,5 +1,5 @@ require 'thor/shell' -require 'docker-sync/preconditions' +require 'docker-sync/preconditions/strategy' require 'docker-sync/execution' require 'open3' require 'socket' @@ -26,9 +26,7 @@ def initialize(sync_name, options) @docker_image = 'eugenmayer/unison' end begin - Preconditions::unison_available - Preconditions::unox_available - Preconditions::macfsevents_available + DockerSync::Preconditions::Strategy.instance.unison_available rescue Exception => e say_status 'error', "#{@sync_name} has been configured to sync with unison, but no unison available", :red say_status 'error', e.message, :red diff --git a/lib/docker-sync/update_check.rb b/lib/docker-sync/update_check.rb index ede5c7ab..7003f34a 100644 --- a/lib/docker-sync/update_check.rb +++ b/lib/docker-sync/update_check.rb @@ -23,21 +23,27 @@ def run # do not check the image if its the first run - since this it will be downloaded anyway unless @config.first_run? - check_rsync_image - check_unison_image - - # stop if there was an update - if @newer_image_found - say_status 'warning', 'One or more images have been updated. Please use "docker-sync clean" before you start docker-sync again', :red - exit 0 + unless has_internet? + check_unison_image + check_rsync_image + # stop if there was an update + if @newer_image_found + say_status 'warning', 'One or more images have been updated. Please use "docker-sync clean" before you start docker-sync again', :red + exit 0 + end end - end check_and_warn(@config['update_enforce']) end + def has_internet? + `ping -c1 -t 1 8.8.8.8 &2>1 /dev/null` + return $?.success? + end + def should_run + return false unless has_internet? now = DateTime.now last_check = DateTime.iso8601(@config['update_last_check']) check_after_days = 2 diff --git a/lib/docker-sync/upgrade_check.rb b/lib/docker-sync/upgrade_check.rb index 050a67ac..7b68b6ee 100644 --- a/lib/docker-sync/upgrade_check.rb +++ b/lib/docker-sync/upgrade_check.rb @@ -23,7 +23,12 @@ def last_upgraded_version def should_run # get the update_status which is the version of the update hook which has been run already upgrade_status = last_upgraded_version - if upgrade_status == '' || Gem::Version.new(upgrade_status) < Gem::Version.new(UpgradeChecker.get_current_version) # thats how we compare the version + if upgrade_status == '' + @config.update! 'upgrade_status' => "#{UpgradeChecker.get_current_version}" + return + end + + if Gem::Version.new(upgrade_status) < Gem::Version.new(UpgradeChecker.get_current_version) # thats how we compare the version return true end @@ -59,6 +64,20 @@ def check_and_warn end end + if Gem::Version.new(last_upgraded_version) < Gem::Version.new('0.3.0') + Thor::Shell::Basic.new.say_status 'warning', "The installation progress of docker-sync 0.3.0 has changed, brew is now mandatory - you need to uninstall docker-sync/unox/unison ! : \n\n_Please_ read :): https://github.com/EugenMayer/docker-sync/wiki/1.3-Upgrade-Guide\n\n", :red + + cmd1 = 'rm -f /usr/loca/bin/unison-fsmonitor && gem uninstall docker-sync && brew tap eugenmayer/dockersync && brew install eugenmayer/dockersync/dockersync"' + Thor::Shell::Basic.new.say_status 'ok', "The installation progress of docker-sync 0.3.0 has changed, brew is now mandatory - you need to uninstall docker-sync/unox/unison ! : \n\n_Please_ read :): https://github.com/EugenMayer/docker-sync/wiki/1.3-Upgrade-Guide\n\n", :rwhite + + if Thor::Shell::Basic.new.yes?('I will install reinstall docker-sync for you using the above command (y/N)') + system cmd1 + else + raise('Please reinstall docker-sync yourself') + exit 1 + end + end + # update the upgrade_status @config.update! 'upgrade_status' => "#{UpgradeChecker.get_current_version}" diff --git a/lib/docker-sync/watch_strategy/dummy.rb b/lib/docker-sync/watch_strategy/dummy.rb index b8e42af4..c732d239 100644 --- a/lib/docker-sync/watch_strategy/dummy.rb +++ b/lib/docker-sync/watch_strategy/dummy.rb @@ -1,6 +1,5 @@ require 'thor/shell' require 'docker-sync/execution' -require 'docker-sync/preconditions' module Docker_Sync module WatchStrategy diff --git a/lib/docker-sync/watch_strategy/fswatch.rb b/lib/docker-sync/watch_strategy/fswatch.rb index 93dd2e42..150e7842 100644 --- a/lib/docker-sync/watch_strategy/fswatch.rb +++ b/lib/docker-sync/watch_strategy/fswatch.rb @@ -1,6 +1,6 @@ require 'thor/shell' require 'docker-sync/execution' -require 'docker-sync/preconditions' +require 'docker-sync/preconditions/strategy' require 'pathname' module Docker_Sync @@ -19,7 +19,7 @@ def initialize(sync_name, options) @events_to_watch = %w(AttributeModified Created Link MovedFrom MovedTo Renamed Removed Updated) begin - Preconditions::fswatch_available + DockerSync::Preconditions::Strategy.instance.fswatch_available rescue Exception => e say_status 'error', e.message, :red exit 1 diff --git a/lib/docker-sync/watch_strategy/unison.rb b/lib/docker-sync/watch_strategy/unison.rb index 2257ed9a..c034fdcb 100644 --- a/lib/docker-sync/watch_strategy/unison.rb +++ b/lib/docker-sync/watch_strategy/unison.rb @@ -1,6 +1,5 @@ require 'thor/shell' require 'docker-sync/execution' -require 'docker-sync/preconditions' require 'docker-sync/sync_strategy/unison' module Docker_Sync diff --git a/spec/lib/docker-sync/preconditions_spec.rb b/spec/lib/docker-sync/preconditions_spec.rb index 107f7857..4b291e6c 100644 --- a/spec/lib/docker-sync/preconditions_spec.rb +++ b/spec/lib/docker-sync/preconditions_spec.rb @@ -1,31 +1,138 @@ -describe Preconditions do +require 'docker-sync/preconditions/strategy' +require 'docker-sync/preconditions/preconditions_osx' +require 'docker-sync/sync_strategy/unison' +require 'pp' +describe DockerSync::Preconditions::Strategy do + specify '.instance always refers to the same instance' do + expect(DockerSync::Preconditions::Strategy.instance).to be_a_kind_of DockerSync::Preconditions::Strategy + expect(DockerSync::Preconditions::Strategy.instance).to equal DockerSync::Preconditions::Strategy.instance + end + + + subject { + Singleton.__init__(DockerSync::Preconditions::Strategy) + described_class.instance + } describe '#check_all_preconditions' do - context 'with simplest config' do + + context 'with unison config' do it do - expect(described_class).to receive(:docker_available) - expect(described_class).to receive(:docker_running) - expect(described_class).to receive(:unison_available) - expect(described_class).to receive(:unox_available) - expect(described_class).to receive(:macfsevents_available) - expect(described_class).to receive(:watchdog_available) + #expect(subject).to receive(:unison_available) + allow(subject.strategy).to receive(:unison_available) { true } + expect(subject).to receive(:unison_available) use_fixture 'simplest' do - described_class.check_all_preconditions(load_config) + config = load_config + Docker_Sync::SyncStrategy::Unison.new('simplest-sync',config['syncs']['simplest-sync']) + end + end + end + + context 'with check all unison precondition' do + it do + allow(subject.strategy).to receive(:should_run_precondition?) { true } + allow(subject.strategy).to receive(:docker_available) { true } + allow(subject.strategy).to receive(:docker_running) { true } + allow(subject.strategy).to receive(:unison_available) { true } + #allow(subject.strategy).to receive(:unox_available) { true } + + expect(subject.strategy).to receive(:docker_available) + expect(subject.strategy).to receive(:docker_running) + expect(subject.strategy).to receive(:unison_available) + #expect(described_class.instance.strategy).to receive(:unox_available) + + use_fixture 'simplest' do + subject.check_all_preconditions(load_config) + end + end + end + + context 'if osx, osx strategy is picked' do + it do + allow(DockerSync::Preconditions::Strategy).to receive(:is_osx) { true } + Singleton.__init__(DockerSync::Preconditions::Strategy) + expect(subject.strategy).to be_a(DockerSync::Preconditions::Osx) + end + end + + context 'if linux, linux strategy is picked' do + it do + allow(DockerSync::Preconditions::Strategy).to receive(:is_osx) { false } + allow(DockerSync::Preconditions::Strategy).to receive(:is_linux) { true } + Singleton.__init__(DockerSync::Preconditions::Strategy) + expect(subject.strategy).to be_a(DockerSync::Preconditions::Linux) + end + end + + context 'on osx without brew, skip precondition tests' do + it do + allow(DockerSync::Preconditions::Strategy).to receive(:is_osx) { true } + Singleton.__init__(DockerSync::Preconditions::Strategy) + expect(subject.strategy).to be_a(DockerSync::Preconditions::Osx) + + Singleton.__init__(DockerSync::Preconditions::Strategy) + allow(subject.strategy).to receive(:has_brew?) { false } + + expect(subject.strategy.send(:should_run_precondition?)).to eq(false) + end + end + + + context 'on osx with brew, have precondition tests' do + it do + allow(DockerSync::Preconditions::Strategy).to receive(:is_osx) { true } + Singleton.__init__(DockerSync::Preconditions::Strategy) + expect(subject.strategy).to be_a(DockerSync::Preconditions::Osx) + + Singleton.__init__(DockerSync::Preconditions::Strategy) + allow(subject.strategy).to receive(:has_brew?) { true } + + expect(subject.strategy.send(:should_run_precondition?)).to eq(true) + end + end + + context 'without docker installed, do raise an exception' do + it do + + Singleton.__init__(DockerSync::Preconditions::Strategy) + + allow(subject.strategy).to receive(:docker_available).and_raise('docker not here') + allow(subject.strategy).to receive(:should_run_precondition?) { true } + allow(subject.strategy).to receive(:has_brew?) { true } + + use_fixture 'simplest' do + expect { subject.check_all_preconditions(load_config) }.to raise_error('docker not here') + end + end + end + + context 'without docker running, do raise an exception' do + it do + Singleton.__init__(DockerSync::Preconditions::Strategy) + + allow(subject.strategy).to receive(:docker_available) {true} + allow(subject.strategy).to receive(:docker_running).and_raise('docker stopped') + + use_fixture 'simplest' do + expect { subject.check_all_preconditions(load_config) }.to raise_error('docker stopped') end end end context 'with rsync config' do it do - expect(described_class).to receive(:docker_available) - expect(described_class).to receive(:docker_running) - expect(described_class).not_to receive(:unison_available) - expect(described_class).not_to receive(:unox_available) - expect(described_class).not_to receive(:macfsevents_available) - expect(described_class).not_to receive(:watchdog_available) + allow(subject.strategy).to receive(:should_run_precondition?) { true } + allow(subject.strategy).to receive(:docker_available) { true } + allow(subject.strategy).to receive(:docker_running) { true } + allow(subject.strategy).to receive(:rsync_available) { true } + + expect(subject.strategy).to receive(:docker_available) + expect(subject.strategy).to receive(:docker_running) + expect(subject.strategy).not_to receive(:unison_available) + expect(subject.strategy).to receive(:rsync_available) use_fixture 'rsync' do - described_class.check_all_preconditions(load_config) + subject.check_all_preconditions(load_config) end end end diff --git a/tasks/daemon/daemon.thor b/tasks/daemon/daemon.thor index e047b9b1..8e9d6566 100644 --- a/tasks/daemon/daemon.thor +++ b/tasks/daemon/daemon.thor @@ -1,6 +1,6 @@ require 'docker-sync' require 'docker-sync/sync_manager' -require 'docker-sync/preconditions' +require 'docker-sync/preconditions/strategy' require 'docker-sync/update_check' require 'docker-sync/upgrade_check' require 'daemons' diff --git a/tasks/stack/stack.thor b/tasks/stack/stack.thor index d1826c3f..f444a73d 100644 --- a/tasks/stack/stack.thor +++ b/tasks/stack/stack.thor @@ -1,6 +1,6 @@ require 'docker-sync' require 'docker-sync/sync_manager' -require 'docker-sync/preconditions' +require 'docker-sync/preconditions/strategy' require 'docker-sync/update_check' require 'docker-sync/upgrade_check' require 'docker/compose' @@ -29,7 +29,7 @@ class Stack < Thor begin config = DockerSync::ProjectConfig.new(config_path: options[:config]) - Preconditions::check_all_preconditions(config) + DockerSync::Preconditions::Strategy.instance.check_all_preconditions(config) rescue Exception => e say_status 'error', e.message, :red exit(1) @@ -70,7 +70,7 @@ class Stack < Thor begin config = DockerSync::ProjectConfig.new(config_path: options[:config]) - Preconditions::check_all_preconditions(config) + DockerSync::Preconditions::Strategy.instance.check_all_preconditions(config) rescue Exception => e say_status 'error', e.message, :red exit(1) diff --git a/tasks/sync/sync.thor b/tasks/sync/sync.thor index 560315aa..0bb21099 100644 --- a/tasks/sync/sync.thor +++ b/tasks/sync/sync.thor @@ -1,6 +1,6 @@ require 'docker-sync' require 'docker-sync/sync_manager' -require 'docker-sync/preconditions' +require 'docker-sync/preconditions/strategy' require 'docker-sync/update_check' require 'docker-sync/upgrade_check' require 'daemons' @@ -29,8 +29,8 @@ class Sync < Thor def start print_version if options[:version] # do run update check in the start command only - UpdateChecker.new().run - UpgradeChecker.new().run + UpdateChecker.new.run + UpgradeChecker.new.run config = config_preconditions @sync_manager = Docker_sync::SyncManager.new(config: config) @@ -132,7 +132,7 @@ class Sync < Thor def config_preconditions # Moved shared preconditions block into separate method to have less/cleaner code begin DockerSync::ProjectConfig.new(config_path: options[:config]).tap do |config| - Preconditions::check_all_preconditions(config) + DockerSync::Preconditions::Strategy.instance.check_all_preconditions(config) end rescue Exception => e say_status 'error', e.message, :red