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)


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

def self.exit_on_failure?
  true
end

.source_rootObject



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

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

Instance Method Details

#buildObject



41
42
43
# File 'lib/stack_car/cli.rb', line 41

def build
  run("docker-compose build #{options[:service]}")
end

#bundle(*args) ⇒ Object



66
67
68
# File 'lib/stack_car/cli.rb', line 66

def bundle(*args)
  run("docker-compose exec #{options[:service]} bundle")
end

#bundle_exec(*args) ⇒ Object



91
92
93
# File 'lib/stack_car/cli.rb', line 91

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

#console(*args) ⇒ Object



98
99
100
# File 'lib/stack_car/cli.rb', line 98

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

#deploy(environment) ⇒ Object



139
140
141
# File 'lib/stack_car/cli.rb', line 139

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

#dockerize(dir = ".") ⇒ Object



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
193
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
220
221
222
# File 'lib/stack_car/cli.rb', line 164

def dockerize(dir=".")
  Dir.chdir(dir)
  @project_name = File.basename(File.expand_path(dir))
  apt_packages << "libpq-dev postgresql-client" if options[:postgres]
  apt_packages << "mysql-client" if options[:mysql]
  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', 'Dockerfile.base', 'docker-compose.yml', 'docker-compose.ci.yml', 'docker-compose.production.yml', '.gitlab-ci.yml', '.env'].each do |template_file|
   puts template_file
    template("#{template_file}.erb", template_file)
 end
 template("database.yml.erb", "config/database.yml")
 template(".env.development.erb", ".env.development")
 template(".env.erb", ".env.production")

 if File.exists?('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
 append_to_file("Gemfile", "gem 'activerecord-nulldb-adapter'")
  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'
  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

#exec(*args) ⇒ Object



78
79
80
# File 'lib/stack_car/cli.rb', line 78

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

#provision(environment) ⇒ Object



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

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

#ps(*args) ⇒ Object



59
60
61
# File 'lib/stack_car/cli.rb', line 59

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

#pull(*args) ⇒ Object



53
54
55
# File 'lib/stack_car/cli.rb', line 53

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

#push(*args) ⇒ Object



47
48
49
# File 'lib/stack_car/cli.rb', line 47

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

#release(environment) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/stack_car/cli.rb', line 104

def release(environment)
  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.exists?("#{ENV['HOME']}/.docker/config.json") && File.readlines("#{ENV['HOME']}/.docker/config.json").grep(/#{ENV['REGISTRY_HOST']}/).size > 0
    run("docker login #{ENV['REGISTRY_HOST']}")
  end
  run("docker tag #{registry}:#{tag} #{registry}:#{environment}-#{timestamp}")
  run("docker push #{registry}:#{environment}-#{timestamp}")
  run("docker tag #{registry}:#{tag} #{registry}:#{sha}")
  run("docker push #{registry}:#{sha}")
  run("docker tag #{registry}:#{tag} #{registry}:#{environment}-latest")
  run("docker push #{registry}:#{environment}-latest")
  run("docker tag #{registry}:#{tag} #{registry}:latest")
  run("docker push #{registry}:latest")
end

#sh(*args) ⇒ Object



85
86
87
# File 'lib/stack_car/cli.rb', line 85

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

#ssh(environment) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/stack_car/cli.rb', line 129

def ssh(environment)
  target = ENV["#{environment.upcase}_SSH"]
  if target
    run(target)
  else
    say "Please set #{environment.upcase}_SSH"
  end
end

#stopObject



33
34
35
36
# File 'lib/stack_car/cli.rb', line 33

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

#upObject



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

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

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

#walk(*args) ⇒ Object



72
73
74
# File 'lib/stack_car/cli.rb', line 72

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