Class: Brut::CLI::Apps::HerokuContainerBasedDeploy::Deploy

Inherits:
Command
  • Object
show all
Defined in:
lib/brut/cli/apps/heroku_container_based_deploy.rb

Instance Method Summary collapse

Methods inherited from Command

args, #args, #before_execute, command_name, default_env, #delegate_to_commands, description, detailed_description, env_var, #err, #global_options, #handle_bootstrap_exception, #initialize, name_matches?, option_parser, #options, opts, #out, requires_project_env, requires_project_env?, #system!

Methods included from Framework::Errors

#abstract_method!, #bug!

Methods included from I18n::ForCLI

#capture, #html_escape, #safe

Methods included from I18n::BaseMethods

#l, #t, #t_direct, #this_field_value

Methods included from ExecutionResults

#abort_execution, #as_execution_result, #cli_usage_error, #continue_execution, #show_cli_usage, #stop_execution

Constructor Details

This class inherits a constructor from Brut::CLI::Command

Instance Method Details

#after_bootstrap(app:) ⇒ Object



28
29
30
31
# File 'lib/brut/cli/apps/heroku_container_based_deploy.rb', line 28

def after_bootstrap(app:)
  @app_id       = app.id
  @organization = app.organization
end

#executeObject



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/brut/cli/apps/heroku_container_based_deploy.rb', line 33

def execute
  options.set_default(:deploy, true)
  options.set_default(:push, true)
  if !options.push?
    options[:deploy] = false
  end
  local_repo_checks = Brut::CLI::Apps::DeployBase::GitChecks.new(
    out: @out,
    err: @err,
    executor: @executor,
    warn_only: options.skip_checks? || options.dry_run?
  )
  if options.dry_run?
    def system!(*args)
      out.puts "DRY RUN, NOT EXECUTING '#{args}'"
    end
  end

  version = begin
              git_guess = %{git rev-parse HEAD}
              stdout, stderr, status = Open3.capture3(git_guess)
              if status.success?
                stdout.strip
              else
                raise "Attempt to use git via command '#{git_guess}' to figure out the version failed: #{stdout}#{stderr}"
              end
            end
  short_version = version[0..7]

  platform = options.platform || "linux/amd64"
  heroku_app_name = @app_id

  out.puts "Reading HerokuConfig:"
  require_relative Brut.container.project_root / "deploy" / "heroku_config"

  additional_images = HerokuConfig.additional_images.map { |name,config|
    cmd = config.fetch(:cmd)
    out.puts "  - #{name} will run #{cmd} in production"
    image_name = %{#{@organization}/#{@app_id}:#{short_version}-#{name}}
    [
      name,
      {
        cmd:,
        image_name:,
        dockerfile: "deploy/Dockerfile.#{name}",
        heroku_image_name: "registry.heroku.com/#{heroku_app_name}/#{name}",
      }
    ]
  }.to_h

  images = {
    "web" => {
      cmd: "bin/run",
      image_name: %{#{@organization}/#{@app_id}:#{short_version}-web},
      dockerfile: "deploy/Dockerfile.web",
      heroku_image_name: "registry.heroku.com/#{heroku_app_name}/web",
    },
    "release" => {
      cmd: "bin/release",
      image_name: %{#{@organization}/#{@app_id}:#{short_version}-release},
      dockerfile: "deploy/Dockerfile.release",
      heroku_image_name: "registry.heroku.com/#{heroku_app_name}/release",
    },
  }.merge(additional_images)

  out.puts "  - release will run bin/release in production"

  local_repo_checks.check!
require_heroku_login!(options)

  FileUtils.chdir Brut.container.project_root do

    out.puts "Generating Dockerfiles"
    images.each do |name,|
      cmd        = .fetch(:cmd)
      dockerfile = .fetch(:dockerfile)

      out.puts "Creating '#{dockerfile}' for '#{name}' that will use command '#{cmd}'"

      File.open(dockerfile,"w") do |file|
        file.puts "# DO NOT EDIT - THIS IS GENERATED"
        file.puts "# To make changes, modifiy deploy/Dockerfile and run #{$0}"
        file.puts File.read("deploy/Dockerfile")
        file.puts
        file.puts "# Added by #{$0}"
        file.puts %{CMD [ "bundle", "exec", "#{cmd}" ]}
      end
    end

    out.puts "Building images"
    docker_quiet_option = if global_options.log_level != "debug"
                            "--quiet"
                          else
                            ""
                          end
    images.each do |name,|
      image_name = .fetch(:image_name)
      dockerfile = .fetch(:dockerfile)


      out.puts "Creating docker image with name '#{image_name}' and platform '#{platform}'"
      command = %{docker build #{docker_quiet_option} --build-arg app_git_sha1=#{version} --file #{Brut.container.project_root}/#{dockerfile} --platform #{platform} --tag #{image_name} .}
      system!(command)
    end

    out.puts "Taggging images for Heroku"
    images.each do |name,|
      image_name        = .fetch(:image_name)
      heroku_image_name = .fetch(:heroku_image_name)

      out.puts "Tagging '#{image_name}' with '#{heroku_image_name}' for Heroku"
      command = %{docker tag #{image_name} #{heroku_image_name}}
      system!(command)
    end

    if options.push?
      out.puts "Pushing to Heroku Registry"
      images.each do |name,|
        heroku_image_name = .fetch(:heroku_image_name)

        out.puts "Pushing '#{heroku_image_name}'"

        command = %{docker push #{docker_quiet_option} #{heroku_image_name}}
        system!(command)
      end
    else
      out.puts "Not pushing images"
    end

    names = images.map(&:first).join(" ")
    deploy_command = "heroku container:release #{names}"
    if options.deploy?
      out.puts "Deploying images to Heroku"
      system!(deploy_command)
    else
      out.puts "Not deploying.  To deploy the images just pushed:"
      out.puts ""
      out.puts "  #{deploy_command}"
    end
  end
end