diff --git a/devops-client/lib/devops-client/handler/stack.rb b/devops-client/lib/devops-client/handler/stack.rb index b32b4b8..7385024 100644 --- a/devops-client/lib/devops-client/handler/stack.rb +++ b/devops-client/lib/devops-client/handler/stack.rb @@ -41,6 +41,9 @@ class Stack < Handler else output end + when :change_stack_template + change_stack_template_handler + output end end @@ -136,6 +139,16 @@ class Stack < Handler puts response.inspect end + def change_stack_template_handler + stack_id, template_id = @args[2], @args[3] + r = inspect_parameters(@options_parser.delete_params, stack_id, template_id) + unless r.nil? + @options_parser.invalid_change_stack_template_command + abort(r) + end + @show = put("/stack/#{stack_id}/stack_template", {stack_template: template_id}) + end + private def provider_stacks(provider) diff --git a/devops-client/lib/devops-client/options/stack_options.rb b/devops-client/lib/devops-client/options/stack_options.rb index 2bfd266..c33c25a 100644 --- a/devops-client/lib/devops-client/options/stack_options.rb +++ b/devops-client/lib/devops-client/options/stack_options.rb @@ -2,7 +2,7 @@ require "devops-client/options/common_options" class StackOptions < CommonOptions - commands :create, :delete, :list, :show, :sync, :resources, :deploy, :reserve, :unreserve + commands :create, :delete, :list, :show, :sync, :resources, :deploy, :reserve, :unreserve, :change_stack_template def initialize args, def_options super(args, def_options) @@ -15,6 +15,7 @@ class StackOptions < CommonOptions self.resources_params = ["STACK"] self.deploy_params = ["STACK"] self.reserve_params = ["STACK"] + self.change_stack_template_params = %w(STACK STACK_TEMPLATE) end def create_options diff --git a/devops-service/app/api2/handlers/stack.rb b/devops-service/app/api2/handlers/stack.rb index ffc1fe0..c6b122d 100644 --- a/devops-service/app/api2/handlers/stack.rb +++ b/devops-service/app/api2/handlers/stack.rb @@ -203,6 +203,13 @@ module Devops end end + def change_stack_template(stack_id) + stack_template_id = parser.change_stack_template + Devops::Db.connector.stack_template(stack_template_id) + stack = Devops::Db.connector.stack(stack_id) + stack.change_stack_template!(stack_template_id) + end + end end end diff --git a/devops-service/app/api2/parsers/stack.rb b/devops-service/app/api2/parsers/stack.rb index c9d5c64..de1ee1c 100644 --- a/devops-service/app/api2/parsers/stack.rb +++ b/devops-service/app/api2/parsers/stack.rb @@ -38,6 +38,12 @@ module Devops @body end + def change_stack_template + template_id = create_object_from_json_body['stack_template'] + check_string(template_id, "Parameter 'stack_template' must be a not empty string") + template_id + end + end end end diff --git a/devops-service/app/api2/routes/stack.rb b/devops-service/app/api2/routes/stack.rb index 37f3413..61ac8ad 100644 --- a/devops-service/app/api2/routes/stack.rb +++ b/devops-service/app/api2/routes/stack.rb @@ -187,6 +187,25 @@ module Devops } app.multi_routes "/stack/:id/tags", {:headers => [:content_type]}, hash + # Change stack stack_template + # + # * *Request* + # - method : PUT + # - headers : + # - Accept: application/json + # - Content-Type: application/json + # - body : + # { + # "stack_template": "template_id" + # } + # + # * *Returns* : + # 200 - OK + app.put_with_headers "/stack/:id/stack_template", :headers => [:accept, :content_type] do |id| + check_privileges("stack", "r") + json Devops::API2_0::Handler::Stack.new(request).change_stack_template(id) + end + puts "Stack routes initialized" end diff --git a/devops-service/db/mongo/models/stack/stack_ec2.rb b/devops-service/db/mongo/models/stack/stack_ec2.rb index 32111c1..588a631 100644 --- a/devops-service/db/mongo/models/stack/stack_ec2.rb +++ b/devops-service/db/mongo/models/stack/stack_ec2.rb @@ -18,6 +18,23 @@ module Devops result end + def update_in_cloud!(params) + parameters = params.keep_if do |key, value| + %w(Parameters TemplateBody TemplateURL Capabilities) + end + + provider_instance.update_stack(self, parameters) + end + + # We should improve this functionality: stack will be rollbacked + # if there are errors during stack_template changing, so we should track + # that process and persist changes only in case of success. + def change_stack_template!(stack_template) + self.stack_template = stack_template + update_in_cloud!('TemplateBody' => template_body) + Devops::Db.connector.stack_update(self) + end + end end end diff --git a/devops-service/providers/ec2.rb b/devops-service/providers/ec2.rb index f8ff36d..c9e97f1 100644 --- a/devops-service/providers/ec2.rb +++ b/devops-service/providers/ec2.rb @@ -261,6 +261,10 @@ module Provider end end + def update_stack(stack, params) + cloud_formation.update_stack(stack.name, params) + end + def set_stack_tags stack, out="" tags = { # "cid:remoteUser" => s.remote_user