putpaws 0.0.6 → 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: '0880d0887eb118ff264bf873fdbe215e039329e9dfc58d004885c7f728f5730a'
4
- data.tar.gz: 25114357952cb895486c6068e1115cf2d8baea0d3a23b20c7e5d569e73acbb93
3
+ metadata.gz: e5383c31d8b2b94dcb948e565f9ab5784137a0b946aa53e2ade93fc5e1bf3152
4
+ data.tar.gz: 30babace1c15c9675752567795e4c43414be155cc0f9affccfdb8bd8907a215d
5
5
  SHA512:
6
- metadata.gz: e3a52e4bb01dc8d0825a96b52083935f9b630256576a36f780fbefea1d8925a0a3a71ecf1be7524026032d38d9ea4a682d6bd11d79294f52c33ea04f9ffefb80
7
- data.tar.gz: 584f4a1b234e0561e5ed0278c8c00410df99577926e4a087671e378b41f3f7541d20a9b6ca857dbd9d26eb51e604960b2f1a5439ea16bfcfcc6eba7590fef1ea
6
+ metadata.gz: f506cafed6700e2fc5bff6334e7e3374e528652740de99c03a36e89a031603b891b77133b5264fff51f8d1d3bc7db22f8549c88a32658f87883249c8cfe9d331
7
+ data.tar.gz: 941ceaa13526fd71c3a37d5d6541490950472dea6bff75aff2b495acc10adb41f46b270e14a6c279d542863792594896443aeca882655321b856a745c38306e0
@@ -12,9 +12,9 @@ module Putpaws::CloudWatch
12
12
 
13
13
  def self.config(config, type: "default")
14
14
  if type == "build"
15
- new(config.build_log_command_params)
15
+ new(**config.build_log_command_params)
16
16
  else
17
- new(config.log_command_params)
17
+ new(**config.log_command_params)
18
18
  end
19
19
  end
20
20
 
@@ -1,9 +1,10 @@
1
1
  require 'aws-sdk-ecs'
2
+ require 'aws-sdk-ssm'
2
3
 
3
4
  module Putpaws::Ecs
4
5
  class TaskCommand
5
6
  def self.config(config)
6
- new(config.ecs_command_params)
7
+ new(**config.ecs_command_params)
7
8
  end
8
9
 
9
10
  attr_reader :ecs_client
@@ -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
@@ -3,7 +3,7 @@ require 'aws-sdk-scheduler'
3
3
  module Putpaws::Scheduler
4
4
  class ScheduleCommand
5
5
  def self.config(config)
6
- new(config.log_command_params)
6
+ new(**config.log_command_params)
7
7
  end
8
8
 
9
9
  attr_reader :client
@@ -1,3 +1,3 @@
1
1
  module Putpaws
2
- VERSION = "0.0.6"
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.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - metheglin
8
- autorequire:
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
@@ -117,7 +131,7 @@ homepage: https://rubygems.org/gems/putpaws
117
131
  licenses:
118
132
  - MIT
119
133
  metadata: {}
120
- post_install_message:
134
+ post_install_message:
121
135
  rdoc_options: []
122
136
  require_paths:
123
137
  - lib
@@ -132,8 +146,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
146
  - !ruby/object:Gem::Version
133
147
  version: '0'
134
148
  requirements: []
135
- rubygems_version: 3.1.4
136
- signing_key:
149
+ rubygems_version: 3.4.10
150
+ signing_key:
137
151
  specification_version: 4
138
152
  summary: Put your paws up!!
139
153
  test_files: []