mina-infinum 2.0.0 → 2.1.0

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: c8b7cc4a4b5015428a7103241d34ec9ce0e672ed95b555f0a04d24b65640de89
4
- data.tar.gz: 6e9499a465eef1bd140a269351899fb08ca12ebc67def5e478c9cb4c50a535ab
3
+ metadata.gz: 7515a358a97fb531c9234b5af9f66d38b21ca179baa2630a6c0cdab7594bc81d
4
+ data.tar.gz: 0d1c15c942585a620cae300808ae5c509dbc8329fd68a17a67913e27203e3ab0
5
5
  SHA512:
6
- metadata.gz: b57397003d8fc8ed3f682c686bcf64ac41df029f62ec7fe65c061198f17b084bbc38ea7e3d3f1ff508f95ea76de4acbd60c3e30fd02056afeb1bb5dea387d4e7
7
- data.tar.gz: e864d2dbebfb3ef831ffc2f1c03d07074865e515a31ff16dbbf7192fcec9b84d73fac0ea659ca5c81eae31c93606791644f1d6c751341a325017f034192d76d7
6
+ metadata.gz: d636c8821a835dc329c263ebe7da0cafc1689aa4b4c41829988e243979c76f1867fedea43b1d2fdad509e11ff2bd96ed0e60a27645b217b47591056c29c386b0
7
+ data.tar.gz: 28ce51888667a163a17dd3274643f9071d1cc88fa2981f593454cb92a2940996665deb17d9512e6cdcfa0c3f319af8003e778e2632faae3306927bd89ab4c725
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [v2.1.0](https://github.com/infinum/mina-infinum/tree/v-2.1.0) (2025-05-29)
4
+ [Full Changelog](https://github.com/infinum/mina-infinum/compare/v2.0.0...v2.1.0)
5
+
6
+ ### Enhancements
7
+
8
+ - Add ECS tasks [\#5](https://github.com/infinum/mina-infinum/pull/5)
9
+
3
10
  ## [v2.0.0](https://github.com/infinum/mina-infinum/tree/v-2.0.0) (2021-12-03)
4
11
  [Full Changelog](https://github.com/infinum/mina-infinum/compare/v1.9.0...v2.0.0)
5
12
 
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Mina::Infinum
2
2
 
3
3
  For `mina 0.3.0` please take a look at [v0.3.0 branch](https://github.com/infinum/mina-infinum/tree/v0.3.0)
4
+
5
+ ## ECS
6
+
7
+ For ECS-related setup, see [this README](lib/mina/infinum/ecs/README.md).
8
+
4
9
  ## Plugins
5
10
 
6
11
  * [mina](https://github.com/mina-deploy/mina), '> 1.0'
@@ -0,0 +1,72 @@
1
+ # ECS tasks
2
+
3
+ This module contains common tasks for accessing ECS containers and Rails applications on them.
4
+
5
+ Tasks are split in several namespaces:
6
+ - `aws`: AWS-related commands (profile creation, SSO login)
7
+ - `db`: interaction with DBs (forwarding remote DB port to local machine)
8
+ - `ecs`: interaction with ECS containers (e.g. running commands)
9
+ - `rails`: interaction with Rails apps on ECS containers (e.g. opening the console)
10
+
11
+ To find available tasks, run `bundle exec mina --tasks`.<br />
12
+ To read detailed task descriptions, run `bundle exec mina --describe/-D`.
13
+
14
+ ## Setup
15
+
16
+ To get started, copy the following into `config/deploy.rb` and adjust variables:
17
+
18
+ ```ruby
19
+ require 'mina/infinum/ecs'
20
+
21
+ set :aws_profile, 'profile_name'
22
+ set :aws_source_profile, 'source_profile_name'
23
+ set :aws_region, 'region'
24
+ set :aws_role_arn, 'role_arn'
25
+ # set :aws_login_profile, 'login_profile_name' (uses :aws_source_profile by default)
26
+
27
+ set :service, 'service_name'
28
+ # set :db_local_port, 4242 (default: 9999)
29
+
30
+ # set :shell, 'zsh' (default: 'bash')
31
+
32
+ task :staging do
33
+ set :rails_env, 'staging'
34
+ set :aws_jump_server_id, 'i-ec2_instance_id'
35
+ set :db_host, 'production_db_host'
36
+ # set :db_port, 3306 (default: 5432)
37
+ set :cluster, 'production_cluster_name'
38
+ end
39
+
40
+ task :production do
41
+ set :rails_env, 'production'
42
+ set :aws_jump_server_id, 'i-ec2_instance_id'
43
+ set :db_host, 'production_db_host'
44
+ # set :db_port, 3306 (default: 5432)
45
+ set :cluster, 'production_cluster_name'
46
+ end
47
+ ```
48
+
49
+ ## Debug mode
50
+
51
+ To see debug output, append `debug=true` to command, e.g.: `bundle exec mina staging db:proxy debug=true`:
52
+ ```
53
+ $ bundle exec mina staging db:proxy debug=true
54
+
55
+ 2025-05-27 09:32:14,455 - MainThread - awscli.clidriver - DEBUG - CLI version: aws-cli/2.2.6 Python/3.8.8 Darwin/24.5.0 exe/x86_64
56
+ 2025-05-27 09:32:14,455 - MainThread - awscli.clidriver - DEBUG - Arguments entered to CLI: ['configure', 'list-profiles', '--debug']
57
+ # and so on...
58
+ ```
59
+
60
+ ## Verbose mode
61
+
62
+ To see commands before they run, append `verbose=true`, e.g.: `bundle exec mina staging db:proxy verbose=true`:
63
+ ```
64
+ $ bundle exec mina staging db:proxy verbose=true
65
+
66
+ $ aws configure list-profiles
67
+ $ aws ssm start-session --target aws_jump_server_id --document-name AWS-StartPortForwardingSessionToRemoteHost --parameters host="db_host",portNumber="5432",localPortNumber="9999" --profile aws_profile
68
+
69
+ Starting session with SessionId: botocore-session-12345
70
+ Port 9999 opened for sessionId botocore-session-12345.
71
+ Waiting for connections...
72
+ ```
@@ -0,0 +1,93 @@
1
+ require 'English'
2
+
3
+ set :debug, false
4
+
5
+ namespace :aws do
6
+ task :ensure_cli_version do
7
+ version = run_cmd "aws --version #{'--debug' if debug?}"
8
+
9
+ error! 'AWS CLI version 2 is required' unless version.start_with?('aws-cli/2')
10
+
11
+ puts "AWS CLI v2 is intalled. Version: #{version}" if debug?
12
+ end
13
+
14
+ desc <<~TXT
15
+ Log in to AWS
16
+
17
+ Retrieves an SSO access token from AWS. Prerequisite for any task
18
+ which requires authentication (for example, ecs:connect).
19
+
20
+ Logs in with profile :aws_login_profile if defined, otherwise uses
21
+ :aws_source_profile.
22
+ TXT
23
+ task login: [:ensure_cli_version] do
24
+ ensure!(:aws_source_profile)
25
+
26
+ login_profile = fetch(:aws_login_profile, fetch(:aws_source_profile))
27
+
28
+ run_cmd "aws sso login --profile #{login_profile}", exec: true
29
+ end
30
+
31
+ desc <<~TXT
32
+ Open AWS console in the browser
33
+
34
+ The console is opened in region :aws_region.
35
+ TXT
36
+ task console: ['profile:check'] do
37
+ ensure!(:aws_region)
38
+
39
+ region = fetch(:aws_region)
40
+
41
+ run_cmd "open https://#{region}.console.aws.amazon.com/console/home?region=#{region}", exec: true
42
+ end
43
+
44
+ namespace :profile do
45
+ task :check do
46
+ ensure!(:aws_profile)
47
+
48
+ unless profile_exists?(fetch(:aws_profile))
49
+ error! "Please create AWS profile '#{fetch(:aws_profile)}' first (use task aws:profile:create)"
50
+ end
51
+ end
52
+
53
+ desc <<~TXT
54
+ Create AWS profile
55
+
56
+ Creates profile :aws_profile on local machine. Prerequisite for
57
+ any task which uses AWS resources (for example, ecs:connect).
58
+
59
+ The profile is set up with parameters:
60
+ - source_profile -> :aws_source_profile
61
+ - region -> :aws_region
62
+ - role_arn -> :aws_role_arn
63
+ TXT
64
+ task create: [:ensure_cli_version] do
65
+ ensure!(:aws_profile)
66
+ ensure!(:aws_source_profile)
67
+ ensure!(:aws_region)
68
+ ensure!(:aws_role_arn)
69
+
70
+ unless profile_exists?(fetch(:aws_source_profile))
71
+ error! "Please create AWS source profile first - #{fetch(:aws_source_profile)}"
72
+ end
73
+
74
+ if profile_exists?(fetch(:aws_profile)) && !fetch(:force)
75
+ puts "Profile '#{fetch(:aws_profile)}' already exists, add 'force=true' to overwrite it"
76
+ else
77
+ puts "Creating profile '#{fetch(:aws_profile)}'..."
78
+ run_cmd "aws configure set source_profile #{fetch(:aws_source_profile)} --profile #{fetch(:aws_profile)} #{'--debug' if debug?}"
79
+ run_cmd "aws configure set region #{fetch(:aws_region)} --profile #{fetch(:aws_profile)} #{'--debug' if debug?}"
80
+ run_cmd "aws configure set role_arn #{fetch(:aws_role_arn)} --profile #{fetch(:aws_profile)} #{'--debug' if debug?}"
81
+ puts 'Done'
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ def profile_exists?(profile)
88
+ profiles = run_cmd "aws configure list-profiles #{'--debug' if debug?}"
89
+
90
+ error! 'Cannot list AWS profiles' unless $CHILD_STATUS.success?
91
+
92
+ profiles.split("\n").include?(profile)
93
+ end
@@ -0,0 +1,33 @@
1
+ require 'mina/infinum/ecs/aws'
2
+
3
+ namespace :db do
4
+ desc <<~TXT
5
+ Forward remote DB port to local port through an SSM session
6
+
7
+ Connects to the AWS resource :aws_jump_server_id through an SSM session
8
+ and forwards port :db_port (default: 5432) on host :db_host to local
9
+ port :db_local_port (default: 9999). Uses profile :aws_profile.
10
+
11
+ You can override local port in config/deploy.rb, or inline:
12
+ $ mina db:proxy db_local_port=4242
13
+ TXT
14
+ task proxy: ['aws:profile:check'] do
15
+ ensure!(:aws_jump_server_id)
16
+ ensure!(:db_host)
17
+
18
+ profile = fetch(:aws_profile)
19
+ jump_server_id = fetch(:aws_jump_server_id)
20
+ host = fetch(:db_host)
21
+ remote_port = fetch(:db_port, 5432)
22
+ local_port = fetch(:db_local_port, 9999)
23
+
24
+ run_cmd squish(<<~CMD), exec: true
25
+ aws ssm start-session
26
+ --target #{jump_server_id}
27
+ --document-name AWS-StartPortForwardingSessionToRemoteHost
28
+ --parameters host="#{host}",portNumber="#{remote_port}",localPortNumber="#{local_port}"
29
+ --profile #{profile}
30
+ #{'--debug' if debug?}
31
+ CMD
32
+ end
33
+ end
@@ -0,0 +1,60 @@
1
+ require 'json'
2
+ require 'mina/infinum/ecs/aws'
3
+
4
+ namespace :ecs do
5
+ desc <<~TXT
6
+ Execute a command on ECS container
7
+
8
+ Executes command :command in interactive mode on container in active
9
+ task on cluster :cluster for service :service. Uses profile :aws_profile.
10
+
11
+ Command is provided as a rake task argument:
12
+ $ mina "ecs:exec[command]"
13
+ TXT
14
+ task :exec, [:command] => ['aws:profile:check'] do |_, args|
15
+ ensure!(:cluster)
16
+ ensure!(:service)
17
+
18
+ command = args.fetch(:command) { error! 'Command is a required argument' }
19
+
20
+ task_arn = find_task_arn
21
+
22
+ run_cmd squish(<<~CMD), exec: true
23
+ aws ecs execute-command
24
+ --task #{task_arn}
25
+ --command \"#{command}\"
26
+ --cluster #{fetch(:cluster)}
27
+ --profile #{fetch(:aws_profile)}
28
+ --interactive
29
+ #{'--debug' if debug?}
30
+ CMD
31
+ end
32
+
33
+ desc <<~TXT
34
+ Connect to the ECS container
35
+
36
+ Uses ecs:exec task to start a shell on the container.
37
+
38
+ The shell is defined with :shell (default: 'bash').
39
+ TXT
40
+ task :connect do
41
+ invoke 'ecs:exec', fetch(:shell, 'bash')
42
+ end
43
+ end
44
+
45
+ def find_task_arn
46
+ output = run_cmd squish(<<~CMD)
47
+ aws ecs list-tasks
48
+ --output json
49
+ --cluster #{fetch(:cluster)}
50
+ --service #{fetch(:service)}
51
+ --profile #{fetch(:aws_profile)}
52
+ #{'--debug' if debug?}
53
+ CMD
54
+
55
+ unless $CHILD_STATUS.success?
56
+ error! "Cannot list ECS tasks... do you need to log in (use task aws:login)? For more info, add debug=true to command"
57
+ end
58
+
59
+ JSON.parse(output).dig('taskArns', 0) || error!('There are no task definitions')
60
+ end
@@ -0,0 +1,42 @@
1
+ require 'mina/infinum/ecs/ecs'
2
+
3
+ desc 'Execute a rails command'
4
+ desc <<~TXT
5
+ Execute a Rails command
6
+
7
+ Uses ecs:exec task to execute a Rails command on the container.
8
+
9
+ Command is provided as a rake task argument:
10
+ $ mina "rails[command]"
11
+ and is executed on the container as:
12
+ $ bundle exec rails [command]
13
+ TXT
14
+ task :rails, [:command] do |_, args|
15
+ ensure!(:rails_env)
16
+
17
+ command = args.fetch(:command) { error! 'Command is a required argument' }
18
+
19
+ invoke 'ecs:exec', "bundle exec rails #{command}"
20
+ end
21
+
22
+ namespace :rails do
23
+ desc <<~TXT
24
+ Open rails console
25
+
26
+ Runs "bundle exec rails console" on the ECS container.
27
+ TXT
28
+ task :console do
29
+ invoke 'rails', 'console'
30
+ end
31
+
32
+ desc <<~TXT
33
+ Tail application log
34
+
35
+ Log is tailed from `log` folder for environment :rails_env.
36
+ TXT
37
+ task :log do
38
+ ensure!(:rails_env)
39
+
40
+ invoke 'ecs:exec', "tail -f log/#{fetch(:rails_env)}.log"
41
+ end
42
+ end
@@ -0,0 +1,27 @@
1
+ require 'mina/default'
2
+ require 'mina/infinum/ecs/rails'
3
+ require 'mina/infinum/ecs/db'
4
+
5
+ # INFO: hides default Mina tasks when running "mina --tasks"
6
+ Rake::Task['run'].clear_comments
7
+ Rake::Task['ssh'].clear_comments
8
+ Rake::Task['ssh_keyscan_domain'].clear_comments
9
+ Rake::Task['ssh_keyscan_repo'].clear_comments
10
+
11
+ def squish(command)
12
+ command.gsub(/[[:space:]]+/, ' ').strip
13
+ end
14
+
15
+ def run_cmd(cmd, exec: false)
16
+ print_command(cmd) if fetch(:verbose)
17
+
18
+ if exec
19
+ Kernel.exec cmd
20
+ else
21
+ Kernel.` cmd
22
+ end
23
+ end
24
+
25
+ def debug?
26
+ fetch(:debug, false)
27
+ end
@@ -1,5 +1,5 @@
1
1
  module Mina
2
2
  module Infinum
3
- VERSION = '2.0.0'.freeze
3
+ VERSION = '2.1.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mina-infinum
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stjepan Hadjic
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2021-12-03 00:00:00.000000000 Z
10
+ date: 2025-05-29 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: bundler
@@ -141,6 +140,12 @@ files:
141
140
  - bin/console
142
141
  - bin/setup
143
142
  - lib/mina/infinum.rb
143
+ - lib/mina/infinum/ecs.rb
144
+ - lib/mina/infinum/ecs/README.md
145
+ - lib/mina/infinum/ecs/aws.rb
146
+ - lib/mina/infinum/ecs/db.rb
147
+ - lib/mina/infinum/ecs/ecs.rb
148
+ - lib/mina/infinum/ecs/rails.rb
144
149
  - lib/mina/infinum/helpers.rb
145
150
  - lib/mina/infinum/tasks.rb
146
151
  - lib/mina/infinum/version.rb
@@ -150,7 +155,6 @@ licenses:
150
155
  - MIT
151
156
  metadata:
152
157
  allowed_push_host: https://rubygems.org
153
- post_install_message:
154
158
  rdoc_options: []
155
159
  require_paths:
156
160
  - lib
@@ -165,8 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
169
  - !ruby/object:Gem::Version
166
170
  version: '0'
167
171
  requirements: []
168
- rubygems_version: 3.1.4
169
- signing_key:
172
+ rubygems_version: 3.6.2
170
173
  specification_version: 4
171
174
  summary: Collection of mina plugins we use in infinum
172
175
  test_files: []