without base routes
This commit is contained in:
parent
d1a555d97e
commit
bc06d015ed
@ -17,7 +17,7 @@ module DeployEnvCommands
|
|||||||
end
|
end
|
||||||
|
|
||||||
def check_image! p, val
|
def check_image! p, val
|
||||||
images = get_images(DevopsService.mongo, p.name)
|
images = get_images(::Devops::Db.connector, p.name)
|
||||||
raise InvalidRecord.new "Invalid image '#{val}'" unless images.map{|i| i["id"]}.include?(val)
|
raise InvalidRecord.new "Invalid image '#{val}'" unless images.map{|i| i["id"]}.include?(val)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ module DeployEnvCommands
|
|||||||
end
|
end
|
||||||
|
|
||||||
def check_users! val
|
def check_users! val
|
||||||
users = DevopsService.mongo.users_names(val)
|
users = ::Devops::Db.connector.users_names(val)
|
||||||
buf = val - users
|
buf = val - users
|
||||||
raise InvalidRecord.new("Invalid users: '#{buf.join("', '")}'") unless buf.empty?
|
raise InvalidRecord.new("Invalid users: '#{buf.join("', '")}'") unless buf.empty?
|
||||||
end
|
end
|
||||||
|
|||||||
@ -278,7 +278,7 @@ module ServerCommands
|
|||||||
def delete_server s, mongo, logger
|
def delete_server s, mongo, logger
|
||||||
if s.static?
|
if s.static?
|
||||||
if !s.chef_node_name.nil?
|
if !s.chef_node_name.nil?
|
||||||
cert = BaseRoutes.mongo.key s.key
|
cert = ::Devops::Db.connector.key s.key
|
||||||
ServerCommands.unbootstrap(s, cert.path)
|
ServerCommands.unbootstrap(s, cert.path)
|
||||||
end
|
end
|
||||||
mongo.server_delete s.id
|
mongo.server_delete s.id
|
||||||
|
|||||||
@ -1,105 +0,0 @@
|
|||||||
require "json"
|
|
||||||
require "db/exceptions/record_not_found"
|
|
||||||
require "db/exceptions/invalid_record"
|
|
||||||
require "exceptions/dependency_error"
|
|
||||||
require "exceptions/invalid_privileges"
|
|
||||||
require "fog"
|
|
||||||
require "logger"
|
|
||||||
require "providers/provider_factory"
|
|
||||||
require "sinatra/json"
|
|
||||||
require "sinatra/base"
|
|
||||||
|
|
||||||
module Version2_0
|
|
||||||
# Basic class for devops routes classes
|
|
||||||
class BaseRoutes < Sinatra::Base
|
|
||||||
|
|
||||||
helpers do
|
|
||||||
def create_response msg, obj=nil, rstatus=200
|
|
||||||
logger.info(msg)
|
|
||||||
status rstatus
|
|
||||||
obj = {} if obj.nil?
|
|
||||||
obj[:message] = msg
|
|
||||||
json(obj)
|
|
||||||
end
|
|
||||||
|
|
||||||
def halt_response msg, rstatus=400
|
|
||||||
obj = {:message => msg}
|
|
||||||
halt(rstatus, json(obj))
|
|
||||||
end
|
|
||||||
|
|
||||||
def check_privileges cmd, p
|
|
||||||
BaseRoutes.mongo.check_user_privileges(request.env['REMOTE_USER'], cmd, p)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def check_provider provider
|
|
||||||
list = ::Provider::ProviderFactory.providers
|
|
||||||
halt_response("Invalid provider '#{provider}', available providers: '#{list.join("', '")}'", 404) unless list.include?(provider)
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_object_from_json_body type=Hash, empty_body=false
|
|
||||||
json = request.body.read.strip
|
|
||||||
return nil if json.empty? and empty_body
|
|
||||||
@body_json = begin
|
|
||||||
JSON.parse(json)
|
|
||||||
rescue => e
|
|
||||||
logger.error e.message
|
|
||||||
logger.debug(json)
|
|
||||||
halt_response("Invalid JSON")
|
|
||||||
end
|
|
||||||
halt_response("Invalid JSON, it should be an #{type == Array ? "array" : "object"}") unless @body_json.is_a?(type)
|
|
||||||
@body_json
|
|
||||||
end
|
|
||||||
|
|
||||||
def check_string val, msg, _nil=false, empty=false
|
|
||||||
check_param val, String, msg, _nil, empty
|
|
||||||
end
|
|
||||||
|
|
||||||
def check_array val, msg, vals_type=String, _nil=false, empty=false
|
|
||||||
check_param val, Array, msg, _nil, empty
|
|
||||||
val.each {|v| halt_response(msg) unless v.is_a?(vals_type)} unless val.nil?
|
|
||||||
val
|
|
||||||
end
|
|
||||||
|
|
||||||
def check_filename file_name, not_string_msg, json_resp=true
|
|
||||||
check_string file_name, not_string_msg
|
|
||||||
r = Regexp.new("^[\\w _\\-.]{1,255}$", Regexp::IGNORECASE)
|
|
||||||
if r.match(file_name).nil?
|
|
||||||
msg = "Invalid file name '#{file_name}'. Expected name with 'a'-'z', '0'-'9', ' ', '_', '-', '.' symbols with length greate then 0 and less then 256 "
|
|
||||||
if json_resp
|
|
||||||
halt_response(msg)
|
|
||||||
else
|
|
||||||
halt(400, msg)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
file_name
|
|
||||||
end
|
|
||||||
|
|
||||||
def check_param val, type, msg, _nil=false, empty=false
|
|
||||||
if val.nil?
|
|
||||||
if _nil
|
|
||||||
return val
|
|
||||||
else
|
|
||||||
halt_response(msg)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if val.is_a?(type)
|
|
||||||
halt_response(msg) if val.empty? and !empty
|
|
||||||
val
|
|
||||||
else
|
|
||||||
halt_response(msg)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Save information about requests with methods POST, PUT, DELETE
|
|
||||||
def statistic msg=nil
|
|
||||||
unless request.get?
|
|
||||||
BaseRoutes.mongo.statistic request.env['REMOTE_USER'], request.path, request.request_method, @body_json, response.status
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -18,9 +18,9 @@ module Devops
|
|||||||
def self.execute_command
|
def self.execute_command
|
||||||
lambda {
|
lambda {
|
||||||
user = request.env['REMOTE_USER']
|
user = request.env['REMOTE_USER']
|
||||||
s = BaseRoutes.mongo.server_by_chef_node_name params[:node_name]
|
s = ::Devops::Db.connector.server_by_chef_node_name params[:node_name]
|
||||||
BaseRoutes.mongo.check_project_auth s.project, s.deploy_env, user
|
::Devops::Db.connector.check_project_auth s.project, s.deploy_env, user
|
||||||
cert = BaseRoutes.mongo.key s.key
|
cert = ::Devops::Db.connector.key s.key
|
||||||
cmd = request.body.read
|
cmd = request.body.read
|
||||||
addr = "#{s.remote_user}@#{s.public_ip || s.private_ip}"
|
addr = "#{s.remote_user}@#{s.public_ip || s.private_ip}"
|
||||||
ssh_cmd = "ssh -i %s #{addr} '#{cmd}'"
|
ssh_cmd = "ssh -i %s #{addr} '#{cmd}'"
|
||||||
@ -49,18 +49,18 @@ module Devops
|
|||||||
body = create_object_from_json_body
|
body = create_object_from_json_body
|
||||||
nodes = check_array(body["nodes"], "Parameter 'nodes' must be a not empty array of strings")
|
nodes = check_array(body["nodes"], "Parameter 'nodes' must be a not empty array of strings")
|
||||||
p = check_array(body["params"], "Parameter 'params' should be a not empty array of strings", String, true)
|
p = check_array(body["params"], "Parameter 'params' should be a not empty array of strings", String, true)
|
||||||
servers = BaseRoutes.mongo.servers_by_names(nodes)
|
servers = ::Devops::Db.connector.servers_by_names(nodes)
|
||||||
return [404, "No servers found for names '#{nodes.join("', '")}'"] if servers.empty?
|
return [404, "No servers found for names '#{nodes.join("', '")}'"] if servers.empty?
|
||||||
user = request.env['REMOTE_USER']
|
user = request.env['REMOTE_USER']
|
||||||
servers.each do |s|
|
servers.each do |s|
|
||||||
BaseRoutes.mongo.check_project_auth s.project, s.deploy_env, user
|
::Devops::Db.connector.check_project_auth s.project, s.deploy_env, user
|
||||||
end
|
end
|
||||||
stream() do |out|
|
stream() do |out|
|
||||||
begin
|
begin
|
||||||
status = []
|
status = []
|
||||||
servers.each do |s|
|
servers.each do |s|
|
||||||
cert = begin
|
cert = begin
|
||||||
BaseRoutes.mongo.key s.key
|
::Devops::Db.connector.key s.key
|
||||||
rescue
|
rescue
|
||||||
out << "No key found for '#{s.chef_node_name}'"
|
out << "No key found for '#{s.chef_node_name}'"
|
||||||
status.push 2
|
status.push 2
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user