Class: JackAndTheElasticBeanstalk::CLI

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

Instance Method Summary collapse

Instance Method Details

#archive(process, output_path) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/jack_and_the_elastic_beanstalk/cli.rb', line 152

def archive(process, output_path)
  zip_path = Pathname(output_path)

  Dir.mktmpdir do |dir|
    dir_path = Pathname(dir)

    runner.stdout.puts "Staging for #{process}..."

    dir_path.mkpath
    service.stage(target_dir: dir_path, process: process)

    runner.stdout.puts "Making application bundle to #{zip_path}..."

    service.archive(input_dir: dir_path, output_path: zip_path)
  end
end

#cleanupObject



355
356
357
358
# File 'lib/jack_and_the_elastic_beanstalk/cli.rb', line 355

def cleanup
  deleted_count = eb.cleanup_versions
  runner.stdout.puts "Cleanup #{deleted_count} versions."
end

#create(configuration, group, *env_var_args) ⇒ Object



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

def create(configuration, group, *env_var_args)
  processes = config.each_process(configuration).to_a
  env_vars = parse_env_args(env_var_args)

  output_dir do |base_path|
    processes.each do |process, hash|
      path = base_path + process
      runner.stdout.puts "Staging for #{process}..."
      service.stage(target_dir: path, process: process)
    end

    each_in_parallel(processes) do |process, hash|
      runner.stdout.puts "Creating new environment for #{process}..."

      path = base_path + process

      service.eb_init(target_dir: path)
      service.eb_create(target_dir: path, configuration: configuration, group: group, process: process, env_vars: env_vars)

      if hash["type"] == "oneoff"
        runner.stdout.puts "Scaling to 0 (#{process} is a oneoff process)"
        env = service.each_environment(group: group).find {|_, p| p == process }.first
        env.set_scale(0)
        env.synchronize_update
      end
    end
  end
end

#deploy(group) ⇒ Object



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

def deploy(group)
  envs = service.each_environment(group: group).to_a

  prefix = Time.now.utc.iso8601

  output_dir do |base_path|
    archives = {}

    envs.each do |_, process|
      path = base_path + process
      path.mkpath

      runner.stdout.puts "Staging for #{process}..."
      service.stage(target_dir: path, process: process)

      name = "#{group}-#{prefix}-#{process}"
      key = "#{config.app_name}/#{name}"

      archive_path = base_path + "#{name}.zip"
      service.archive(input_dir: path, output_path: archive_path)

      archives[process] = [key, name, archive_path]
    end

    each_in_parallel(envs) do |_, process|
      runner.stdout.puts "Deploying to #{process}..."

      s3_key, label, archive_path = archives[process]

      service.deploy(group: group,
                     process: process,
                     archive_path: archive_path,
                     s3_key: s3_key,
                     label: label)
    end
  end
end

#destroy(group, process = nil) ⇒ Object



216
217
218
219
220
221
222
223
# File 'lib/jack_and_the_elastic_beanstalk/cli.rb', line 216

def destroy(group, process=nil)
  service.each_environment(group: group) do |env, p|
    try_process p, is: process do
      runner.stdout.puts "Destroying #{p}: #{env.environment_name}..."
      env.destroy
    end
  end
end

#exec(group, *command) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/jack_and_the_elastic_beanstalk/cli.rb', line 240

def exec(group, *command)
  env = service.each_environment(group: group).find {|_, p| p == "oneoff" }&.first
  if env
    begin
      env.synchronize_update do
        runner.stdout.puts "Starting #{env.environment_name} for oneoff process..."
        env.set_scale 1
      end

      output_dir do |path|
        runner.chdir(path) do
          service.eb_init target_dir: path

          runner.stdout.puts "Waiting for EB to complete deploy..."
          sleep 30

          start = Time.now

          while true
            dirs, _ = runner.capture3! "eb", "ssh", env.environment_name, "-c", "ls /var/app"

            if dirs =~ /ondeck/
              logger.info("jeb::cli") { "Waiting for deploy..." }
            end
            if dirs =~ /current/ && dirs !~ /ondeck/
              break
            end
            if Time.now - start > options[:timeout]*60
              raise "Timed out for waiting deploy..."
            end

            sleep 15
          end

          commandline = if options[:docker]
                          "sudo docker ps --filter=ancestor=aws_beanstalk/current-app --latest --format='{{.ID}}' | xargs -I{} sudo docker exec {} #{command.join(' ')}"
                        else
                          "cd /var/app/current && sudo -E -u webapp env PATH=$PATH #{command.join(' ')}"
                        end
          out, err, status = runner.capture3 "eb", "ssh", env.environment_name, "-c", commandline

          runner.stdout.print out
          runner.stderr.print err

          raise status.to_s unless status.success?
        end
      end
    ensure
      unless options[:keep]
        env.synchronize_update do
          runner.stdout.puts "Shutting down #{env.environment_name}..."
          env.set_scale 0
        end
      end
    end
  else
    runner.stdout.puts "Could not find environment associated to oneoff process..."
  end
end

#listObject



313
314
315
316
317
# File 'lib/jack_and_the_elastic_beanstalk/cli.rb', line 313

def list
  service.each_group do |group, envs|
    runner.stdout.puts "#{group}: #{envs.map(&:environment_name).join(", ")}"
  end
end

#printenv(group, process = nil) ⇒ Object



170
171
172
173
174
175
176
177
178
179
# File 'lib/jack_and_the_elastic_beanstalk/cli.rb', line 170

def printenv(group, process=nil)
  service.each_environment(group: group) do |env, p|
    try_process(p, is: process) do
      puts "#{p} (#{env.environment_name}):"
      env.env_vars.each do |key, value|
        runner.stdout.puts "  #{key}=#{value}"
      end
    end
  end
end

#resources(group, process_name = nil) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/jack_and_the_elastic_beanstalk/cli.rb', line 330

def resources(group, process_name=nil)
  resources = {}

  service.each_environment(group: group) do |env, process|
    try_process process, is: process_name do
      ress = env.resources.environment_resources
      resources[process] = {
        environment_name: env.environment_name,
        environment_id: env.environment_id,
        auto_scaling_groups: ress.auto_scaling_groups.map(&:name),
        instances: ress.instances.map(&:id),
        launch_configurations: ress.launch_configurations.map(&:name),
        load_balancers: ress.load_balancers.map(&:name),
        queues: ress.queues.map(&:url).reject(&:empty?),
        triggers: ress.triggers.map(&:name)
      }
    end
  end

  unless resources.empty?
    runner.stdout.puts JSON.pretty_generate(resources)
  end
end

#restart(group, process = nil) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/jack_and_the_elastic_beanstalk/cli.rb', line 203

def restart(group, process=nil)
  envs = service.each_environment(group: group)
  each_in_parallel(envs) do |env, p|
    try_process(p, is: process) do
      runner.stdout.puts "Restarting #{p}..."
      env.synchronize_update do
        env.restart
      end
    end
  end
end

#scale(group, process, min, max = min) ⇒ Object



301
302
303
304
305
306
307
308
309
310
# File 'lib/jack_and_the_elastic_beanstalk/cli.rb', line 301

def scale(group, process, min, max=min)
  service.each_environment(group: group) do |env, p|
    try_process p, is: process do
      runner.stdout.puts "Scaling #{group} (#{env.environment_name}) to min=#{min}, max=#{max}..."
      env.synchronize_update do
        env.set_scale(min...max)
      end
    end
  end
end

#setenv(group, *args) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/jack_and_the_elastic_beanstalk/cli.rb', line 182

def setenv(group, *args)
  process = if args.first !~ /=/
              args.shift
            end

  hash = parse_env_args(args)

  logger.info("jeb::cli") { "Setting environment hash: #{hash.inspect}" }

  envs = service.each_environment(group: group)
  each_in_parallel(envs) do |env, p|
    try_process(p, is: process) do
      runner.stdout.puts "Updating #{p}'s environment variable..."
      env.synchronize_update do
        env.set_env_vars hash
      end
    end
  end
end

#stage(process, output_dir) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/jack_and_the_elastic_beanstalk/cli.rb', line 136

def stage(process, output_dir)
  path = Pathname(output_dir)

  if path.directory?
    runner.stdout.puts "Deleting #{path}..."
    path.rmtree
  end

  runner.stdout.puts "Staging for #{process} in #{path}..."

  path.mkpath
  service.eb_init target_dir: path
  service.stage(target_dir: path, process: process)
end

#status(group, process = nil) ⇒ Object



226
227
228
229
230
231
232
233
234
235
# File 'lib/jack_and_the_elastic_beanstalk/cli.rb', line 226

def status(group, process=nil)
  service.each_environment(group: group) do |env, p|
    try_process p, is: process do
      h = env.health
      ih = h.instances_health
      total = ih.no_data + ih.ok + ih.info + ih.warning + ih.degraded + ih.severe + ih.pending
      runner.stdout.puts "#{p}: name=#{env.environment_name}, status=#{env.status}, health: #{h.health_status}, instances: #{total}, scale: #{env.scale}"
    end
  end
end

#synchronize(group) ⇒ Object



320
321
322
323
324
325
326
327
# File 'lib/jack_and_the_elastic_beanstalk/cli.rb', line 320

def synchronize(group)
  service.each_environment(group: group) do |env, process|
    if env.status != "Ready"
      runner.stdout.puts "Waiting for #{process} (#{env.environment_name})..."
      env.synchronize_update
    end
  end
end

#versionObject



361
362
363
# File 'lib/jack_and_the_elastic_beanstalk/cli.rb', line 361

def version
  puts VERSION
end