forked from kitchenplan/kitchenplan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kitchenplan
executable file
·140 lines (119 loc) · 4.24 KB
/
kitchenplan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env ruby
# Run Kitchenplan.
module Tty extend self
def blue; bold 34; end
def white; bold 39; end
def red; underline 31; end
def reset; escape 0; end
def bold n; escape "1;#{n}" end
def underline n; escape "4;#{n}" end
def escape n; "\033[#{n}m" if STDOUT.tty? end
end
class Array
def shell_s
cp = dup
first = cp.shift
cp.map{ |arg| arg.gsub " ", "\\ " }.unshift(first) * " "
end
end
def ohai *args
puts "#{Tty.blue}==>#{Tty.white} #{args.shell_s}#{Tty.reset}"
end
def warn warning
puts "#{Tty.red}Warning#{Tty.reset}: #{warning.chomp}"
end
def system *args
abort "Failed during: #{args.shell_s}" unless Kernel.system *args
end
def warnandexit message
warn message
exit
end
def sudo *args
args = if args.length > 1
args.unshift "/usr/bin/sudo"
else
"/usr/bin/sudo #{args.first}"
end
ohai *args
system *args
end
def normaldo *args
ohai *args
system *args
end
def macos_version
@macos_version ||= `/usr/bin/sw_vers -productVersion`.chomp[/10\.\d+/]
end
########################################
abort "OSX too old, you need at least Mountain Lion" if macos_version < "10.8"
abort "Don't run this as root!" if Process.uid == 0
abort <<-EOABORT unless `groups`.split.include? "admin"
This script requires the user #{ENV['USER']} to be an Administrator.
EOABORT
# Put us where we belong, in the root dir of our kitchenplan repo.
require 'pathname'
Dir.chdir Pathname.new(__FILE__).realpath + ".."
# Flags
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = 'Usage: kitchenplan [options]'
opts.on("-d", "--debug", "Show debug information") do |debug|
options[:debug] = debug
end
opts.on("-c", "--update-cookbooks", "Update the Chef cookbooks") do |update_cookbooks|
options[:update_cookbooks] = update_cookbooks
end
options[:chef] = true
opts.on("--[no-]chef", "Run chef (defaults to yes)") do |chef|
options[:chef] = chef
end
options[:recipes] = false
opts.on("--recipes x,y,z", Array, "Run Kitchenplan with all attributes, but only the recipes passed along on the command line. Useful for testing or fast runs.") do |list|
options[:recipes] = list
end
opts.separator ""
opts.separator "Common options:"
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail("--version", "Show version") do
puts "1.0.1"
exit
end
end.parse!
# Bootstrapping dependencies
sudo "sudo gem install bundler --no-rdoc --no-ri" unless Kernel.system "gem query --name-matches '^bundler$' --installed > /dev/null 2>&1"
sudo "bundle install --binstubs=bin #{(options[:debug] ? '--verbose' : '--quiet')}"
# Add local deps to the load path.
require "rubygems"
require "bundler/setup"
require 'json'
# Trying to get some metrics for usage, just comment out if you don't want it.
ohai 'Sending a ping to Google Analytics to count usage'
require 'Gabba'
Gabba::Gabba.new("UA-46288146-1", "github.com").event("Kitchenplan", "Run", ENV['USER'])
# Generate the chef config
ohai 'Generating the Chef configs'
$: << File.join((File.expand_path("../", Pathname.new(__FILE__).realpath)), "/lib")
require "kitchenplan/config"
config = Kitchenplan::Config.new
File.open("kitchenplan-attributes.json", 'w') do |out|
out.write(JSON.pretty_generate(config.config['attributes']))
end
File.open("solo.rb", 'w') do |out|
out.write("cookbook_path [ \"#{Dir.pwd}/cookbooks\" ]")
end
# Installing
sudo "apt-get install -y git" if config.platform == "debian"
normaldo "bin/librarian-chef install --clean #{(options[:debug] ? '--verbose' : '--quiet')}" unless File.exists?("cookbooks")
normaldo "bin/librarian-chef update #{(options[:debug] ? '--verbose' : '--quiet')}" if options[:update_cookbooks]
# Run Chef
if options[:recipes]
sudo "bin/chef-solo --log_level #{(options[:debug] ? 'debug' : 'error')} -c solo.rb -j kitchenplan-attributes.json -o #{options[:recipes].join(",")}" if options[:chef]
else
sudo "bin/chef-solo --log_level #{(options[:debug] ? 'debug' : 'error')} -c solo.rb -j kitchenplan-attributes.json -o #{config.config['recipes'].join(",")}" if options[:chef]
end
ohai "Chef run complete, your computer should be installed. You can always rerun Kitchenplan to adjust your settings."