Module: Kontena::Cli::Apps::Common
- Includes:
- Services::ServicesHelper
- Included in:
- BuildCommand, ConfigCommand, DeployCommand, DockerComposeGenerator, DockerfileGenerator, InitCommand, KontenaYmlGenerator, ListCommand, LogsCommand, MonitorCommand, RemoveCommand, RestartCommand, ScaleCommand, ShowCommand, StartCommand, StopCommand
- Defined in:
- lib/kontena/cli/apps/common.rb
Instance Method Summary collapse
- #abort_on_validation_errors(errors) ⇒ Object
- #app_json ⇒ Hash
- #create_yml(services, file = 'kontena.yml') ⇒ Object
- #current_dir ⇒ String
- #display_notifications(messages, color = :yellow) ⇒ Object
- #generate_services(yaml_services, version) ⇒ Hash
- #hint_on_validation_notifications(errors) ⇒ Object
- #prefixed_name(name) ⇒ String
- #project_name_from_yaml(file) ⇒ Object
- #read_yaml(filename) ⇒ Object
- #require_config_file(filename) ⇒ Object
- #service_exists?(name) ⇒ Boolean
- #service_prefix ⇒ Object
- #services_from_yaml(filename, service_list, prefix, skip_validation = false) ⇒ Hash
- #set_env_variables(project, grid) ⇒ Object
- #token ⇒ String
- #valid_addons(prefix = nil) ⇒ Object
Instance Method Details
#abort_on_validation_errors(errors) ⇒ Object
133 134 135 136 137 |
# File 'lib/kontena/cli/apps/common.rb', line 133 def abort_on_validation_errors(errors) $stderr.puts "YAML validation failed! Aborting.".colorize(:red) display_notifications(errors, :red) abort end |
#app_json ⇒ Hash
101 102 103 104 105 106 |
# File 'lib/kontena/cli/apps/common.rb', line 101 def app_json if !@app_json && File.exist?('app.json') @app_json = JSON.parse(File.read('app.json')) end @app_json ||= {} end |
#create_yml(services, file = 'kontena.yml') ⇒ Object
94 95 96 97 98 |
# File 'lib/kontena/cli/apps/common.rb', line 94 def create_yml(services, file = 'kontena.yml') yml = File.new(file, 'w') yml.puts services.to_yaml yml.close end |
#current_dir ⇒ String
82 83 84 |
# File 'lib/kontena/cli/apps/common.rb', line 82 def current_dir File.basename(Dir.getwd) end |
#display_notifications(messages, color = :yellow) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/kontena/cli/apps/common.rb', line 108 def display_notifications(, color = :yellow) .each do |files| files.each do |file, services| $stderr.puts "#{file}:".colorize(color) services.each do |service| service.each do |name, errors| $stderr.puts " #{name}:".colorize(color) if errors.is_a?(String) $stderr.puts " - #{errors}".colorize(color) else errors.each do |key, error| $stderr.puts " - #{key}: #{error.to_json}".colorize(color) end end end end end end end |
#generate_services(yaml_services, version) ⇒ Hash
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/kontena/cli/apps/common.rb', line 35 def generate_services(yaml_services, version) services = {} if version.to_i == 2 generator_klass = ServiceGeneratorV2 else generator_klass = ServiceGenerator end yaml_services.each do |service_name, config| exit_with_error("Image is missing for #{service_name}. Aborting.") unless config['image'] services[service_name] = generator_klass.new(config).generate end services end |
#hint_on_validation_notifications(errors) ⇒ Object
128 129 130 131 |
# File 'lib/kontena/cli/apps/common.rb', line 128 def hint_on_validation_notifications(errors) $stderr.puts "YAML contains the following unsupported options and they were rejected:".colorize(:yellow) display_notifications(errors) end |
#prefixed_name(name) ⇒ String
76 77 78 79 |
# File 'lib/kontena/cli/apps/common.rb', line 76 def prefixed_name(name) return name if service_prefix.strip == "" "#{service_prefix}-#{name}" end |
#project_name_from_yaml(file) ⇒ Object
64 65 66 67 |
# File 'lib/kontena/cli/apps/common.rb', line 64 def project_name_from_yaml(file) reader = YAML::Reader.new(file, true) reader.stack_name end |
#read_yaml(filename) ⇒ Object
49 50 51 52 53 |
# File 'lib/kontena/cli/apps/common.rb', line 49 def read_yaml(filename) reader = YAML::Reader.new(filename) outcome = reader.execute outcome end |
#require_config_file(filename) ⇒ Object
11 12 13 |
# File 'lib/kontena/cli/apps/common.rb', line 11 def require_config_file(filename) exit_with_error("File #{filename} does not exist") unless File.exists?(filename) end |
#service_exists?(name) ⇒ Boolean
88 89 90 |
# File 'lib/kontena/cli/apps/common.rb', line 88 def service_exists?(name) get_service(token, prefixed_name(name)) rescue false end |
#service_prefix ⇒ Object
60 61 62 |
# File 'lib/kontena/cli/apps/common.rb', line 60 def service_prefix @service_prefix ||= project_name || project_name_from_yaml(filename) || current_dir end |
#services_from_yaml(filename, service_list, prefix, skip_validation = false) ⇒ Hash
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/kontena/cli/apps/common.rb', line 20 def services_from_yaml(filename, service_list, prefix, skip_validation = false) set_env_variables(prefix, current_grid) reader = YAML::Reader.new(filename, skip_validation) outcome = reader.execute hint_on_validation_notifications(outcome[:notifications]) if outcome[:notifications].size > 0 abort_on_validation_errors(outcome[:errors]) if outcome[:errors].size > 0 kontena_services = generate_services(outcome[:services], outcome[:version]) kontena_services.delete_if { |name, service| !service_list.include?(name)} unless service_list.empty? kontena_services end |
#set_env_variables(project, grid) ⇒ Object
55 56 57 58 |
# File 'lib/kontena/cli/apps/common.rb', line 55 def set_env_variables(project, grid) ENV['project'] = project ENV['grid'] = grid end |
#token ⇒ String
70 71 72 |
# File 'lib/kontena/cli/apps/common.rb', line 70 def token @token ||= require_token end |
#valid_addons(prefix = nil) ⇒ Object
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 |
# File 'lib/kontena/cli/apps/common.rb', line 139 def valid_addons(prefix=nil) if prefix prefix = "#{prefix}-" end { 'openredis' => { 'image' => 'redis:latest', 'environment' => ["REDIS_URL=redis://#{prefix}openredis:6379"] }, 'redis' => { 'image' => 'redis:latest', 'environment' => ["REDIS_URL=redis://#{prefix}redis:6379"] }, 'rediscloud' => { 'image' => 'redis:latest', 'environment' => ["REDISCLOUD_URL=redis://#{prefix}rediscloud:6379"] }, 'postgresql' => { 'image' => 'postgres:latest', 'environment' => ["DATABASE_URL=postgres://#{prefix}postgres:@postgresql:5432/postgres"] }, 'mongolab' => { 'image' => 'mongo:latest', 'environment' => ["MONGOLAB_URI=#{prefix}mongolab:27017"] }, 'memcachedcloud' => { 'image' => 'memcached:latest', 'environment' => ["MEMCACHEDCLOUD_SERVERS=#{prefix}memcachedcloud:11211"] } } end |