mirror of
https://github.com/zhigang1992/boxen.git
synced 2026-01-12 17:12:46 +08:00
Add preflights and postflights
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
13
lib/boxen/postflight.rb
Normal file
13
lib/boxen/postflight.rb
Normal file
@@ -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
|
||||
16
lib/boxen/postflight/active.rb
Normal file
16
lib/boxen/postflight/active.rb
Normal file
@@ -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
|
||||
29
lib/boxen/postflight/env.rb
Normal file
29
lib/boxen/postflight/env.rb
Normal file
@@ -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
|
||||
13
lib/boxen/preflight.rb
Normal file
13
lib/boxen/preflight.rb
Normal file
@@ -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
|
||||
34
lib/boxen/preflight/creds.rb
Normal file
34
lib/boxen/preflight/creds.rb
Normal file
@@ -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
|
||||
12
lib/boxen/preflight/etc_my_cnf.rb
Normal file
12
lib/boxen/preflight/etc_my_cnf.rb
Normal file
@@ -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
|
||||
13
lib/boxen/preflight/homebrew.rb
Normal file
13
lib/boxen/preflight/homebrew.rb
Normal file
@@ -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
|
||||
16
lib/boxen/preflight/identity.rb
Normal file
16
lib/boxen/preflight/identity.rb
Normal file
@@ -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
|
||||
11
lib/boxen/preflight/os.rb
Normal file
11
lib/boxen/preflight/os.rb
Normal file
@@ -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
|
||||
12
lib/boxen/preflight/rbenv.rb
Normal file
12
lib/boxen/preflight/rbenv.rb
Normal file
@@ -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
|
||||
29
test/boxen_postflight_active_test.rb
Normal file
29
test/boxen_postflight_active_test.rb
Normal file
@@ -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
|
||||
6
test/boxen_postflight_env_test.rb
Normal file
6
test/boxen_postflight_env_test.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
require "boxen/test"
|
||||
require "boxen/postflight"
|
||||
|
||||
class BoxenPostflightEnvTest < Boxen::Test
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user