Class: StackCar::HammerOfTheGods

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/stack_car/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/stack_car/cli.rb', line 14

def self.exit_on_failure?
  true
end

.source_rootObject



10
11
12
# File 'lib/stack_car/cli.rb', line 10

def self.source_root
  File.join(File.dirname(__FILE__), '..', '..', 'templates')
end

Instance Method Details

#buildObject



92
93
94
95
96
# File 'lib/stack_car/cli.rb', line 92

def build
  setup
  ensure_development_env
  run_with_exit("#{dotenv} docker compose build #{options[:service]}")
end

#bundle(*args) ⇒ Object



122
123
124
125
# File 'lib/stack_car/cli.rb', line 122

def bundle(*args)
  setup
  run_with_exit("#{dotenv} docker compose exec #{options[:service]} bundle")
end

#bundle_exec(*args) ⇒ Object



151
152
153
154
# File 'lib/stack_car/cli.rb', line 151

def bundle_exec(*args)
  setup
  run_with_exit("#{dotenv} docker compose exec #{options[:service]} bundle exec #{args.join(' ')}")
end

#console(*args) ⇒ Object



159
160
161
162
# File 'lib/stack_car/cli.rb', line 159

def console(*args)
  setup
  run_with_exit("#{dotenv} docker compose exec #{options[:service]} bundle exec rails console #{args.join(' ')}")
end

#deploy(environment) ⇒ Object



206
207
208
209
# File 'lib/stack_car/cli.rb', line 206

def deploy(environment)
  setup
  run_with_exit("DEPLOY_HOOK=$DEPLOY_HOOK_#{environment.upcase} #{dotenv(environment)} ansible-playbook -i ops/hosts -l #{environment}:localhost ops/deploy.yml")
end

#dockerize(dir = ".") ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/stack_car/cli.rb', line 236

def dockerize(dir=".")
  Dir.chdir(dir)
  self.destination_root = dir
  setup
  # Commandline overrides config files
  # options = file_config.merge(options)
  # Sets project name to parent directory name if working with stack_car dir
  @project_name = @sc_dir ? File.basename(File.expand_path('..')) : File.basename(File.expand_path(dir))
  apt_packages << "libpq-dev postgresql-client" if options[:postgres]
  apt_packages << "mysql-client" if options[:mysql]
  apt_packages << "imagemagick" if options[:imagemagick]
  pre_apt << "echo 'Downloading Packages'"
  post_apt << "echo 'Packages Downloaded'"

  if options[:yarn]
    apt_packages << 'yarn'
    pre_apt << "curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -"
    pre_apt << "echo 'deb https://dl.yarnpkg.com/debian/ stable main' | tee /etc/apt/sources.list.d/yarn.list"
    post_apt << "yarn config set no-progress"
    post_apt << "yarn config set silent"
  end

  if options[:fcrepo]
    apt_packages << "libc6-dev libreoffice imagemagick unzip ghostscript ffmpeg"

    post_apt << "mkdir -p /opt/fits"
    post_apt << "curl -fSL -o /opt/fits-1.0.5.zip http://projects.iq.harvard.edu/files/fits/files/fits-1.0.5.zip"
    post_apt << "cd /opt && unzip fits-1.0.5.zip && chmod +X fits-1.0.5/fits.sh"
  end

 ['.dockerignore', 'Dockerfile', 'docker-compose.yml', '.gitlab-ci.yml', '.env'].each do |template_file|
   puts template_file
    template("#{template_file}.erb", template_file)
 end
 directory('.gitlab', '.gitlab')
 template(".env.development.erb", ".env.development")
 template(".env.erb", ".env.production")
 template(".sops.yaml.erb", ".sops.yaml")
 template("decrypt-secrets", "bin/decrypt-secrets")
 template("encrypt-secrets", "bin/encrypt-secrets")
 template("database.yml.erb", "config/database.yml")
 template("development.rb.erb", "config/environments/development.rb")
 template("production.rb.erb", "config/environments/production.rb")

 if options[:solr]
  template("solrcloud-upload-configset.sh", "bin/solrcloud-upload-configset.sh")
  template("solrcloud-assign-configset.sh", "bin/solrcloud-assign-configset.sh")
 end

 if File.exist?('README.md')
   prepend_to_file "README.md" do
     File.read("#{self.class.source_root}/README.md")
   end
 else
   create_file "README.md" do
     File.read("#{self.class.source_root}/README.md")
   end
 end

  if File.exist?('Gemfile')
    append_to_file('Gemfile', "gem 'activerecord-nulldb-adapter'")
  else
    append_to_file('../Gemfile', "gem 'activerecord-nulldb-adapter'", { verbose: false })
    # TODO: remove '../' from message after other status messages are prepended with 'stack_car/'
   append_to_file("../Gemfile", "gem 'pronto', groups: [:development, :test]")
   append_to_file("../Gemfile", "gem 'pronto-rubocop', groups: [:development, :test]")
    say_status(:append, '../Gemfile')
  end

  if options[:deploy] || options[:rancher]
    directory('ops')
    ['hosts', 'deploy.yml', 'provision.yml'].each do |template_file|
      template("#{template_file}.erb", "ops/#{template_file}")
    end

    say 'Please update ops/hosts with the correct server addresses'
  elsif options[:helm]
    directory('chart')
    if options[:fcrepo]
      directory('chart-fcrepo', 'chart/templates')
    end
    if options[:sidekiq]
      directory('chart-sidekiq', 'chart/templates')
    end
 else
    empty_directory('ops')
  end

  # Do this after we figure out whether to use an empty ops directory or a full one
  ['env.conf', 'webapp.conf', 'worker.sh', 'nginx.sh'].each do |template_file|
    template("#{template_file}.erb", "ops/#{template_file}")
  end

  say 'Please find and replace all CHANGEME lines'
end

#downObject



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
83
84
85
86
87
88
# File 'lib/stack_car/cli.rb', line 50

def down
  setup
  ensure_development_env

  if options[:help]
    run('docker compose down --help')
    say 'Additional stack_car options:'
    say '    -a, --all               Removes all containers, networks, volumes, and'
    say '                            images created by `up`.'
    say '    -s, --service           Specify a service defined in the Compose file'
    say '                            whose containers and volumes should be removed.'
    exit(0)
  end

  if options[:service]
    rm_vol = true if options[:volumes]

    remove_container(options[:service], rm_vol)
    exit(0)
  end

  run_conf = 'Running down will stop and remove all of the Docker containers and networks ' \
             'defined in the docker-compose.yml file. Continue?'
  prompt_run_confirmation(run_conf)

  args = []
  if options[:all]
    prompt_run_confirmation('--all will remove all containers, volumes, networks, and local images. Continue?')

    args = %w[--volumes --rmi=local]
  else
    args << '--volumes' if options[:volumes]
    args << '--rmi=local' if options[:rmi]
    args << '--timeout' if options[:timeout]
  end

  run("#{dotenv} docker compose down #{args.join(' ')}")
  run_with_exit('rm -rf tmp/pids/*')
end

#exec(*args) ⇒ Object



136
137
138
139
# File 'lib/stack_car/cli.rb', line 136

def exec(*args)
  setup
  run_with_exit("#{dotenv} docker compose exec #{options[:service]} #{args.join(' ')}")
end

#provision(environment) ⇒ Object



187
188
189
190
191
# File 'lib/stack_car/cli.rb', line 187

def provision(environment)
  setup
  # TODO make dotenv load a specific environment?
  run_with_exit("DEPLOY_ENV=#{environment} #{dotenv(environment)} ansible-playbook -i ops/hosts -l #{environment}:localhost ops/provision.yml")
end

#ps(*args) ⇒ Object



114
115
116
117
# File 'lib/stack_car/cli.rb', line 114

def ps(*args)
  setup
  run_with_exit("#{dotenv} docker compose ps #{options[:service]} #{args.join(' ')}")
end

#pull(*args) ⇒ Object



107
108
109
110
# File 'lib/stack_car/cli.rb', line 107

def pull(*args)
  setup
  run_with_exit("#{dotenv} docker compose pull #{options[:service]} #{args.join(' ')}")
end

#push(*args) ⇒ Object



100
101
102
103
# File 'lib/stack_car/cli.rb', line 100

def push(*args)
  setup
  run_with_exit("#{dotenv} docker compose push #{options[:service]} #{args.join(' ')}")
end

#release(environment) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/stack_car/cli.rb', line 166

def release(environment)
  Dotenv.load(".env.#{environment}", '.env')
  setup
  timestamp = Time.now.strftime("%Y%m%d%I%M%S")
  sha = `git rev-parse HEAD`[0..8]
  registry = "#{ENV['REGISTRY_HOST']}#{ENV['REGISTRY_URI']}"
  tag = ENV["TAG"] || 'latest'
  unless File.exist?("#{ENV['HOME']}/.docker/config.json") && File.readlines("#{ENV['HOME']}/.docker/config.json").grep(/#{ENV['REGISTRY_HOST']}/).size > 0
    run_with_exit("#{dotenv(environment)} docker login #{ENV['REGISTRY_HOST']}")
  end
  run_with_exit("#{dotenv(environment)} docker tag #{registry}:#{tag} #{registry}:#{environment}-#{timestamp}")
  run_with_exit("#{dotenv(environment)} docker push #{registry}:#{environment}-#{timestamp}")
  run_with_exit("#{dotenv(environment)} docker tag #{registry}:#{tag} #{registry}:#{sha}")
  run_with_exit("#{dotenv(environment)} docker push #{registry}:#{sha}")
  run_with_exit("#{dotenv(environment)} docker tag #{registry}:#{tag} #{registry}:#{environment}-latest")
  run_with_exit("#{dotenv(environment)} docker push #{registry}:#{environment}-latest")
  run_with_exit("#{dotenv(environment)} docker tag #{registry}:#{tag} #{registry}:latest")
  run_with_exit("#{dotenv(environment)} docker push #{registry}:latest")
end

#sh(*args) ⇒ Object



144
145
146
147
# File 'lib/stack_car/cli.rb', line 144

def sh(*args)
  setup
  run_with_exit("#{dotenv} docker compose exec -e COLUMNS=\"\`tput cols\`\" -e LINES=\"\`tput lines\`\" #{options[:service]} bash #{args.join(' ')}")
end

#ssh(environment) ⇒ Object



194
195
196
197
198
199
200
201
202
203
# File 'lib/stack_car/cli.rb', line 194

def ssh(environment)
  Dotenv.load(".env.#{environment}", '.env')
  setup
  target = ENV["#{environment.upcase}_SSH"]
  if target
    run_with_exit(target)
  else
    say "Please set #{environment.upcase}_SSH"
  end
end

#stopObject



36
37
38
39
40
41
# File 'lib/stack_car/cli.rb', line 36

def stop
  setup
  ensure_development_env
  run("#{dotenv} docker compose stop #{options[:service]}")
  run_with_exit("rm -rf tmp/pids/*")
end

#upObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/stack_car/cli.rb', line 22

def up
  setup
  ensure_development_env
  args = []
  args << '--build' if options[:build]
  if options[:build]
    run("#{dotenv} docker compose pull #{options[:service]}")
  end

  run_with_exit("#{dotenv} docker compose up #{args.join(' ')} #{options[:service]}")
end

#walk(*args) ⇒ Object



129
130
131
132
# File 'lib/stack_car/cli.rb', line 129

def walk(*args)
  setup
  run_with_exit("#{dotenv} docker compose run #{options[:service]} #{args.join(' ')}")
end