Class: Rig::Model::Environment
- Inherits:
-
Object
- Object
- Rig::Model::Environment
- Defined in:
- lib/rig/model/environment.rb
Instance Attribute Summary collapse
-
#balancers ⇒ Object
readonly
Returns the value of attribute balancers.
-
#flavor ⇒ Object
readonly
Returns the value of attribute flavor.
-
#image ⇒ Object
readonly
Returns the value of attribute image.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
-
#servers ⇒ Object
readonly
Returns the value of attribute servers.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
-
#template_name ⇒ Object
readonly
Returns the value of attribute template_name.
Class Method Summary collapse
- .create(name, template, opts = { }) ⇒ Object
- .destroy(name, template = nil, opts = { }) ⇒ Object
- .find(name, template = nil, opts = { }) ⇒ Object
- .list ⇒ Object
Instance Method Summary collapse
- #destroy ⇒ Object
-
#initialize(name, template = nil, opts = { }) ⇒ Environment
constructor
TODO: overall, need better handling for global config, template and local overrides.
- #protected? ⇒ Boolean
- #reload ⇒ Object
- #save ⇒ Object
- #servers_without_dns ⇒ Object
Constructor Details
#initialize(name, template = nil, opts = { }) ⇒ Environment
TODO: overall, need better handling for global config, template and local overrides
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rig/model/environment.rb', line 47 def initialize(name, template=nil, opts={ }) @options = { }.merge(opts) @name = name @template = Rig.get_template(template) unless template.nil? @template_name = template @servers = [] @dnsdelayed = [] @balancers = [] end |
Instance Attribute Details
#balancers ⇒ Object (readonly)
Returns the value of attribute balancers.
5 6 7 |
# File 'lib/rig/model/environment.rb', line 5 def balancers @balancers end |
#flavor ⇒ Object (readonly)
Returns the value of attribute flavor.
11 12 13 |
# File 'lib/rig/model/environment.rb', line 11 def flavor @flavor end |
#image ⇒ Object (readonly)
Returns the value of attribute image.
9 10 11 |
# File 'lib/rig/model/environment.rb', line 9 def image @image end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/rig/model/environment.rb', line 6 def name @name end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
10 11 12 |
# File 'lib/rig/model/environment.rb', line 10 def region @region end |
#servers ⇒ Object (readonly)
Returns the value of attribute servers.
4 5 6 |
# File 'lib/rig/model/environment.rb', line 4 def servers @servers end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
7 8 9 |
# File 'lib/rig/model/environment.rb', line 7 def template @template end |
#template_name ⇒ Object (readonly)
Returns the value of attribute template_name.
8 9 10 |
# File 'lib/rig/model/environment.rb', line 8 def template_name @template_name end |
Class Method Details
.create(name, template, opts = { }) ⇒ Object
27 28 29 30 31 |
# File 'lib/rig/model/environment.rb', line 27 def create(name, template, opts={ }) env = self.new(name, template, opts) env.save env end |
.destroy(name, template = nil, opts = { }) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rig/model/environment.rb', line 33 def destroy(name, template=nil, opts={ }) env = self.find(name, template, opts) raise "environment has protected instances" if env.protected? Rig::Log.info "destroying: #{name}" env.destroy true rescue => e puts "exception: #{e.}" puts e.backtrace.join("\n") false end |
.find(name, template = nil, opts = { }) ⇒ Object
21 22 23 24 25 |
# File 'lib/rig/model/environment.rb', line 21 def find(name, template=nil, opts={ }) env = self.new(name, template, opts) env.reload env end |
.list ⇒ Object
14 15 16 17 18 19 |
# File 'lib/rig/model/environment.rb', line 14 def list filters = { } list = Rig::Connection.compute.servers.all(filters) list.inject([]) { |a, e| a << e.["Environment"] }.compact.uniq.sort end |
Instance Method Details
#destroy ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/rig/model/environment.rb', line 162 def destroy Rig::Log.info "removing all server dns entries" @servers.each do |server| #Rig::Model::Dns.destroy("#{server.tags['Name']}.#{Rig.get_config(:dns_zone)}") if server.tags['Name'] Rig::Model::Dns.remove_instance(server.dns_name) if server.dns_name end Rig::Log.info "removing all environment dns entries" Rig::Model::Dns.remove_environment(@name) roles = @servers.collect { |s| s.['Role'] }.compact roles.each do |r| Rig::Log.info "removing role balancer dns (#{r})" Rig::Model::Dns.destroy("#{r}.#{name}.env.#{Rig.get_config(:dns_zone)}") end Rig::Log.info "removing primary dns" Rig::Model::Dns.destroy("#{name}.env.#{Rig.get_config(:dns_zone)}") Rig::Log.info "destroying servers" Rig::Model::Instance.destroy(@servers) Rig::Log.info "destroying balancers" Rig::Model::Balancer.destroy(@balancers) Rig::Plugin.run "environment:destroy", self end |
#protected? ⇒ Boolean
196 197 198 |
# File 'lib/rig/model/environment.rb', line 196 def protected? @protected end |
#reload ⇒ Object
190 191 192 193 194 |
# File 'lib/rig/model/environment.rb', line 190 def reload find_servers find_balancers @protected = @servers.any? { |s| s.["Protected"] == "true" } end |
#save ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/rig/model/environment.rb', line 60 def save Rig::Log.info "creating instances" load_config set = { } @template.servers.each do |s| Rig::Log.debug "server[#{s.count}]= #{s.inspect}" 1.upto(s.count) do |i| n = "#{s.name}#{i}.#@name.env" Rig::Log.debug " name= #{n}" userdata = s.userdata || Rig.get_config(:userdata) o = { :image_id => s.image || @image, :flavor_id => s.flavor || @flavor, :key_name => s.keypair || @keypair, :groups => s.groups || @groups, :user_data => Rig::Model::Userdata.create(n, s.roles, @name, :package => userdata) } # throw this error, because Fog and AWS don't care if you don't set a keypair # but you won't be able to easily connect to it raise "No key_name (keypair) set." unless o[:key_name] Rig::Log.info "creating instance: name:#{n} environment:#@name roles:#{s.roles.inspect}" instance = Rig::Model::Instance.create(o, 'Name' => n, 'Environment' => name, 'Roles' => s.roles.join(",")) if s.balancer set[s.balancer] ||= [] set[s.balancer] << instance end @servers << instance @dnsdelayed << { :server => instance.id, } if s.count == 1 && s.dnsnames.count > 0 s.dnsnames.each do |dns| @dnsdelayed << { :record => "#{dns}.#{Rig.get_config(:dns_zone)}", :server => instance.id, } end end end end @template.balancers.each do |b| if set[b.name] zones = set[b.name].collect { |e| e.availability_zone } balancer = Rig::Model::Balancer.create("#@name-#{b.name}", b.listeners, zones) balancer.register_instances(set[b.name].collect { |e| e.id }) balancer.save @balancers << balancer if b.primary Rig::Log.info ".. primary: #{name}.env.#@zone #{balancer.attributes[:dns_name]}" Rig::Model::Dns.create("#{name}.env.#@zone", balancer.attributes[:dns_name], :ttl => 30) end if b.sticky Rig::Log.info ".. setting stickiness policy" policy_name = 'RigLBStickinessPolicy' type = b.sticky_type = b.sticky_arg case type.to_sym when :app, :application Rig::Connection.balancer.("#@name-#{b.name}", policy_name, ) when :lb, :load_balancer Rig::Connection.balancer.("#@name-#{b.name}", policy_name, ) else raise "unknown sticky type #{type}" end end end end wait_for_dns Rig::Log.info "associating servers' DNS" @dnsdelayed.each do |h| server = @server_hash[h[:server]] record = h[:record] record ||= "#{server.['Name']}.#{Rig.get_config(:dns_zone)}" if server. next unless record dns = server.attributes[:dns_name] ip = server.attributes[:public_ip_address] Rig::Log.info ".. #{record} dns=#{dns} ip=#{ip}" unless dns Rig::Log.warn "server #{server.id} doesn't have dns_name" next end Rig::Model::Dns.create(record, dns) end Rig::Plugin.run "environment:create", self rescue => e Rig::Log.error "error creating environment:\n#{e.} at #{e.backtrace.join("\n")}" destroy end |
#servers_without_dns ⇒ Object
200 201 202 |
# File 'lib/rig/model/environment.rb', line 200 def servers_without_dns @servers.select{|e| e.dns_name.nil? }.count end |