ecs_helper 0.0.33 → 0.0.35

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: 871394ee1f21e808a91e123a5affccb713a0e7f44fcbb1d308bcc849eb97e992
4
- data.tar.gz: bd42356f7c7984047d85e41810fd12a9aa5a0ef95e63fdab370c38ff20278117
3
+ metadata.gz: 4b8c83b0859617581d45d79cb3a8ca067858266427fa3f1024f85aadb12640f5
4
+ data.tar.gz: 1a0416def41b5089f63b2a7beb98d61ec22e30e04c570da57025752a291ad7d0
5
5
  SHA512:
6
- metadata.gz: 709ccc270f6143206e63d4f55b2ea68b556f8929407e0a44cf9bfd626595b39973a76ade7376072371bb17b965ee56e8340650fc44f22467c15fe4dbd28512c8
7
- data.tar.gz: d7a5f1635642a612a3abe7c3c85887a91475c37fbe3f2bb2d15e8b793d39f7dff29018a55b85e12db43ca5a31ad36fcb3fdd539909bdc0ddce84a95c22eaa7d9
6
+ metadata.gz: 9682932c0088f66054ced4231eab331a8bc244a783db1e98958116dd6cebea145ea5fd24695e78d0b34a3f607831b6a42f6729e224252268d275b12199788282
7
+ data.tar.gz: cb72b81d624af0d0cfa68d4b8ea30d73fc1818aaef39117ab6bd470395b1e1fddc5257a38d3192d2da8f97d755ba034061c105c451f7f85e8ac517e5e17c3e17
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ecs_helper (0.0.33)
4
+ ecs_helper (0.0.35)
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)
@@ -18,7 +18,7 @@ GEM
18
18
  ast (2.4.2)
19
19
  awesome_print (1.9.2)
20
20
  aws-eventstream (1.2.0)
21
- aws-partitions (1.515.0)
21
+ aws-partitions (1.516.0)
22
22
  aws-sdk-core (3.121.1)
23
23
  aws-eventstream (~> 1, >= 1.0.2)
24
24
  aws-partitions (~> 1, >= 1.239.0)
@@ -97,6 +97,7 @@ GEM
97
97
 
98
98
  PLATFORMS
99
99
  aarch64-linux
100
+ arm64-darwin-22
100
101
  x86_64-darwin-18
101
102
  x86_64-linux
102
103
 
@@ -117,4 +118,4 @@ DEPENDENCIES
117
118
  webmock
118
119
 
119
120
  BUNDLED WITH
120
- 2.2.27
121
+ 2.4.12
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # ECS Helper - A tool for managing the deployment process of an application in Amazon Elastic Container Service (ECS)
2
+
3
+ ## Introduction
4
+
5
+ ECS Helper is a command-line tool written in Ruby that allows you to control the deployment process of your application
6
+ in Amazon Elastic Container Service. The tool provides various commands for building and pushing images, deploying your
7
+ application, exporting images, logging in to Amazon Elastic Container Registry (ECR), running commands, exporting
8
+ environment variables, and more. To use it an ECS Cluster with a service running there and have task_definitons is
9
+ required. Docker images are stored in the ECR Elastic Container Registry.
10
+
11
+ ## Installation
12
+
13
+ ### Ruby Gem
14
+
15
+ To use ECS Helper, you need to install the ecs_helper gem. You can install it using the following command:
16
+
17
+ ```bash
18
+ gem install ecs_helper
19
+ ```
20
+
21
+ You can use the ecs_helper command followed by the desired command and arguments to control your application deployment
22
+ process.
23
+
24
+ ### Docker Image
25
+
26
+ Alternatively, you can use the Docker image `partos/ecs_toolbox`. This image contains the ecs_helper gem and the AWS CLI
27
+ tool. You can use the image to run the ecs_helper command in a container.
28
+
29
+ ## The available commands are:
30
+
31
+ - **build_and_push**: builds and pushes the Docker image to Amazon Elastic Container Registry (ECR).
32
+ - **deploy**: deploys the Docker image to Amazon Elastic Container Service (ECS).
33
+ - **export_images**: exports Docker images to a file.
34
+ - **ecr_login**: logs in to Amazon Elastic Container Registry (ECR).
35
+ - **run_command**: runs a command in a container.
36
+ - **export_env_secrets**: exports environment variables to a file.
37
+ - **exec**: executes a command in a running container.
38
+ - **check_exec**: checks if the command in the running container was executed successfully.
39
+
40
+ You can select the desired command by passing the argument to the ecs_helper command. For example, to build and push an
41
+ image with the tag api, you can use the following command:
42
+
43
+ ```bash
44
+ ecs_helper build_and_push --image=api
45
+ ```
46
+
47
+ ## Using in GitLab CI
48
+
49
+ ECS Helper can also be used in GitLab CI by using a pre-built Docker image. Here's an example of how to use ECS Helper
50
+ in a GitLab CI pipeline:
51
+
52
+ ```yaml
53
+ stages:
54
+ - build
55
+ - deploy
56
+
57
+
58
+ variables:
59
+ DOCKER_DRIVER: overlay2
60
+ DOCKER_TLS_CERTDIR: ''
61
+ DOCKER_IMAGE: docker:20.10.6
62
+ PROJECT: test_project
63
+ TOOLBOX_IMAGE: partos/ecs_toolbox:0.0.34
64
+ AWS_REGION: us-east-1
65
+ AWS_DEFAULT_REGION: us-east-1
66
+ APPLICATION: app
67
+
68
+ .ci_deploy: &ci_deploy
69
+
70
+ only:
71
+ - master
72
+ - staging
73
+
74
+ build_app:
75
+ <<: *ci_deploy
76
+ stage: build
77
+ image: $TOOLBOX_IMAGE
78
+ script:
79
+ - mkdir -p ./apps/api/dist/apps && cp -r ./dist/apps/api ./apps/api/dist/apps
80
+ - ecs_helper build_and_push --image=api --cache -d ./apps/api
81
+
82
+ deploy_app:
83
+ <<: *ci_deploy
84
+ stage: deploy
85
+ image: $TOOLBOX_IMAGE
86
+ variables:
87
+ APPLICATION: app
88
+ script:
89
+ - ecs_helper deploy --timeout 600
90
+ ```
91
+
92
+ In this example, ECS Helper is used to build and push the api Docker image in the build_app job, and to deploy the
93
+ application in the deploy_app job.
94
+
95
+ When a new version of an application is deployed, a new task definition revision is created in the target service.
@@ -40,10 +40,6 @@ class ECSHelper::Client
40
40
  ecs.register_task_definition(params).task_definition
41
41
  end
42
42
 
43
- def deregister_task_definition(params = {})
44
- ecs.deregister_task_definition(params).task_definition
45
- end
46
-
47
43
  def update_service(params = {})
48
44
  ecs.update_service(params)
49
45
  end
@@ -62,4 +62,3 @@ class ECSHelper::Command::Base
62
62
  value.start_with?('=') ? value[1..-1] : value
63
63
  end
64
64
  end
65
-
@@ -24,7 +24,6 @@ class ECSHelper::Command::BuildAndPush < ECSHelper::Command::Base
24
24
  end
25
25
  opts.on('-c', '--cache', 'Cache image before build, default false') { options[:cache] = true }
26
26
  opts.on('--build-arg=VALUE', 'Pass --build-arg to the build command') { |o| options[:build_args] << o }
27
- opts.on('-e', '--env-prefix', 'Add environment name as a prefix to the version tag, default false') { options[:env_prefix] = true }
28
27
  end
29
28
  [parser, options]
30
29
  end
@@ -34,17 +33,17 @@ class ECSHelper::Command::BuildAndPush < ECSHelper::Command::Base
34
33
  end
35
34
 
36
35
  def run
37
- log("Command", type)
38
- log("Options", options)
39
- log("Repository", repository)
40
- log("Auth Private", auth_private)
41
- should_cache? && log("Pull", pull)
42
- log("Build", build)
43
- log("Push", push)
36
+ log('Command', type)
37
+ log('Options', options)
38
+ log('Repository', repository)
39
+ log('Auth Private', auth_private)
40
+ should_cache? && log('Pull', pull)
41
+ log('Build', build)
42
+ log('Push', push)
44
43
  end
45
44
 
46
45
  def auth_private
47
- auth_cmd = Terrapin::CommandLine.new('aws ecr get-login --no-include-email | sh')
46
+ auth_cmd = helper.auth_private_cmd
48
47
  auth_cmd.run
49
48
  end
50
49
 
@@ -91,11 +90,7 @@ class ECSHelper::Command::BuildAndPush < ECSHelper::Command::Base
91
90
  end
92
91
 
93
92
  def version_tag
94
- if options[:env_prefix]
95
- "#{repository}:#{helper.environment}-#{helper.version}"
96
- else
97
- "#{repository}:#{helper.version}"
98
- end
93
+ "#{repository}:#{helper.version}"
99
94
  end
100
95
 
101
96
  def project
@@ -34,14 +34,13 @@ class ECSHelper::Command::Deploy < ECSHelper::Command::Base
34
34
  log("Service task definition", service_task_definition.task_definition_arn)
35
35
  log("Containers", task_definition_helper.pretty_container_definitions)
36
36
  log("New task definition", new_task_definition.task_definition_arn)
37
- update_service(new_task_definition.task_definition_arn, service_task_definition.task_definition_arn) && log("Update service", "Service task definition was updated")
37
+ update_service(new_task_definition.task_definition_arn) && log("Update service", "Service task definition was updated")
38
38
  log("Waiting for deployment...")
39
39
  wait_for_deployment && log("Success", "Application was succesfully deployed", :cyan)
40
40
  end
41
41
 
42
- def update_service(task_definition_arn, old_task_definition_arn)
42
+ def update_service(task_definition_arn)
43
43
  helper.update_service(cluster_arn, service_arn, task_definition_arn)
44
- helper.client.deregister_task_definition(task_definition: old_task_definition_arn)
45
44
  end
46
45
 
47
46
  def wait_for_deployment(time = 0)
@@ -70,10 +69,10 @@ class ECSHelper::Command::Deploy < ECSHelper::Command::Base
70
69
  end
71
70
 
72
71
  def timeout
73
- options[:timeout] || DEFAULT_TIMEOUT
72
+ (options[:timeout] || DEFAULT_TIMEOUT).to_i
74
73
  end
75
74
 
76
75
  def service
77
76
  helper.client.describe_service(cluster_arn, service_arn)
78
77
  end
79
- end
78
+ end
@@ -20,7 +20,7 @@ class ECSHelper::Command::ECRLogin < ECSHelper::Command::Base
20
20
  end
21
21
 
22
22
  def auth_private
23
- auth_cmd = Terrapin::CommandLine.new("aws ecr get-login --no-include-email | sh")
23
+ auth_cmd = helper.auth_private_cmd
24
24
  auth_cmd.run
25
25
  end
26
26
  end
@@ -124,7 +124,7 @@ class ECSHelper::Command::RunCommand < ECSHelper::Command::Base
124
124
  end
125
125
 
126
126
  def timeout
127
- options[:timeout] || DEFAULT_TIMEOUT
127
+ (options[:timeout] || DEFAULT_TIMEOUT).to_i
128
128
  end
129
129
 
130
130
  def service
@@ -142,4 +142,4 @@ class ECSHelper::Command::RunCommand < ECSHelper::Command::Base
142
142
  def container_name
143
143
  options[:container_name]
144
144
  end
145
- end
145
+ end
@@ -1,5 +1,6 @@
1
1
  BRANCH_TO_ENV_MAPPING = {
2
2
  master: 'production',
3
+ main: 'production',
3
4
  qa: 'qa',
4
5
  uat: 'uat',
5
6
  staging: 'staging',
@@ -8,27 +9,66 @@ BRANCH_TO_ENV_MAPPING = {
8
9
 
9
10
  class ECSHelper::CommonHelper
10
11
  attr_accessor :helper, :branch, :version, :env
12
+
11
13
  def initialize(helper)
12
14
  @helper = helper
13
15
  end
14
16
 
15
17
  def branch
16
- @branch ||= ENV["CI_COMMIT_BRANCH"] || `git rev-parse --abbrev-ref HEAD`.strip
18
+ @branch ||= ENV['CI_COMMIT_BRANCH'] || `git rev-parse --abbrev-ref HEAD`.strip
17
19
  end
18
20
 
19
21
  def version
20
- @version ||= ENV["CI_COMMIT_SHA"] || `git rev-parse HEAD`.strip
22
+ @version ||=
23
+ begin
24
+ if use_image_tag_env_prefix?
25
+ "#{environment}-#{commit_sha}"
26
+ else
27
+ commit_sha
28
+ end
29
+ end
21
30
  end
22
31
 
23
32
  def environment
24
- @env ||= helper.options[:environment] || ENV["ENVIRONMENT"] || BRANCH_TO_ENV_MAPPING[branch.to_sym] || raise(StandardError.new("Environment not detected"))
33
+ @env ||= helper.options[:environment] || ENV['ENVIRONMENT'] || env_from_branch || raise(StandardError, 'Environment not detected')
25
34
  end
26
35
 
27
36
  def project
28
- ENV["PROJECT"]
37
+ ENV['PROJECT']
29
38
  end
30
39
 
31
40
  def application
32
- ENV["APPLICATION"]
41
+ ENV['APPLICATION']
42
+ end
43
+
44
+ def region
45
+ @region ||= ENV['AWS_REGION']
46
+ end
47
+
48
+ def account_id
49
+ @account_id ||= ENV['AWS_ACCOUNT_ID'] || `aws sts get-caller-identity --query "Account" --output text`.strip
33
50
  end
34
- end
51
+
52
+ def auth_private_cmd
53
+ @auth_private_cmd ||= Terrapin::CommandLine.new("docker login -u AWS -p $(aws ecr get-login-password --region=#{region}) #{account_id}.dkr.ecr.#{region}.amazonaws.com")
54
+ end
55
+
56
+ private
57
+
58
+ def env_from_branch
59
+ BRANCH_TO_ENV_MAPPING[branch.to_sym]
60
+ end
61
+
62
+ def deployable_branch?
63
+ !env_from_branch.nil?
64
+ end
65
+
66
+ def commit_sha
67
+ ENV['CI_COMMIT_SHA'] || `git rev-parse HEAD`.strip
68
+ end
69
+
70
+ def use_image_tag_env_prefix?
71
+ !ENV['USE_IMAGE_TAG_ENV_PREFIX'].nil?
72
+ end
73
+
74
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ECSHelper
4
- VERSION = '0.0.33'
4
+ VERSION = '0.0.35'
5
5
  end
data/lib/ecs_helper.rb CHANGED
@@ -17,7 +17,7 @@ class ECSHelper
17
17
  autoload :Command, 'ecs_helper/command'
18
18
 
19
19
  def_delegators :client, :task_definitions, :clusters, :services, :tasks, :repositories, :repositories, :task_definition, :run_task
20
- def_delegators :common_helper, :version, :branch, :environment, :project, :application
20
+ def_delegators :common_helper, :version, :branch, :environment, :project, :application, :region, :account_id, :auth_private_cmd
21
21
  def_delegators :cluster_helper, :current_cluster, :clusters
22
22
  def_delegators :service_helper, :current_service, :services, :update_service
23
23
  def_delegators :command, :run, :options, :type
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.33
4
+ version: 0.0.35
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-10-14 00:00:00.000000000 Z
11
+ date: 2023-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print
@@ -338,6 +338,7 @@ files:
338
338
  - Gemfile
339
339
  - Gemfile.lock
340
340
  - Makefile
341
+ - README.md
341
342
  - Rakefile
342
343
  - bin/console
343
344
  - bin/ecs_helper
@@ -385,8 +386,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
385
386
  - !ruby/object:Gem::Version
386
387
  version: '0'
387
388
  requirements: []
388
- rubyforge_project:
389
- rubygems_version: 2.7.6
389
+ rubygems_version: 3.4.12
390
390
  signing_key:
391
391
  specification_version: 4
392
392
  summary: ECSHelper