Class: Rig::Model::Instance

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

Class Method Summary collapse

Class Method Details

.all(filters = {}) ⇒ Object



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

def all(filters={})
  @all ||= Rig::Connection.compute.servers.all(filters)
end

.create(opts, tags = {}) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/rig/model/instance.rb', line 41

def create(opts, tags={})
  fog = Rig::Connection.compute
  server = fog.servers.create(opts)
  fog.create_tags(server.id, tags) if tags.count > 0
  Rig::Log.info ".. created: #{server.id}"
  Rig::Plugin.run "instance:create", server
  server
end

.destroy(list) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rig/model/instance.rb', line 50

def destroy(list)
  list = [*list]

  if list.count > 0
    compute = Rig::Connection.compute
    ids = list.map {|e| e.kind_of?(String) ? e : e.id}
    Rig::Log.info ".. destroying: #{ids.inspect}"

    # destroy the instances
    list.each do |e|
      e.destroy

      Rig::Plugin.run "instance:destroy", e, e.tags
    end

    tags = %w{Name Environment Role}.inject({}) {|h, e| h[e] = nil; h }
    compute.create_tags(ids, tags)
  end
end

.find(id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/rig/model/instance.rb', line 15

def find(id)
  if id =~ /^i-/
    Rig::Log.info "find #{id}"
    Rig::Connection.compute.servers.get(id)
  else
    # must be tag:Name
    Rig::Log.info "all tag:Name => #{id}"
    Rig::Connection.compute.servers.all({"tag:Name"=>id})
  end
end

.find_by_environment(env) ⇒ Object



26
27
28
# File 'lib/rig/model/instance.rb', line 26

def find_by_environment(env)
  Rig::Connection.compute.servers.all({"tag:Environment"=>env})
end

.find_by_nick(name) ⇒ Object



34
35
36
37
38
39
# File 'lib/rig/model/instance.rb', line 34

def find_by_nick(name)
  (role, env) = name.match(/(\w+)\.(\w+)/)[1..2]
  find_by_environment(env).select {|s| s.tags['Name'] =~ /^#{role}/ }
rescue => e
  Rig::Log.error "error finding server by nickname #{name}. name should be of the form: role#.environment"
end

.find_by_role_and_environment(role, env) ⇒ Object



30
31
32
# File 'lib/rig/model/instance.rb', line 30

def find_by_role_and_environment(role, env)
  Rig::Connection.compute.servers.all({"tag:Environment"=> env, "tag:Role" => role})
end

.runningObject



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

def running
  self.all({"instance-state-name"=>"running"})
end