Class: Kontena::Cli::Apps::KontenaYmlGenerator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

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

Constructor Details

#initialize(image_name, service_prefix) ⇒ KontenaYmlGenerator

Returns a new instance of KontenaYmlGenerator.



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

def initialize(image_name, service_prefix)
  @image_name = image_name
  @service_prefix = service_prefix
end

Instance Attribute Details

#image_nameObject (readonly)

Returns the value of attribute image_name.



7
8
9
# File 'lib/kontena/cli/apps/kontena_yml_generator.rb', line 7

def image_name
  @image_name
end

#service_prefixObject (readonly)

Returns the value of attribute service_prefix.



7
8
9
# File 'lib/kontena/cli/apps/kontena_yml_generator.rb', line 7

def service_prefix
  @service_prefix
end

Instance Method Details

#create_yml(services, filename) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/kontena/cli/apps/kontena_yml_generator.rb', line 88

def create_yml(services, filename)
  if File.exist?(filename) && !File.zero?(filename)
    kontena_services = yml_services(File.read(filename))
    services.each do |name, options|
      if kontena_services[name]
        services[name].merge!(kontena_services[name])
      end
    end
  end
  kontena_services = {
    'version' => '2',
    'name' => service_prefix,
    'services' => services
  }
  super(kontena_services, filename)
end

#generate(procfile, addons, env_file) ⇒ Object



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
# File 'lib/kontena/cli/apps/kontena_yml_generator.rb', line 55

def generate(procfile, addons, env_file)
  image = image_name || "registry.kontena.local/#{File.basename(Dir.getwd)}:latest"
  if procfile.keys.size > 0
    # generate services found in Procfile
    services = {}
    procfile.keys.each do |name|
      services[name] = {'image' => image}
      services[name]['environment'] = ['PORT=5000'] if app_json && name == 'web' # Heroku generates PORT env variable so should we do too
      services[name]['command'] = "/start #{name}" if name != 'web'
      services[name]['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[name]['links'] ||= []
          services[name]['links'] << "#{addon_service}:#{addon_service}"
          services[name]['environment'] ||= []
          services[name]['environment'] += valid_addons(service_prefix)[addon_service]['environment']
          services[addon_service] = {'image' => valid_addons[addon_service]['image'], 'stateful' => true}
        end
      end
    end
  else
    # no Procfile found, create dummy web service
    services = {'web' => { 'image' => image}}
    services['web']['env_file'] = env_file if env_file
  end
  # create kontena.yml file
  create_yml(services, 'kontena.yml')
end

#generate_from_compose_file(docker_compose_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
# File 'lib/kontena/cli/apps/kontena_yml_generator.rb', line 14

def generate_from_compose_file(docker_compose_file)
  services = {}
  # extend services from docker-compose.yml
  file = File.read(docker_compose_file)

  yml_services(file).each do |name, options|
    services[name] = {'extends' => { 'file' => 'docker-compose.yml', 'service' => name }}
    if options.has_key?('build')
      image = image_name || "registry.kontena.local/#{File.basename(Dir.getwd)}:latest"
      services[name]['image'] = image
    end

    # set Heroku addon service as stateful by default
    if valid_addons.has_key?(name)
      services[name]['stateful'] = true
    end

    # we have to generate Kontena urls to env vars for Heroku addons
    # redis://openredis:6379 -> redis://project-name-openredis:6379
    if options['links']
      options['links'].each do |link|
        service_link = link.split(':').first
        if valid_addons.has_key?(service_link)
          services[name]['environment'] ||= []
          services[name]['environment'] += valid_addons(service_prefix)[service_link]['environment']
        end
      end
    end
  end
  create_yml(services, 'kontena.yml')
end

#yml_services(file) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/kontena/cli/apps/kontena_yml_generator.rb', line 46

def yml_services(file)
  yml = ::YAML.safe_load(file)
  if yml['version'] == '2'
    yml['services']
  else
    yml
  end
end