Class: Brut::CLI::Apps::Deploy::DockerCompose::Check

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



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

def default_rack_env = "development"

#descriptionObject



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

def description = "Check if the existing docker-compose.yml is consistent with the deploy config"

#runObject



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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/brut/cli/apps/deploy.rb', line 181

def run
  docker_compose_path = Brut.container.project_root / "deploy" / "docker-compose.yml"
  if !docker_compose_path.exist?
    fatal "Could not find #{docker_compose_path}"
    return 1
  end
  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
    docker_compose_contents = YAML.load(File.read(docker_compose_path))
    missing = []
    extra   = []
    wrong   = {}
    failed  = false
    configured_services = []
    expected_image_name = "#{Brut.container.app_organization}/#{Brut.container.app_id}:${DOCKER_IMAGE_TAG}"
    if config.registry_hostname
      expected_image_name = "#{config.registry_hostname}/#{expected_image_name}"
    end
    config.processes.each do |process_description|
      configured_services << process_description.name
      service = docker_compose_contents["services"][process_description.name]
      if service
        image = service["image"]
        cmd   = service["command"]
        if image != expected_image_name
          wrong[process_description] ||= {}
          wrong[process_description][:image] = {
            expected: expected_image_name,
            actual: image
          }
          failed = true
        end
        if cmd != process_description.cmd
          wrong[process_description] ||= {}
          wrong[process_description][:command] = {
            expected: process_description.cmd,
            actual: cmd
          }
          failed = true
        end
      else
        missing << process_description
        failed = true
      end
    end
    docker_compose_contents["services"].each do |service_name,configuration|
      if !configured_services.include?(service_name)
        extra << service_name
        failed = true
      end
    end
    if failed
      missing.each do |process_description|
        fatal "service #{process_description.name}: MISSING"
      end
      wrong.each do |process_description, problems|
        problems.each do |key,expected_actual|
          fatal "service #{process_description.name}: #{key} incorrect. Expected '#{expected_actual[:expected]}', but got '#{expected_actual[:actual]}'"
        end
      end
      extra.each do |service_name|
        fatal "service #{service_name}: not in deploy config"
      end
      return 1
    end
    0
  rescue LoadError => ex
    fatal "Could not find #{deploy_config_path}: #{ex}"
    1
  end
end