Class: DanarchySys::OpenStack::Compute

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

Instance Method Summary collapse

Constructor Details

#initialize(provider) ⇒ Compute

Returns a new instance of Compute.



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

def initialize(provider)
  danarchysys_config = DanarchySys::ConfigManager::Config.new
  connection = danarchysys_config[:connections][provider.to_sym]
  @settings = danarchysys_config[:global_settings]
  @compute = Fog::Compute::OpenStack.new(connection)
end

Instance Method Details

#fallback_ssh(ipv4, pemfile) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/danarchy_sys/openstack/compute.rb', line 75

def fallback_ssh(ipv4, pemfile)
  users = ['ubuntu','debian','centos','fedora','core']
  ssh = false

  users.each do |user|
    puts "Attempting connection as user: #{user} => #{ipv4}"
    connect = "ssh #{user}@#{ipv4} -i '#{pemfile}'"
    ssh = system(connect)
    return true if ssh == true
  end

  if ssh == false
    puts 'Unable to connect after 3 tries!'
    return false
  end
end

#flavorsObject



28
29
30
# File 'lib/danarchy_sys/openstack/compute.rb', line 28

def flavors
  ComputeFlavors.new(@compute)
end

#imagesObject



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

def images
  ComputeImages.new(@compute)
end

#instancesObject



16
17
18
# File 'lib/danarchy_sys/openstack/compute.rb', line 16

def instances
  ComputeInstances.new(@compute, @settings)
end

#keypairsObject



20
21
22
# File 'lib/danarchy_sys/openstack/compute.rb', line 20

def keypairs
  ComputeKeypairs.new(@compute, @settings)
end

#secgroupsObject



32
33
34
# File 'lib/danarchy_sys/openstack/compute.rb', line 32

def secgroups
  ComputeSecgroups.new(@compute)
end

#ssh(instance_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/danarchy_sys/openstack/compute.rb', line 36

def ssh(instance_name)
  (comp_inst, comp_kp, comp_img) = instances, keypairs, images
  instance = comp_inst.get_instance(instance_name)
  keypair_name = instance.key_name
  pemfile = comp_kp.pemfile_path(keypair_name)
  
  addrs = comp_inst.get_public_addresses(instance_name)
  ipv4 = addrs.grep(/\./).first
  ipv6 = addrs.grep(/:/).first

  # Define user by image_id
  image_id = instance.image['id']
  image = comp_img.get_image_by_id(image_id)

  if image == nil
    puts "Image not found for #{instance.name}! This instance needs to be rebuild with a current image."
    return fallback_ssh(ipv4, pemfile)          
  end

  # CoreOS is an exception with user as simply 'core' and not 'coreos'
  user = 'ubuntu' if image.name =~ /ubuntu/i
  user = 'debian' if image.name =~ /debian/i
  user = 'centos' if image.name =~ /centos/i
  user = 'fedora' if image.name =~ /fedora/i
  user = 'core'   if image.name =~ /coreos/i

  puts "Connecting as user: #{user} => #{ipv4}"
  connect = "ssh #{user}@#{ipv4} -i '#{pemfile}'"
  ssh = system(connect)

  attempts = 1
  until ssh == true || attempts > 3
    puts "Connection failed. Attempting again after 5 seconds..."
    sleep(5)
    fallback_ssh(ipv4, pemfile)
    attempts += 1
  end
end