| 
									
										
										
										
											2014-12-15 14:26:54 +03:00
										 |  |  | module Devops | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |   module API3 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |           app.define_policy :read_scripts, "Get scripts list or script details" | 
					
						
							|  |  |  |           app.define_policy :add_scripts, "Add new scripts" | 
					
						
							|  |  |  |           app.define_policy :delete_scripts, "Delete scripts" | 
					
						
							|  |  |  |           app.define_policy :execute_scripts, "Execute scripts" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 14:26:54 +03:00
										 |  |  |           # Get scripts names | 
					
						
							|  |  |  |           # | 
					
						
							|  |  |  |           # * *Request* | 
					
						
							|  |  |  |           #   - method : GET | 
					
						
							|  |  |  |           #   - headers : | 
					
						
							|  |  |  |           #     - Accept: application/json | 
					
						
							|  |  |  |           # | 
					
						
							|  |  |  |           # * *Returns* : | 
					
						
							|  |  |  |           #   [ | 
					
						
							|  |  |  |           #     "script_1" | 
					
						
							|  |  |  |           #   ] | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           app.get_with_headers "/scripts" do | 
					
						
							|  |  |  |             check_policy(:read_scripts) | 
					
						
							|  |  |  |             json Devops::API3::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 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           app.post_with_statistic "/script/command/:server_id" do |server_id| | 
					
						
							|  |  |  |             check_policy(:execute_scripts) | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |             stream() do |out| | 
					
						
							|  |  |  |               begin | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |                 Devops::API3::Handler::Script.new(request).execute_command(out, server_id) | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           app.post_with_headers "/script/run/:script_name" do |script_name| | 
					
						
							|  |  |  |             check_policy(:execute_scripts) | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |             stream() do |out| | 
					
						
							|  |  |  |               begin | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |                 status = Devops::API3::Handler::Script.new(request).run_script out, script_name | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |                 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| | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |             check_policy(:add_scripts) | 
					
						
							|  |  |  |             Devops::API3::Handler::Script.new(request).create_script(script_name) | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |             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| | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |             check_policy(:delete_scripts) | 
					
						
							|  |  |  |             Devops::API3::Handler::Script.new(request).delete_script script_name | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |             create_response("File '#{script_name}' deleted") | 
					
						
							| 
									
										
										
										
											2015-07-23 16:56:51 +03:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           app.multi_routes "/script/:script_name", 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 |