Merge branch 'cid-381-improve_chef_node_name_setting' into features

This commit is contained in:
Anton Chuchkalov 2016-02-26 17:53:58 +03:00
commit 50f0a0508d
9 changed files with 96 additions and 15 deletions

View File

@ -50,8 +50,19 @@ class Stack < Handler
env = fetcher.fetch_project(q[:project])['deploy_envs'].detect {|env| env['identifier'] == q[:deploy_env]}
q[:provider] = env['provider']
filepath = options[:parameters_file] || enter_parameter(I18n.t('handler.stack.create.parameters_file'))
q[:parameters] = JSON.parse(File.read(filepath))
params_filepath = options[:parameters_file] || enter_parameter_or_empty(I18n.t('handler.stack.create.parameters_file'))
if params_filepath.empty?
q[:parameters] = {}
else
q[:parameters] = JSON.parse(File.read(params_filepath))
end
tags_filepath = options[:tags_file] || enter_parameter_or_empty(I18n.t('handler.stack.create.tags_file'))
if tags_filepath.empty?
q[:tags] = {}
else
q[:tags] = JSON.parse(File.read(tags_filepath))
end
json = JSON.pretty_generate(q)
if question(I18n.t("handler.stack.question.create")) {puts json}

View File

@ -28,6 +28,7 @@ class StackOptions < CommonOptions
parser.recognize_option_value(:deploy_env)
parser.recognize_option_value(:stack_template)
parser.recognize_option_value(:parameters_file)
parser.recognize_option_value(:tags_file)
parser.recognize_option_value(:run_list)
parser.recognize_option_value(:without_bootstrap, type: :switch, switch_value: true)
end

View File

@ -115,8 +115,9 @@ en:
create:
id: "Id: "
deploy_env: "Deploy env: "
parameters_file: "Path to file with JSON parameters: "
parameters_file: "Path to file with parameters in JSON (or Enter): "
run_list: "Run list: "
tags_file: "Path to file with tags in JSON (or Enter): "
question:
create: "Are you sure to create stack?"
delete: "Are you sure to delete stack '%{name}'?"
@ -374,6 +375,7 @@ en:
project: Stack project
stack_template: Stack template
parameters_file: File with parameters JSON
tags_file: File with tags JSON
run_list: Stack run list
without_bootstrap: Skip bootsraping phase on created instances
stack_template:

View File

@ -21,6 +21,29 @@ module Devops
json Devops::API2_0::Handler::Stack.new(request).stack_servers(name).map(&:to_hash)
end
# Create stack
#
# * *Request*
# - method : PATCH
# - headers :
# - Accept: application/json
# - Content-Type: application/json
# - body :
# {
# "without_bootstrap": null,
# "project": "project_name",
# "deploy_env": "test",
# "provider": "ec2",
# "tags": {
# "tagName": "tagValue"
# },
# "parameters": {
# "KeyName": "Value"
# }
# }
#
# * *Returns* :
# [report_id]
app.post_with_headers "/stack", :headers => [:accept, :content_type] do
check_privileges("stack", "w")
json Devops::API2_0::Handler::Stack.new(request).create_stack

View File

@ -41,6 +41,9 @@ module Devops
::Validators::FieldValidator::FieldType::Array,
::Validators::FieldValidator::RunList]
set_field_validators :tags, [::Validators::FieldValidator::Nil,
::Validators::FieldValidator::FieldType::Hash]
def initialize attrs={}
self.set_provider(attrs)
self.id = attrs['id']
@ -52,6 +55,7 @@ module Devops
self.owner = attrs['owner']
self.run_list = attrs['run_list'] || []
self.stack_status = attrs['stack_status']
self.tags = attrs['tags'] || {}
self
end
@ -65,7 +69,8 @@ module Devops
parameters: parameters,
stack_status: stack_status,
owner: owner,
run_list: run_list
run_list: run_list,
tags: tags
}.merge(provider_hash)
end

View File

@ -220,6 +220,7 @@ module Provider
out << "Stack name: #{stack.name}\n"
out << "Stack template: #{stack.stack_template}\n"
out << "Stack parameters: #{stack.parameters}\n"
out << "Stack tags: #{JSON.pretty_generate(stack_tags(stack))}\n"
out.flush
response = cloud_formation.create_stack(stack.name,
{
@ -254,7 +255,7 @@ module Provider
"cid:project" => stack.project,
"cid:deployEnv" => stack.deploy_env,
"cid:user" => stack.owner
}
}.merge(stack.tags)
end
def update_stack(stack, params)

View File

@ -19,6 +19,7 @@ RSpec.describe Devops::Model::StackEc2, type: :model do
include_examples 'field type validation', :name, :maybe_nil, :non_empty_string, :field_validator
include_examples 'field type validation', :owner, :not_nil, :non_empty_string, :field_validator
include_examples 'field type validation', :run_list, :maybe_nil, :maybe_empty_array, :run_list, :field_validator
include_examples 'field type validation', :tags, :maybe_nil, :field_validator
it 'validates provider' do
stack.provider = nil
@ -27,11 +28,13 @@ RSpec.describe Devops::Model::StackEc2, type: :model do
stack.provider = ''
expect(stack).not_to be_valid
end
pending 'stack tags should be a hash'
end
describe '#to_hash_without_id' do
it 'returns hash with several fields' do
expect(stack.to_hash_without_id.keys).to include('provider', :project, :deploy_env, :stack_template, :name, :owner, :run_list)
expect(stack.to_hash_without_id.keys).to include('provider', :project, :deploy_env, :stack_template, :name, :owner, :run_list, :tags)
end
end
@ -43,6 +46,10 @@ RSpec.describe Devops::Model::StackEc2, type: :model do
it 'sets run_list to empty array if it\'s blank' do
expect(described_class.new.run_list).to eq []
end
it 'sets tags to empty hash if it\'s blank' do
expect(described_class.new.tags).to eq({})
end
end

View File

@ -36,7 +36,7 @@ RSpec.describe ChefNodeNameBuilder do
expect(build_node_name).to eq 'proj-master01-dev'
end
it 'substitutes project, env, instanceid and cfname' do
it 'substitutes $project, $env, $instanceid and $cfname' do
set_mask('$project/$env/$instanceid/$cfname')
expect(build_node_name).to eq 'proj/dev/i-fac32c7e/master01'
end
@ -46,6 +46,21 @@ RSpec.describe ChefNodeNameBuilder do
expect(build_node_name).to match /proj-\d+/
end
it 'substitutes :project, :env, :instanceid and :cfname' do
set_mask(':project/:env/:instanceid/:cfname')
expect(build_node_name).to eq 'proj/dev/i-fac32c7e/master01'
end
it 'substitutes :time' do
set_mask(':project-:time')
expect(build_node_name).to match /proj-\d+/
end
it 'works with both colon and dollar variables' do
set_mask('$project/$env/:instanceid/:cfname')
expect(build_node_name).to eq 'proj/dev/i-fac32c7e/master01'
end
it 'substitutes underscores to dashes' do
server_info['tags']['Name'] = 'server_1'
expect(build_node_name).to match 'proj-server-1-dev'

View File

@ -3,16 +3,32 @@ class ChefNodeNameBuilder
def initialize(server_info, project, env)
@server_info, @project, @env = server_info, project, env
@mask = (server_info['tags']['cid:node-name-mask'] || DEFAULT_MASK).dup
@mask = server_info['tags']['cid:node-name-mask'] || DEFAULT_MASK
end
def build_node_name
@mask.gsub!('$project', @project.id)
@mask.gsub!('$env', @env.identifier)
@mask.gsub!('$instanceid', @server_info['id'])
@mask.gsub!('$cfname', @server_info['tags']['Name'] || '')
@mask.gsub!('$time', Time.now.to_i.to_s)
@mask.gsub!('_', '-')
@mask
result = @mask.dup
replace_dollar_variables!(result)
replace_colon_variables!(result)
result.gsub!('_', '-')
result
end
private
def replace_dollar_variables!(result)
result.gsub!('$project', @project.id)
result.gsub!('$env', @env.identifier)
result.gsub!('$instanceid', @server_info['id'])
result.gsub!('$cfname', @server_info['tags']['Name'] || '')
result.gsub!('$time', Time.now.to_i.to_s)
end
def replace_colon_variables!(result)
result.gsub!(':project', @project.id)
result.gsub!(':env', @env.identifier)
result.gsub!(':instanceid', @server_info['id'])
result.gsub!(':cfname', @server_info['tags']['Name'] || '')
result.gsub!(':time', Time.now.to_i.to_s)
end
end