Class: Kontena::Cli::Apps::DockerComposeGenerator

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/kontena/cli/apps/docker_compose_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#abort_on_validation_errors, #app_json, #create_yml, #current_dir, #display_notifications, #generate_services, #hint_on_validation_notifications, #prefixed_name, #project_name_from_yaml, #read_yaml, #require_config_file, #service_exists?, #service_prefix, #services_from_yaml, #set_env_variables, #token, #valid_addons

Constructor Details

#initialize(filename) ⇒ DockerComposeGenerator

Returns a new instance of DockerComposeGenerator.



10
11
12
# File 'lib/kontena/cli/apps/docker_compose_generator.rb', line 10

def initialize(filename)
  @docker_compose_file = filename
end

Instance Attribute Details

#docker_compose_fileObject (readonly)

Returns the value of attribute docker_compose_file.



8
9
10
# File 'lib/kontena/cli/apps/docker_compose_generator.rb', line 8

def docker_compose_file
  @docker_compose_file
end

Instance Method Details

#generate(procfile, addons, env_file) ⇒ Object



14
15
16
17
18
19
20
21
22
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
# File 'lib/kontena/cli/apps/docker_compose_generator.rb', line 14

def generate(procfile, addons, env_file)
  if procfile.keys.size > 0
    # generate services found in Procfile
    docker_compose = {
      'version' => '2'
    }
    services = {}
    procfile.each do |service, command|
      services[service] = {'build' => '.' }
      if app_json && service == 'web' # Heroku generates PORT env variable so should we do too
        services[service]['environment'] = ['PORT=5000']
        services[service]['ports'] = ['5000:5000']
      end
      services[service]['command'] =  "/start #{service}" if service != 'web'
      services[service]['env_file'] = env_file if env_file

      # generate addon services
      addons.each do |addon|
        addon_service = addon.split(":")[0]
        addon_service.slice!('heroku-')
        if valid_addons.has_key?(addon_service)
          services[service]['links'] = [] unless services[service]['links']
          services[service]['links'] << "#{addon_service}:#{addon_service}"
          services[service]['environment'] = [] unless services[service]['environment']
          services[service]['environment'] += valid_addons[addon_service]['environment']
          services[addon_service] = {'image' => valid_addons[addon_service]['image']}
        end
      end
    end
    docker_compose['services'] = services
  else
    # no Procfile found, create dummy web service
    docker_compose = {
      'version' => '2',
      'services' => {
        'web' => {
          'build' => '.'
        }
      }
    }

    docker_compose['services']['web']['env_file'] = env_file if env_file
  end
  # create docker-compose.yml file
  create_yml(docker_compose, docker_compose_file)
end