fluke/devops-service/routes/v2.0/deploy.rb

91 lines
2.8 KiB
Ruby
Raw Normal View History

2014-05-08 15:34:26 +04:00
require "commands/knife_commands"
require "routes/v2.0/base_routes"
require "providers/provider_factory"
require "commands/deploy"
require "commands/status"
2014-10-22 15:01:55 +04:00
require "workers/deploy_worker"
2014-05-08 15:34:26 +04:00
module Version2_0
class DeployRoutes < BaseRoutes
include DeployCommands
include StatusCommands
def initialize wrapper
super wrapper
puts "Deploy routes initialized"
end
after "/deploy" do
statistic
end
2014-05-23 17:36:16 +04:00
# Run chef-client on reserved server
2014-05-08 15:34:26 +04:00
#
# * *Request*
# - method : POST
# - headers :
# - Content-Type: application/json
# - body :
# {
2014-10-22 15:01:55 +04:00
# "names": [], -> array of servers names to run chef-client
# "tags": [], -> array of tags to apply on each server before running chef-client
# "trace": true -> return output in stream
2014-05-08 15:34:26 +04:00
# }
#
# * *Returns* : text stream
post "/deploy" do
check_headers :content_type
check_privileges("server", "x")
2014-05-08 15:34:26 +04:00
r = create_object_from_json_body
names = check_array(r["names"], "Parameter 'names' should be a not empty array of strings")
tags = check_array(r["tags"], "Parameter 'tags' should be an array of strings", String, true) || []
2014-10-22 15:01:55 +04:00
servers = BaseRoutes.mongo.servers(nil, nil, names, true)
2014-05-23 17:36:16 +04:00
halt(404, "No reserved servers found for names '#{names.join("', '")}'") if servers.empty?
2014-05-08 15:34:26 +04:00
keys = {}
servers.sort_by!{|s| names.index(s.chef_node_name)}
2014-10-22 15:01:55 +04:00
if r.key?("trace")
stream() do |out|
status = []
2014-05-08 15:34:26 +04:00
begin
2014-10-22 15:01:55 +04:00
servers.each do |s|
project = begin
BaseRoutes.mongo.check_project_auth s.project, s.deploy_env, request.env['REMOTE_USER']
rescue InvalidPrivileges, RecordNotFound => e
out << e.message + "\n"
status.push 2
2014-05-08 15:34:26 +04:00
next
end
2014-10-22 15:01:55 +04:00
res = deploy_server_proc.call(out, s, BaseRoutes.mongo, tags)
status.push(res)
2014-05-08 15:34:26 +04:00
end
out << create_status(status)
rescue IOError => e
logger.error e.message
break
end
2014-10-22 15:01:55 +04:00
end # stream
else
2014-11-17 14:23:59 +03:00
dir = DevopsService.config[:report_dir_v2]
2014-10-22 15:01:55 +04:00
files = []
uri = URI.parse(request.url)
servers.each do |s|
project = begin
BaseRoutes.mongo.check_project_auth s.project, s.deploy_env, request.env['REMOTE_USER']
rescue InvalidPrivileges, RecordNotFound => e
next
end
2014-11-17 14:23:59 +03:00
jid = DeployWorker.perform_async(dir, s.to_hash, tags, request.env['REMOTE_USER'], DevopsService.config)
2014-10-22 15:01:55 +04:00
logger.info "Job '#{jid}' has been started"
2014-11-17 14:23:59 +03:00
uri.path = "#{DevopsService.config[:url_prefix]}/v2.0/report/" + jid
2014-10-22 15:01:55 +04:00
files.push uri.to_s
2014-05-08 15:34:26 +04:00
end
2014-11-17 14:23:59 +03:00
sleep 1
2014-10-22 15:01:55 +04:00
json files
2014-05-08 15:34:26 +04:00
end
end
end
end