Class: StackCar::HammerOfTheGods
- Inherits:
-
Thor
- Object
- Thor
- StackCar::HammerOfTheGods
- Includes:
- Thor::Actions
- Defined in:
- lib/stack_car/cli.rb
Class Method Summary collapse
Instance Method Summary collapse
- #build ⇒ Object
- #bundle(*args) ⇒ Object
- #bundle_exec(*args) ⇒ Object
- #config ⇒ Object
- #console(*args) ⇒ Object
- #deploy(environment) ⇒ Object
- #dockerize(dir = ".") ⇒ Object
- #down ⇒ Object
- #exec(*args) ⇒ Object
- #provision(environment) ⇒ Object
- #ps(*args) ⇒ Object
- #pull(*args) ⇒ Object
- #push(*args) ⇒ Object
- #release(environment) ⇒ Object
- #sh(*args) ⇒ Object
- #ssh(environment) ⇒ Object
- #stop ⇒ Object
- #up ⇒ Object
- #walk(*args) ⇒ Object
Class Method Details
.exit_on_failure? ⇒ Boolean
19 20 21 |
# File 'lib/stack_car/cli.rb', line 19 def self.exit_on_failure? true end |
Instance Method Details
#build ⇒ Object
99 100 101 102 |
# File 'lib/stack_car/cli.rb', line 99 def build setup run_with_exit("#{dotenv} docker compose build #{[:service]}") end |
#bundle(*args) ⇒ Object
135 136 137 138 |
# File 'lib/stack_car/cli.rb', line 135 def bundle(*args) setup run_with_exit("#{dotenv} docker compose exec #{[:service]} bundle") end |
#bundle_exec(*args) ⇒ Object
164 165 166 167 |
# File 'lib/stack_car/cli.rb', line 164 def bundle_exec(*args) setup run_with_exit("#{dotenv} docker compose exec #{[:service]} bundle exec #{args.join(' ')}") end |
#config ⇒ Object
128 129 130 131 |
# File 'lib/stack_car/cli.rb', line 128 def config setup run_with_exit("#{dotenv} docker compose config #{args.join(' ')} #{[:service]}") end |
#console(*args) ⇒ Object
172 173 174 175 |
# File 'lib/stack_car/cli.rb', line 172 def console(*args) setup run_with_exit("#{dotenv} docker compose exec #{[:service]} bundle exec rails console #{args.join(' ')}") end |
#deploy(environment) ⇒ Object
219 220 221 222 |
# File 'lib/stack_car/cli.rb', line 219 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
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 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/stack_car/cli.rb', line 249 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.('..')) : File.basename(File.(dir)) apt_packages << "libpq-dev postgresql-client" if [:postgres] apt_packages << "mysql-client" if [:mysql] apt_packages << "imagemagick" if [:imagemagick] pre_apt << "echo 'Downloading Packages'" post_apt << "echo 'Packages Downloaded'" if [: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 [: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 [: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 [:deploy] || [: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 [:helm] directory('chart') if [:fcrepo] directory('chart-fcrepo', 'chart/templates') end if [: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 |
#down ⇒ Object
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 89 90 91 92 93 94 95 |
# File 'lib/stack_car/cli.rb', line 58 def down setup if [: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 [:service] rm_vol = true if [:volumes] remove_container([: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 [:all] prompt_run_confirmation('--all will remove all containers, volumes, networks, and local images. Continue?') args = %w[--volumes --rmi=local] else args << '--volumes' if [:volumes] args << '--rmi=local' if [:rmi] args << '--timeout' if [:timeout] end run("#{dotenv} docker compose down #{args.join(' ')}") run_with_exit('rm -rf tmp/pids/*') end |
#exec(*args) ⇒ Object
149 150 151 152 |
# File 'lib/stack_car/cli.rb', line 149 def exec(*args) setup run_with_exit("#{dotenv} docker compose exec #{[:service]} #{args.join(' ')}") end |
#provision(environment) ⇒ Object
200 201 202 203 204 |
# File 'lib/stack_car/cli.rb', line 200 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
120 121 122 123 |
# File 'lib/stack_car/cli.rb', line 120 def ps(*args) setup run_with_exit("#{dotenv} docker compose ps #{[:service]} #{args.join(' ')}") end |
#pull(*args) ⇒ Object
113 114 115 116 |
# File 'lib/stack_car/cli.rb', line 113 def pull(*args) setup run_with_exit("#{dotenv} docker compose pull #{[:service]} #{args.join(' ')}") end |
#push(*args) ⇒ Object
106 107 108 109 |
# File 'lib/stack_car/cli.rb', line 106 def push(*args) setup run_with_exit("#{dotenv} docker compose push #{[:service]} #{args.join(' ')}") end |
#release(environment) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/stack_car/cli.rb', line 179 def release(environment) Dotenv.load(".env.#{environment}", '.env') setup = 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}-#{}") run_with_exit("#{dotenv(environment)} docker push #{registry}:#{environment}-#{}") 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
157 158 159 160 |
# File 'lib/stack_car/cli.rb', line 157 def sh(*args) setup run_with_exit("#{dotenv} docker compose exec -e COLUMNS=\"\`tput cols\`\" -e LINES=\"\`tput lines\`\" #{[:service]} bash #{args.join(' ')}") end |
#ssh(environment) ⇒ Object
207 208 209 210 211 212 213 214 215 216 |
# File 'lib/stack_car/cli.rb', line 207 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 |
#stop ⇒ Object
45 46 47 48 49 |
# File 'lib/stack_car/cli.rb', line 45 def stop setup run("#{dotenv} docker compose stop #{[:service]}") run_with_exit("rm -rf tmp/pids/*") end |
#up ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/stack_car/cli.rb', line 31 def up setup args = [] args << '--build' if [:build] args << '--detach' if [:detach] if [:build] run("#{dotenv} docker compose pull #{[:service]}") end run_with_exit("#{dotenv} docker compose up #{args.join(' ')} #{[:service]}") end |
#walk(*args) ⇒ Object
142 143 144 145 |
# File 'lib/stack_car/cli.rb', line 142 def walk(*args) setup run_with_exit("#{dotenv} docker compose run #{[:service]} #{args.join(' ')}") end |