Class: DanarchySys::OpenStack::Compute

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

Instance Method Summary collapse

Constructor Details

#initialize(connection, settings) ⇒ Compute

Returns a new instance of Compute.



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

def initialize(connection, settings)
  @settings = settings
  @compute = Fog::OpenStack::Compute.new(connection)
  @instances = @compute.servers
  @images = @compute.images(filters: {'status' => ['ACTIVE']})
  @flavors = @compute.flavors
end

Instance Method Details

#fallback_ssh(connector) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/danarchy_sys/openstack/compute.rb', line 68

def fallback_ssh(connector)
  users = %w[gentoo debian ubuntu centos fedora core]
  fb_opts = { quiet: true, command: 'uptime' }

  users.each do |username|
    connector[:ssh_user] = username
    print 'Attempting connection as user: '
    print connector[:ssh_user], ' @ ', connector[:ipv4], ' => '
    fallback_result = SSH.new(connector, fb_opts)

    if fallback_result[:stdout]
      puts 'success'
      break
    else
      puts 'failed'
      connector[:ssh_user] = nil
    end
  end

  puts 'Unable to determine user or SSHd is not running!' if ! connector[:ssh_user]
  connector
end

#flavorsObject



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

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

#imagesObject



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

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

#instancesObject



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

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

#keypairsObject



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

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

#secgroupsObject



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

def secgroups
  ComputeSecgroups.new(@compute)
end

#ssh(instance, *cmd) ⇒ Object



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

def ssh(instance, *cmd)
  instance = instances.get_instance(instance) if instance.class == String
  pemfile = keypairs.pemfile_path(instance.key_name)
  addrs = instances.get_public_addresses(instance)

  opts = { quiet: true }
  opts[:command] = cmd ? cmd.shift : nil
  connector = { ipv4: addrs.grep(/\./).first,
                ipv6: addrs.grep(/:/).first,
                ssh_key: pemfile,
                ssh_user: nil }
  # Define user by image_id
  image = images.get_image_by_id(instance.image['id'])
  if image == nil
    puts "Image not found for #{instance.name}! This instance needs to be rebuild with a current image."
    puts "Attempting to determine the correct username and log in..."
    connector = fallback_ssh(connector)
  else
    connector[:ssh_user] = 'gentoo' if image.name =~ /gentoo/i
    connector[:ssh_user] = 'ubuntu' if image.name =~ /ubuntu/i
    connector[:ssh_user] = 'debian' if image.name =~ /debian/i
    connector[:ssh_user] = 'centos' if image.name =~ /centos/i
    connector[:ssh_user] = 'fedora' if image.name =~ /fedora/i
    connector[:ssh_user] = 'core'   if image.name =~ /coreos/i
  end

  return if ! connector[:ssh_user]
  SSH.new(connector, opts)
end