Class: Brut::CLI::Apps::Deploy::Build::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, #puts, #stdin, #system!, #theme

Constructor Details

#initialize(push: false) ⇒ Docker

Returns a new instance of Docker.



142
143
144
# File 'lib/brut/cli/apps/deploy.rb', line 142

def initialize(push: false)
  @push = push
end

Instance Method Details

#default_rack_envObject



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

def default_rack_env = "development"

#descriptionObject



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

def description = "Build a series of Docker images from a template Dockerfile"

#optsObject



135
136
137
138
139
# File 'lib/brut/cli/apps/deploy.rb', line 135

def opts = [
  [ "--platform=PLATFORM","Override default platform. Can be any Docker platform." ],
  [ "--dry-run", "Only show what would happen, don't actually do anything" ],
  [ "--skip-checks", "If true, skip pre-build checks (default )" ],
]

#runObject



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
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
223
224
225
226
227
228
229
230
231
232
# File 'lib/brut/cli/apps/deploy.rb', line 146

def run
  if !options.skip_checks?
    execute_result = Brut::CLI::ExecuteResult.new do
      delegate_to_command(Brut::CLI::Apps::Deploy::Check.new)
    end
    if execute_result.failed?
      puts theme.error.render("Pre-build checks failed.")
      return execute_result.exit_status do |error_message|
        puts theme.error.render("Error message from checks: #{error_message}")
      end
    end
  end
  version = ""
  git_guess = %{git rev-parse HEAD}
  system!(git_guess) do |output|
    version << output
  end
  version.strip!.chomp!
  if version == ""
    error "Attempt to use git via command '#{git_guess}' to figure out the version failed"
    return 1
  end
  short_version = version[0..7]
  app_docker_files = AppDockerImages.new(
    project_root: Brut.container.project_root,
    organization: Brut.container.app_organization,
    app_id: Brut.container.app_id,
    short_version:
  )
  options.set_default(:platform, app_docker_files.platform || "linux/amd64")

  FileUtils.chdir Brut.container.project_root do

    puts
    puts theme.header.render("Generating Dockerfiles")
    puts
    rows = []
    app_docker_files.each do |name:, cmd:, dockerfile:|

      rows << [theme.subheader.render(name), theme.code.render(dockerfile), theme.code.render(cmd) ]

      if !options.dry_run?
        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
    end
    table = Lipgloss::Table.new.headers(["Name", "Dockerfile", "CMD"]).
      rows(rows).
      style_func(rows: rows.length, columns: 3) { Lipgloss::Style.new.padding_right(1).padding_left(1) }
    puts table.render

    puts
    puts theme.header.render("Images")
    puts
    rows = []
    items = []
    push_or_load = @push ? "--push" : "--load"
    app_docker_files.each do |name:, image_name:, dockerfile:|
      if @push && @push.kind_of?(String)
        image_name = @push % { name: name }
      end
      rows << [ name, theme.code.render(image_name) ]
      command = %{docker buildx build --provenance=false --build-arg app_git_sha1=#{version} --file #{Brut.container.project_root}/#{dockerfile} --platform #{options.platform} #{push_or_load} --tag #{image_name} . 2>&1}
      items << theme.code.render(theme.wrap(command, first_indent: false, indent: 7, newline: " \\\n"))
      if !options.dry_run?
        puts theme.subheader.render("Building #{@push ? 'and pushing' : '' } '#{name}' image")
        system!(command)
      end
    end
    if options.dry_run?
      table = Lipgloss::Table.new.headers(["Name", "Image Name" ]).
        rows(rows).
        style_func(rows: rows.length, columns: 3) { Lipgloss::Style.new.padding_right(1).padding_left(1) }
      puts table.render
      puts
      puts theme.subheader.render("Commands:")
      puts
      puts Lipgloss::List.new.items(items).item_style(theme.code).render
    end
  end
end