Class: EY::Enzyme::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ey_enzyme/cli.rb

Constant Summary collapse

IGNORABLE_EXCEPTIONS =
[NoMemoryError, SignalException, Interrupt, SystemExit]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ CLI

Returns a new instance of CLI.



113
114
115
116
117
118
119
120
121
122
# File 'lib/ey_enzyme/cli.rb', line 113

def initialize(opts={})
  @opts   = opts
  @logger = opts[:logger] = MultiLogger.new(opts[:logfile])
  @api = API
  @api.configure(opts[:api], opts[:instance_id], opts[:token], opts[:logger])

  @logger.level = opts[:log_level]

  @opts[:timestamp] = Time.now.strftime("%Y-%m-%dT%H-%M-%S")
end

Class Method Details

.run(args) ⇒ Object



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
# File 'lib/ey_enzyme/cli.rb', line 5

def self.run(args)
  $stdout.sync = true

  defaults = {
    chef_bin:   'chef-client',
    run_list:   ['recipe[ey-init::main]'],
    config:     '/etc/engineyard/dracul.yml',
    base_path:  '/etc/chef/',
    logfile:    '/var/log/ey-enzyme.log',
    log_level:  :info,
    keep:       5,
    run_custom: true,
    download:   true,
    mode:       :deploy
  }
  options = {}

  opts = OptionParser.new do |opts|
    opts.version = EY::Enzyme::VERSION
    opts.banner = "Usage: ey-enzyme <options> [argument]"
    opts.define_head "ey-enzyme: running recipes..."
    opts.separator '*'*80

    opts.on("--logfile", "Set the logfile (default: #{defaults[:logfile]})") do |path|
      options[:logfile] = path
    end

    opts.on("--config CONFIG", "Use config file (default: #{defaults[:config]})") do |config|
      options[:config] = config
    end

    opts.on("--cookbooks PATH", "Use the given path for retrieving and using the cookbooks (default: #{defaults[:base_path]}") do |path|
      options[:base_path] = path
    end

    opts.on("--chef-bin PATH", "Use this chef-solo executable to run recipes") do |binary|
      options[:chef_bin] = binary
    end

    opts.on("--run-list RECIPES", "Comma-separated list of recipes to run") do |recipe_list|
      options[:run_list] = recipe_list.split(',').collect {|r| "recipe[#{r}]" }
    end

    opts.on("--report message", "Report a status") do |message|
      options[:mode] = :report
      options[:message] = message
    end

    opts.on("--no-custom", "Do not overlay custom recipes") do
      options[:run_custom] = false
    end

    opts.on("--cached", "Used the cached version of the cookbooks and dna.json") do
      options[:download] = false
    end

    opts.on("--refresh-dna", "Write new dna even if --cached is on") do
	  options[:refresh_dna] = true
    end

    opts.on("--verbose", "Show debug logs as well") do
      options[:log_level] = :debug
    end
  end

  opts.parse!(args)

  config = options[:config] || defaults[:config]
  cli    = new(defaults.merge(YAML.load_file(config)).merge(options))

  if options[:mode] == :report
    cli.report(options[:message])
  else
    cli.deploy
  end

rescue OptionParser::InvalidOption
  $stderr.puts "#{File.basename($0)} #{$!.message}"
  abort opts.to_s

rescue *IGNORABLE_EXCEPTIONS
  raise

rescue DeployError
  if cli
    cli.report $!.message
    cli.notify_user_error($!)
    exit 0
  end
  raise

rescue DNAError
  if cli
    cli.report $!.message
    cli.notify_system_error($!)
    exit 0
  end
  raise

rescue Exception
  if cli
    cli.notify_system_error($!)
    exit 0
  end
  raise

end

Instance Method Details

#cookbook_setObject



147
148
149
# File 'lib/ey_enzyme/cli.rb', line 147

def cookbook_set
  CookbookSet.new(@api, @opts)
end

#deployObject



137
138
139
140
141
142
143
144
145
# File 'lib/ey_enzyme/cli.rb', line 137

def deploy
  @logger.info "Starting configuration run"

  ensure_resolv_conf_is_functional
  update_dna

  run_cookbooks
  @api.notify_success
end

#notify_system_error(error) ⇒ Object



129
130
131
# File 'lib/ey_enzyme/cli.rb', line 129

def notify_system_error(error)
  @api.notify_error("system", error)
end

#notify_user_error(error) ⇒ Object



133
134
135
# File 'lib/ey_enzyme/cli.rb', line 133

def notify_user_error(error)
  @api.notify_error("user", error)
end

#report(message) ⇒ Object



124
125
126
127
# File 'lib/ey_enzyme/cli.rb', line 124

def report(message)
  @logger.debug "REPORT MESSAGE: #{message}"
  @api.report(message)
end