Class: Putpaws::Ecs::TaskCommand
- Inherits:
-
Object
- Object
- Putpaws::Ecs::TaskCommand
- Defined in:
- lib/putpaws/ecs/task_command.rb
Instance Attribute Summary collapse
-
#cluster ⇒ Object
readonly
Returns the value of attribute cluster.
-
#ecs_client ⇒ Object
readonly
Returns the value of attribute ecs_client.
-
#ecs_task ⇒ Object
Returns the value of attribute ecs_task.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
-
#task_name_prefix ⇒ Object
readonly
Returns the value of attribute task_name_prefix.
Class Method Summary collapse
Instance Method Summary collapse
- #build_session_manager_plugin_command(session:, target:) ⇒ Object
- #get_attach_command(container: nil) ⇒ Object
- #get_port_forwarding_command(container: nil, remote_port:, remote_host:, local_port: nil) ⇒ Object
- #get_session_target(container: 'app') ⇒ Object
-
#initialize(region:, cluster:, task_name_prefix: nil) ⇒ TaskCommand
constructor
A new instance of TaskCommand.
- #list_ecs_tasks ⇒ Object
Constructor Details
#initialize(region:, cluster:, task_name_prefix: nil) ⇒ TaskCommand
Returns a new instance of TaskCommand.
13 14 15 16 17 18 19 |
# File 'lib/putpaws/ecs/task_command.rb', line 13 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
#cluster ⇒ Object (readonly)
Returns the value of attribute cluster.
11 12 13 |
# File 'lib/putpaws/ecs/task_command.rb', line 11 def cluster @cluster end |
#ecs_client ⇒ Object (readonly)
Returns the value of attribute ecs_client.
10 11 12 |
# File 'lib/putpaws/ecs/task_command.rb', line 10 def ecs_client @ecs_client end |
#ecs_task ⇒ Object
Returns the value of attribute ecs_task.
12 13 14 |
# File 'lib/putpaws/ecs/task_command.rb', line 12 def ecs_task @ecs_task end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
11 12 13 |
# File 'lib/putpaws/ecs/task_command.rb', line 11 def region @region end |
#task_name_prefix ⇒ Object (readonly)
Returns the value of attribute task_name_prefix.
11 12 13 |
# File 'lib/putpaws/ecs/task_command.rb', line 11 def task_name_prefix @task_name_prefix end |
Class Method Details
.config(config) ⇒ Object
6 7 8 |
# File 'lib/putpaws/ecs/task_command.rb', line 6 def self.config(config) new(**config.ecs_command_params) end |
Instance Method Details
#build_session_manager_plugin_command(session:, target:) ⇒ Object
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 |
# File 'lib/putpaws/ecs/task_command.rb', line 70 def build_session_manager_plugin_command(session:, target:) ssm_region = ENV['AWS_REGION_SSM'] || @region # https://github.com/aws/aws-cli/blob/2a6136010d8656a605d41d1e7b5fdab3c2930cad/awscli/customizations/ecs/executecommand.py#L105 session_json = if session.respond_to?(:session_id) { "SessionId" => session.session_id, "StreamUrl" => session.stream_url, "TokenValue" => session.token_value, }.to_json elsif session.is_a?(Hash) session.to_json else session end target_json = { "Target" => target }.to_json cmd = [ "session-manager-plugin", session_json.dump, @region, "StartSession", 'test', target_json.dump, "https://ssm.#{ssm_region}.amazonaws.com" ] cmd.join(' ') end |
#get_attach_command(container: nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/putpaws/ecs/task_command.rb', line 57 def get_attach_command(container: nil) container ||= 'app' target = get_session_target(container: container) res = ecs_client.execute_command({ cluster: cluster, container: container, command: '/bin/bash', interactive: true, task: ecs_task.task_arn, }) build_session_manager_plugin_command(session: res.session, target: target) end |
#get_port_forwarding_command(container: nil, remote_port:, remote_host:, local_port: nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/putpaws/ecs/task_command.rb', line 39 def get_port_forwarding_command(container: nil, remote_port:, remote_host:, local_port: nil) container ||= 'app' target = get_session_target(container: container) ssm_client = Aws::SSM::Client.new({region: region}) local_port ||= (1050..1079).map(&:to_s).shuffle.first puts "Starting to use local port: #{local_port}" res = ssm_client.start_session({ target: target, document_name: "AWS-StartPortForwardingSessionToRemoteHost", parameters: { portNumber: [remote_port], localPortNumber: [local_port], host: [remote_host] } }) build_session_manager_plugin_command(session: res, target: target) end |
#get_session_target(container: 'app') ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/putpaws/ecs/task_command.rb', line 31 def get_session_target(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 "ecs:#{cluster}_#{task_id}_#{ctn.runtime_id}" end |
#list_ecs_tasks ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/putpaws/ecs/task_command.rb', line 21 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 |