Class: Brut::CLI::Apps::Deploy::DockerCompose::Generate

Inherits:
Commands::BaseCommand show all
Defined in:
lib/brut/cli/apps/deploy.rb

Instance Attribute Summary

Attributes inherited from Commands::BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from Commands::BaseCommand

#accepts, #args_description, #argv, #bootstrap?, #commands, #delegate_to_command, #detailed_description, #env, #env_vars, #execute, #name, #options, #opts, #print, #puts, #stdin, #system!, #theme

Instance Method Details

#default_rack_envObject



265
# File 'lib/brut/cli/apps/deploy.rb', line 265

def default_rack_env = "development"

#descriptionObject



264
# File 'lib/brut/cli/apps/deploy.rb', line 264

def description = "Generate or update the existing docker-compose.yml based on current deploy config"

#runObject



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
# File 'lib/brut/cli/apps/deploy.rb', line 266

def run
  docker_compose_path = Brut.container.project_root / "deploy" / "docker-compose.yml"
  deploy_config_path = Brut.container.project_root / "deploy" / "deploy_config.rb"
  begin
    require deploy_config_path
    if !defined?(AppDeployConfig)
      fatal "#{deploy_config_path} must define the class AppDeployConfig"
      return 1
    end
    if !AppDeployConfig.ancestors.include?(Brut::CLI::Apps::Deploy::DeployConfig)
      fatal "#{deploy_config_path} must define a subclass of Brut::CLI::Apps::Deploy::DeployConfig"
      return 1
    end
    config = AppDeployConfig.new
    yaml_contents = if docker_compose_path.exist?
                      YAML.load(File.read(docker_compose_path))
                    else
                      {}
                    end
    yaml_contents["services"] ||= {}

    image_name = "#{Brut.container.app_organization}/#{Brut.container.app_id}:${DOCKER_IMAGE_TAG}"
    if config.registry_hostname
      image_name = "#{config.registry_hostname}/#{image_name}"
    end
    configured_services = []
    config.processes.each do |process_description|
      configured_services << process_description.name
      existing = yaml_contents["services"][process_description.name]
      if !existing
        puts "Creating configuration for '#{process_description.name}'"
        existing = {
          "env_file" => "/etc/#{Brut.container.app_id}/env",
          "extra_hosts" => [
            "host.docker.internal:host-gateway",
          ],
          "restart" => "unless-stopped",
        }
        if process_description.name == "web"
          existing["ports"] = [
            "127.0.0.1:6502:6502",
          ]
        end
      else
        puts "Updating image and command for '#{process_description.name}'"
      end
      existing["image"] = image_name
      existing["command"] = process_description.cmd
      yaml_contents["services"][process_description.name] = existing
    end
    trimmed_services = yaml_contents["services"].select { |service_name, service_configuration|
      configured_services.include?(service_name).tap { |exists|
        if !exists
          puts "Removing configuration for '#{service_name}'"
        end
      }
    }.to_h
    yaml_contents["services"] = trimmed_services

    File.open(docker_compose_path,"w") do |file|
      file.puts YAML.dump(yaml_contents)
    end
    0
  rescue LoadError => ex
    fatal "Could not find #{deploy_config_path}: #{ex}"
    1
  end
end