ecs_helper 0.0.25 → 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6fcdfa5ea72481d6e07bea7f9bf9ec044ca79ece2c680233f538ed7a4e3a7683
4
- data.tar.gz: de5b3ea5643b878c6c27989d8fce5a87771c3da25fef2968494fccd422e0774d
3
+ metadata.gz: d9360d89edeb8333e9caf152cb45a4a1d6042c3f614ab0d6d5d45e8d6bcd6e61
4
+ data.tar.gz: b3d2c66d829a1cf4b35e0d9dd476427287770fd14beb4e28729cc263d58ec878
5
5
  SHA512:
6
- metadata.gz: 5061f5c736a06834cfd0486891b82aa5d93cd0938acb9d7c8f8e492e01269b4ee6aac18a5d73a1eeacbb5a8c0ee45c34ea31d358a1729cfc8def3771c7a77d6b
7
- data.tar.gz: 4c488f4fc1b8badd9a82cee368dc1f21a7b505f047daa16ca51cf93f29e2559211d54a406ef2e05a6c3ffc39bf87d0192595129f7db871d2e0b70b00e0a925f9
6
+ metadata.gz: 20bc088dc9e4f3cbd8d04d83354faa39e3e319abcfe10335e0444fda5d755c32e883854f32a072c93f96129e98ba13b7d5dd53187a0b81dfc2cdcd3bc5bb8075
7
+ data.tar.gz: b588b0c7003c7e08afd46ce196b04cae8756c0c7b2ba3aafabbbc20c9a9e99ef319fc952d14f23a4bd9b19291714d20843149d86bb50371f689218868d5de4f3
data/Gemfile.lock CHANGED
@@ -1,10 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ecs_helper (0.0.24)
4
+ ecs_helper (0.0.29)
5
5
  aws-sdk-ecr (~> 1.42, >= 1.42)
6
6
  aws-sdk-ecrpublic (~> 1.3, >= 1.3)
7
7
  aws-sdk-ecs (~> 1.80, >= 1.80)
8
+ aws-sdk-ssm (~> 1.0.0.rc7)
8
9
  colorize (~> 0.8, >= 0.8)
9
10
  json (~> 2.5, >= 2.5)
10
11
  terrapin (~> 0.6, >= 0.6)
@@ -17,13 +18,13 @@ GEM
17
18
  ast (2.4.2)
18
19
  awesome_print (1.9.2)
19
20
  aws-eventstream (1.2.0)
20
- aws-partitions (1.496.0)
21
+ aws-partitions (1.503.0)
21
22
  aws-sdk-core (3.121.0)
22
23
  aws-eventstream (~> 1, >= 1.0.2)
23
24
  aws-partitions (~> 1, >= 1.239.0)
24
25
  aws-sigv4 (~> 1.1)
25
26
  jmespath (~> 1.0)
26
- aws-sdk-ecr (1.45.0)
27
+ aws-sdk-ecr (1.46.0)
27
28
  aws-sdk-core (~> 3, >= 3.120.0)
28
29
  aws-sigv4 (~> 1.1)
29
30
  aws-sdk-ecrpublic (1.6.0)
@@ -32,8 +33,12 @@ GEM
32
33
  aws-sdk-ecs (1.85.0)
33
34
  aws-sdk-core (~> 3, >= 3.120.0)
34
35
  aws-sigv4 (~> 1.1)
36
+ aws-sdk-ssm (1.0.0)
37
+ aws-sdk-core (~> 3)
38
+ aws-sigv4 (~> 1.0)
35
39
  aws-sigv4 (1.4.0)
36
40
  aws-eventstream (~> 1, >= 1.0.2)
41
+ bump (0.10.0)
37
42
  byebug (11.1.3)
38
43
  climate_control (0.2.0)
39
44
  coderay (1.1.3)
@@ -91,10 +96,12 @@ GEM
91
96
  hashdiff (>= 0.4.0, < 2.0.0)
92
97
 
93
98
  PLATFORMS
94
- x86_64-linux-musl
99
+ x86_64-darwin-18
100
+ x86_64-linux
95
101
 
96
102
  DEPENDENCIES
97
103
  awesome_print
104
+ bump
98
105
  bundler (~> 2.2)
99
106
  ecs_helper!
100
107
  minitest
@@ -109,4 +116,4 @@ DEPENDENCIES
109
116
  webmock
110
117
 
111
118
  BUNDLED WITH
112
- 2.2.22
119
+ 2.2.27
data/Makefile ADDED
@@ -0,0 +1,30 @@
1
+ .PHONY: test build bump-major bump-minor bump-patch help
2
+ .DEFAULT_GOAL := test
3
+ CURRENT_VERSION=$(shell bump current)
4
+ NEXT_PATCH=$(shell bump show-next patch)
5
+ NEXT_MINOR=$(shell bump show-next minor)
6
+ NEXT_MAJOR=$(shell bump show-next major)
7
+ GEM_VERSION=ecs_helper-${CURRENT_VERSION}.gem
8
+
9
+ test: ## Run the unit tests
10
+ bundle exec rake --trace
11
+
12
+ build: ## Build the ruby gem
13
+ gem build ecs_helper.gemspec
14
+
15
+ push:
16
+ gem push ${GEM_VERSION}
17
+
18
+ release:
19
+ bump set ${NEXT_PATCH}
20
+ make build
21
+ make push
22
+
23
+ bump-major: ## Bump the major version (1.0.0 -> 2.0.0)
24
+ bump major
25
+
26
+ bump-minor: ## Bump the minor version (0.1.0 -> 0.2.0)
27
+ bump minor
28
+
29
+ bump-patch: ## Bump the patch version (0.0.1 -> 0.0.2)
30
+ bump patch
data/ecs_helper.gemspec CHANGED
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  s.add_development_dependency 'rubocop'
30
30
  s.add_development_dependency 'rubocop-minitest'
31
31
  s.add_development_dependency 'webmock'
32
+ s.add_development_dependency 'bump'
32
33
 
33
34
  s.add_development_dependency "bundler", "~> 2.2"
34
35
  s.add_development_dependency "rake"
@@ -36,6 +37,7 @@ Gem::Specification.new do |s|
36
37
  s.add_runtime_dependency 'aws-sdk-ecr', '~> 1.42', '>= 1.42'
37
38
  s.add_runtime_dependency 'aws-sdk-ecrpublic', '~> 1.3', '>= 1.3'
38
39
  s.add_runtime_dependency 'aws-sdk-ecs', '~> 1.80', '>= 1.80'
40
+ s.add_runtime_dependency 'aws-sdk-ssm', '~> 1.0.0.rc7'
39
41
  s.add_runtime_dependency 'colorize', '~> 0.8', '>= 0.8'
40
42
  s.add_runtime_dependency 'json', '~> 2.5', '>= 2.5'
41
43
  s.add_runtime_dependency 'terrapin', '~> 0.6', '>= 0.6'
@@ -1,11 +1,13 @@
1
1
  require 'aws-sdk-ecs'
2
2
  require 'aws-sdk-ecr'
3
+ require 'aws-sdk-ssm'
3
4
 
4
5
  class ECSHelper::Client
5
- attr_accessor :ecs, :ecr
6
+ attr_accessor :ecs, :ecr, :ssm
6
7
  def initialize
7
8
  @ecs = Aws::ECS::Client.new
8
9
  @ecr = Aws::ECR::Client.new
10
+ @ssm = Aws::SSM::Client.new
9
11
  end
10
12
 
11
13
  # ECS
@@ -54,6 +56,14 @@ class ECSHelper::Client
54
56
  ecs.describe_tasks(params).tasks
55
57
  end
56
58
 
59
+ def list_tasks(params = {})
60
+ ecs.list_tasks(params).task_arns
61
+ end
62
+
63
+ def execute_command(params = {})
64
+ ecs.execute_command(params)
65
+ end
66
+
57
67
  # ECR
58
68
  def private_repositories(params = {})
59
69
  ecr.describe_repositories(params).repositories
@@ -62,4 +72,9 @@ class ECSHelper::Client
62
72
  def describe_images(params = {})
63
73
  ecr.describe_images(params).image_details[0]
64
74
  end
75
+
76
+ # SSM
77
+ def get_ssm_parameters(params = {})
78
+ ssm.get_parameters(params).parameters
79
+ end
65
80
  end
@@ -6,6 +6,7 @@ class ECSHelper::Command::Base
6
6
  include ECSHelper::Logging
7
7
  attr_accessor :type, :options, :helper, :client, :option_parser
8
8
 
9
+
9
10
  def initialize(helper)
10
11
  @client = helper.client
11
12
  @helper = helper
@@ -21,6 +22,15 @@ class ECSHelper::Command::Base
21
22
  helper.project
22
23
  end
23
24
 
25
+ def check_bin(bin)
26
+ check_cmd = Terrapin::CommandLine.new("which #{bin}")
27
+ result = check_cmd.run
28
+ "success"
29
+ rescue Terrapin::CommandNotFoundError, Terrapin::ExitStatusError => e
30
+ messages = ["#{bin} not found"]
31
+ raise ECSHelper::Error::BinNotFound.new(messages)
32
+ end
33
+
24
34
  def application
25
35
  helper.application
26
36
  end
@@ -28,20 +38,26 @@ class ECSHelper::Command::Base
28
38
  def validate
29
39
  required.each do |r|
30
40
  value = options[r]
31
- unless value
32
- puts "'#{r}' required for command '#{type}'".light_white
33
- puts option_parser.help
34
- exit
41
+ if value.nil?
42
+ messages = [
43
+ "'#{r}' required for command '#{type}'".light_white,
44
+ option_parser.help
45
+ ]
46
+ raise ECSHelper::Error::CommandValidationError.new(messages)
35
47
  end
36
48
  end
37
49
  end
38
50
 
39
- private
40
-
41
51
  def required
42
52
  []
43
53
  end
44
54
 
55
+ def printable?
56
+ false
57
+ end
58
+
59
+ private
60
+
45
61
  def processEqual(value)
46
62
  value.start_with?('=') ? value[1..-1] : value
47
63
  end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'terrapin'
4
+
5
+ class ECSHelper::Command::Exec < ECSHelper::Command::Base
6
+ def cmd_option_parser
7
+ options = { command: 'bash -c' }
8
+ parser = ::OptionParser.new do |opts|
9
+ opts.banner = 'Usage: ecs_helper exec [options]. require session-manager-plugin and aws cli v2'
10
+ opts.on('-p VALUE', '--project VALUE',
11
+ "Set project name, if not specified will look at ENV['PROJECT'], will be used to detect cluster") do |p|
12
+ options[:project] = processEqual(p)
13
+ end
14
+ opts.on('-a VALUE', '--application VALUE',
15
+ "Set application name, if not specified will look at ENV['APPLICATION'], will be used to detect service and task definition") do |a|
16
+ options[:application] = processEqual(a)
17
+ end
18
+ opts.on('-c', '--container VALUE', 'Cache image before build, default false') { |c| options[:container] = c }
19
+ opts.on('--command VALUE', 'Command to execute') { |c| options[:command] = c }
20
+ end
21
+ [parser, options]
22
+ end
23
+
24
+ def required
25
+ [:container]
26
+ end
27
+
28
+ def check_session_manager_plugin
29
+ check_bin('session-manager-plugin')
30
+ end
31
+
32
+ def check_aws_cli_version
33
+ check_cmd = Terrapin::CommandLine.new("aws --version")
34
+ result = check_cmd.run
35
+ version = parse_version(result)
36
+ if version === "1"
37
+ messages = [
38
+ "Exec command requires aws cli v2".light_white,
39
+ cmd_option_parser[0].help
40
+ ]
41
+ raise ECSHelper::Error::CommandValidationError.new(messages)
42
+ end
43
+ end
44
+
45
+ def check_aws_cli
46
+ check_bin('aws')
47
+ check_aws_cli_version
48
+ "success"
49
+ end
50
+
51
+
52
+ def check_aws_cli_version
53
+ check_cmd = Terrapin::CommandLine.new("aws --version")
54
+ result = check_cmd.run
55
+ version = parse_version(result)
56
+ if version === "1"
57
+ messages = [
58
+ "Exec command requires aws cli v2".light_white,
59
+ cmd_option_parser[0].help
60
+ ]
61
+ raise ECSHelper::Error::CommandValidationError.new(messages)
62
+ end
63
+ end
64
+
65
+ def run
66
+ log("Command", type)
67
+ log("Cluster", cluster_arn)
68
+ log("Service", service_arn)
69
+ log("Task", task_arn)
70
+ log("Options", options)
71
+ log("Check session-manager-plugin ", check_session_manager_plugin)
72
+ log("Check aws cli version", check_aws_cli)
73
+ exec
74
+ end
75
+
76
+ def exec
77
+ exec_cmd = Terrapin::CommandLine.new("aws ecs execute-command --cluster #{cluster_arn} --task #{task_arn} --container=#{helper.options[:container]} --command=#{helper.options[:command]} --interactive")
78
+ # --region us-east-1
79
+ exec_cmd.run
80
+ end
81
+
82
+ private
83
+
84
+ def parse_version(string)
85
+ string.split('/')[1][0]
86
+ end
87
+
88
+ def cluster_arn
89
+ helper.current_cluster
90
+ end
91
+
92
+ def service_arn
93
+ helper.current_service
94
+ end
95
+
96
+ def task_arn
97
+ @task_arn ||= helper.client.list_tasks({cluster: cluster_arn, service_name: service_arn, desired_status: "RUNNING"})[0]
98
+ end
99
+ end
@@ -0,0 +1,51 @@
1
+ require 'terrapin'
2
+
3
+ class ECSHelper::Command::ExportEnvSecrets < ECSHelper::Command::Base
4
+ attr_accessor :params
5
+
6
+ def cmd_option_parser
7
+ options = {
8
+ env_vars: []
9
+ }
10
+ parser = ::OptionParser.new do |opts|
11
+ opts.banner = "Usage: ecs_helper export_env_secrets"
12
+ opts.on('-n', '--name=VARIABLE', '') { |o| options[:env_vars] << o }
13
+ end
14
+ [parser, options]
15
+ end
16
+
17
+ def required
18
+ []
19
+ end
20
+
21
+ def printable?
22
+ true
23
+ end
24
+
25
+ def run
26
+ return log("No ENV secrets to export. Please pass ENV variables names using -n") if options[:env_vars].empty?
27
+ export_values
28
+ end
29
+
30
+ private
31
+
32
+ def to_aws_ssm_name(name)
33
+ "/#{helper.project}-#{helper.application}-#{helper.environment}/#{name}"
34
+ end
35
+
36
+ def export_values
37
+ params_name = options[:env_vars].map {|var_name| to_aws_ssm_name(var_name)}
38
+ aws_ssm_params = client.get_ssm_parameters(names: params_name, with_decryption: true)
39
+
40
+ variables = (['export'] + aws_ssm_params.map do |aws_ssm_param|
41
+ next if aws_ssm_param.empty?
42
+ value = aws_ssm_param.value
43
+ name = aws_ssm_param.name.split('/').last
44
+
45
+ "#{name}=#{value}"
46
+ rescue Aws::SSM::Errors::ParameterNotFound
47
+ next
48
+ end).join(' ')
49
+ variables
50
+ end
51
+ end
@@ -14,8 +14,12 @@ class ECSHelper::Command::ExportImages < ECSHelper::Command::Base
14
14
  []
15
15
  end
16
16
 
17
+ def printable?
18
+ true
19
+ end
20
+
17
21
  def run
18
- puts export_images
22
+ export_images
19
23
  end
20
24
 
21
25
  private
@@ -5,7 +5,9 @@ class ECSHelper::Command
5
5
  autoload :Deploy, 'ecs_helper/command/deploy'
6
6
  autoload :ExportImages, 'ecs_helper/command/export_images'
7
7
  autoload :ECRLogin, 'ecs_helper/command/ecr_login'
8
+ autoload :Exec, 'ecs_helper/command/exec'
8
9
  autoload :RunCommand, 'ecs_helper/command/run_command'
10
+ autoload :ExportEnvSecrets, 'ecs_helper/command/export_env_secrets'
9
11
 
10
12
  CMD_MAPPING = {
11
13
  "build_and_push" => BuildAndPush,
@@ -13,6 +15,8 @@ class ECSHelper::Command
13
15
  "export_images" => ExportImages,
14
16
  "ecr_login" => ECRLogin,
15
17
  "run_command" => RunCommand,
18
+ "export_env_secrets" => ExportEnvSecrets,
19
+ "exec" => Exec,
16
20
  }
17
21
  AVAILABLE_COMMANDS = CMD_MAPPING.keys
18
22
 
@@ -41,7 +45,9 @@ class ECSHelper::Command
41
45
 
42
46
  def run
43
47
  command.validate
44
- command.run
48
+ result = command.run
49
+ puts result if command.printable?
50
+ result
45
51
  end
46
52
  end
47
53
 
@@ -0,0 +1,2 @@
1
+ class ECSHelper::Error::BinNotFound < ECSHelper::Error::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class ECSHelper::Error::CommandValidationError < ECSHelper::Error::Base
2
+ end
@@ -3,4 +3,6 @@
3
3
  class ECSHelper::Error
4
4
  autoload :Base, 'ecs_helper/error/base'
5
5
  autoload :CommandNotFound, 'ecs_helper/error/command_not_found'
6
+ autoload :CommandValidationError, 'ecs_helper/error/command_validation_error'
7
+ autoload :BinNotFound, 'ecs_helper/error/bin_not_found'
6
8
  end
@@ -1,6 +1,6 @@
1
1
  module ECSHelper::Logging
2
2
  def console(message)
3
- puts message if ENV["SKIP_LOGS"].nil?
3
+ puts message if ENV["SKIP_LOGS"] != 'true'
4
4
  end
5
5
 
6
6
  def log(title, message = nil, color = "light_white")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ECSHelper
4
- VERSION = '0.0.25'
4
+ VERSION = '0.0.29'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecs_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.25
4
+ version: 0.0.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Petrov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-19 00:00:00.000000000 Z
11
+ date: 2021-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print
@@ -150,6 +150,20 @@ dependencies:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: bump
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
153
167
  - !ruby/object:Gem::Dependency
154
168
  name: bundler
155
169
  requirement: !ruby/object:Gem::Requirement
@@ -238,6 +252,20 @@ dependencies:
238
252
  - - "~>"
239
253
  - !ruby/object:Gem::Version
240
254
  version: '1.80'
255
+ - !ruby/object:Gem::Dependency
256
+ name: aws-sdk-ssm
257
+ requirement: !ruby/object:Gem::Requirement
258
+ requirements:
259
+ - - "~>"
260
+ - !ruby/object:Gem::Version
261
+ version: 1.0.0.rc7
262
+ type: :runtime
263
+ prerelease: false
264
+ version_requirements: !ruby/object:Gem::Requirement
265
+ requirements:
266
+ - - "~>"
267
+ - !ruby/object:Gem::Version
268
+ version: 1.0.0.rc7
241
269
  - !ruby/object:Gem::Dependency
242
270
  name: colorize
243
271
  requirement: !ruby/object:Gem::Requirement
@@ -309,6 +337,7 @@ files:
309
337
  - Dockerfile
310
338
  - Gemfile
311
339
  - Gemfile.lock
340
+ - Makefile
312
341
  - Rakefile
313
342
  - bin/console
314
343
  - bin/ecs_helper
@@ -322,12 +351,16 @@ files:
322
351
  - lib/ecs_helper/command/build_and_push.rb
323
352
  - lib/ecs_helper/command/deploy.rb
324
353
  - lib/ecs_helper/command/ecr_login.rb
354
+ - lib/ecs_helper/command/exec.rb
355
+ - lib/ecs_helper/command/export_env_secrets.rb
325
356
  - lib/ecs_helper/command/export_images.rb
326
357
  - lib/ecs_helper/command/run_command.rb
327
358
  - lib/ecs_helper/common_helper.rb
328
359
  - lib/ecs_helper/error.rb
329
360
  - lib/ecs_helper/error/base.rb
361
+ - lib/ecs_helper/error/bin_not_found.rb
330
362
  - lib/ecs_helper/error/command_not_found.rb
363
+ - lib/ecs_helper/error/command_validation_error.rb
331
364
  - lib/ecs_helper/logging.rb
332
365
  - lib/ecs_helper/service_helper.rb
333
366
  - lib/ecs_helper/task_definition_helper.rb