smdev 0.4.0 → 0.6.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: a5e395ee05d1f53774b0b9f9fa37beac7c1a6f5f921605c38427fbc06e07e76e
4
+ data.tar.gz: 6fc9f20054b455d1c4ba7ab2447d4bda9cb5bf55df98f15f4938d7df948369e0
5
5
  SHA512:
6
- metadata.gz: be026965b5d6a63e10f2742dfe7bafa4ef693cdbb34c7087e345e0f1e04f0597ea232958befb26c58093b67bbc95e41d951e2d6c1e20537dece8a147d1e1d782
7
- data.tar.gz: 4fb3be2d42430d9338c8f5865e0bf35ff88fecfd891be0d3b71b867f8d0f98250f4651930cb253a67ce29bd2113927410b6b4e72abc27148f309c8cee501e790
6
+ metadata.gz: 43f5e4b2112b6d7e338e56655e567d1a73fd8a3c2210499fb3278f467fa4394780755b597d4a67e5337cce6f9c32494a440b28d95adf23cc95cc78d1677dec77
7
+ data.tar.gz: e70efeab8b186294b7a1a7847cdf1f91953a7fa82eaedae3d60f69511fdad360e562a12b3d85cd7dddc1ac85c76ea0f7743cc06eda8d4e31794ad646dffeedf3
@@ -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
@@ -192,9 +199,22 @@ module Smdev
192
199
  ssh_url = repo.ssh_url
193
200
  repo_name = repo.name
194
201
  url = options[:https] ? clone_url : ssh_url
195
- `git clone #{url} #{repo_name}`
202
+
203
+ if Dir.exist?(repo_name)
204
+ puts "Repository '#{repo_name}' already exists locally. Skipping clone."
205
+ else
206
+ `git clone #{url} #{repo_name}`
207
+ end
196
208
  end
197
209
 
198
210
  end
211
+
212
+ private
213
+
214
+ def open_ssh(options)
215
+ options[:service] ||= "prod-worker"
216
+ options[:cluster] ||= Dir.pwd.split('/').last
217
+ Smdev::EcsExec.ssh(options)
218
+ end
199
219
  end
200
220
  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.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derek Neighbors
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-11 00:00:00.000000000 Z
11
+ date: 2024-01-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: StrongMind Development Tool
14
14
  email: derek.neighbors@strongmind.com
@@ -19,12 +19,12 @@ 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
26
26
  metadata: {}
27
- post_install_message:
27
+ post_install_message:
28
28
  rdoc_options: []
29
29
  require_paths:
30
30
  - lib
@@ -39,8 +39,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  requirements: []
42
- rubygems_version: 3.3.5
43
- signing_key:
42
+ rubygems_version: 3.4.13
43
+ signing_key:
44
44
  specification_version: 4
45
45
  summary: StrongMind Development Tool
46
46
  test_files: []
@@ -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