Class: Putpaws::Ecs::TaskCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/putpaws/ecs/task_command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region:, cluster:, task_name_prefix: nil) ⇒ TaskCommand

Returns a new instance of TaskCommand.



12
13
14
15
16
17
18
# File 'lib/putpaws/ecs/task_command.rb', line 12

def initialize(region:, cluster:, task_name_prefix: nil)
  @ecs_client = Aws::ECS::Client.new({region: region})
  @region = region
  @cluster = cluster
  @task_name_prefix = task_name_prefix
  @ecs_task = nil
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



10
11
12
# File 'lib/putpaws/ecs/task_command.rb', line 10

def cluster
  @cluster
end

#ecs_clientObject (readonly)

Returns the value of attribute ecs_client.



9
10
11
# File 'lib/putpaws/ecs/task_command.rb', line 9

def ecs_client
  @ecs_client
end

#ecs_taskObject

Returns the value of attribute ecs_task.



11
12
13
# File 'lib/putpaws/ecs/task_command.rb', line 11

def ecs_task
  @ecs_task
end

#regionObject (readonly)

Returns the value of attribute region.



10
11
12
# File 'lib/putpaws/ecs/task_command.rb', line 10

def region
  @region
end

#task_name_prefixObject (readonly)

Returns the value of attribute task_name_prefix.



10
11
12
# File 'lib/putpaws/ecs/task_command.rb', line 10

def task_name_prefix
  @task_name_prefix
end

Class Method Details

.config(config) ⇒ Object



5
6
7
# File 'lib/putpaws/ecs/task_command.rb', line 5

def self.config(config)
  new(config.ecs_command_params)
end

Instance Method Details

#get_attach_command(container: 'app') ⇒ Object



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
# File 'lib/putpaws/ecs/task_command.rb', line 30

def get_attach_command(container: 'app')
  raise "ECS Task Not Set" unless ecs_task
  ctn = ecs_task.containers.detect{|c| c.name == container}
  task_id = ecs_task.task_arn.split('/').last
  raise "Container: #{container} not found" unless ctn
  res = ecs_client.execute_command({
    cluster: cluster,
    container: container,
    command: '/bin/bash',
    interactive: true,
    task: ecs_task.task_arn,
  })

  ssm_region = ENV['AWS_REGION_SSM'] || @region
  
  # https://github.com/aws/aws-cli/blob/2a6136010d8656a605d41d1e7b5fdab3c2930cad/awscli/customizations/ecs/executecommand.py#L105
  session_json = {
    "SessionId" => res.session.session_id,
    "StreamUrl" => res.session.stream_url,
    "TokenValue" => res.session.token_value,
  }.to_json
  target_json = {
    "Target" => "ecs:#{cluster}_#{task_id}_#{ctn.runtime_id}"
  }.to_json
  cmd = [
    "session-manager-plugin",
    session_json.dump,
    @region,
    "StartSession",
    'test',
    target_json.dump,
    "https://ssm.#{ssm_region}.amazonaws.com"
  ]
  cmd.join(' ')
end

#list_ecs_tasksObject



20
21
22
23
24
25
26
27
28
# File 'lib/putpaws/ecs/task_command.rb', line 20

def list_ecs_tasks
  res = ecs_client.list_tasks(cluster: cluster)
  res = ecs_client.describe_tasks(tasks: res.task_arns, cluster: cluster)
  return res.tasks unless task_name_prefix
  res.tasks.select{|t| 
    _, name = t.task_definition_arn.split('task-definition/')
    name.start_with?(task_name_prefix)
  }
end