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_instancesObject



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

def all_instances
  @compute.servers
end

#check_instance(instance_name) ⇒ Object



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

def check_instance(instance_name)
  instances = list_all_instances

  return true if instances.include?(instance_name)
  false
end

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



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

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

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

#delete_instance(instance_name) ⇒ Object



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

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_addresses(instance_name) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 49

def get_addresses(instance_name)
  instance = get_instance(instance_name)
 (ipv6, ipv4) = nil, nil
 addresses = instance.addresses['public']

  addresses.each do |i|
    ipv4 = i['addr'] if i['addr'].include?('.')
    ipv6 = i['addr'] if i['addr'].include?(':')
  end

  return ipv4, ipv6
end

#get_instance(instance_name) ⇒ Object



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

def get_instance(instance_name)
  servers = all_instances

  # Get servers ID based on input instance_name
  instance = 'nil'
  servers.each do |i|
    instance = i if i.name.end_with?(instance_name)
  end

  return false unless instance
  instance
end

#list_active_instancesObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 18

def list_active_instances
  instances = all_instances
  instance_list = []

  instances.each do |i|
    instance_list.push(i.name) if i.state == 'ACTIVE'
  end

  instance_list
end

#list_all_instancesObject



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

def list_all_instances
  instances = all_instances
  instances.map(&:name)
end

#pause(instance_name) ⇒ Object



62
63
64
65
66
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 62

def pause(instance_name)
  instance = get_instance(instance_name)
  return false unless instance.state == 'ACTIVE'
  @compute.pause_server(instance.id)
end

#resume(instance_name) ⇒ Object



80
81
82
83
84
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 80

def resume(instance_name)
  instance = get_instance(instance_name)
  return false unless instance.state == 'SUSPENDED'
  @compute.resume_server(instance.id)
end

#start(instance_name) ⇒ Object



86
87
88
89
90
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 86

def start(instance_name)
  instance = get_instance(instance_name)
  return false unless instance.state == 'SHUTOFF'
  @compute.start_server(instance.id)
end

#stop(instance_name) ⇒ Object



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

def stop(instance_name)
  instance = get_instance(instance_name)
  return false unless instance.state == 'ACTIVE'
  @compute.stop_server(instance.id)
end

#suspend(instance_name) ⇒ Object



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

def suspend(instance_name)
  instance = get_instance(instance_name)
  return false unless instance.state == 'ACTIVE'
  @compute.suspend_server(instance.id)
end

#unpause(instance_name) ⇒ Object



68
69
70
71
72
# File 'lib/danarchy_sys/openstack/compute/instances.rb', line 68

def unpause(instance_name)
  instance = get_instance(instance_name)
  return false unless instance.state == 'PAUSED'
  @compute.unpause_server(instance.id)
end