fluke/devops-service/app/api2/routes/script.rb

107 lines
3.2 KiB
Ruby
Raw Normal View History

2014-12-15 14:26:54 +03:00
module Devops
2015-07-27 18:27:52 +03:00
module API2_0
2014-12-22 14:22:04 +03:00
module Routes
2014-12-15 14:26:54 +03:00
module ScriptRoutes
2014-05-08 15:34:26 +04:00
2014-12-15 14:26:54 +03:00
def self.registered(app)
# Get scripts names
#
# * *Request*
# - method : GET
# - headers :
# - Accept: application/json
#
# * *Returns* :
# [
# "script_1"
# ]
2015-07-23 16:56:51 +03:00
app.get_with_headers "/scripts", :headers => [:accept] do
check_privileges("script", "r")
2015-07-30 15:37:43 +03:00
json Devops::API2_0::Handler::Script.new(request).scripts
2015-07-23 16:56:51 +03:00
end
2014-05-08 15:34:26 +04:00
2014-12-15 14:26:54 +03:00
# Run command on node :node_name
#
# * *Request*
# - method : POST
# - body :
# command to run
#
# * *Returns* : text stream
2015-07-23 16:56:51 +03:00
app.post_with_statistic "/script/command/:node_name" do |node_name|
check_privileges("script", "x")
stream() do |out|
begin
2015-07-30 15:37:43 +03:00
Devops::API2_0::Handler::Script.new(request).execute_command(out, node_name)
2015-07-23 16:56:51 +03:00
rescue IOError => e
logger.error e.message
end
end
end
2014-05-08 15:34:26 +04:00
2014-12-15 14:26:54 +03:00
# Run script :script_name on nodes
#
# * *Request*
# - method : POST
# - headers :
# - Content-Type: application/json
# - body :
# {
# "nodes": [], -> array of nodes names
# "params": [] -> array of script arguments
# }
#
# * *Returns* : text stream
2015-07-23 16:56:51 +03:00
app.post_with_headers "/script/run/:script_name", :headers => [:content_type] do |script_name|
check_privileges("script", "x")
stream() do |out|
begin
2015-07-30 15:37:43 +03:00
status = Devops::API2_0::Handler::Script.new(request).run_script out, script_name
out << create_status(status)
2015-07-23 16:56:51 +03:00
rescue IOError => e
logger.error e.message
end
end
end
2014-05-08 15:34:26 +04:00
2015-02-19 11:27:56 +03:00
hash = {}
2014-12-15 14:26:54 +03:00
# Create script :script_name
#
# * *Request*
# - method : PUT
# - headers :
# - Accept: application/json
# - body : script content
#
# * *Returns* :
# 201 - Created
2015-07-30 15:37:43 +03:00
hash["PUT"] = lambda { |script_name|
2015-07-23 16:56:51 +03:00
check_privileges("script", "w")
2015-07-30 15:37:43 +03:00
Devops::API2_0::Handler::Script.new(request).create_script(script_name)
create_response("File '#{script_name}' created", nil, 201)
2015-07-23 16:56:51 +03:00
}
2014-12-15 14:26:54 +03:00
# Delete script :script_name
#
# * *Request*
# - method : Delete
# - headers :
# - Accept: application/json
#
# * *Returns* :
# 200 - Deleted
2015-07-30 15:37:43 +03:00
hash["DELETE"] = lambda { |script_name|
2015-07-23 16:56:51 +03:00
check_privileges("script", "w")
2015-07-30 15:37:43 +03:00
Devops::API2_0::Handler::Script.new(request).delete_script script_name
create_response("File '#{script_name}' deleted")
2015-07-23 16:56:51 +03:00
}
2015-02-19 11:27:56 +03:00
app.multi_routes "/script/:script_name", {:headers => [:accept]}, hash
2014-05-08 15:34:26 +04:00
2014-12-15 14:26:54 +03:00
puts "Script routes initialized"
end
end
2014-05-08 15:34:26 +04:00
end
end
end