From 61780340fd97ffb924ae03dea203a9052cac26df Mon Sep 17 00:00:00 2001 From: John Barnette Date: Tue, 2 Oct 2012 14:33:25 -0700 Subject: [PATCH] Add preflights and postflights --- Gemfile.lock | 2 ++ boxen.gemspec | 1 + lib/boxen.rb | 8 ++++--- lib/boxen/check.rb | 6 +++++ lib/boxen/flags.rb | 2 +- lib/boxen/postflight.rb | 13 +++++++++++ lib/boxen/postflight/active.rb | 16 +++++++++++++ lib/boxen/postflight/env.rb | 29 ++++++++++++++++++++++++ lib/boxen/preflight.rb | 13 +++++++++++ lib/boxen/preflight/creds.rb | 34 ++++++++++++++++++++++++++++ lib/boxen/preflight/etc_my_cnf.rb | 12 ++++++++++ lib/boxen/preflight/homebrew.rb | 13 +++++++++++ lib/boxen/preflight/identity.rb | 16 +++++++++++++ lib/boxen/preflight/os.rb | 11 +++++++++ lib/boxen/preflight/rbenv.rb | 12 ++++++++++ test/boxen_postflight_active_test.rb | 29 ++++++++++++++++++++++++ test/boxen_postflight_env_test.rb | 6 +++++ 17 files changed, 219 insertions(+), 4 deletions(-) create mode 100644 lib/boxen/postflight.rb create mode 100644 lib/boxen/postflight/active.rb create mode 100644 lib/boxen/postflight/env.rb create mode 100644 lib/boxen/preflight.rb create mode 100644 lib/boxen/preflight/creds.rb create mode 100644 lib/boxen/preflight/etc_my_cnf.rb create mode 100644 lib/boxen/preflight/homebrew.rb create mode 100644 lib/boxen/preflight/identity.rb create mode 100644 lib/boxen/preflight/os.rb create mode 100644 lib/boxen/preflight/rbenv.rb create mode 100644 test/boxen_postflight_active_test.rb create mode 100644 test/boxen_postflight_env_test.rb diff --git a/Gemfile.lock b/Gemfile.lock index d1dd8c9..eb61f62 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,6 +3,7 @@ PATH specs: boxen (0.0.0) ansi + highline json_pure octokit puppet @@ -20,6 +21,7 @@ GEM hashie (1.2.0) hiera (1.1.0.rc1) json + highline (1.6.15) json (1.7.5) json_pure (1.7.5) metaclass (0.0.1) diff --git a/boxen.gemspec b/boxen.gemspec index ac37f28..0cc02bc 100644 --- a/boxen.gemspec +++ b/boxen.gemspec @@ -14,6 +14,7 @@ Gem::Specification.new do |gem| gem.require_paths = ["lib"] gem.add_dependency "ansi" + gem.add_dependency "highline" gem.add_dependency "json_pure" gem.add_dependency "octokit" gem.add_dependency "puppet" diff --git a/lib/boxen.rb b/lib/boxen.rb index bc84e33..11387c2 100644 --- a/lib/boxen.rb +++ b/lib/boxen.rb @@ -1,5 +1,7 @@ require "boxen/config" require "boxen/flags" +require "boxen/preflight" +require "boxen/postflight" module Boxen @@ -24,11 +26,11 @@ module Boxen # Run the preflight checks. - #Boxen::Preflight.run config + Boxen::Preflight.run config # Save the config for Puppet (and next time). - #Boxen::Config.save config + Boxen::Config.save config # Make the magic happen. @@ -36,7 +38,7 @@ module Boxen # Run the postflight checks. - #Boxen::Postflight.run config + Boxen::Postflight.run config if code.zero? p :config => config diff --git a/lib/boxen/check.rb b/lib/boxen/check.rb index 32d7668..4e253e3 100644 --- a/lib/boxen/check.rb +++ b/lib/boxen/check.rb @@ -16,6 +16,12 @@ module Boxen map { |c| c.new config } end + # Search `dir` and load all Ruby files under it. + + def self.register(dir) + Dir["#{dir}/*.rb"].sort.each { |f| load f } + end + # Check each instance against `config`. def self.run(config) diff --git a/lib/boxen/flags.rb b/lib/boxen/flags.rb index 06c9a42..fb222c8 100644 --- a/lib/boxen/flags.rb +++ b/lib/boxen/flags.rb @@ -132,7 +132,7 @@ module Boxen end # Parse `args` as an array of CLI argument Strings. Raises - # Setup::Error if anything goes wrong. Returns `self`. + # Boxen::Error if anything goes wrong. Returns `self`. def parse(*args) @args = @options.parse! args.flatten.compact.map(&:to_s) diff --git a/lib/boxen/postflight.rb b/lib/boxen/postflight.rb new file mode 100644 index 0000000..4c6e3d0 --- /dev/null +++ b/lib/boxen/postflight.rb @@ -0,0 +1,13 @@ +require "boxen/check" + +module Boxen + + # The superclass for postflight checks. + + class Postflight < Boxen::Check + + # Load all available postflight checks. + + register File.expand_path("../postflight", __FILE__) + end +end diff --git a/lib/boxen/postflight/active.rb b/lib/boxen/postflight/active.rb new file mode 100644 index 0000000..f233e0e --- /dev/null +++ b/lib/boxen/postflight/active.rb @@ -0,0 +1,16 @@ +require "boxen" +require "boxen/postflight" + +# Checks to see if the basic environment is loaded. + +class Boxen::Postflight::Active < Boxen::Postflight + def ok? + Boxen.active? + end + + def run + warn "You haven't loaded Boxen's environment yet!", + "To permanently fix this, source #{config.envfile} at the end", + "of your shell's startup file." + end +end diff --git a/lib/boxen/postflight/env.rb b/lib/boxen/postflight/env.rb new file mode 100644 index 0000000..17981e3 --- /dev/null +++ b/lib/boxen/postflight/env.rb @@ -0,0 +1,29 @@ +require "boxen/postflight" + +class Boxen::Postflight::Env < Boxen::Postflight + + # Calculate an MD5 checksum for the current environment. + + def self.checksum + + # We can't get this from config 'cause it's static (gotta happen + # on load), and GH_HOME might not be set. + + home = ENV["BOXEN_HOME"] || "/opt/boxen" + return unless File.file? "#{home}/env.sh" + + `find #{home}/env* -type f 2>&1 | sort | xargs /sbin/md5 | /sbin/md5 -q`.strip + end + + # The checksum when this file was loaded. + + CHECKSUM = self.checksum + + def ok? + self.class.checksum == CHECKSUM + end + + def run + warn "Source #{config.envfile} or restart your shell for new stuff!" + end +end diff --git a/lib/boxen/preflight.rb b/lib/boxen/preflight.rb new file mode 100644 index 0000000..af60a0d --- /dev/null +++ b/lib/boxen/preflight.rb @@ -0,0 +1,13 @@ +require "boxen/check" + +module Boxen + + # The superclass for preflight checks. + + class Preflight < Boxen::Check + + # Load all available preflight checks. + + register File.expand_path("../preflight", __FILE__) + end +end diff --git a/lib/boxen/preflight/creds.rb b/lib/boxen/preflight/creds.rb new file mode 100644 index 0000000..331e2c6 --- /dev/null +++ b/lib/boxen/preflight/creds.rb @@ -0,0 +1,34 @@ +require "highline" +require "boxen/preflight" + +# HACK: Unless this is `false`, HighLine has some really bizarre +# problems with empty/expended streams at bizarre intervals. + +HighLine.track_eof = false + +class Boxen::Preflight::Creds < Boxen::Preflight + def ok? + config.api.user rescue nil + end + + def run + console = HighLine.new + + warn "Hey, I need your current GitHub credentials to continue." + + config.login = console.ask "GitHub login: " do |q| + q.default = config.login || config.user + end + + config.password = console.ask "GitHub password: " do |q| + q.echo = "*" + end + + unless ok? + puts # i <3 vertical whitespace + + abort "Sorry, I can't auth you on GitHub.", + "Please check your credentials and teams and give it another try." + end + end +end diff --git a/lib/boxen/preflight/etc_my_cnf.rb b/lib/boxen/preflight/etc_my_cnf.rb new file mode 100644 index 0000000..ec7a43f --- /dev/null +++ b/lib/boxen/preflight/etc_my_cnf.rb @@ -0,0 +1,12 @@ +require "boxen/preflight" + +class Boxen::Preflight::EtcMyCnf < Boxen::Preflight + def run + abort "You have an /etc/my.cnf file.", + "This will confuse The Setup's MySQL a lot. Please remove it." + end + + def ok? + !File.file? "/etc/my.cnf" + end +end diff --git a/lib/boxen/preflight/homebrew.rb b/lib/boxen/preflight/homebrew.rb new file mode 100644 index 0000000..4c86062 --- /dev/null +++ b/lib/boxen/preflight/homebrew.rb @@ -0,0 +1,13 @@ +require "boxen/preflight" + +class Boxen::Preflight::Homebrew < Boxen::Preflight + def run + warn "You have an existing Homebrew install in /usr/local", + "The Boxen provides its own Homebrew, so consider deleting yours.", + "Keeping both will confuse many projects." + end + + def ok? + !File.exist? "/usr/local/Library/Homebrew" + end +end diff --git a/lib/boxen/preflight/identity.rb b/lib/boxen/preflight/identity.rb new file mode 100644 index 0000000..915b69c --- /dev/null +++ b/lib/boxen/preflight/identity.rb @@ -0,0 +1,16 @@ +require "boxen/preflight" + +class Boxen::Preflight::Identity < Boxen::Preflight + def ok? + !user || (config.email && config.name) + end + + def run + config.email = user.email + config.name = user.name + end + + def user + @user ||= config.api.user rescue nil + end +end diff --git a/lib/boxen/preflight/os.rb b/lib/boxen/preflight/os.rb new file mode 100644 index 0000000..6ecf414 --- /dev/null +++ b/lib/boxen/preflight/os.rb @@ -0,0 +1,11 @@ +require "boxen/preflight" + +class Boxen::Preflight::OS < Boxen::Preflight + def ok? + `sw_vers -productVersion`.start_with? "10.8" + end + + def run + abort "You must be running OS X 10.8 (Mountain Lion)." + end +end diff --git a/lib/boxen/preflight/rbenv.rb b/lib/boxen/preflight/rbenv.rb new file mode 100644 index 0000000..a718416 --- /dev/null +++ b/lib/boxen/preflight/rbenv.rb @@ -0,0 +1,12 @@ +require "boxen/preflight" + +class Boxen::Preflight::Rbenv < Boxen::Preflight + def run + warn "You have an existing rbenv installed in ~/.rbenv.", + "Boxen provides its own rbenv, so consider deleting yours." + end + + def ok? + !File.exist? "#{ENV['HOME']}/.rbenv" + end +end diff --git a/test/boxen_postflight_active_test.rb b/test/boxen_postflight_active_test.rb new file mode 100644 index 0000000..a5e872a --- /dev/null +++ b/test/boxen_postflight_active_test.rb @@ -0,0 +1,29 @@ +require "boxen/test" +require "boxen/postflight" + +class BoxenPostflightActiveTest < Boxen::Test + def setup + @check = Boxen::Postflight::Active.new :config + end + + def test_ok? + Boxen.expects(:active?).returns true + assert @check.ok? + end + + def test_ok_bad + Boxen.expects(:active?).returns false + refute @check.ok? + end + + def test_run + config = stub :envfile => "foo" + @check = Boxen::Postflight::Active.new config + + stdout, stderr = capture_io do + @check.run + end + + assert_match "loaded", stderr + end +end diff --git a/test/boxen_postflight_env_test.rb b/test/boxen_postflight_env_test.rb new file mode 100644 index 0000000..c6de198 --- /dev/null +++ b/test/boxen_postflight_env_test.rb @@ -0,0 +1,6 @@ +require "boxen/test" +require "boxen/postflight" + +class BoxenPostflightEnvTest < Boxen::Test + +end