smdev 0.4.0 → 0.5.0

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: 6fe50e69b90b235fbc3901bc01411e560676f55443f2f3cb1a064f4c400f0183
4
- data.tar.gz: eb6ade0d5e8b4c649da5707c51e70bba39ed9ae50cfc939b84fb5b76d0b4d1cf
3
+ metadata.gz: 61964b5c32328c5e2265a32622cb85c366a70780eeaab4b95e154577942602b9
4
+ data.tar.gz: 39c56ddd43405021c465494bf439e174681e431146e59a808937f0c6592a2f97
5
5
  SHA512:
6
- metadata.gz: be026965b5d6a63e10f2742dfe7bafa4ef693cdbb34c7087e345e0f1e04f0597ea232958befb26c58093b67bbc95e41d951e2d6c1e20537dece8a147d1e1d782
7
- data.tar.gz: 4fb3be2d42430d9338c8f5865e0bf35ff88fecfd891be0d3b71b867f8d0f98250f4651930cb253a67ce29bd2113927410b6b4e72abc27148f309c8cee501e790
6
+ metadata.gz: 54d844cb8292bea2cf364c473809f385bba2375d4bedede0927192c620e33bc685289831b1f7973a17a19d7b8471acfda9cf46de845367b7f2d2ab15ee3994e5
7
+ data.tar.gz: 123be6e3958715b82e1c0ae92c25e9a63f580ad0cbf55f43a2369ee1dbea782fe6d3b7dbb71e551ce4b66ef767ec817097ea0a8d6cd39ade8d3818c14482d13d
@@ -0,0 +1,92 @@
1
+ require 'open3'
2
+
3
+ module Smdev
4
+ module EcsExec
5
+ def self.ssh(options)
6
+ cluster_name = self.cluster_name(options)
7
+ return if cluster_name == nil
8
+ puts "Found cluster #{cluster_name}"
9
+
10
+ service_name = self.service_name(options, cluster_name)
11
+ return if service_name == nil
12
+ puts "Found service #{service_name}"
13
+
14
+ task_arn = self.task_arn(cluster_name, service_name)
15
+ puts "Found first task #{task_arn}"
16
+ command = options[:command]
17
+
18
+ execute_command_cmd = %W[
19
+ aws ecs execute-command
20
+ --cluster #{cluster_name}
21
+ --task #{task_arn}
22
+ --interactive
23
+ --command \"#{command}\"
24
+ ].join(' ')
25
+
26
+ puts "Running \"#{command}\""
27
+ exec(execute_command_cmd)
28
+ end
29
+
30
+ def self.cluster_name(options)
31
+ cluster = options[:cluster]
32
+ command = "aws ecs list-clusters | grep #{cluster}"
33
+ stdout, stderr, status = Open3.capture3(command)
34
+
35
+ if status.success?
36
+ match = stdout.match(/cluster\/([^"]+)/)
37
+ cluster_name = match[1] if match
38
+
39
+ return cluster_name
40
+ else
41
+ if stderr != ""
42
+ puts "Error executing command: #{stderr}"
43
+ else
44
+ puts "Could not find cluster whose name contains \"#{cluster}\""
45
+ end
46
+ end
47
+ end
48
+
49
+ def self.service_name(options, cluster_name)
50
+ service = options[:service]
51
+ command = "aws ecs list-services --cluster #{cluster_name} | grep #{service}"
52
+ stdout, stderr, status = Open3.capture3(command)
53
+
54
+ if status.success?
55
+ match = stdout.match(/#{cluster_name}\/([^"]+)/)
56
+ service_name = match[1] if match
57
+
58
+ return service_name
59
+ else
60
+ if stderr != ""
61
+ puts "Error executing command: #{stderr}"
62
+ else
63
+ puts "Could not find service whose name contains \"#{service}\""
64
+ end
65
+ end
66
+ end
67
+
68
+ def self.task_arn(cluster_name, service_name)
69
+ list_tasks_cmd = "aws ecs list-tasks --cluster #{cluster_name} --service-name #{service_name}"
70
+ stdout, stderr, status = Open3.capture3(list_tasks_cmd)
71
+
72
+ if status.success?
73
+ begin
74
+ tasks = JSON.parse(stdout)
75
+ task_arn = tasks['taskArns'][0]
76
+
77
+ if task_arn.nil? || task_arn.empty?
78
+ puts 'No task ARN found.'
79
+ exit 1
80
+ end
81
+ return task_arn
82
+ rescue JSON::ParserError
83
+ puts 'Error parsing JSON'
84
+ exit 1
85
+ end
86
+ else
87
+ puts "Error executing command: #{stderr}"
88
+ exit 1
89
+ end
90
+ end
91
+ end
92
+ end
data/lib/smdev.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  require 'io/console'
3
3
  require 'octokit'
4
4
  require 'optparse'
5
- require 'smdev/rails_console'
5
+ require 'smdev/ecs_exec'
6
6
 
7
7
  module Smdev
8
8
  class CLI
@@ -11,7 +11,9 @@ module Smdev
11
11
  commands = ["local_app_setup : Install local requirements for application. (Rails Only Currently)\n",
12
12
  "system_install : Install Homebrew, Ruby, PostgreSQL, and Ruby on Rails\n",
13
13
  "checkout_repos : Checkout all repositories for StrongMind\n",
14
- "console : Open a rails console\n"]
14
+ "console : Open a rails console on ECS\n",
15
+ "ssh : Open a bash shell on ECS\n"
16
+ ]
15
17
 
16
18
  OptionParser.new do |opts|
17
19
  opts.banner = "Usage: smdev.rb [options] <command>"
@@ -24,12 +26,16 @@ module Smdev
24
26
  options[:https] = true
25
27
  end
26
28
 
27
- opts.on("-e", "--env ENVIRONMENT", "Set the environment for the rails console") do |env|
28
- options[:env] = env
29
+ opts.on("-r", "--cluster CLUSTER", "Lookup this cluster for the ECS execute (defaults to the name of your current folder)") do |cluster|
30
+ options[:cluster] = cluster
29
31
  end
30
32
 
31
- opts.on("-a", "--app APP_NAME", "Set the app name for the rails console") do |app|
32
- options[:app] = app
33
+ opts.on("-v", "--service SERVICE", "Lookup this service for the ECS execute (defaults to \"prod-worker\")") do |svc|
34
+ options[:service] = svc
35
+ end
36
+
37
+ opts.on("-c", "--command COMMAND", "Set the command for the ECS execute (defaults to a bash shell when you use 'ssh' and rails console when you use 'console')") do |ecs_command|
38
+ options[:command] = ecs_command
33
39
  end
34
40
 
35
41
  opts.on("-h", "--help", "Prints this help message") do
@@ -61,10 +67,11 @@ module Smdev
61
67
  when "checkout_repos"
62
68
  checkout_repos(options)
63
69
  when "console"
64
- options[:env] ||= "development"
65
- options[:app] ||= Dir.pwd.split('/').last
66
- puts "Opening a rails console to #{options[:app]} (#{options[:env]})"
67
- Smdev::RailsConsole.console(options)
70
+ options[:command] = "rails c"
71
+ open_ssh(options)
72
+ when "ssh"
73
+ options[:command] ||= "/bin/sh"
74
+ open_ssh(options)
68
75
  else
69
76
  puts "Invalid command: #{command}. Use --help for a list of commands."
70
77
  end
@@ -196,5 +203,13 @@ module Smdev
196
203
  end
197
204
 
198
205
  end
206
+
207
+ private
208
+
209
+ def open_ssh(options)
210
+ options[:service] ||= "prod-worker"
211
+ options[:cluster] ||= Dir.pwd.split('/').last
212
+ Smdev::EcsExec.ssh(options)
213
+ end
199
214
  end
200
215
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smdev
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derek Neighbors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-11 00:00:00.000000000 Z
11
+ date: 2023-10-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: StrongMind Development Tool
14
14
  email: derek.neighbors@strongmind.com
@@ -19,7 +19,7 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - bin/smdev
21
21
  - lib/smdev.rb
22
- - lib/smdev/rails_console.rb
22
+ - lib/smdev/ecs_exec.rb
23
23
  homepage: https://github.com/StrongMind/Helpers/tree/main/smdev
24
24
  licenses:
25
25
  - MIT
@@ -1,87 +0,0 @@
1
- require 'open3'
2
- require 'pty'
3
-
4
- module Smdev
5
- module RailsConsole
6
- def self.console(options)
7
- cluster_name = self.cluster_name(options)
8
- container_name = cluster_name
9
- task_arn = self.tasks_list(cluster_name)
10
-
11
- execute_command_cmd = %W[
12
- aws ecs execute-command
13
- --cluster #{container_name}
14
- --task #{task_arn}
15
- --container #{container_name}
16
- --interactive
17
- --command \"rails c\"
18
- ].join(' ')
19
-
20
- begin
21
- PTY.spawn(execute_command_cmd) do |stdout, stdin, pid|
22
- begin
23
- # Use IO.select for bi-directional IO
24
- loop do
25
- r, w, e = IO.select([stdout, $stdin], [stdin], [])
26
-
27
- # If there's something to read from stdout, print it
28
- if r.include? stdout
29
- output = stdout.read_nonblock(4096)
30
- print output
31
- end
32
-
33
- # If there's input from the terminal, send it to stdin
34
- if r.include? $stdin
35
- input = $stdin.getc
36
- stdin.write(input) if w.include? stdin
37
- end
38
- end
39
- rescue Errno::EIO, EOFError
40
- # End of file or error. Exit loop.
41
- end
42
- end
43
- rescue PTY::ChildExited => e
44
- puts "The child process exited with status #{e.status.exitstatus}"
45
- end
46
-
47
- end
48
-
49
- def self.cluster_name(options)
50
- command = "aws ecs list-clusters | grep #{options[:app]} | grep #{options[:env]} | grep web"
51
- stdout, stderr, status = Open3.capture3(command)
52
-
53
- if status.success?
54
- match = stdout.match(/cluster\/([^"]+)/)
55
- cluster_name = match[1] if match
56
-
57
- return cluster_name
58
- else
59
- puts "Error executing command: #{stderr}"
60
- end
61
- end
62
-
63
- def self.tasks_list(cluster_name)
64
- list_tasks_cmd = "aws ecs list-tasks --cluster #{cluster_name}"
65
- stdout, stderr, status = Open3.capture3(list_tasks_cmd)
66
-
67
- if status.success?
68
- begin
69
- tasks = JSON.parse(stdout)
70
- task_arn = tasks['taskArns'][0]
71
-
72
- if task_arn.nil? || task_arn.empty?
73
- puts 'No task ARN found.'
74
- exit 1
75
- end
76
- return task_arn
77
- rescue JSON::ParserError
78
- puts 'Error parsing JSON'
79
- exit 1
80
- end
81
- else
82
- puts "Error executing command: #{stderr}"
83
- exit 1
84
- end
85
- end
86
- end
87
- end