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, #default_command, #default_command_class, #default_rack_env, #env, #env_vars, #execute, #name, #options, #puts, #stderr, #stdin, #stdout, #system!

Instance Method Details

#descriptionObject



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

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

#optsObject



128
129
130
131
# File 'lib/brut/cli/apps/deploy.rb', line 128

def opts = [
  [ "--platform=PLATFORM","Override default platform. Can be any Docker platform." ],
  [ "--dry-run", "Only show what would happen, don't actually do anything" ],
]

#runObject



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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/brut/cli/apps/deploy.rb', line 133

def run
  version = ""
  git_guess = %{git rev-parse HEAD}
  system!(git_guess) do |output|
    version << output
  end
  version.strip!.chomp!
  if version == ""
    stderr.puts "Attempt to use git via command '#{git_guess}' to figure out the version failed"
    return 1
  end
  short_version = version[0..7]

  app_docker_images = AppDockerImages.new(
    project_root: Brut.container.project_root,
    organization: Brut.container.organization,
    app_id: Brut.container.app_id,
    short_version:
  )

  FileUtils.chdir Brut.container.project_root do

    stdout.puts "Generating Dockerfiles"
    images.each do |name:, cmd:, dockerfile:|

      stdout.puts "Creating '#{dockerfile}' for '#{name}' that will use command '#{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

    stdout.puts "Building images"
    docker_quiet_option = if global_options.log_level != "debug"
                            "--quiet"
                          else
                            ""
                          end
    images.each do |image_name:, dockerfile:|
      stdout.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} .}
      if options.dry_run?
        stdout.puts "Would run '#{command}'"
      else
        system!(command)
      end
    end
  end
end