ecs-rails 0.0.1 → 0.0.3

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: 6381c88cb4b0507a1f9f667290768c0b0d060b1308108b4699e7b0d6107f048b
4
- data.tar.gz: 1ca8e857d5ea22a9dae7e07388c5124f85f41a81d20b4a5e169852ce942fcba0
3
+ metadata.gz: 2c315c2e992cf5b44e419f99fbca24d590b67bde002ff99c87eace906039d479
4
+ data.tar.gz: eea04176308d22f38e5c4ae44ce4bf9fff10f5e45f33ac5a1904b794e602791f
5
5
  SHA512:
6
- metadata.gz: ab8bda757afb402c1cdb8096a24ec51a8973fb78e8be9c6f4241e936b68aff153f3d913fa5c15d7336421365972e750130bb36ce91a3da410cf6077cd47321f8
7
- data.tar.gz: 87c30bb9091b7fff68cb535770d22dd4f6d8da21ee9c119eeec06e649f36a8e42f9e9a27d8c2815c67d2894904ff7b38c3c879c79465f3f34e28773c0406f251
6
+ metadata.gz: bd23d26514639ac9f015bb8af4b82491a0896e670c10b701f642981d8a2872b1230c009f570c5dc6a9a7f787842c7972b2573762de69b7b3ac62289a5c7a8142
7
+ data.tar.gz: e6f6e8043df16a541c81cdba7cf316c301e8a850082de85a694b0b7c1b8e3c27064e29431857fd66764de7ffeaa01c1f7c6983c9e755fdc32e21ffd4e75df244
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Franck D'agostini
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ If you are hosting your Rails app on ECS Fargate and want to connect to the Rails console or launch a bash session, you need to use AWS cli like so :
2
+
3
+ ```bash
4
+ aws ecs execute-command --region eu-west-3 --cluster CLUSTER_ARN --task TASK_ARN --container CONTAINER_NAME --command 'bundle exec rails console' --interactive
5
+ ```
6
+
7
+ This gem helps to get the correct cluster arn and task id so that you don't have to get them yourself.
8
+
9
+ ```bash
10
+ ecs console
11
+ ecs bash
12
+ ```
13
+
14
+ # Installation
15
+
16
+ Install [AWS cli](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
17
+
18
+ Install [AWS Session Manager plugin](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html)
19
+
20
+ Install gem
21
+
22
+ ```ruby
23
+ gem install 'ecs-rails'
24
+ ```
25
+
26
+ # Configuration
27
+
28
+ ## Plain Ruby
29
+
30
+ Via environment variables:
31
+
32
+ ```
33
+ export ENV['AWS_REGION'] = 'us-east-1'
34
+ export ENV['AWS_ACCESS_KEY_ID'] = 'your-access-key-id'
35
+ export ENV['AWS_SECRET_ACCESS_KEY'] = 'your-secret-access-key'
36
+ export ENV['CONTAINER_NAME'] = 'your-container-name'
37
+ ```
38
+
39
+ ## Rails
40
+
41
+ ```ruby
42
+ # config/initializers/ecs-rails.rb
43
+ EcsRails.aws_region = 'us-east-1'
44
+ EcsRails.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
45
+ EcsRails.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
46
+ EcsRails.container_name = 'webapp'
47
+ ```
48
+
49
+ # Usage
50
+
51
+ Connect to Rails console on a running Task.
52
+
53
+ ```ruby
54
+ ecs console
55
+
56
+ Select a cluster:
57
+ 1) cluster-prod
58
+ 2) cluster-staging
59
+ Choose 1-2 [1]:
60
+
61
+ Select a service:
62
+ 1) app
63
+ 2) worker
64
+ Choose 1-2 [1]:
65
+
66
+ irb(main)>
67
+ ```
68
+
69
+ You can specify cluster name via -c option by giving a string included in cluster arn.
70
+ You can specify service name via -s option by giving a string included in service arn.
71
+
72
+ ```ruby
73
+ # with cluster name: webapp-cluster-prod-e950a13
74
+ # with service name: webapp-app-prod-7c7cad7
75
+ ecs console -c prod -s app
76
+ irb(main)>
77
+ ```
data/bin/ecs CHANGED
@@ -1,15 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'byebug'
4
-
5
4
  require 'optparse'
6
5
  require_relative '../lib/ecs-rails'
7
6
 
8
- EcsRails.aws_region = ENV['AWS_REGION']
9
- EcsRails.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
10
- EcsRails.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
11
- EcsRails.container_name = ENV['CONTAINER_NAME'] || 'webapp'
12
-
13
7
  options = {}
14
8
  OptionParser.new do |opts|
15
9
  opts.banner = "Usage: ecsconsole [options]"
@@ -25,9 +19,4 @@ end.parse!
25
19
 
26
20
  command_keyword = ARGV[0]
27
21
 
28
- # byebug
29
-
30
- # command = EcsRails::CommandFactory.new(command_keyword, options).command
31
- # command.call
32
-
33
22
  EcsRails::CommandExecutor.call(command_keyword, options)
data/ecs-rails.gemspec CHANGED
@@ -16,6 +16,8 @@ Gem::Specification.new do |s|
16
16
 
17
17
  s.files = `git ls-files -z`.split("\x0")
18
18
 
19
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+
19
21
  s.add_development_dependency 'byebug', '~> 11.1'
20
22
  s.add_development_dependency 'rspec', '~> 3.6', '>= 3.6.0'
21
23
 
@@ -0,0 +1,22 @@
1
+ module EcsRails
2
+ class Bash < Command
3
+
4
+ def call
5
+ return full_command if test_mode?
6
+
7
+ puts "Executing command: #{full_command}"
8
+ system(full_command)
9
+ end
10
+
11
+ private
12
+
13
+ def full_command
14
+ @full_command ||= "aws ecs execute-command --region #{region} --cluster #{selected_cluster} --task #{task_id} --container #{container_name} --command \'#{command}\' --interactive"
15
+ end
16
+
17
+
18
+ def command
19
+ "bash"
20
+ end
21
+ end
22
+ end
@@ -38,10 +38,7 @@ module EcsRails
38
38
  attr_reader :client, :filter
39
39
 
40
40
  def connecting_to_cluster(cluster_name)
41
- puts('')
42
41
  puts("Connecting to cluster '#{cluster_name}':")
43
- puts('')
44
- puts('=' * 50)
45
42
  end
46
43
 
47
44
  def ask_for_cluster(clusters)
@@ -31,12 +31,10 @@ module EcsRails
31
31
  end
32
32
 
33
33
  def selected_service
34
- puts("Selected cluster: #{selected_cluster}")
35
34
  @selected_service ||= EcsRails::ServiceSelector.new(client, selected_cluster, service_name).call
36
35
  end
37
36
 
38
37
  def task_id
39
- puts("Selected service: #{selected_service}")
40
38
  @task_id ||= EcsRails::TaskSelector.new(client, selected_cluster, selected_service).call
41
39
  end
42
40
 
@@ -0,0 +1,9 @@
1
+ module EcsRails
2
+ class CommandExecutor
3
+
4
+ def self.call(command_keyword, options = {})
5
+ CommandFactory.new(command_keyword, options).command.call
6
+ end
7
+
8
+ end
9
+ end
@@ -12,10 +12,10 @@ module EcsRails
12
12
  end
13
13
 
14
14
  def initialize
15
- @aws_region = 'us-east-1'
16
- @aws_access_key_id = nil
17
- @aws_secret_access_key = nil
18
- @container_name = nil
15
+ @aws_region = ENV['AWS_REGION'] || 'us-east-1'
16
+ @aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
17
+ @aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
18
+ @container_name = ENV['CONTAINER_NAME'] || 'webapp'
19
19
  end
20
20
 
21
21
  def aws_region=(region)
@@ -43,10 +43,7 @@ module EcsRails
43
43
  attr_reader :client, :cluster, :service_filter
44
44
 
45
45
  def connecting_to_service(service_name)
46
- puts('')
47
46
  puts("Connecting to service '#{service_name}':")
48
- puts('')
49
- puts('=' * 50)
50
47
  end
51
48
 
52
49
  def ask_for_service(services)
@@ -27,20 +27,8 @@ module EcsRails
27
27
  puts(" Task: #{task_arns.first}")
28
28
  return task_arns.first
29
29
  else
30
- tasks_for_deployment.each_with_index do |task, index|
31
- puts("#{index+1}. Task: #{task.task_arn.split('/').last}, Last Status: #{task.last_status}, Desired Status: #{task.desired_status}")
32
- end
30
+ ask_for_task(tasks_for_deployment)
33
31
  end
34
-
35
- selection = $stdin.gets.chomp.to_i
36
- puts("Selected task: #{tasks_for_deployment[selection - 1].task_arn}")
37
-
38
- unless selection.between?(1, task_arns.size)
39
- puts('Invalid selection.')
40
- exit
41
- end
42
-
43
- return task_arns[selection - 1]
44
32
  end
45
33
  end
46
34
  end
@@ -48,5 +36,12 @@ module EcsRails
48
36
  private
49
37
 
50
38
  attr_reader :client, :cluster_name, :service_name
39
+
40
+ def ask_for_task(tasks)
41
+ prompt = TTY::Prompt.new
42
+ choices = tasks.map { |task| task.split('/').last }
43
+ choice = prompt.enum_select("Select a task:", choices)
44
+ tasks.grep(Regexp.new(choice)).first
45
+ end
51
46
  end
52
47
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EcsRails
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.3'
5
5
  end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Ecs binary' do
4
+
5
+ context 'without arguments' do
6
+ it 'should run ecs help' do
7
+ output = EcsRails::CommandExecutor.call('', {})
8
+ expect(output).to include('You must specify a command')
9
+ end
10
+ end
11
+
12
+ context 'with console command argument' do
13
+ it 'calls the command factory with correct command keyword' do
14
+ command_factory_instance = double('command_factory_instance')
15
+
16
+ expect(EcsRails::CommandFactory).to receive(:new).with('console', {}).and_return(command_factory_instance)
17
+
18
+ command_instance = double('command_instance')
19
+
20
+ expect(command_factory_instance).to receive(:command).and_return(command_instance)
21
+ expect(command_instance).to receive(:call)
22
+
23
+ EcsRails::CommandExecutor.call('bash', {})
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ require 'ecs-rails'
2
+ require 'byebug'
3
+
4
+ RSpec.configure do |config|
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecs-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franck D'agostini
@@ -106,18 +106,23 @@ dependencies:
106
106
  version: '0.23'
107
107
  description: Connect to your AWS ECS tasks
108
108
  email: franck.dagostini@gmail.com
109
- executables: []
109
+ executables:
110
+ - ecs
110
111
  extensions: []
111
112
  extra_rdoc_files: []
112
113
  files:
113
114
  - ".gitignore"
114
115
  - Gemfile
116
+ - LICENSE
117
+ - README.md
115
118
  - bin/ecs
116
119
  - ecs-rails.gemspec
117
120
  - lib/ecs-rails.rb
121
+ - lib/ecs-rails/bash.rb
118
122
  - lib/ecs-rails/client.rb
119
123
  - lib/ecs-rails/cluster_selector.rb
120
124
  - lib/ecs-rails/command.rb
125
+ - lib/ecs-rails/command_executor.rb
121
126
  - lib/ecs-rails/command_factory.rb
122
127
  - lib/ecs-rails/console.rb
123
128
  - lib/ecs-rails/ecs_rails_configuration.rb
@@ -125,6 +130,8 @@ files:
125
130
  - lib/ecs-rails/service_selector.rb
126
131
  - lib/ecs-rails/task_selector.rb
127
132
  - lib/ecs-rails/version.rb
133
+ - spec/bin/ecs_spec.rb
134
+ - spec/spec_helper.rb
128
135
  homepage: https://rubygems.org/gems/ecs-rails
129
136
  licenses:
130
137
  - MIT