add ability to view project stacks

This commit is contained in:
Anton Chuchkalov 2015-07-22 20:29:42 +04:00
parent 7ff7fd6731
commit 499d05ad32
9 changed files with 77 additions and 8 deletions

View File

@ -49,6 +49,10 @@ class Project < Handler
self.options = @options_parser.servers_options
servers_handler @options_parser.args
output(output_type: :servers)
when "stacks"
self.options = @options_parser.stacks_options
stacks_handler @options_parser.args
output(output_type: :stacks)
when "set"
case ARGV[2]
when "run_list"
@ -162,6 +166,20 @@ class Project < Handler
@servers = get "/project/#{args[2]}/servers", o
end
def stacks_handler args
project, deploy_env = args[2], args[3]
r = inspect_parameters @options_parser.stacks_params, project, deploy_env
unless r.nil?
@options_parser.invalid_stacks_command
abort(r)
end
options = {}
unless deploy_env.nil?
options[:deploy_env] = deploy_env
end
@list = get "/project/#{args[2]}/stacks", options
end
def user_add_handler args
r = inspect_parameters @options_parser.user_add_params, args[3], args[4]
unless r.nil?

View File

@ -3,7 +3,7 @@ require "set"
class ProjectOptions < CommonOptions
commands :create, :delete, :deploy, :list, {:multi => [:create]}, :servers, {:set => [:run_list]}, :show, :test, :update, {:user => [:add, :delete]}
commands :create, :delete, :deploy, :list, {:multi => [:create]}, :servers, :stacks, {:set => [:run_list]}, :show, :test, :update, {:user => [:add, :delete]}
def initialize args, def_options
super(args, def_options)
@ -17,6 +17,7 @@ class ProjectOptions < CommonOptions
self.deploy_params = [id, "[#{env}]"]
self.set_run_list_params = [id, env, "[(recipe[mycookbook::myrecipe])|(role[myrole]) ...]"]
self.servers_params = [id, "[#{env}]"]
self.stacks_params = [id, "[#{env}]"]
self.multi_create_params = [id]
self.update_params = [id, "FILE"]
self.user_add_params = [id, "USER_NAME", "[USER_NAME ...]"]

View File

@ -19,6 +19,11 @@ module Output
title += " " + ARGV[3] unless ARGV[3].nil?
title = I18n.t("output.title.project.servers", :title => title)
create_servers(@data)
when :stacks
title = ARGV[2]
title += " " + ARGV[3] unless ARGV[3].nil?
title = I18n.t("output.title.project.stacks", :title => title)
create_stacks(@data)
when :test
title = I18n.t("output.title.project.test", :project => ARGV[2], :env => ARGV[3])
create_test(@data)
@ -101,7 +106,7 @@ module Output
end
def create_servers servers
abort(I18n.t("output.not_found.project.servers")) if servers.empty?
abort(I18n.t("output.not_found.project.servers", name: ARGV[2])) if servers.empty?
rows = []
servers.each do |s|
rows.push [ s["project"], s["deploy_env"], s["chef_node_name"], s["remote_user"], s["provider"], s["id"] ]
@ -136,5 +141,13 @@ module Output
return headers, rows
end
def create_stacks(stacks)
abort(I18n.t("output.not_found.project.stacks", name: ARGV[2])) if stacks.empty?
fields_to_output = %w(id deploy_env stack_template cloud_stack_id stack_status provider)
headers_and_rows(stacks, fields_to_output)
end
end
end

View File

@ -37,14 +37,14 @@ module Output
def create_list
abort(I18n.t("output.not_found.stack.list")) if @data.empty?
fields_to_output = %w(id deploy_env stack_template cloud_stack_id stack_status)
fields_to_output = %w(id project deploy_env stack_template cloud_stack_id stack_status)
fields_to_output << 'provider' unless provider_given?
headers_and_rows(@data, fields_to_output)
end
def create_show
headers_and_rows([@data], %w(id deploy_env stack_template cloud_stack_id stack_status))
headers_and_rows([@data], %w(id project deploy_env stack_template cloud_stack_id stack_status))
end
def create_servers_list

View File

@ -259,6 +259,7 @@ en:
project:
list: "No project found"
servers: "No servers for project '%{name}' found"
stacks: "No stacks for project '%{name}' found"
provider:
list: "Empty providers list"
script:

View File

@ -10,9 +10,8 @@ module Connectors
self.collection = db.collection('stacks')
end
def stacks(provider=nil)
query = (provider.nil? ? {} : {'provider' => provider})
list(query)
def stacks(options={})
list(options)
end
private

View File

@ -42,6 +42,16 @@ module Devops
}
end
def self.get_project_stacks
lambda {
check_privileges("project", "r")
settings.mongo.project(params[:project])
options = {project: params[:project]}
options[:deploy_env] = params[:deploy_env] if params[:deploy_env]
json settings.mongo.stacks(options).map{|s| s.to_hash}
}
end
# TODO: multi project
def self.create_project
lambda {

View File

@ -17,7 +17,7 @@ module Devops
lambda {
check_privileges("stack", "r")
check_provider(params[:provider])
stacks = settings.mongo.stacks(params[:provider])
stacks = settings.mongo.stacks(provider: params[:provider])
json stacks.map(&:to_hash)
}
end

View File

@ -137,6 +137,33 @@ module Devops
# ]
app.get_with_headers "/project/:project/servers", :headers => [:accept], &Devops::Version2_0::Handler::Project.get_project_servers
# 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"
# },
# "details": null,
# "stack_status": null,
# "id": "openstack_stack"
# ]
app.get_with_headers "/project/:project/stacks", :headers => [:accept], &Devops::Version2_0::Handler::Project.get_project_stacks
# Create project and chef roles
#
# * *Request*