Class: ECSHelper::Command::Exec

Inherits:
Base
  • Object
show all
Defined in:
lib/ecs_helper/command/exec.rb

Instance Attribute Summary

Attributes inherited from Base

#client, #helper, #option_parser, #options, #type

Instance Method Summary collapse

Methods inherited from Base

#application, #check_bin, #initialize, #project, #validate

Methods included from Logging

#console, #error, #log

Constructor Details

This class inherits a constructor from ECSHelper::Command::Base

Instance Method Details

#check_aws_cliObject



45
46
47
48
# File 'lib/ecs_helper/command/exec.rb', line 45

def check_aws_cli
  check_bin('aws')
  check_aws_cli_version
end

#check_aws_cli_versionObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ecs_helper/command/exec.rb', line 32

def check_aws_cli_version
  check_cmd = Terrapin::CommandLine.new("aws --version")
  result = check_cmd.run
  version = parse_version(result)
  if version === "1"
    messages = [
      "Exec command requires aws cli v2".light_white,
      cmd_option_parser[0].help
    ]
    raise ECSHelper::Error::CommandValidationError.new(messages)
  end
end

#check_session_manager_pluginObject



28
29
30
# File 'lib/ecs_helper/command/exec.rb', line 28

def check_session_manager_plugin
  check_bin('session-manager-plugin')
end

#cmd_option_parserObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ecs_helper/command/exec.rb', line 6

def cmd_option_parser
  options = { command: '/bin/bash' }
  parser = ::OptionParser.new do |opts|
    opts.banner = 'Usage: ecs_helper exec [options]. require session-manager-plugin and aws cli v2'
    opts.on('-p VALUE', '--project VALUE',
            "Set project name, if not specified will look at ENV['PROJECT'], will be used to detect cluster") do |p|
      options[:project] = processEqual(p)
    end
    opts.on('-a VALUE', '--application VALUE',
            "Set application name, if not specified will look at ENV['APPLICATION'], will be used to detect service and task definition") do |a|
      options[:application] = processEqual(a)
    end
    opts.on('-c', '--container VALUE', 'Cache image before build, default false') { |c| options[:container] = processEqual(c) }
    opts.on('--command VALUE', 'Command to execute') { |c| options[:command] = processEqual(c) }
  end
  [parser, options]
end

#exec_commandObject



60
61
62
# File 'lib/ecs_helper/command/exec.rb', line 60

def exec_command
  "aws ecs execute-command --cluster #{cluster_arn} --task #{task_arn} --container #{helper.options[:container]} --command #{helper.options[:command]} --interactive"
end

#printable?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/ecs_helper/command/exec.rb', line 50

def printable?
  true
end

#requiredObject



24
25
26
# File 'lib/ecs_helper/command/exec.rb', line 24

def required
  [:container]
end

#runObject



54
55
56
57
58
# File 'lib/ecs_helper/command/exec.rb', line 54

def run
  check_session_manager_plugin
  check_aws_cli
  exec_command
end