Class: ComputeInstances

Inherits:
Object
  • Object
show all
Defined in:
lib/danarchy_sys/openstack/compute/instances.rb

Overview

OpenStack Instance Management

Instance Method Summary collapse

Constructor Details

#initialize(compute, settings) ⇒ ComputeInstances

Returns a new instance of ComputeInstances.



4
5
6
7
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 4

def initialize(compute, settings)
  @compute = compute
  @settings = settings
end

Instance Method Details

#all_instances(*filter) ⇒ Object



9
10
11
12
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 9

def all_instances(*filter)
  filter = filter.shift || {}
  @compute.servers(filters: filter)
end

#check_instance(instance_name) ⇒ Object



22
23
24
25
26
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 22

def check_instance(instance_name)
  return false if instance_name == nil || instance_name.empty? == true
  return true if get_instance(instance_name)
  false
end

#create_instance(instance_name, image_id, flavor_id, keypair_name, *user_data) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 109

def create_instance(instance_name, image_id, flavor_id, keypair_name, *user_data)
  user_data = nil if user_data.empty?

  instance = @compute.servers.create(name: instance_name,
                                    image_ref: image_id,
                                    flavor_ref: flavor_id,
                                    key_name: keypair_name,
                                    user_data: user_data)
  
  # add security_group

  # Put error handling from instance_prompts here
  
  instance.wait_for { ready? }
  instance
end

#delete_instance(instance_name) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 126

def delete_instance(instance_name)
  # check for and delete instance
  instance = get_instance(instance_name)
  return 1 if instance == false
  @compute.delete_server(instance.id)

  attempt = 1
  until check_instance(instance_name) == false
    return false if attempt == 5
    sleep(5)
    attempt += 1
  end

  return true
end

#get_instance(instance_name) ⇒ Object



28
29
30
31
32
33
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 28

def get_instance(instance_name)
  instance = all_instances({ 'name' => [instance_name] })
  return false if !instance.first
  return false if !instance.collect{ |i| i.name }.include?(instance_name)
  instance.first
end

#get_private_addresses(instance) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 45

def get_private_addresses(instance)
  if instance.class == String
    instance = get_instance(instance)
  end

  addrs = instance.addresses
  return false if !addrs['private']
  addrs['public'].map{|a| a['addr']}
end

#get_public_addresses(instance) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 35

def get_public_addresses(instance)
  if instance.class == String
    instance = get_instance(instance)
  end

  addrs = instance.addresses
  return false if !addrs['public']
  addrs['public'].map{|a| a['addr']}
end

#list_active_instancesObject



18
19
20
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 18

def list_active_instances
  all_instances({ 'status' => ['ACTIVE'] }).collect { |i| i.name }
end

#list_all_instancesObject



14
15
16
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 14

def list_all_instances
  all_instances.collect { |i| i.name }
end

#pause(instance) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 55

def pause(instance)
  if instance.class == String
    instance = get_instance(instance)
  end

  return false unless instance.state == 'ACTIVE'
  instance.pause
end

#resume(instance) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 82

def resume(instance)
  if instance.class == String
    instance = get_instance(instance)
  end

  return false unless instance.state == 'SUSPENDED'
  instance.start
end

#start(instance) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 91

def start(instance)
  if instance.class == String
    instance = get_instance(instance)
  end

  return false unless instance.state == 'SHUTOFF'
  instance.start
end

#stop(instance) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 100

def stop(instance)
  if instance.class == String
    instance = get_instance(instance)
  end

  return false unless instance.state == 'ACTIVE'
  instance.stop
end

#suspend(instance) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 73

def suspend(instance)
  if instance.class == String
    instance = get_instance(instance)
  end

  return false unless instance.state == 'ACTIVE'
  instance.suspend
end

#unpause(instance) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 64

def unpause(instance)
  if instance.class == String
    instance = get_instance(instance)
  end

  return false unless instance.state == 'PAUSED'
  instance.start
end