putpaws 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ba984b5ce0261d5c757b4112e6d849aa0815ded01898ee9a25f42cebbb4254f
4
- data.tar.gz: e3b2871eacd65cd2d9904cd09090fce6a8d6fa2e55a227b385a2acbeb11c7c4f
3
+ metadata.gz: e5383c31d8b2b94dcb948e565f9ab5784137a0b946aa53e2ade93fc5e1bf3152
4
+ data.tar.gz: 30babace1c15c9675752567795e4c43414be155cc0f9affccfdb8bd8907a215d
5
5
  SHA512:
6
- metadata.gz: 7c2a4f49328c01dccf7a8efaa9bbdcb0fda07fe1fd68d2632cb6043329401f9059378ac0ed90d3211dc3d4dfcadefc2fe747dd494ecd9f0dae52681ffa5ef9d5
7
- data.tar.gz: acdad73a867e604275325d95c30d3b85f7b784b753cc4461d2838a385ac1b5b0f33cf93c87ecff58e778325a9416baad8994125c1b9108f803418ec7ee74edeb
6
+ metadata.gz: f506cafed6700e2fc5bff6334e7e3374e528652740de99c03a36e89a031603b891b77133b5264fff51f8d1d3bc7db22f8549c88a32658f87883249c8cfe9d331
7
+ data.tar.gz: 941ceaa13526fd71c3a37d5d6541490950472dea6bff75aff2b495acc10adb41f46b270e14a6c279d542863792594896443aeca882655321b856a745c38306e0
@@ -1,4 +1,5 @@
1
1
  require 'aws-sdk-ecs'
2
+ require 'aws-sdk-ssm'
2
3
 
3
4
  module Putpaws::Ecs
4
5
  class TaskCommand
@@ -27,11 +28,35 @@ module Putpaws::Ecs
27
28
  }
28
29
  end
29
30
 
30
- def get_attach_command(container: 'app')
31
+ def get_session_target(container: 'app')
31
32
  raise "ECS Task Not Set" unless ecs_task
32
33
  ctn = ecs_task.containers.detect{|c| c.name == container}
33
34
  task_id = ecs_task.task_arn.split('/').last
34
35
  raise "Container: #{container} not found" unless ctn
36
+ "ecs:#{cluster}_#{task_id}_#{ctn.runtime_id}"
37
+ end
38
+
39
+ def get_port_forwarding_command(container: nil, remote_port:, remote_host:, local_port: nil)
40
+ container ||= 'app'
41
+ target = get_session_target(container: container)
42
+ ssm_client = Aws::SSM::Client.new({region: region})
43
+ local_port ||= (1050..1079).map(&:to_s).shuffle.first
44
+ puts "Starting to use local port: #{local_port}"
45
+ res = ssm_client.start_session({
46
+ target: target,
47
+ document_name: "AWS-StartPortForwardingSessionToRemoteHost",
48
+ parameters: {
49
+ portNumber: [remote_port],
50
+ localPortNumber: [local_port],
51
+ host: [remote_host]
52
+ }
53
+ })
54
+ build_session_manager_plugin_command(session: res, target: target)
55
+ end
56
+
57
+ def get_attach_command(container: nil)
58
+ container ||= 'app'
59
+ target = get_session_target(container: container)
35
60
  res = ecs_client.execute_command({
36
61
  cluster: cluster,
37
62
  container: container,
@@ -39,17 +64,26 @@ module Putpaws::Ecs
39
64
  interactive: true,
40
65
  task: ecs_task.task_arn,
41
66
  })
67
+ build_session_manager_plugin_command(session: res.session, target: target)
68
+ end
42
69
 
70
+ def build_session_manager_plugin_command(session:, target:)
43
71
  ssm_region = ENV['AWS_REGION_SSM'] || @region
44
-
72
+
45
73
  # https://github.com/aws/aws-cli/blob/2a6136010d8656a605d41d1e7b5fdab3c2930cad/awscli/customizations/ecs/executecommand.py#L105
46
- session_json = {
47
- "SessionId" => res.session.session_id,
48
- "StreamUrl" => res.session.stream_url,
49
- "TokenValue" => res.session.token_value,
50
- }.to_json
74
+ session_json = if session.respond_to?(:session_id)
75
+ {
76
+ "SessionId" => session.session_id,
77
+ "StreamUrl" => session.stream_url,
78
+ "TokenValue" => session.token_value,
79
+ }.to_json
80
+ elsif session.is_a?(Hash)
81
+ session.to_json
82
+ else
83
+ session
84
+ end
51
85
  target_json = {
52
- "Target" => "ecs:#{cluster}_#{task_id}_#{ctn.runtime_id}"
86
+ "Target" => target
53
87
  }.to_json
54
88
  cmd = [
55
89
  "session-manager-plugin",
@@ -22,7 +22,28 @@ namespace :ecs do
22
22
  ecs_task = fetch(:ecs_task)
23
23
  aws = Putpaws::Ecs::TaskCommand.config(fetch(:app))
24
24
  aws.ecs_task = ecs_task
25
- cmd = aws.get_attach_command
25
+ cmd = aws.get_attach_command(container: ENV['container'])
26
+ puts cmd
27
+ system(cmd)
28
+ end
29
+
30
+ desc "Run port forwarding session. You need to enable ECS Exec on a specified task and also install session-manager-plugin."
31
+ task forward: :set_task do
32
+ ecs_task = fetch(:ecs_task)
33
+ aws = Putpaws::Ecs::TaskCommand.config(fetch(:app))
34
+ aws.ecs_task = ecs_task
35
+
36
+ remote_host, remote_port = (ENV['remote'] || '').split(':').map(&:strip)
37
+ _, local_port = (ENV['local'] || '').split(':').map(&:strip)
38
+ if remote_host.nil? or remote_port.nil?
39
+ raise "Remote Host, Port is required: (Ex) remote=192.168.1.1:80"
40
+ end
41
+ cmd = aws.get_port_forwarding_command(
42
+ container: ENV['container'],
43
+ remote_host: remote_host,
44
+ remote_port: remote_port,
45
+ local_port: local_port
46
+ )
26
47
  puts cmd
27
48
  system(cmd)
28
49
  end
@@ -1,3 +1,3 @@
1
1
  module Putpaws
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: putpaws
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - metheglin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-18 00:00:00.000000000 Z
11
+ date: 2024-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk-ssm
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: aws-sdk-ecs
43
57
  requirement: !ruby/object:Gem::Requirement