626 lines
		
	
	
		
			21 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			626 lines
		
	
	
		
			21 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| module Devops
 | |
|   module API2_0
 | |
|     module Routes
 | |
|       module ProjectRoutes
 | |
| 
 | |
|         def self.registered(app)
 | |
| 
 | |
|           # Get project types
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : GET
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   [
 | |
|           #     "type"
 | |
|           #   ]
 | |
|           app.get_with_headers "/project_types", :headers => [:accept] do
 | |
|             check_privileges("project", "r")
 | |
|             json Devops::API2_0::Handler::Project.new(request).project_types
 | |
|           end
 | |
| 
 | |
|           # Get projects list
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : GET
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #   - params :
 | |
|           #     - fields - show project fields, available values: deploy_envs, type
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   [
 | |
|           #     {"name" : "project_1"}
 | |
|           #   ]
 | |
|           app.get_with_headers "/projects", :headers => [:accept] do
 | |
|             check_privileges("project", "r")
 | |
|             projects = Devops::API2_0::Handler::Project.new(request).projects
 | |
|             json projects.map(&:to_hash)
 | |
|           end
 | |
| 
 | |
|           # Get project by id
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : GET
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   {
 | |
|           #     "deploy_envs": [
 | |
|           #       {
 | |
|           #         "flavor": "flavor",
 | |
|           #         "identifier": "prod",
 | |
|           #         "image": "image id",
 | |
|           #         "run_list": [
 | |
|           #           "role[project_1-prod]"
 | |
|           #         ],
 | |
|           #         "subnets": [
 | |
|           #           "private"
 | |
|           #         ],
 | |
|           #         "expires": null,
 | |
|           #         "provider": "openstack",
 | |
|           #         "groups": [
 | |
|           #           "default"
 | |
|           #         ],
 | |
|           #         "users": [
 | |
|           #           "user"
 | |
|           #         ]
 | |
|           #       }
 | |
|           #     ],
 | |
|           #     "name": "project_1"
 | |
|           #   }
 | |
|           hash = {}
 | |
|           hash["GET"] = lambda {|project|
 | |
|             check_privileges("project", "r")
 | |
|             json Devops::API2_0::Handler::Project.new(request).project(project)
 | |
|           }
 | |
| 
 | |
|           # Update project
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : PUT
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #     - Content-Type: application/json
 | |
|           #   - body :
 | |
|           #     {
 | |
|           #       "run_list": [],
 | |
|           #       "description": "Description",
 | |
|           #       "named_tasks": [{
 | |
|           #         "name": "restart",
 | |
|           #         "run_list": ["role[restart_service]"]
 | |
|           #       }]
 | |
|           #     }
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   200 - Updated
 | |
|           hash["PUT"] = lambda { |project|
 | |
|             check_privileges("project", "w")
 | |
|             r = Devops::API2_0::Handler::Project.new(request).update_project project
 | |
|             create_response("Project '#{project}' has been updated.")
 | |
|           }
 | |
| 
 | |
|           # Delete project
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : DELETE
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #     - Content-Type: application/json
 | |
|           #   - body :
 | |
|           #     {
 | |
|           #       "deploy_env": "env" -> if not null, will be deleted environment only
 | |
|           #     }
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   200 - Deleted
 | |
|           hash["DELETE"] = lambda {|project|
 | |
|             check_privileges("project", "w")
 | |
|             info = Devops::API2_0::Handler::Project.new(request).delete_project(project)
 | |
|             create_response(info)
 | |
|           }
 | |
|           app.multi_routes "/project/:project", {}, hash
 | |
| 
 | |
|           # Get project servers
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : GET
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #   - parameters :
 | |
|           #     - deploy_env=:env -> show servers with environment :env
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   [
 | |
|           #     {
 | |
|           #       "provider": "openstack",
 | |
|           #       "chef_node_name": "project_1_server",
 | |
|           #       "remote_user": "root",
 | |
|           #       "project": "project_1",
 | |
|           #       "deploy_env": "prod",
 | |
|           #       "private_ip": "10.8.8.8",
 | |
|           #       "public_ip": null,
 | |
|           #       "created_at": "2014-04-23 13:35:18 UTC",
 | |
|           #       "created_by": "user",
 | |
|           #       "static": false,
 | |
|           #       "key": "ssh key",
 | |
|           #       "id": "nstance id"
 | |
|           #     }
 | |
|           #   ]
 | |
|           servers_routes_hash = {}
 | |
|           servers_routes_hash["GET"] = lambda { |project|
 | |
|             check_privileges("project", "r")
 | |
|             json Devops::API2_0::Handler::Project.new(request).project_servers(project).map(&:to_hash)
 | |
|           }
 | |
| 
 | |
|           # Deletes project servers
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : DELETE
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #     - Content-Type: application/json
 | |
|           #   - body :
 | |
|           #     {
 | |
|           #       "dry_run": false # set to true if you'd like to just see list of servers to delete
 | |
|           #       "deploy_env": null # set to env's identifier to delete that env's servers
 | |
|           #     }
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   200 -
 | |
|           # {
 | |
|           #   "to_delete": ['server1', 'server2'],
 | |
|           #   "deleted": ['server1'],
 | |
|           #   "failed": ['server2']
 | |
|           # }
 | |
|           servers_routes_hash["DELETE"] = lambda { |project_id|
 | |
|             check_privileges("project", "w")
 | |
|             json Devops::API2_0::Handler::Project.new(request).delete_project_servers(project_id)
 | |
|           }
 | |
|           app.multi_routes "/project/:project/servers", {}, servers_routes_hash
 | |
| 
 | |
|           # Get project stacks
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : GET
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #   - parameters :
 | |
|           #     - deploy_env=:env -> show stacks with environment :env
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   [
 | |
|           #     "provider": "openstack",
 | |
|           #     "project": "test_openstack",
 | |
|           #     "deploy_env": "test",
 | |
|           #     "stack_template": "openstack_template",
 | |
|           #     "cloud_stack_id": "4c712026-dcd5-4664-90b8-0915494c1332",
 | |
|           #     "parameters": {
 | |
|           #       "admin_pass": "Pass12",
 | |
|           #       "key_name": "devops",
 | |
|           #       "image": "CirrOS_0.3.1"
 | |
|           #     },
 | |
|           #     "stack_status": null,
 | |
|           #     "id": "openstack_stack"
 | |
|           #   ]
 | |
|           app.get_with_headers "/project/:project/stacks", :headers => [:accept] do |project|
 | |
|             check_privileges("project", "r")
 | |
|             json Devops::API2_0::Handler::Project.new(request).project_stacks(project).map(&:to_hash)
 | |
|           end
 | |
| 
 | |
|           # Get project deploy environments
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : GET
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           app.get_with_headers "/project/:project/deploy_envs", :headers => [:accept] do |project|
 | |
|             check_privileges("project", "r")
 | |
|             json Devops::API2_0::Handler::Project.new(request).project_deploy_envs(project)
 | |
|           end
 | |
| 
 | |
|           # Add project deploy environment
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : POST
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #   - body :
 | |
|           #     {
 | |
|           #       "identifier": "prod",
 | |
|           #       "provider": "openstack",
 | |
|           #       "flavor": "m1.small",
 | |
|           #       "image": "image id",
 | |
|           #       "subnets": [
 | |
|           #         "private"
 | |
|           #       ],
 | |
|           #       "groups": [
 | |
|           #         "default"
 | |
|           #       ],
 | |
|           #       "users": [
 | |
|           #         "user"
 | |
|           #       ],
 | |
|           #       "run_list": [ ],
 | |
|           #       "expires": null
 | |
|           #     }
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   200 - Added
 | |
|           app.post_with_headers "/project/:project/deploy_env", :headers => [:accept, :content_type] do |project|
 | |
|             check_privileges("project", "w")
 | |
|             res, env = Devops::API2_0::Handler::Project.new(request).add_deploy_env(project)
 | |
|             create_response(res, {environment: env.to_hash}, 200)
 | |
|           end
 | |
| 
 | |
|           deploy_env_hash = {}
 | |
|           # Get project deploy environment
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : GET
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           deploy_env_hash["GET"] = lambda{|project, env|
 | |
|             check_privileges("project", "r")
 | |
|             json Devops::API2_0::Handler::Project.new(request).project_deploy_env(project, env)
 | |
|           }
 | |
| 
 | |
|           # Update deploy_env, you can send few fields in the body
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : PUT
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #     - Content-Type: application/json
 | |
|           #   - body :
 | |
|           #     {
 | |
|           #       "identifier": "prod",
 | |
|           #       "provider": "openstack",
 | |
|           #       "flavor": "m1.small",
 | |
|           #       "image": "image id",
 | |
|           #       "subnets": [
 | |
|           #         "private"
 | |
|           #       ],
 | |
|           #       "groups": [
 | |
|           #         "default"
 | |
|           #       ],
 | |
|           #       "users": [
 | |
|           #         "user"
 | |
|           #       ],
 | |
|           #       "run_list": [ ],
 | |
|           #       "expires": null
 | |
|           #     }
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   200 - Updated
 | |
|           deploy_env_hash["PUT"] = lambda{|id, deploy_env|
 | |
|             check_privileges("project", "w")
 | |
|             begin
 | |
|               res = Devops::API2_0::Handler::Project.new(request).update_deploy_env(id, deploy_env)
 | |
|               create_response(res, nil, 200)
 | |
|             rescue InvalidRecord => e
 | |
|               halt_response(e.message)
 | |
|             end
 | |
|           }
 | |
| 
 | |
|           # Delete deploy_env
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : DELETE
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   200 - Deleted
 | |
|           deploy_env_hash["DELETE"] = lambda{|id, deploy_env|
 | |
|             check_privileges("project", "w")
 | |
|             begin
 | |
|               res = Devops::API2_0::Handler::Project.new(request).delete_deploy_env(id, deploy_env)
 | |
|               create_response(res, nil, 200)
 | |
|             rescue InvalidRecord => e
 | |
|               halt_response(e.message)
 | |
|             end
 | |
|           }
 | |
|           app.multi_routes "/project/:id/deploy_envs/:deploy_env", {}, deploy_env_hash
 | |
| 
 | |
|           app.put_with_headers "/project/:id/deploy_env/:deploy_env/:field", :headers => [:accept, :content_type] do |project_id, deploy_env, field|
 | |
|             check_privileges("project", "w")
 | |
|             res = Devops::API2_0::Handler::Project.new(request).update_deploy_env_field(project_id, deploy_env, field)
 | |
|             create_response(res, nil, 200)
 | |
|           end
 | |
| 
 | |
|           # Create project
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : POST
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #     - Content-Type: application/json
 | |
|           #   - body :
 | |
|           #     {
 | |
|           #       "deploy_envs": [
 | |
|           #         {
 | |
|           #           "identifier": "prod",
 | |
|           #           "provider": "openstack",
 | |
|           #           "flavor": "m1.small",
 | |
|           #           "image": "image id",
 | |
|           #           "subnets": [
 | |
|           #             "private"
 | |
|           #           ],
 | |
|           #           "groups": [
 | |
|           #             "default"
 | |
|           #           ],
 | |
|           #           "users": [
 | |
|           #             "user"
 | |
|           #           ],
 | |
|           #           "run_list": [
 | |
|           #
 | |
|           #           ],
 | |
|           #           "expires": null
 | |
|           #         }
 | |
|           #       ],
 | |
|           #       "name": "project_1"
 | |
|           #     }
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   201 - Created
 | |
|           app.post_with_headers "/project", :headers => [:accept, :content_type] do
 | |
|             check_privileges("project", "w")
 | |
|             res = Devops::API2_0::Handler::Project.new(request).create_project
 | |
|             res = "Created. " + res
 | |
|             create_response(res, nil, 201)
 | |
|           end
 | |
| 
 | |
| =begin
 | |
|           # Set components to project
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : POST
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #     - Content-Type: application/json
 | |
|           #   - body :
 | |
|           #     components: {
 | |
|           #       "some_id": {
 | |
|           #         "filename" : "some.war"
 | |
|           #       }
 | |
|           #     }
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   200 - Updated
 | |
|           app.post_with_headers "/project/:id/components", :headers => [:accept, :content_type] do |id|
 | |
|             check_privileges("project", "w")
 | |
|             res = Devops::API2_0::Handler::Project.new(request).set_project_components(id)
 | |
|             create_response(res)
 | |
|           end
 | |
| =end
 | |
| 
 | |
|           users_hash = {}
 | |
|           # Add users to project environment
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : PUT
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #     - Content-Type: application/json
 | |
|           #   - body :
 | |
|           #     {
 | |
|           #       "users": [
 | |
|           #         "user1"
 | |
|           #       ],
 | |
|           #       "deploy_env": "env" -> if null, users will be added to all environments
 | |
|           #     }
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   200 - Updated
 | |
|           users_hash["PUT"] = lambda { |project|
 | |
|             check_privileges("project", "w")
 | |
|             info = Devops::API2_0::Handler::Project.new(request).update_project_users(project)
 | |
|             create_response(info)
 | |
|           }
 | |
| 
 | |
|           # Delete users from project environment
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : DELETE
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #     - Content-Type: application/json
 | |
|           #   - body :
 | |
|           #     {
 | |
|           #       "users": [
 | |
|           #         "user1"
 | |
|           #       ],
 | |
|           #       "deploy_env": "env" -> if null, users will be deleted from all environments
 | |
|           #     }
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   200 - Updated
 | |
|           users_hash["DELETE"] = lambda {|project|
 | |
|             check_privileges("project", "w")
 | |
|             info = Devops::API2_0::Handler::Project.new(request).delete_project_users(project)
 | |
|             create_response(info)
 | |
|           }
 | |
|           app.multi_routes "/project/:id/user", {}, users_hash
 | |
| 
 | |
|           # Set run_list to project
 | |
|           #
 | |
|           # * *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 "/project/:id/run_list", :headers => [:accept, :content_type] do |project|
 | |
|             check_privileges("project", "w")
 | |
|             info = Devops::API2_0::Handler::Project.new(request).set_project_run_list(project)
 | |
|             create_response(info)
 | |
|           end
 | |
| 
 | |
|           # Set run_list to project environment
 | |
|           #
 | |
|           # * *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 "/project/:id/:env/run_list", :headers => [:accept, :content_type] do |project, deploy_env|
 | |
|             check_privileges("project", "w")
 | |
|             info = Devops::API2_0::Handler::Project.new(request).set_project_env_run_list(project, deploy_env)
 | |
|             create_response(info)
 | |
|           end
 | |
| 
 | |
|           # Run chef-client on reserved project servers
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : POST
 | |
|           #   - headers :
 | |
|           #     - X-Stream: true -> return output in text stream
 | |
|           #     - Content-Type: application/json
 | |
|           #   - body :
 | |
|           #     {
 | |
|           #       "servers": [
 | |
|           #         "server_1"
 | |
|           #       ], -> deploy servers from list, all servers if null
 | |
|           #       "deploy_env": "env" -> deploy servers with environment 'env' or all project servers if null
 | |
|           #     }
 | |
|           #
 | |
|           # * *Returns* : text stream
 | |
|           app.post_with_headers "/project/:id/deploy", :headers => [:content_type] do |project|
 | |
|             check_privileges("project", "x")
 | |
|             handler = Devops::API2_0::Handler::Project.new(request)
 | |
|             if request["X-Stream"]
 | |
|               stream() do |out|
 | |
|                 begin
 | |
|                   status = handler.deploy_project_stream out, project
 | |
|                   out << create_status(status)
 | |
|                 rescue IOError => e
 | |
|                   logger.error e.message
 | |
|                 end
 | |
|               end
 | |
|             else
 | |
|               json handler.deploy_project project
 | |
|             end
 | |
|           end
 | |
| 
 | |
|           # Set project to archivated state
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : POST
 | |
|           #   - headers :
 | |
|           #     - Content-Type: application/json
 | |
|           app.post_with_headers "/project/:project/archive", :headers => [:content_type] do |project|
 | |
|             check_privileges("project", "w")
 | |
|             info = Devops::API2_0::Handler::Project.new(request).archive_project(project)
 | |
|             create_response(info)
 | |
|           end
 | |
| 
 | |
|           # Set project to normal state
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : POST
 | |
|           #   - headers :
 | |
|           #     - Content-Type: application/json
 | |
|           app.post_with_headers "/project/:project/unarchive", :headers => [:content_type] do |project|
 | |
|             check_privileges("project", "w")
 | |
|             info = Devops::API2_0::Handler::Project.new(request).unarchive_project(project)
 | |
|             create_response(info)
 | |
|           end
 | |
| 
 | |
|           # Test project environment
 | |
|           #
 | |
|           # Run tests:
 | |
|           #   - run server
 | |
|           #   - bootstrap server
 | |
|           #   - delete server
 | |
|           #
 | |
|           # * *Request*
 | |
|           #   - method : POST
 | |
|           #   - headers :
 | |
|           #     - Accept: application/json
 | |
|           #     - Content-Type: application/json
 | |
|           #
 | |
|           # * *Returns* :
 | |
|           #   200 -
 | |
|           #   {
 | |
|           #     "servers": [
 | |
|           #       {
 | |
|           #         "id": "132958f0-61c5-4665-8cc3-66e1bacd285b",
 | |
|           #         "create": {
 | |
|           #           "status": true,
 | |
|           #           "time": "155s"
 | |
|           #         },
 | |
|           #         "chef_node_name": "chef name",
 | |
|           #         "bootstrap": {
 | |
|           #           "status": true,
 | |
|           #           "log": "\nWaiting for SSH...\n"
 | |
|           #           "return_code": 0
 | |
|           #         },
 | |
|           #         "delete": {
 | |
|           #           "status": true,
 | |
|           #           "time": "2s"
 | |
|           #           "log": {
 | |
|           #             "chef_node": "Deleted node[chef name]",
 | |
|           #             "chef_client": "Deleted client[chef name]",
 | |
|           #             "server": "Server with id '132958f0-61c5-4665-8cc3-66e1bacd285b' terminated"
 | |
|           #           }
 | |
|           #         },
 | |
|           #       }
 | |
|           #     ],
 | |
|           #     "project": {
 | |
|           #       "deploy_envs": [
 | |
|           #         {
 | |
|           #           "flavor": "flavor",
 | |
|           #           "identifier": "prod",
 | |
|           #           "image": "image id",
 | |
|           #           "run_list": [
 | |
|           #             "role[prod]"
 | |
|           #           ],
 | |
|           #           "subnets": [
 | |
|           #             "private"
 | |
|           #           ],
 | |
|           #           "expires": null,
 | |
|           #           "provider": "openstack",
 | |
|           #           "groups": [
 | |
|           #             "default"
 | |
|           #           ],
 | |
|           #           "users": [
 | |
|           #             "root"
 | |
|           #           ]
 | |
|           #         }
 | |
|           #       ],
 | |
|           #       "name": "prject_1"
 | |
|           #     },
 | |
|           #     "message": "Test project 'project_1' and environment 'prod'"
 | |
|           #   }
 | |
|           app.post_with_headers "/project/test/:id/:env", :headers => [:accept, :content_type] do |project, deploy_env|
 | |
|             check_privileges("project", "r")
 | |
|             json Devops::API2_0::Handler::Project.new(request).test_project(project, deploy_env)
 | |
|           end
 | |
| 
 | |
|           puts "Project routes initialized"
 | |
|         end
 | |
| 
 | |
|       end
 | |
|     end
 | |
|   end
 | |
| end
 | |
| 
 | 
