lib/kontena/cli/apps/deploy_command.rb in kontena-cli-0.8.4 vs lib/kontena/cli/apps/deploy_command.rb in kontena-cli-0.9.0

- old
+ new

@@ -1,52 +1,59 @@ require 'yaml' require_relative 'common' +require_relative 'docker_helper' module Kontena::Cli::Apps class DeployCommand < Clamp::Command include Kontena::Cli::Common include Common + include DockerHelper option ['-f', '--file'], 'FILE', 'Specify an alternate Kontena compose file', attribute_name: :filename, default: 'kontena.yml' + option ['--no-build'], :flag, 'Don\'t build an image, even if it\'s missing', default: false option ['-p', '--project-name'], 'NAME', 'Specify an alternate project name (default: directory name)' parameter "[SERVICE] ...", "Services to start" - attr_reader :services, :service_prefix + attr_reader :services, :service_prefix, :deploy_queue def execute require_api_url require_token - @services = load_services_from_yml - Dir.chdir(File.dirname(filename)) - init_services(services) + require_config_file(filename) + dir = Dir.getwd + @deploy_queue = [] + @service_prefix = project_name || current_dir + @services = load_services(filename, service_list, service_prefix) + Dir.chdir(dir) + process_docker_images(services) if !no_build? && dockerfile_exist? + create_or_update_services(services) deploy_services(deploy_queue) end private - def init_services(services) + def create_or_update_services(services) services.each do |name, config| create_or_update_service(name, config) end end def deploy_services(queue) queue.each do |service| - puts "deploying #{service['id']}" + puts "deploying #{service['id'].colorize(:cyan)}" data = {} if service['deploy'] data[:strategy] = service['deploy']['strategy'] if service['deploy']['strategy'] data[:wait_for_port] = service['deploy']['wait_for_port'] if service['deploy']['wait_for_port'] end deploy_service(token, service['id'].split('/').last, data) end end def create_or_update_service(name, options) - - # skip if service is already created or updated or it's not present + # skip if service is already processed or it's not present return nil if in_deploy_queue?(name) || !services.keys.include?(name) # create/update linked services recursively before continuing unless options['links'].nil? parse_links(options['links']).each_with_index do |linked_service, index| @@ -55,10 +62,11 @@ create_or_update_service(linked_service[:name], services[linked_service[:name]]) unless in_deploy_queue?(linked_service[:name]) end end + merge_external_links(options) merge_env_vars(options) if service_exists?(name) service = update(name, options) else @@ -75,20 +83,20 @@ get_service(token, prefixed_name(name)) rescue nil end def create(name, options) name = prefixed_name(name) - puts "creating #{name}" + puts "creating #{name.colorize(:cyan)}" data = {name: name} data.merge!(parse_data(options)) create_service(token, current_grid, data) end def update(id, options) id = prefixed_name(id) data = parse_data(options) - puts "updating #{id}" + puts "updating #{id.colorize(:cyan)}" update_service(token, id, data) end def in_deploy_queue?(name) deploy_queue.find {|service| service['name'] == prefixed_name(name)} != nil @@ -105,10 +113,18 @@ end options['environment'].uniq! {|s| s.split('=').first} end + def merge_external_links(options) + if options['external_links'] + options['links'] ||= [] + options['links'] = options['links'] + options['external_links'] + options.delete('external_links') + end + end + def read_env_file(path) File.readlines(path).delete_if { |line| line.start_with?('#') || line.empty? } end ## @@ -127,15 +143,17 @@ data[:volumes_from] = options['volumes_from'] if options['volumes_from'] data[:cmd] = options['command'].split(" ") if options['command'] data[:affinity] = options['affinity'] if options['affinity'] data[:user] = options['user'] if options['user'] data[:stateful] = options['stateful'] == true + data[:privileged] = options['privileged'] unless options['privileged'].nil? data[:cap_add] = options['cap_add'] if options['cap_add'] data[:cap_drop] = options['cap_drop'] if options['cap_drop'] + data[:net] = options['net'] if options['net'] + data[:log_driver] = options['log_driver'] if options['log_driver'] + data[:log_opts] = options['log_opt'] if options['log_opt'] && !options['log_opt'].empty? + data end - def deploy_queue - @deploy_queue ||= [] - end end end