smdev 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/smdev/rails_console.rb +87 -0
  3. data/lib/smdev.rb +143 -121
  4. metadata +7 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3cb7ef8e326aefb0cec817caef086d1079b26404c1cc6d04ebc91c8db5ccf7b5
4
- data.tar.gz: 12b8cc2b5899723602ae6347c31f5cffb9ecf6026ebc39f595d7bc670abe0346
3
+ metadata.gz: 6fe50e69b90b235fbc3901bc01411e560676f55443f2f3cb1a064f4c400f0183
4
+ data.tar.gz: eb6ade0d5e8b4c649da5707c51e70bba39ed9ae50cfc939b84fb5b76d0b4d1cf
5
5
  SHA512:
6
- metadata.gz: b8e0db249fa4c0f91eee94c4ba0408ee6ea7517a58d6c0b8e91b12f700786ee0a8e504b0fe05bf25b4ba76f7a03b4f5297d755648ae429ee071cc73c221967b7
7
- data.tar.gz: 0d31f663ac690004e11f3a3a05c45f995dc4f7c9cf51e5f193b08e73398dddfc36f5718c4602f3a053ed320a0c4892a82bae2be1a0434edc4e43c7cd68c4b105
6
+ metadata.gz: be026965b5d6a63e10f2742dfe7bafa4ef693cdbb34c7087e345e0f1e04f0597ea232958befb26c58093b67bbc95e41d951e2d6c1e20537dece8a147d1e1d782
7
+ data.tar.gz: 4fb3be2d42430d9338c8f5865e0bf35ff88fecfd891be0d3b71b867f8d0f98250f4651930cb253a67ce29bd2113927410b6b4e72abc27148f309c8cee501e790
@@ -0,0 +1,87 @@
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
data/lib/smdev.rb CHANGED
@@ -2,6 +2,7 @@
2
2
  require 'io/console'
3
3
  require 'octokit'
4
4
  require 'optparse'
5
+ require 'smdev/rails_console'
5
6
 
6
7
  module Smdev
7
8
  class CLI
@@ -9,7 +10,8 @@ module Smdev
9
10
  options = {}
10
11
  commands = ["local_app_setup : Install local requirements for application. (Rails Only Currently)\n",
11
12
  "system_install : Install Homebrew, Ruby, PostgreSQL, and Ruby on Rails\n",
12
- "checkout_repos : Checkout all repositories for StrongMind\n"]
13
+ "checkout_repos : Checkout all repositories for StrongMind\n",
14
+ "console : Open a rails console\n"]
13
15
 
14
16
  OptionParser.new do |opts|
15
17
  opts.banner = "Usage: smdev.rb [options] <command>"
@@ -22,6 +24,14 @@ module Smdev
22
24
  options[:https] = true
23
25
  end
24
26
 
27
+ opts.on("-e", "--env ENVIRONMENT", "Set the environment for the rails console") do |env|
28
+ options[:env] = env
29
+ end
30
+
31
+ opts.on("-a", "--app APP_NAME", "Set the app name for the rails console") do |app|
32
+ options[:app] = app
33
+ end
34
+
25
35
  opts.on("-h", "--help", "Prints this help message") do
26
36
  puts opts
27
37
  puts "\nCommands:"
@@ -31,125 +41,6 @@ module Smdev
31
41
 
32
42
  end.parse!
33
43
 
34
- def system_install
35
- puts "Installing Homebrew..."
36
- system({ 'SHELL' => '/bin/bash' }, '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"')
37
- # system('/bin/bash', '-c', "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)")
38
-
39
- # Add Homebrew to PATH and run initialization script
40
- puts "adding homebrew to .zshrc"
41
- File.open("#{ENV['HOME']}/.zshrc", "a") do |file|
42
- file.write("\n")
43
- file.write('eval "$(/opt/homebrew/bin/brew shellenv)"')
44
- end
45
-
46
- # Update PATH to include Homebrew
47
- ENV['PATH'] = "#{ENV['HOME']}/.homebrew/bin:#{ENV['HOME']}/.homebrew/sbin:#{ENV['PATH']}"
48
-
49
- # Install PostgreSQL
50
- puts "Installing PostgreSQL..."
51
- system('brew', 'install', 'libpq')
52
-
53
- puts "Installing lib-pq..."
54
- system('brew', 'install', 'postgresql@15')
55
-
56
- # Install Code Climate
57
- puts "Preparing Code Climate Formulae..."
58
- system('brew', 'tap', 'codeclimate/formulae')
59
-
60
- puts "Installing Code Climate..."
61
- system('brew', 'install', 'codeclimate')
62
-
63
- # TODO: need to add the path & compiler flags
64
- # echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc
65
- # export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"
66
- # export CPPFLAGS="-I/opt/homebrew/opt/libpq/include"
67
-
68
- # Install RBenv
69
- puts "Installing RBenv..."
70
- system('brew', 'install', 'rbenv')
71
- puts "Initialize RBenv..."
72
- File.open("#{ENV['HOME']}/.zshrc", "a") do |file|
73
- file.write("\n")
74
- file.write('eval "$(rbenv init - zsh)"')
75
- end
76
- # Load the contents of .zshrc into a string
77
- zshrc_contents = File.read(File.expand_path('~/.zshrc'))
78
-
79
- # Execute a new shell session with the .zshrc contents
80
- exec('zsh', '-c', zshrc_contents)
81
-
82
- # Install Ruby
83
- puts "Installing Ruby via RBenv..."
84
- system('rbenv', 'install', '3.2.2')
85
- puts "Making ruby 3.2.2 global ..."
86
- system('rbenv', 'global', '3.2.2')
87
-
88
- # Install Ruby on Rails
89
- puts "Installing Ruby on Rails..."
90
- system('sudo', 'gem', 'install', 'rails')
91
- end
92
- def checkout_repos(options)
93
- token = ENV['GITHUB_TOKEN']
94
- if token.nil?
95
- # Prompt the user for their GitHub personal access token
96
- print "GitHub personal access token: "
97
- token = STDIN.noecho(&:gets).chomp
98
- puts ""
99
- end
100
- # Authenticate with the GitHub API using the user's credentials
101
- client = Octokit::Client.new(access_token: token)
102
-
103
- # Specify the organization name
104
- org_name = "StrongMind"
105
-
106
- if options[:team]
107
- # A team name was provided as a command-line argument
108
- team_name = options[:team]
109
- # Get a list of all teams in the organization
110
- teams = client.organization_teams(org_name)
111
- # Find the team with the specified name
112
- team = teams.find { |t| t.name == team_name }
113
- if team.nil?
114
- puts "Error: Could not find team with name '#{team_name}' in organization '#{org_name}'"
115
- exit
116
- end
117
- # Get a list of all repositories in the organization that are assigned to the specified team
118
- #
119
- repos = client.org_repos(org_name, { :affiliation => 'organization_member', :team_id => team.id })
120
- puts "Team Name #{team_name} - Team ID: #{team.id}"
121
- else
122
- repos = []
123
- page_number = 1
124
- per_page = 100
125
- loop do
126
- repos_page = client.organization_repositories(org_name, { :affiliation => 'owner,organization_member', :per_page => per_page, :page => page_number })
127
- repos.concat(repos_page)
128
- break if repos_page.length < per_page
129
- page_number += 1
130
- end
131
-
132
- # # No team name was provided, so get a list of all teams in the organization
133
- # teams = client.organization_teams(org_name)
134
- # # Get a list of all repositories in the organization that are assigned to each team
135
- # repos = []
136
- # teams.each do |team|
137
- # team_repos = client.team_repos(team.id)
138
- # repos.concat(team_repos)
139
- # end
140
- end
141
-
142
- # Clone each repository to a local directory
143
- repos.each do |repo|
144
- clone_url = repo.clone_url
145
- ssh_url = repo.ssh_url
146
- repo_name = repo.name
147
- url = options[:https] ? clone_url : ssh_url
148
- `git clone #{url} #{repo_name}`
149
- end
150
-
151
- end
152
-
153
44
  command = args.shift
154
45
 
155
46
  case command
@@ -169,10 +60,141 @@ module Smdev
169
60
  system_install
170
61
  when "checkout_repos"
171
62
  checkout_repos(options)
63
+ 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)
172
68
  else
173
69
  puts "Invalid command: #{command}. Use --help for a list of commands."
174
70
  end
175
71
 
176
72
  end
73
+
74
+ def system_install
75
+ puts "Installing Homebrew..."
76
+ system({ 'SHELL' => '/bin/bash' }, '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"')
77
+ # system('/bin/bash', '-c', "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)")
78
+
79
+ # Add Homebrew to PATH and run initialization script
80
+ puts "adding homebrew to .zshrc"
81
+ File.open("#{ENV['HOME']}/.zshrc", "a") do |file|
82
+ file.write("\n")
83
+ file.write('eval "$(/opt/homebrew/bin/brew shellenv)"')
84
+ end
85
+
86
+ # Update PATH to include Homebrew
87
+ ENV['PATH'] = "#{ENV['HOME']}/.homebrew/bin:#{ENV['HOME']}/.homebrew/sbin:#{ENV['PATH']}"
88
+
89
+ # Install PostgreSQL
90
+ puts "Installing PostgreSQL..."
91
+ system('brew', 'install', 'libpq')
92
+
93
+ puts "Installing lib-pq..."
94
+ system('brew', 'install', 'postgresql@15')
95
+
96
+ # Install Code Climate
97
+ puts "Preparing Code Climate Formulae..."
98
+ system('brew', 'tap', 'codeclimate/formulae')
99
+
100
+ puts "Installing Code Climate..."
101
+ system('brew', 'install', 'codeclimate')
102
+
103
+ puts "Installing AWS CLI..."
104
+ system('brew', 'install', 'awscli')
105
+
106
+ puts "Installing AWS Session Manager Plugin..."
107
+ system('brew', 'install', '--cask', 'session-manager-plugin')
108
+
109
+ # TODO: need to add the path & compiler flags
110
+ # echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc
111
+ # export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"
112
+ # export CPPFLAGS="-I/opt/homebrew/opt/libpq/include"
113
+
114
+ # Install RBenv
115
+ puts "Installing RBenv..."
116
+ system('brew', 'install', 'rbenv')
117
+ puts "Initialize RBenv..."
118
+ File.open("#{ENV['HOME']}/.zshrc", "a") do |file|
119
+ file.write("\n")
120
+ file.write('eval "$(rbenv init - zsh)"')
121
+ end
122
+ # Load the contents of .zshrc into a string
123
+ zshrc_contents = File.read(File.expand_path('~/.zshrc'))
124
+
125
+ # Execute a new shell session with the .zshrc contents
126
+ exec('zsh', '-c', zshrc_contents)
127
+
128
+ # Install Ruby
129
+ puts "Installing Ruby via RBenv..."
130
+ system('rbenv', 'install', '3.2.2')
131
+ puts "Making ruby 3.2.2 global ..."
132
+ system('rbenv', 'global', '3.2.2')
133
+
134
+ # Install Ruby on Rails
135
+ puts "Installing Ruby on Rails..."
136
+ system('sudo', 'gem', 'install', 'rails')
137
+ end
138
+
139
+ def checkout_repos(options)
140
+ token = ENV['GITHUB_TOKEN']
141
+ if token.nil?
142
+ # Prompt the user for their GitHub personal access token
143
+ print "GitHub personal access token: "
144
+ token = STDIN.noecho(&:gets).chomp
145
+ puts ""
146
+ end
147
+ # Authenticate with the GitHub API using the user's credentials
148
+ client = Octokit::Client.new(access_token: token)
149
+
150
+ # Specify the organization name
151
+ org_name = "StrongMind"
152
+
153
+ if options[:team]
154
+ # A team name was provided as a command-line argument
155
+ team_name = options[:team]
156
+ # Get a list of all teams in the organization
157
+ teams = client.organization_teams(org_name)
158
+ # Find the team with the specified name
159
+ team = teams.find { |t| t.name == team_name }
160
+ if team.nil?
161
+ puts "Error: Could not find team with name '#{team_name}' in organization '#{org_name}'"
162
+ exit
163
+ end
164
+ # Get a list of all repositories in the organization that are assigned to the specified team
165
+ #
166
+ repos = client.org_repos(org_name, { :affiliation => 'organization_member', :team_id => team.id })
167
+ puts "Team Name #{team_name} - Team ID: #{team.id}"
168
+ else
169
+ repos = []
170
+ page_number = 1
171
+ per_page = 100
172
+ loop do
173
+ repos_page = client.organization_repositories(org_name, { :affiliation => 'owner,organization_member', :per_page => per_page, :page => page_number })
174
+ repos.concat(repos_page)
175
+ break if repos_page.length < per_page
176
+ page_number += 1
177
+ end
178
+
179
+ # # No team name was provided, so get a list of all teams in the organization
180
+ # teams = client.organization_teams(org_name)
181
+ # # Get a list of all repositories in the organization that are assigned to each team
182
+ # repos = []
183
+ # teams.each do |team|
184
+ # team_repos = client.team_repos(team.id)
185
+ # repos.concat(team_repos)
186
+ # end
187
+ end
188
+
189
+ # Clone each repository to a local directory
190
+ repos.each do |repo|
191
+ clone_url = repo.clone_url
192
+ ssh_url = repo.ssh_url
193
+ repo_name = repo.name
194
+ url = options[:https] ? clone_url : ssh_url
195
+ `git clone #{url} #{repo_name}`
196
+ end
197
+
198
+ end
177
199
  end
178
- end
200
+ 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.3.0
4
+ version: 0.4.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-09-11 00:00:00.000000000 Z
11
+ date: 2023-10-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: StrongMind Development Tool
14
14
  email: derek.neighbors@strongmind.com
@@ -19,11 +19,12 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - bin/smdev
21
21
  - lib/smdev.rb
22
+ - lib/smdev/rails_console.rb
22
23
  homepage: https://github.com/StrongMind/Helpers/tree/main/smdev
23
24
  licenses:
24
25
  - MIT
25
26
  metadata: {}
26
- post_install_message:
27
+ post_install_message:
27
28
  rdoc_options: []
28
29
  require_paths:
29
30
  - lib
@@ -38,8 +39,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
39
  - !ruby/object:Gem::Version
39
40
  version: '0'
40
41
  requirements: []
41
- rubygems_version: 3.4.13
42
- signing_key:
42
+ rubygems_version: 3.3.5
43
+ signing_key:
43
44
  specification_version: 4
44
45
  summary: StrongMind Development Tool
45
46
  test_files: []