Class: EY::Recipes::CLI

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

Constant Summary collapse

LOG_FILE =
"/var/log/chef.log"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ CLI

Returns a new instance of CLI.



93
94
95
96
97
# File 'lib/ey_recipes/cli.rb', line 93

def initialize(opts)
  @opts = opts
  @api = API.connect(opts[:api], opts[:aws_secret_id], opts[:aws_secret_key])
  @lock = Mutex.new
end

Class Method Details

.run(args) ⇒ Object



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

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

  command = :list_envs
  defaults = {
    :config => '~/.ey-cloud.yml',
    :type => 'recipes',
    :keep => 5
  }

  options = {}
  # Build a parser for the command line arguments
  opts = OptionParser.new do |opts|
    opts.version = "0.0.1"

    opts.banner = "Usage: ey-recipes [-flag] [argument]"
    opts.define_head "ey-recipes: managing your recipes..."
    opts.separator '*'*80

    opts.on("-c", "--config CONFIG", "Use config file.") do |config|
      options[:config] = config
    end

    opts.on("-l", "--list-recipes ENV", "List recipes for ENV") do |env|
      command = :list_recipes
      options[:env_name] = env
    end

    opts.on("-u", "--upload ENV", "Upload a new recipe set so s3 but don't run it. Be sure to commit your changes to your git repo before deploying as we create a git archive of HEAD.") do |env|
      command = :upload
      options[:env_name] = env
    end

    opts.on("--clear ENV", "Clear out any customer recipes attached to ENV") do |env|
      command = :clear
      options[:env_name] = env
    end

    opts.on("-d", "--deploy ENV", "Upload a new recipe set and run them on your instances. Be sure to commit your changes to your git repo before deploying as we create a git archive of HEAD.") do |env|
      command = :deploy_custom
      options[:env_name] = env
    end

    opts.on("-r", "--rollback ENV", "Roll back to the previous recipe set for ENV and run them on your instances.") do |env|
      command = :rollback
      options[:env_name] = env
    end

    opts.on("--deploy-main ENV", "Redeploy the main EY recipe set and run it on your environment ENV") do |env|
      command = :deploy_main
      options[:env_name] = env
    end

    opts.on("--view-main-log ENV", "view the last main ey recipe run log file for ENV") do |env|
      command = :view_main_logs
      options[:env_name] = env
    end

    opts.on("--view-log ENV", "view the last custom chef recipe run log file for ENV") do |env|
      command = :view_custom_logs
      options[:env_name] = env
    end
  end

  begin
    opts.parse!(args)
  rescue OptionParser::ParseError => e
    abort(e.message)
  end

  ey = nil
  if File.exist?(config = File.expand_path(options[:config] || defaults[:config]))
    ey = new(options = defaults.merge(YAML::load(File.read(config))).merge(options))
  else
    puts "You need to have an ~/.ey-cloud.yml file with your credentials in it to use this tool.\nOr point it at a yaml file with -c path/to/ey-cloud.yml"
    exit 1
  end

  if ey.respond_to?(command)
    ey.send(command)
  else
    abort "Unknown command: #{command}"
  end
end

Instance Method Details

#abort(message) ⇒ Object



3
4
5
# File 'lib/ey_recipes/cli.rb', line 3

def abort(message)
  raise message
end

#clearObject



115
116
117
# File 'lib/ey_recipes/cli.rb', line 115

def clear
  recipes_bucket.clear_bucket
end

#deploy_customObject



119
120
121
122
# File 'lib/ey_recipes/cli.rb', line 119

def deploy_custom
  upload_recipes
  deploy_custom_recipes
end

#deploy_custom_recipesObject



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

def deploy_custom_recipes
  deploy_recipes(:custom, 'logs')
end

#deploy_mainObject



128
129
130
# File 'lib/ey_recipes/cli.rb', line 128

def deploy_main
  deploy_recipes(:main, 'main.logs')
end

#deploy_recipes(type, log_type) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ey_recipes/cli.rb', line 149

def deploy_recipes(type, log_type)
  unless env['instances'] > 0
    abort "There are no running instances for ENV: #{env_name}"
  end

  puts "deploying #{type} recipes..."
  if @api.deploy_recipes(type, env['id'])
    wait_for_logs(log_type)
  else
    abort "deploying #{type} recipes failed..."
  end
end

#get_jsonObject



145
146
147
# File 'lib/ey_recipes/cli.rb', line 145

def get_json
  @api.get_json(@opts[:instance])
end

#list_envsObject



103
104
105
106
107
108
109
# File 'lib/ey_recipes/cli.rb', line 103

def list_envs
  puts "Current Environments:"
  envs.each do |name,data|
    puts "env: #{name}  running instances: #{data['instances']}"
    puts "  instance_ids: #{data['instance_ids'].inspect}"
  end
end

#list_recipesObject



99
100
101
# File 'lib/ey_recipes/cli.rb', line 99

def list_recipes
  recipes_bucket.list(true)
end

#rollbackObject



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

def rollback
  recipes_bucket.rollback
  deploy_custom_recipes
end

#uploadObject



111
112
113
# File 'lib/ey_recipes/cli.rb', line 111

def upload
  upload_recipes
end

#upload_recipesObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ey_recipes/cli.rb', line 162

def upload_recipes
  unless File.exist?("cookbooks")
    abort "you must run this command from the root of your chef recipe git repo"
  end

  file = "recipes.#{$$}.#{Time.now.to_i}.tmp.tgz"
  tarcmd = "git archive --format=tar HEAD | gzip > #{file}"
  if system(tarcmd)
    recipes_bucket.upload_object(file)
    recipes_bucket.cleanup
  else
    abort "Failed to upload recipes for #{env_name}"
  end
end

#view_custom_logsObject



141
142
143
# File 'lib/ey_recipes/cli.rb', line 141

def view_custom_logs
  view_logs('logs')
end

#view_logs(log_type) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/ey_recipes/cli.rb', line 177

def view_logs(log_type)
  env['instance_ids'].each do |instance_id|
    log_bucket = log_bucket_for(log_type, instance_id)
    puts "Logs for: #{instance_id}"
    puts display_logs(log_bucket.list.last)
  end
end

#view_main_logsObject



137
138
139
# File 'lib/ey_recipes/cli.rb', line 137

def view_main_logs
  view_logs('main.logs')
end