forked from kitchenplan/kitchenplan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kitchenplan
executable file
·136 lines (104 loc) · 3.95 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
#!/usr/bin/env ruby
# Run Kitchenplan.
require 'pathname'
if ENV['USER'] == 'root'
abort "Run this as a normal user, I'll sudo if I need to."
end
# Make sure only one kitchenplan process runs at a time.
myself = File.new __FILE__
unless myself.flock File::LOCK_EX | File::LOCK_NB
abort "You're already running a kitchenpan process! Know a patience."
end
# Yeah yeah, I like to be explicit.
at_exit { myself.flock File::LOCK_UN }
# Put us where we belong, in the root dir of our kitchenplan repo.
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[:soloist] = true
opts.on("--[no-]soloist", "Run soloist (defaults to yes)") do |soloist|
options[:soloist] = soloist
end
options[:update] = true
opts.on("--[no-]update", "Run the kitchenplan update (defaults to yes)") do |update|
options[:update] = update
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
unless system("(gem spec bundler -v '~> 1.3.0' > /dev/null 2>&1) || sudo gem install bundler -v '~> 1.3.0' --no-rdoc --no-ri")
abort "Failed to install bundler"
end
bundle_command = ["sudo", "bundle"]
bundle_command = bundle_command + [ "install", "--binstubs=bin"]
bundle_command << "--verbose" if options[:debug]
bundle_command << "--quiet" unless options[:debug]
warn bundle_command.join(" ") if options[:debug]
unless system *bundle_command
abort "There was a problem bootstrapping. Run using --debug for more information"
end
# Add local deps to the load path.
require "rubygems"
require "bundler/setup"
# Generate the soloist config
$: << File.join((File.expand_path("../", Pathname.new(__FILE__).realpath)), "/lib")
require "kitchenplan/config"
config = Kitchenplan::Config.new
soloistrc = {}
soloistrc["cookbook_paths"] = ["#{Dir.pwd}/vendor/cookbooks"]
soloistrc["recipes"] = config.config['recipes']
soloistrc["node_attributes"] = config.config['attributes']
File.open("soloistrc", 'w') do |out|
YAML.dump(soloistrc, out)
end
# Possibly updating the cookbooks
if config.platform == "debian"
git_install_command = ["sudo" ,"apt-get", "install", "-y", "git" ]
warn git_install_command.join(" ") if options[:debug]
unless system *git_install_command
abort "Can't install git. Run using --debug if you need more information."
end
end
unless File.exists?("vendor/cookbooks")
librarian_command = ["bin/librarian-chef", "install", "--path=vendor/cookbooks", "--clean" ]
librarian_command << "--verbose" if options[:debug]
librarian_command << "--quiet" unless options[:debug]
warn librarian_command.join(" ") if options[:debug]
unless system *librarian_command
abort "Can't install Chef cookbooks with librarian-chef. Run using --debug if you need more information."
end
end
if options[:update_cookbooks]
librarian_command = ["bin/librarian-chef", "update", "--verbose" ]
warn librarian_command.join(" ") if options[:debug]
unless system *librarian_command
abort "Can't update Chef cookbooks with librarian-chef. Run using --debug if you need more information."
end
end
# Run soloist
exit 0 unless options[:soloist]
soloist_command = ["bin/soloist"]
ENV["LOG_LEVEL"] = "error"
ENV["LOG_LEVEL"] = "debug" if options[:debug]
warn soloist_command.join(" ") if options[:debug]
unless system *soloist_command
abort "There was a problem running solist. Run using --debug for more information"
end