Skip to content
This repository has been archived by the owner on Jan 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #424 from ripienaar/401.0
Browse files Browse the repository at this point in the history
(#401) Remove the YAML playbooks from the CLI
  • Loading branch information
ripienaar authored Mar 5, 2018
2 parents f2ff0eb + b61f1f1 commit 6912163
Showing 1 changed file with 16 additions and 103 deletions.
119 changes: 16 additions & 103 deletions lib/mcollective/application/playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@ class Playbook < Application
run - run the playbook as your local user
The PLAYBOOK is a YAML file or Puppet Plan describing the
tasks
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.
A report can be produced using the --report argument
when running a playbook
USAGE

exclude_argument_sections "common", "filter", "rpc"
Expand All @@ -45,47 +41,25 @@ def pre_parse_find_playbook

pb = ARGV[cmd_idx + 1]

pb if pb =~ Regexp.union(/(yaml|yml)\Z/, /\A([a-z][a-z0-9_]*)?(::[a-z][a-z0-9_]*)*\Z/)
end

# Determines the playbook type from its name
#
# @return [:yaml, :plan]
def playbook_type(playbook_name=nil)
playbook_name ||= configuration[:__playbook]

return :yaml if playbook_name =~ /(yml|yaml)$/

:plan
pb if pb =~ /\A([a-z][a-z0-9_]*)?(::[a-z][a-z0-9_]*)*\Z/
end

# Creates an instance of the playbook
#
# @param file [String] path to a playbook yaml
# @return [Util::Playbook]
def playbook(file, loglevel=nil)
if playbook_type(file) == :yaml
unless File.exist?(file)
raise("Cannot find supplied playbook file %s" % file)
end

Util.loadclass("MCollective::Util::Playbook")
playbook = Util::Playbook.new(loglevel)
playbook.from_hash(YAML.load_file(file))
playbook
else
require "mcollective/util/bolt_support"
runner = Util::BoltSupport::PlanRunner.new(
file,
configuration[:__tmpdir],
configuration[:__modulepath] || Dir.pwd,
configuration[:__loglevel] || "info"
)

raise("Cannot find supplied plan %s" % file) unless runner.exist?

runner
end
# @param plan [String] path to a playbook yaml
# @return [Util::BoltSupport::PlanRunner]
def playbook(plan, loglevel=nil)
require "mcollective/util/bolt_support"
runner = Util::BoltSupport::PlanRunner.new(
plan,
configuration[:__tmpdir],
configuration[:__modulepath] || Dir.pwd,
configuration[:__loglevel] || "info"
)

raise("Cannot find supplied Playbook %s" % plan) unless runner.exist?

runner
end

# Adds the playbook inputs as CLI options before starting the app
Expand All @@ -112,17 +86,6 @@ def run
:description => "Path to find Puppet module when using the Plan DSL",
:type => String

self.class.option :__report,
:arguments => ["--report"],
:description => "Produce a report in YAML format",
:default => false,
:type => :boolean

self.class.option :__report_file,
:arguments => ["--report-file FILE"],
:description => "Override the default file name for the report",
:type => String

self.class.option :__loglevel,
:arguments => ["--loglevel LEVEL"],
:description => "Override the loglevel set in the playbook (debug, info, warn, error, fatal)",
Expand Down Expand Up @@ -164,10 +127,6 @@ def validate_configuration(configuration)
if options[:verbose] && !configuration.include?(:loglevel)
configuration[:__loglevel] = "debug"
end

if configuration[:__report] && playbook_type == :plan
abort("Reports are only supported for YAML playbooks")
end
end

# List of valid commands this application respond to
Expand All @@ -177,27 +136,12 @@ def valid_commands
methods.grep(/_command$/).map {|c| c.to_s.gsub("_command", "")}
end

def show_report_summary(report)
puts
puts "Summary for %s %s started at %s" % [
Util.colorize(:bold, report["playbook"]["name"]),
Util.colorize(:bold, report["playbook"]["version"]),
Util.colorize(:bold, report["report"]["timestamp"])
]
puts
puts "Overall outcome: %s" % [report["report"]["success"] ? Util.colorize(:green, "success") : Util.colorize(:red, "failed")]
puts " Tasks Ran: %s" % [Util.colorize(:bold, report["metrics"]["task_count"])]
puts " Run Time: %s seconds" % [Util.colorize(:bold, report["metrics"]["run_time"].round(2))]
puts
end

def run_command
pb_config = configuration.clone
pb_config.delete_if {|k, _| k.to_s.start_with?("__")}

pb = playbook(configuration[:__playbook], configuration[:__loglevel])

run_playbook(pb, pb_config) if playbook_type == :yaml
run_plan(pb, pb_config)
end

Expand Down Expand Up @@ -240,37 +184,6 @@ def run_plan(pb, pb_config)
success ? exit(0) : exit(1)
end

def run_playbook(pb, pb_config)
Log.warn("YAML playbooks on the command line are deprecated and will be removed soon, please use Plan DSL playbooks")

if configuration[:__report]
# windows can't have : in file names, ffs
report_name = configuration.fetch(:__report_file, "playbook-%s-%s.yaml" % [pb.name, pb.report.timestamp.strftime("%F_%H-%M-%S")])

if File.exist?(report_name)
abort("Could not write report the file %s: it already exists" % [report_name])
end

begin
report_file = File.open(report_name, "w")
rescue
abort("Could not write report the file %s: it can not be created: %s: %s" % [report_name, $!.class, $!.to_s])
end
end

report = pb.run!(pb_config)

show_report_summary(report)

if configuration[:__report]
report_file.print report.to_yaml
puts
puts "Report saved to %s" % report_name
end

report["report"]["success"] ? exit(0) : exit(1)
end

def main
send("%s_command" % configuration[:__command])
end
Expand Down

0 comments on commit 6912163

Please sign in to comment.