CID-418: add ability to set chef_client_options for single deploy

This commit is contained in:
Anton Chuchkalov 2016-03-01 17:41:51 +03:00
parent c32cded09b
commit 03532e68e8
7 changed files with 26 additions and 9 deletions

View File

@ -20,13 +20,12 @@ class Deploy < Handler
end
def deploy_handler args
tags = options[:tags]
names = args[1..-1]
if names.empty?
@options_parser.invalid_deploy_command
abort()
end
job_ids = post("/deploy", :names => names, :tags => tags)
job_ids = post("/deploy", names: names, tags: options[:tags], chef_client_options: options[:chef_client_options])
reports_urls(job_ids)
end

View File

@ -13,10 +13,12 @@ class DeployOptions < CommonOptions
def deploy_options
options do |parser, options|
parser.banner << self.banner
parser.resource_name = :deploy
parser.recognize_option_value(:tag, resource_name: :deploy, variable_name: 'TAG1,TAG2...') do |tags|
parser.recognize_option_value(:tag, variable: 'TAG1,TAG2...') do |tags|
options[:tags] = tags.split(",")
end
parser.recognize_option_value(:chef_client_options)
end
end

View File

@ -314,6 +314,7 @@ en:
descriptions:
deploy:
tag: 'Tag names, comma separated list'
chef_client_options: 'String like "-o role[foo]"'
image:
provider: Image provider
image_id: Image identifier

View File

@ -19,6 +19,7 @@ module Devops
names = body["names"]
tags = body["tags"] || []
run_list = body["run_list"]
single_run_chef_client_options = body['chef_client_options']
files = []
jid = nil
owner = parser.current_user
@ -33,6 +34,7 @@ module Devops
begin
deploy_info = create_deploy_info(s, project, body["build_number"])
deploy_info["run_list"] = run_list if run_list
set_chef_client_options(deploy_info, s, project, single_run_chef_client_options)
jid = Worker.start_async(DeployWorker,
server_attrs: s.to_hash,
@ -117,6 +119,17 @@ module Devops
@deploy_info_buf[buf_key] = project.deploy_info(deploy_env_model, build_number)
end
end
private
# env's chef client options may be nil or empty string; it's OK.
def set_chef_client_options(deploy_info, server, project, single_run_options)
if single_run_options
deploy_info['chef_client_options'] = single_run_options
else
deploy_info['chef_client_options'] = project.deploy_env(server.deploy_env).chef_client_options
end
end
end
end
end

View File

@ -18,6 +18,7 @@ module Devops
# "tags": [], -> array of tags to apply on each server before running chef-client
# "build_number": "", -> string, build number to deploy
# "run_list": [], -> array of strings to set run_list for chef-client
# "chef_client_options": "", String, optional. May be used to redefine run_list
# }
#
# * *Returns* : text stream

View File

@ -394,8 +394,9 @@ module Devops
@out.flush
cmd << " -j http://#{DevopsConfig.config[:address]}:#{DevopsConfig.config[:port]}/#{DevopsConfig.config[:url_prefix]}/v2.0/deploy/data/#{file}"
else
if @deploy_env.chef_client_options && !@deploy_env.chef_client_options.empty?
cmd << " #{@deploy_env.chef_client_options}"
chef_client_options = deploy_info['chef_client_options']
if chef_client_options && !chef_client_options.empty?
cmd << " #{chef_client_options}"
else
cmd << " -r #{deploy_info["run_list"].join(",")}" unless @server.stack.nil?
end

View File

@ -576,14 +576,14 @@ RSpec.describe Devops::Executor::ServerExecutor, type: :executor, stubbed_connec
deploy_server
end
it "uses deploy_env's chef_client_options if they are set" do
deploy_env.chef_client_options = '-r role'
it "uses chef_client_options from deploy_info if it is set" do
deploy_info['chef_client_options'] = '-r role'
expect(stubbed_knife).to receive(:ssh_stream).with(anything, 'chef-client --no-color -r role', any_args)
deploy_server
end
it "doesn't use deploy_env's chef_client_options if it's blank string" do
deploy_env.chef_client_options = ''
it "doesn't use chef_client_options from deploy_info if it's blank string" do
deploy_info['chef_client_options'] = ''
expect(stubbed_knife).to receive(:ssh_stream).with(anything, 'chef-client --no-color', any_args)
deploy_server
end