fluke/devops-service/db/mongo/models/stack/stack_base.rb

165 lines
5.6 KiB
Ruby
Raw Normal View History

2015-08-04 12:36:10 +03:00
require "db/mongo/models/model_with_provider"
2015-08-28 17:00:48 +03:00
require "providers/exceptions/name_conflict"
2015-03-06 12:20:30 +03:00
module Devops
module Model
class StackBase < MongoModel
2015-08-04 12:36:10 +03:00
include ModelWithProvider
2015-11-17 13:23:13 +03:00
attr_accessor :id, :name, :project, :deploy_env, :stack_template, :parameters, :details, :events, :owner, :run_list
2015-03-06 12:20:30 +03:00
types id: {type: String, empty: false},
provider: {type: String, empty: false},
project: {type: String},
deploy_env: {type: String},
2015-07-14 16:51:40 +03:00
stack_template: {type: String, empty: false},
2015-08-12 13:32:26 +03:00
name: {type: String, nil: true},
2015-08-04 12:36:10 +03:00
owner: {type: String},
run_list: {type: Array, value_type: String, empty: true, nil: true}
# details: {type: Hash, nil: true} # Hash type isn't supported yet
2015-03-06 12:20:30 +03:00
2015-12-04 12:43:07 +03:00
set_field_validators :id, [::Validators::FieldValidator::NotNil,
2015-11-03 11:46:54 +03:00
::Validators::FieldValidator::FieldType::String,
2015-12-04 12:43:07 +03:00
::Validators::FieldValidator::NotEmpty]
2015-11-03 11:46:54 +03:00
2015-12-04 12:43:07 +03:00
set_field_validators :provider, [::Validators::FieldValidator::NotNil,
2015-11-03 11:46:54 +03:00
::Validators::FieldValidator::FieldType::String,
2015-12-04 12:43:07 +03:00
::Validators::FieldValidator::NotEmpty]
2015-11-03 11:46:54 +03:00
2015-12-04 12:43:07 +03:00
set_field_validators :project, [::Validators::FieldValidator::NotNil,
2015-11-03 11:46:54 +03:00
::Validators::FieldValidator::FieldType::String,
2015-12-04 12:43:07 +03:00
::Validators::FieldValidator::NotEmpty]
2015-11-03 11:46:54 +03:00
2015-12-04 12:43:07 +03:00
set_field_validators :deploy_env, [::Validators::FieldValidator::NotNil,
2015-11-03 11:46:54 +03:00
::Validators::FieldValidator::FieldType::String,
2015-12-04 12:43:07 +03:00
::Validators::FieldValidator::NotEmpty]
2015-11-03 11:46:54 +03:00
2015-12-04 12:43:07 +03:00
set_field_validators :stack_template, [::Validators::FieldValidator::NotNil,
2015-11-03 11:46:54 +03:00
::Validators::FieldValidator::FieldType::String,
2015-12-04 12:43:07 +03:00
::Validators::FieldValidator::NotEmpty]
2015-11-03 11:46:54 +03:00
2015-12-04 12:43:07 +03:00
set_field_validators :name, [::Validators::FieldValidator::Nil,
2015-11-03 11:46:54 +03:00
::Validators::FieldValidator::FieldType::String,
2015-12-04 12:43:07 +03:00
::Validators::FieldValidator::NotEmpty]
2015-11-03 11:46:54 +03:00
2015-12-04 12:43:07 +03:00
set_field_validators :owner, [::Validators::FieldValidator::NotNil,
2015-11-03 11:46:54 +03:00
::Validators::FieldValidator::FieldType::String,
2015-12-04 12:43:07 +03:00
::Validators::FieldValidator::NotEmpty]
2015-11-03 11:46:54 +03:00
2015-12-04 12:43:07 +03:00
set_field_validators :run_list, [::Validators::FieldValidator::NotNil,
2015-11-03 11:46:54 +03:00
::Validators::FieldValidator::FieldType::Array,
2015-12-04 12:43:07 +03:00
::Validators::FieldValidator::RunList]
2015-11-03 11:46:54 +03:00
2015-03-06 12:20:30 +03:00
def initialize attrs={}
# self.provider = self.class.provider
self.set_provider(attrs)
2015-03-06 12:20:30 +03:00
self.id = attrs['id']
self.project = attrs['project']
self.deploy_env = attrs['deploy_env']
self.stack_template = attrs['stack_template']
2015-08-12 13:32:26 +03:00
self.name = attrs['name']
self.parameters = attrs['parameters']
2015-07-14 16:51:40 +03:00
self.details = attrs['details']
2015-07-30 02:14:45 +03:00
self.owner = attrs['owner']
self.run_list = attrs['run_list'] || []
2015-03-06 12:20:30 +03:00
self
end
2015-11-03 11:46:54 +03:00
# TODO: use string keys
2015-03-06 12:20:30 +03:00
def to_hash_without_id
{
project: project,
deploy_env: deploy_env,
stack_template: stack_template,
2015-08-12 13:32:26 +03:00
name: name,
2015-07-14 16:51:40 +03:00
parameters: parameters,
# details are required to proper status handling
2015-08-28 17:00:48 +03:00
details: bson_safe_details,
2015-07-30 02:14:45 +03:00
stack_status: stack_status,
2015-11-03 11:46:54 +03:00
owner: owner,
run_list: run_list
}.merge(provider_hash)
2015-03-06 12:20:30 +03:00
end
2015-08-28 17:00:48 +03:00
# overrided in ec2
def bson_safe_details
details
end
def create_stack_in_cloud! out
2015-07-14 16:51:40 +03:00
begin
provider_instance.create_stack(self, out)
2015-07-14 16:51:40 +03:00
rescue ProviderErrors::NameConflict
raise InvalidRecord.new "Duplicate key error: stack with name '#{id}' already exists in cloud"
end
end
2015-07-14 16:51:40 +03:00
def delete_stack_in_cloud!
2015-08-04 12:36:10 +03:00
provider_instance.delete_stack(self)
2015-03-06 12:20:30 +03:00
end
2015-07-14 16:51:40 +03:00
def sync_details!
2015-08-28 17:00:48 +03:00
self.details = provider_instance.stack_details(self)
2015-11-17 13:23:13 +03:00
self.events = provider_instance.stack_events(self)
2015-07-15 18:37:27 +03:00
end
def resources
2015-08-04 12:36:10 +03:00
provider_instance.stack_resources(self)
2015-03-06 12:20:30 +03:00
end
2015-07-23 12:54:36 +03:00
# resource_id is logical
def resource(resource_id)
2015-08-04 12:36:10 +03:00
provider_instance.stack_resource(self, resource_id)
end
2015-08-28 14:55:33 +03:00
# there is only one way to update stack status right now:
# stack.sync_details!
# mongo.stack_update(stack)
# It's because syncing updates @details instance variable, but doesn'not persist it.
# Also that's why we need to have "details" key in #to_mongo_hash
2015-07-20 18:59:26 +03:00
def stack_status
raise 'override me'
end
def stack_statuses
# maybe they differ in different providers, so use method instead of hardcoding
{
in_progress: 'CREATE_IN_PROGRESS',
complete: 'CREATE_COMPLETE'
}
end
2015-07-30 02:14:45 +03:00
def template_body
2015-12-15 18:56:14 +03:00
stack_template_model.template_body
end
def stack_template_model
Devops::Db.connector.stack_template(stack_template)
2015-07-30 02:14:45 +03:00
end
2015-07-14 16:51:40 +03:00
class << self
# attrs should include:
# - id (String)
# - deploy_env (String)
# - stack_template (String)
# - provider (String)
# - provider_account (String)
def create(attrs, out)
2015-07-14 16:51:40 +03:00
model = new(attrs)
model.create_stack_in_cloud!(out)
2015-07-20 18:59:26 +03:00
model.sync_details!
2015-07-14 16:51:40 +03:00
model
end
def build_from_bson(attrs)
attrs['id'] = attrs["_id"]
self.new(attrs)
end
end
2015-03-06 12:20:30 +03:00
end
2015-02-12 13:01:05 +03:00
end
end