Class: Rig::Model::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/rig/model/environment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#balancersObject (readonly)

Returns the value of attribute balancers.



5
6
7
# File 'lib/rig/model/environment.rb', line 5

def balancers
  @balancers
end

#flavorObject (readonly)

Returns the value of attribute flavor.



11
12
13
# File 'lib/rig/model/environment.rb', line 11

def flavor
  @flavor
end

#imageObject (readonly)

Returns the value of attribute image.



9
10
11
# File 'lib/rig/model/environment.rb', line 9

def image
  @image
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/rig/model/environment.rb', line 6

def name
  @name
end

#regionObject (readonly)

Returns the value of attribute region.



10
11
12
# File 'lib/rig/model/environment.rb', line 10

def region
  @region
end

#serversObject (readonly)

Returns the value of attribute servers.



4
5
6
# File 'lib/rig/model/environment.rb', line 4

def servers
  @servers
end

#templateObject (readonly)

Returns the value of attribute template.



7
8
9
# File 'lib/rig/model/environment.rb', line 7

def template
  @template
end

#template_nameObject (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.message}"
  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

.listObject



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.tags["Environment"] }.compact.uniq.sort
end

Instance Method Details

#destroyObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/rig/model/environment.rb', line 158

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.tags['Role'] }.compact
  roles.each do |r|
    Rig::Log.info "removing role balancer dns (#{r})"
    Rig::Model::Dns.destroy("#{r}.#{name}.#{Rig.get_config(:dns_subdomain)}.#{Rig.get_config(:dns_zone)}")
  end

  Rig::Log.info "removing primary dns"
  Rig::Model::Dns.destroy("#{name}.#{Rig.get_config(:dns_subdomain)}.#{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

Returns:

  • (Boolean)


192
193
194
# File 'lib/rig/model/environment.rb', line 192

def protected?
  @protected
end

#reloadObject



186
187
188
189
190
# File 'lib/rig/model/environment.rb', line 186

def reload
  find_servers
  find_balancers
  @protected = @servers.any? { |s| s.tags["Protected"] == "true" }
end

#saveObject



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
# 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.#{Rig.get_config(:dns_subdomain)}"
      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}.#{Rig.get_config(:dns_subdomain)}.#@zone #{balancer.attributes[:dns_name]}"
        Rig::Model::Dns.create("#{name}.#{Rig.get_config(:dns_subdomain)}.#@zone", balancer.attributes[:dns_name], :ttl => 30)
      end
    end
  end

  wait_for_dns

  @template.balancers.each do |b|
    if set[b.name]
      if b.sticky
        Rig::Log.info "#@name-#{b.name}: setting stickiness policy"
        type = b.sticky_type
        expires = b.sticky_arg
        Rig::Model::Balancer.sticky(b.name, type, expires, 443, "AWSConsolePolicy-1")
      end
    end
  end

  Rig::Log.info "associating servers' DNS"
  @dnsdelayed.each do |h|
    server = @server_hash[h[:server]]
    record = h[:record]
    record ||= "#{server.tags['Name']}.#{Rig.get_config(:dns_zone)}" if server.tags
    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.message} at #{e.backtrace.join("\n")}"
  destroy
end

#servers_without_dnsObject



196
197
198
# File 'lib/rig/model/environment.rb', line 196

def servers_without_dns
  @servers.select{|e| e.dns_name.nil? }.count
end