Class: Smdev::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/smdev.rb

Instance Method Summary collapse

Instance Method Details

#clone_repos(options) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/smdev.rb', line 222

def clone_repos(options)
  repos = get_repos(options)


  # Clone each repository to a local directory
  repos.each do |repo|
    clone_url = repo.clone_url
    ssh_url = repo.ssh_url
    repo_name = repo.name
    url = options[:https] ? clone_url : ssh_url

    if Dir.exist?(repo_name)
      puts "Repository '#{repo_name}' already exists locally. Skipping clone."
    else
      `git clone #{url} #{repo_name}`
    end
  end

end

#get_repos(options) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/smdev.rb', line 149

def get_repos(options)
  token = ENV['GITHUB_TOKEN']
  if token.nil?
    # Prompt the user for their GitHub personal access token
    print "GitHub personal access token: "
    token = STDIN.noecho(&:gets).chomp
    puts ""
  end

  # Authenticate with the GitHub API using the user's credentials
  client = Octokit::Client.new(access_token: token)

  # Specify the organization name
  org_name = "StrongMind"

  if options[:team]
    # A team name was provided as a command-line argument
    team_name = options[:team]
    # Get a list of all teams in the organization
    teams = client.organization_teams(org_name)
    # Find the team with the specified name
    team = teams.find { |t| t.name == team_name }
    if team.nil?
      puts "Error: Could not find team with name '#{team_name}' in organization '#{org_name}'"
      exit
    end
    # Get a list of all repositories in the organization that are assigned to the specified team
    #
    repos = client.org_repos(org_name, { :affiliation => 'organization_member', :team_id => team.id })
    puts "Team Name #{team_name} - Team ID: #{team.id}"
  else
    repos = []
    page_number = 1
    per_page = 100
    loop do
      repos_page = client.organization_repositories(org_name, { :affiliation => 'owner,organization_member', :per_page => per_page, :page => page_number })
      repos.concat(repos_page)
      break if repos_page.length < per_page
      page_number += 1
    end
  end

  return repos
end

#run(args = ARGV) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/smdev.rb', line 9

def run(args = ARGV)
  options = {}
  commands = ["local_app_setup : Install local requirements for application. (Rails Only Currently)\n",
              "system_install : Install Homebrew, Ruby, PostgreSQL, and Ruby on Rails\n",
              "clone_repos : Checkout all repositories for StrongMind\n",
              "update_repos : Checkout all repositories for StrongMind\n",
              "console : Open a rails console on ECS\n",
              "ssh : Open a bash shell on ECS\n"
  ]

  OptionParser.new do |opts|
    opts.banner = "Usage: smdev.rb [options] <command>"

    opts.on("-t", "--team TEAM_NAME", "Pull repositories assigned to a specific team") do |team_name|
      options[:team] = team_name
    end

    opts.on("-s", "--https", "Use HTTPS to pull repos") do
      options[:https] = true
    end

    opts.on("-r", "--cluster CLUSTER", "Lookup this cluster for the ECS execute (defaults to the name of your current folder)") do |cluster|
      options[:cluster] = cluster
    end

    opts.on("-v", "--service SERVICE", "Lookup this service for the ECS execute (defaults to \"prod-worker\")") do |svc|
      options[:service] = svc
    end

    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|
      options[:command] = ecs_command
    end

    opts.on("-h", "--help", "Prints this help message") do
      puts opts
      puts "\nCommands:"
      commands.each { |cmd| puts "  #{cmd}" }
      exit
    end

  end.parse!

  command = args.shift

  case command
  when "local_app_setup"
    puts "Running Bundle Install"
    system('bundle', 'install')

    puts "Copying env file"
    system('cp', '.env.example', '.env')

    puts "Create database"
    system('bin/rails', 'db:create')

    puts "Run database migrations"
    system('rails', 'db:migrate')
  when "system_install"
    system_install
  when "clone_repos"
    clone_repos(options)
  when "update_repos"
    update_repos(options)
  when "console"
    options[:command] = "rails c"
    open_ssh(options)
  when "ssh"
    options[:command] ||= "/bin/sh"
    open_ssh(options)
  else
    puts "Invalid command: #{command}.  Use --help for a list of commands."
  end

end

#system_installObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/smdev.rb', line 84

def system_install
  puts "Installing Homebrew..."
  system({ 'SHELL' => '/bin/bash' }, '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"')
  # system('/bin/bash', '-c', "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)")

  # Add Homebrew to PATH and run initialization script
  puts "adding homebrew to .zshrc"
  File.open("#{ENV['HOME']}/.zshrc", "a") do |file|
    file.write("\n")
    file.write('eval "$(/opt/homebrew/bin/brew shellenv)"')
  end

  # Update PATH to include Homebrew
  ENV['PATH'] = "#{ENV['HOME']}/.homebrew/bin:#{ENV['HOME']}/.homebrew/sbin:#{ENV['PATH']}"

  # Install PostgreSQL
  puts "Installing PostgreSQL..."
  system('brew', 'install', 'libpq')

  puts "Installing lib-pq..."
  system('brew', 'install', 'postgresql@15')

  # Install Code Climate
  puts "Preparing Code Climate Formulae..."
  system('brew', 'tap', 'codeclimate/formulae')

  puts "Installing Code Climate..."
  system('brew', 'install', 'codeclimate')

  puts "Installing AWS CLI..."
  system('brew', 'install', 'awscli')

  puts "Installing AWS Session Manager Plugin..."
  system('brew', 'install', '--cask', 'session-manager-plugin')

  # TODO: need to add the path & compiler flags
  # echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc
  # export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"
  # export CPPFLAGS="-I/opt/homebrew/opt/libpq/include"

  # Install RBenv
  puts "Installing RBenv..."
  system('brew', 'install', 'rbenv')
  puts "Initialize RBenv..."
  File.open("#{ENV['HOME']}/.zshrc", "a") do |file|
    file.write("\n")
    file.write('eval "$(rbenv init - zsh)"')
  end
  # Load the contents of .zshrc into a string
  zshrc_contents = File.read(File.expand_path('~/.zshrc'))

  # Execute a new shell session with the .zshrc contents
  exec('zsh', '-c', zshrc_contents)

  # Install Ruby
  puts "Installing Ruby via RBenv..."
  system('rbenv', 'install', '3.2.2')
  puts "Making ruby 3.2.2 global ..."
  system('rbenv', 'global', '3.2.2')

  # Install Ruby on Rails
  puts "Installing Ruby on Rails..."
  system('sudo', 'gem', 'install', 'rails')
end

#update_repos(options) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/smdev.rb', line 194

def update_repos(options)
  repos = get_repos(options)
  # Clone each repository to a local directory
  repos.each do |repo|
    clone_url = repo.clone_url
    ssh_url = repo.ssh_url
    repo_name = repo.name
    url = options[:https] ? clone_url : ssh_url

    if Dir.exist?(repo_name)
      puts "Checking for updates to '#{repo_name}'"
      if options[:https]
        puts "Using HTTPS to pull repo"
        token = ENV['GITHUB_TOKEN']
        url_with_token = url.sub('https://', "https://#{token}@")
        `cd #{repo_name} && git -c http.extraHeader="Authorization: Bearer #{token}" pull #{url_with_token}`
      else
        puts "Using SSH to pull repo"
        `cd #{repo_name} && git pull`
      end
    else
      puts "Repository '#{repo_name}' isn't cloned locally. Skipping update."
    end
  end

end