This repository has been archived by the owner on Jan 4, 2021. It is now read-only.
/
playbook.rb
203 lines (160 loc) · 5.81 KB
/
playbook.rb
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
module MCollective
class Application
class Playbook < Application
description "Choria Playbook Runner"
usage <<-USAGE
mco playbook [OPTIONS] <ACTION> <PLAYBOOK>
The ACTION can be one of the following:
run - run the playbook as your local user
The PLAYBOOK Puppet Plan describing the tasks to perform
Passing --help as well as a PLAYBOOK argument will show
flags and help related to the specific playbook.
Any inputs to the playbook should be given on the CLI.
USAGE
exclude_argument_sections "common", "filter", "rpc"
def pre_parse_modulepath
words = Shellwords.shellwords(ARGV.join(" "))
words.each_with_index do |word, idx|
if word == "--modulepath"
configuration[:__modulepath] = words[idx + 1]
break
end
end
end
# Playbook should be given right after the command, this finds the value after the command
#
# @return [String,nil]
def pre_parse_find_playbook
commands = Regexp.union(valid_commands)
cmd_idx = ARGV.index {|a| a.match(commands)}
return nil unless cmd_idx
pb = ARGV[cmd_idx + 1]
pb if pb =~ /\A([a-z][a-z0-9_]*)?(::[a-z][a-z0-9_]*)*\Z/
end
# Creates an instance of the plan runner
#
# @param plan [String] the name of a plan
# @return [Util::BoltSupport::PlanRunner]
def runner(plan, loglevel=nil)
unless configuration[:__modulepath]
configuration[:__modulepath] = File.expand_path("~/.puppetlabs/etc/code/modules")
end
require "mcollective/util/bolt_support"
runner = Util::BoltSupport::PlanRunner.new(
plan,
configuration[:__tmpdir],
configuration[:__modulepath] || Dir.pwd,
configuration[:__loglevel] || "info"
)
unless runner.exist?
STDERR.puts("Cannot find supplied Playbook %s" % plan)
STDERR.puts
STDERR.puts("Module Path:")
STDERR.puts
STDERR.puts(Util.align_text(configuration[:__modulepath].split(":").join("\n")))
exit(1)
end
runner
end
# Adds the playbook inputs as CLI options before starting the app
def run
pre_parse_modulepath
Dir.mktmpdir("choria") do |dir|
configuration[:__tmpdir] = dir
if playbook_name = pre_parse_find_playbook
configuration[:__playbook] = playbook_name
runner(playbook_name).add_cli_options(self, false)
end
# Hackily done here to force it below the playbook options
self.class.option :__json_input,
:arguments => ["--input INPUT"],
:description => "JSON input to pass to the task",
:required => false,
:type => String
self.class.option :__modulepath,
:arguments => ["--modulepath PATH"],
:description => "Path to find Puppet module when using the Plan DSL",
:type => String
self.class.option :__loglevel,
:arguments => ["--loglevel LEVEL"],
:description => "Override the loglevel set in the playbook (debug, info, warn, error, fatal)",
:type => String,
:validate => ->(level) { ["error", "fatal", "debug", "warn", "info"].include?(level) }
super
end
end
def post_option_parser(configuration)
if ARGV.length >= 1
configuration[:__command] = ARGV.shift
else
abort("Please specify a command, valid commands are: %s" % valid_commands.join(", "))
end
if input = configuration[:__json_input]
result = {}
if input.start_with?("@")
input.sub!("@", "")
result = JSON.parse(File.read(input)) if input.end_with?("json")
result = YAML.safe_load(File.read(input)) if input.end_with?("yaml")
else
result = JSON.parse(input)
end
configuration.merge!(result)
end
end
# Validates the configuration
#
# @return [void]
def validate_configuration(configuration)
abort("Please specify a playbook to run") unless configuration[:__playbook]
if options[:verbose] && !configuration.include?(:loglevel)
configuration[:__loglevel] = "debug"
end
end
# List of valid commands this application respond to
#
# @return [Array<String>] like `plan` and `run`
def valid_commands
methods.grep(/_command$/).map {|c| c.to_s.gsub("_command", "")}
end
def run_command
pb_config = configuration.clone
pb_config.delete_if {|k, _| k.to_s.start_with?("__")}
pb = runner(configuration[:__playbook], configuration[:__loglevel])
run_plan(pb, pb_config)
end
def run_plan(pb, pb_config)
startime = Time.now
success = true
result = pb.run!(pb_config)
rescue
success = false
ensure
disconnect
endtime = Time.now
color = :green
msg = "OK"
unless success
color = :red
msg = "FAILED"
end
puts
puts "Plan %s ran in %.2f seconds: %s" % [
Util.colorize(:bold, configuration[:__playbook]),
endtime - startime,
Util.colorize(color, msg)
]
unless result.nil?
puts
puts "Result: "
puts
puts Util.align_text(JSON.pretty_generate(result), 10000)
puts
end
success ? exit(0) : exit(1)
end
def main
send("%s_command" % configuration[:__command])
end
end
end
end