class VPS::CLI::Playbook

Constants

YML

Attributes

command[R]

Public Class Methods

all() click to toggle source
# File lib/vps/cli/playbook.rb, line 14
def self.all
  Dir["#{VPS::PLAYBOOKS}/*#{YML}"].collect do |playbook|
    command = File.basename(playbook, YML)
    new(playbook, command)
  end
end
new(playbook, command = nil) click to toggle source
# File lib/vps/cli/playbook.rb, line 34
def initialize(playbook, command = nil)
  unless File.exists?(playbook)
    raise NotFoundError, "Could not find playbook #{playbook.inspect}"
  end

  @playbook = {"constants" => {}}.merge(YAML.load_file(playbook))
  unless (playbooks = Dir[playbook.gsub(/\.\w+$/, "/*")].collect{|yml| File.basename(yml, ".yml")}).empty?
    @playbook["constants"]["playbooks"] = playbooks
  end

  @command = command
end
run(playbook, state) click to toggle source
# File lib/vps/cli/playbook.rb, line 21
def self.run(playbook, state)
  playbook = File.expand_path(playbook, VPS::PLAYBOOKS)

  if File.directory?(playbook)
    playbook += "/#{state.server_version}"
  end
  unless File.extname(playbook) == YML
    playbook += YML
  end

  new(playbook).run(state)
end

Public Instance Methods

arguments() click to toggle source
# File lib/vps/cli/playbook.rb, line 55
def arguments
  playbook["arguments"] || []
end
constants() click to toggle source
# File lib/vps/cli/playbook.rb, line 59
def constants
  playbook["constants"] || {}
end
description() click to toggle source
# File lib/vps/cli/playbook.rb, line 47
def description
  playbook["description"]
end
options() click to toggle source
# File lib/vps/cli/playbook.rb, line 63
def options
  options = (playbook["options"] || {}).inject({}) do |hash, (key, value)|
    hash[key.to_sym] = value.symbolize_keys
    hash
  end
  options[:d] = {:type => :boolean, :aliases => "dry-run"}
  options
end
run(state) click to toggle source
# File lib/vps/cli/playbook.rb, line 102
def run(state)
  state.scope(constants) do
    tasks.run(state)
  end
end
run!(args, options) click to toggle source
# File lib/vps/cli/playbook.rb, line 89
def run!(args, options)
  hash = Hash[arguments.zip(args)]
  state = State.new(hash.merge(options))

  self.options.each do |(name, opts)|
    if opts[:aliases]
      state[opts[:aliases].underscore] = state[name]
    end
  end

  run(state)
end
tasks() click to toggle source
# File lib/vps/cli/playbook.rb, line 72
def tasks
  @tasks ||= begin
    tasks = [playbook["tasks"]].flatten.compact

    if requires_confirmation?
      tasks.unshift({
        "task" => :confirm,
        "question" => playbook["confirm"],
        "indent" => false,
        "n" => :abort
      })
    end

    Tasks.new(tasks)
  end
end
usage() click to toggle source
# File lib/vps/cli/playbook.rb, line 51
def usage
  playbook["usage"] || arguments.collect(&:upcase).unshift(@command).join(" ")
end

Private Instance Methods

playbook() click to toggle source
# File lib/vps/cli/playbook.rb, line 110
def playbook
  @playbook
end
requires_confirmation?() click to toggle source
# File lib/vps/cli/playbook.rb, line 114
def requires_confirmation?
  playbook["confirm"].to_s.strip != ""
end