Class: Brut::CLI::Apps::Deploy::Docker

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, #print, #puts, #stdin, #system!, #theme

Instance Method Details

#default_rack_envObject



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

def default_rack_env = "development"

#descriptionObject



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

def description = "Build one docker image to use for all commands in production"

#optsObject



19
20
21
# File 'lib/brut/cli/apps/deploy.rb', line 19

def opts = [
  [ "--build-only", "Only generate Dockerfiles and build images, do not deploy" ],
]

#runObject



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

def run
  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
    dockerfile = Brut.container.project_root / "deploy" / "Dockerfile"
    if !dockerfile.exist?
      fatal "#{dockerfile} does not exist - it should've been created when you added the Docker Deploy segment"
      return 1
    end
    git_checks = Brut::CLI::Apps::Deploy::GitChecks.new(executor: execution_context.executor)
    results = git_checks.check!
    if results.errors?
      results.errors.each do |_,message|
        fatal message
      end
      return 1
    end
    version = ""
    git_guess = %{git rev-parse HEAD}
    version = capture!(git_guess).strip.chomp
    if version == ""
      fatal "Attempt to use git via command '#{git_guess}' to figure out the version failed"
      return 1
    end
    short_version = version[0..7]

    config = AppDeployConfig.new

    image_name = %{#{Brut.container.app_organization}/#{Brut.container.app_id}:#{short_version}}
    if config.registry_hostname
      image_name = "#{config.registry_hostname}/#{image_name}"
    end
    dockerfile = Brut.container.project_root / "deploy" / "Dockerfile"
    FileUtils.chdir Brut.container.project_root do
      command = %{docker build --build-arg app_git_sha1=#{version} --file #{dockerfile} --platform #{config.platform} --tag #{image_name} . 2>&1}
      system!(command, output: :stream)
    end
    if options.build_only?
      puts "Not pushing image"
    else
      system!("docker image push #{image_name}", output: :stream)
    end

    0
  rescue LoadError => ex
    fatal "Could not find #{deploy_config_path}: #{ex}"
    1
  end

end