64 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
		
		
			
		
	
	
			64 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
|   | require "db/mongo/models/deploy_env_base" | ||
|  | require "providers/provider_factory" | ||
|  | 
 | ||
|  | class DeployEnvEc2 < DeployEnvBase | ||
|  | 
 | ||
|  |   attr_accessor :flavor, :image, :subnets, :groups | ||
|  | 
 | ||
|  |   types :identifier => {:type => String, :empty => false}, | ||
|  |         :image => {:type => String, :empty => false}, | ||
|  |         :flavor => {:type => String, :empty => false}, | ||
|  |         :provider => {:type => String, :empty => false}, | ||
|  |         :expires => {:type => String, :empty => false, :nil => true}, | ||
|  |         :run_list => {:type => Array, :empty => true}, | ||
|  |         :users => {:type => Array, :empty => true}, | ||
|  |         :subnets => {:type => Array, :empty => true}, | ||
|  |         :groups => {:type => Array, :empty => false} | ||
|  | 
 | ||
|  |   def initialize d={} | ||
|  |     super(d) | ||
|  |     self.flavor = d["flavor"] | ||
|  |     self.image = d["image"] | ||
|  |     b = d["subnets"] || [] | ||
|  |     self.subnets = if b.is_a?(Array) | ||
|  |       (b.size > 1 ? [ b[0] ] : b) | ||
|  |     else | ||
|  |       b | ||
|  |     end | ||
|  |     b = d["groups"] || ["default"] | ||
|  |     self.groups = (b.is_a?(Array) ? b.uniq : b) | ||
|  |   end | ||
|  | 
 | ||
|  |   def validate! | ||
|  |     super | ||
|  | 
 | ||
|  |     p = ::Provider::ProviderFactory.get(self.provider) | ||
|  |     check_flavor!(p, self.flavor) | ||
|  |     check_image!(p, self.image) | ||
|  |     check_subnets_and_groups!(p, self.subnets, self.groups) do |networks| | ||
|  |       unless self.subnets.empty? | ||
|  |         {"vpc-id" => networks.detect{|n| n["name"] == self.subnets[0]}["vpcId"] } | ||
|  |       end | ||
|  |     end | ||
|  | 
 | ||
|  |     true | ||
|  |   rescue InvalidRecord => e | ||
|  |     raise InvalidRecord.new "Deploy environment '#{self.identifier}'. " + e.message | ||
|  |   end | ||
|  | 
 | ||
|  |   def to_hash | ||
|  |     h = super | ||
|  |     h.merge!({ | ||
|  |       "flavor" => self.flavor, | ||
|  |       "image" => self.image, | ||
|  |       "subnets" => self.subnets, | ||
|  |       "groups" => self.groups | ||
|  |     }) | ||
|  |   end | ||
|  | 
 | ||
|  |   def self.create hash | ||
|  |     DeployEnvEc2.new(hash) | ||
|  |   end | ||
|  | 
 | ||
|  | end |