232 lines
7.5 KiB
Ruby
232 lines
7.5 KiB
Ruby
module Devops
|
|
module API2_0
|
|
module Routes
|
|
module StackRoutes
|
|
|
|
def self.registered(app)
|
|
|
|
app.get_with_headers '/stacks', :headers => [:accept] do
|
|
check_privileges("stack", "r")
|
|
json Devops::API2_0::Handler::Stack.new(request).stacks.map(&:to_hash)
|
|
end
|
|
|
|
app.get_with_headers '/stacks/provider/:provider', :headers => [:accept] do |provider|
|
|
check_privileges("stack", "r")
|
|
check_provider(provider)
|
|
json Devops::API2_0::Handler::Stack.new(request).stacks_for_provider(provider).map(&:to_hash)
|
|
end
|
|
|
|
app.get_with_headers '/stack/:name/servers', :headers => [:accept] do |name|
|
|
check_privileges("stack", "r")
|
|
json Devops::API2_0::Handler::Stack.new(request).stack_servers(name).map(&:to_hash)
|
|
end
|
|
|
|
# Create stack
|
|
#
|
|
# * *Request*
|
|
# - method : POST
|
|
# - headers :
|
|
# - Accept: application/json
|
|
# - Content-Type: application/json
|
|
# - body :
|
|
# {
|
|
# "stack_attributes": {
|
|
# "project": "project_name",
|
|
# "deploy_env": "test",
|
|
# "provider": "ec2",
|
|
# "tags": {
|
|
# "tagName": "tagValue"
|
|
# },
|
|
# "parameters": {
|
|
# "KeyName": "Value"
|
|
# }
|
|
# }
|
|
# "without_bootstrap": false,
|
|
# "skip_rollback": false
|
|
# }
|
|
#
|
|
# * *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
|
|
#create_response "Created", nil, 201
|
|
end
|
|
|
|
hash = {}
|
|
|
|
hash['GET'] = lambda { |stack_id|
|
|
check_privileges("stack", "r")
|
|
json Devops::API2_0::Handler::Stack.new(request).stack(stack_id).to_hash
|
|
}
|
|
|
|
hash['DELETE'] = lambda { |stack_id|
|
|
check_privileges("stack", "w")
|
|
Devops::API2_0::Handler::Stack.new(request).delete_stack(stack_id)
|
|
create_response("Stack '#{stack_id}' has been removed")
|
|
}
|
|
app.multi_routes '/stack/:stack_id', {:headers => [:accept]}, hash
|
|
|
|
app.post_with_headers "/stack/:stack_id/sync", :headers => [:accept] do |stack_id|
|
|
check_privileges("stack", "w")
|
|
json Devops::API2_0::Handler::Stack.new(request).sync(stack_id).to_hash
|
|
end
|
|
|
|
# Set run_list to stack
|
|
#
|
|
# * *Request*
|
|
# - method : PATCH
|
|
# - headers :
|
|
# - Accept: application/json
|
|
# - Content-Type: application/json
|
|
# - body :
|
|
# [
|
|
# "role[role_1]",
|
|
# "recipe[recipe_1]"
|
|
# ]
|
|
#
|
|
# * *Returns* :
|
|
# 200 - Updated
|
|
app.patch_with_headers "/stack/:id/run_list", :headers => [:accept, :content_type] do |id|
|
|
check_privileges("stack", "w")
|
|
info = Devops::API2_0::Handler::Stack.new(request).set_run_list(id)
|
|
create_response(info)
|
|
end
|
|
|
|
# Deploy stack instances
|
|
#
|
|
# * *Request*
|
|
# - method : POST
|
|
# - headers :
|
|
# - Accept: application/json
|
|
# - Content-Type: application/json
|
|
# - body :
|
|
# {
|
|
# "tags" : ["tag1", "tag2"]
|
|
# }
|
|
#
|
|
# * *Returns* :
|
|
# 200 - Updated
|
|
app.post_with_headers "/stack/:id/deploy", :headers => [:accept, :content_type] do |id|
|
|
check_privileges("stack", "x")
|
|
if request["X-Stream"]
|
|
stream() do |out|
|
|
status = []
|
|
begin
|
|
status = Devops::API2_0::Handler::Stack.new(request).deploy_stream(id, out)
|
|
out << create_status(status)
|
|
rescue IOError => e
|
|
logger.error e.message
|
|
break
|
|
end
|
|
end # stream
|
|
else
|
|
files = Devops::API2_0::Handler::Stack.new(request).deploy(id)
|
|
sleep 1
|
|
json files
|
|
end
|
|
end
|
|
|
|
|
|
# Reserve stack instances
|
|
#
|
|
# * *Request*
|
|
# - method : POST
|
|
# - headers :
|
|
# - Accept: application/json
|
|
# - Content-Type: application/json
|
|
#
|
|
# * *Returns* :
|
|
# 200 - OK
|
|
app.post_with_headers "/stack/:id/reserve", :headers => [:accept, :content_type] do |id|
|
|
check_privileges("stack", "r")
|
|
check_privileges("server", "w")
|
|
Devops::API2_0::Handler::Stack.new(request).reserve_servers(id)
|
|
create_response("Servers of stack '#{id}' has been reserved")
|
|
end
|
|
|
|
# Unreserve stack instances
|
|
#
|
|
# * *Request*
|
|
# - method : POST
|
|
# - headers :
|
|
# - Accept: application/json
|
|
# - Content-Type: application/json
|
|
#
|
|
# * *Returns* :
|
|
# 200 - OK
|
|
app.post_with_headers "/stack/:id/unreserve", :headers => [:accept, :content_type] do |id|
|
|
check_privileges("stack", "r")
|
|
check_privileges("server", "w")
|
|
Devops::API2_0::Handler::Stack.new(request).unreserve_servers(id)
|
|
create_response("Servers of stack '#{id}' has been unreserved")
|
|
end
|
|
|
|
|
|
hash = {}
|
|
# Add tags to stack instances
|
|
#
|
|
# * *Request*
|
|
# - method : PUT
|
|
# - headers :
|
|
# - Content-Type: application/json
|
|
# - body :
|
|
# {
|
|
# "tag name": "tag value"
|
|
# }
|
|
#
|
|
# * *Returns* :
|
|
# 200 - Added
|
|
hash["PUT"] = lambda {|id|
|
|
check_privileges("stack", "w")
|
|
Devops::API2_0::Handler::Stack.new(request).set_tags(id)
|
|
create_response("Added")
|
|
}
|
|
|
|
# Delete tags from stack instances
|
|
#
|
|
# * *Request*
|
|
# - method : DELETE
|
|
# - headers :
|
|
# - Content-Type: application/json
|
|
# - body :
|
|
# {
|
|
# "tag name": "tag value"
|
|
# }
|
|
#
|
|
# * *Returns* :
|
|
# 200 - Deleted
|
|
hash["DELETE"] = lambda {|id|
|
|
check_privileges("stack", "w")
|
|
Devops::API2_0::Handler::Stack.new(request).unset_tags(id)
|
|
create_response("Deleted")
|
|
}
|
|
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
|
|
|
|
end
|
|
end
|
|
end
|
|
end
|