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
15
16
17
# 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)
  @instances = @compute.servers
  @images = @compute.images(filters: {'status' => ['ACTIVE']})
  @flavors = @compute.flavors
end

Instance Method Details

#fallback_ssh(ipv4, pemfile) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/danarchy_sys/openstack/compute.rb', line 72

def fallback_ssh(ipv4, pemfile)
  users = %w[centos ubuntu debian fedora core]
  ssh, user = nil

  users.each do |username|
    print "Attempting connection as user: #{username} @ #{ipv4} => "
    connect = "/usr/bin/ssh -i '#{pemfile}' #{username}@#{ipv4}"
    ssh = system("#{connect} 'uptime' &>/dev/null")
    puts 'failed' if ssh ==false

    if ssh == true
      puts 'success'
      user = username
      break
    end
  end

  if ssh == false
    puts 'Unable to connect! User unknown or SSHd is not running on the instance.' 
    return ssh, nil
  else
    return [ssh, user]
  end
end

#flavorsObject



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

def flavors
  ComputeFlavors.new(@compute, @flavors)
end

#imagesObject



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

def images
  ComputeImages.new(@compute, @images)
end

#instancesObject



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

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

#keypairsObject



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

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

#secgroupsObject



35
36
37
# File 'lib/danarchy_sys/openstack/compute.rb', line 35

def secgroups
  ComputeSecgroups.new(@compute)
end

#ssh(instance_name) ⇒ Object



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
# File 'lib/danarchy_sys/openstack/compute.rb', line 39

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)

  ssh, user = nil
  if image == nil
    puts "Image not found for #{instance.name}! This instance needs to be rebuild with a current image."
    ssh, user = fallback_ssh(ipv4, pemfile)
  else
    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
  end

  return if !user

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