Class: PromptsCreateInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/danarchy_sys/cli/instance_manager/prompts_create_instance.rb

Overview

CLI Prompt to create a new instance

Class Method Summary collapse

Class Method Details

.create_instance(os_compute, instance_name) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
# File 'lib/danarchy_sys/cli/instance_manager/prompts_create_instance.rb', line 4

def self.create_instance(os_compute, instance_name)
  comp_inst = os_compute.instances
  comp_imgs = os_compute.images
  comp_flvs = os_compute.flavors
  comp_keys = os_compute.keypairs

  # Prompt for and check that instance_name is unused
  if instance_name == 'nil'
    print "\nWhat should we name the instance?: "
    instance_name = gets.chomp
  end

  # Make sure instance_name isn't already in use
  until comp_inst.check_instance(instance_name) == false
    print "\n#{instance_name} already exists! Try another name: "
    instance_name = gets.chomp
  end

  puts "Creating instance: #{instance_name}"

  # Prompt for image
  puts "\nSelect an image (operating system) for #{instance_name}"
  image = PromptsCreateInstance.image(comp_imgs)

  # Prompt for flavor
  puts "\nSelect a flavor (instance size) for #{instance_name}"
  flavor = PromptsCreateInstance.flavor(comp_flvs)

  # Prompt for keypair
  puts "\nSelect a keypair (SSH key) for #{instance_name}"
  keypair = PromptsCreateInstance.keypair(comp_keys)

  # Print summary and prompt to continue
  puts "\nInstance Name: #{instance_name}"
  puts "        Linux: #{image.name}"
  puts "Instance Size: #{flavor.name}"
  puts "      Keypair: #{keypair.name}"

  print 'Should we continue with creating the instance? (Y/N): '
  instance = 'nil'
  continue = gets.chomp

  if continue =~ /^y(es)?$/i
    puts "Creating instance: #{instance_name}"
    instance = comp_inst.create_instance(instance_name, image.id, flavor.id, keypair.name)
  else
    puts "Abandoning creation of #{instance_name}"
    return false
  end

  instance_check = comp_inst.check_instance(instance_name)

  if instance_check == true
    puts "Instance #{instance.name} is ready!"
    return instance
  else
    raise "Error: Could not create instance: #{instance_name}" if instance_check == false
  end
end

.flavor(comp_flvs) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/danarchy_sys/cli/instance_manager/prompts_create_instance.rb', line 98

def self.flavor(comp_flvs)
  flavors = Helpers.objects_to_numhash(comp_flvs.all_flavors.sort_by(&:ram))
  flavor_name = 'nil'

  puts "\nAvailable Instance Flavors:"
  puts sprintf("%0s %-15s %-10s %-10s %0s", 'Id', 'Name', 'RAM', 'VCPUs', 'Disk')
  flavors.each do |id, flavor|
    print sprintf("%0s %-15s %-10s %-10s %0s\n",
                  "#{id}.", flavor[:name].split('.')[1], flavor[:ram], flavor[:vcpus], flavor[:disk])
  end

  print 'Which flavor should we use for this instance?: '
  flavor_check = false

  until flavor_check == true
    flavor_name = gets.chomp

    if flavor_name =~ /^[0-9]*$/
      until flavors.keys.include?(flavor_name.to_i)
        print "#{flavor_name} is not a valid Id. Enter an Id from above: "
        flavor_name = gets.chomp
      end

      flavor_name = flavors[flavor_name.to_i][:name].split('.')[1]
    end
    
    flavors.each_value do |flavor|
      flavor_check = true if flavor[:name].end_with?(flavor_name)
    end

    print "#{flavor_name} is not a valid flavor. Please enter an option from above: " if flavor_check == false
  end

  print "Flavor Name: #{flavor_name}\n"
  comp_flvs.get_flavor(flavor_name)
end

.image(comp_imgs) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/danarchy_sys/cli/instance_manager/prompts_create_instance.rb', line 64

def self.image(comp_imgs)
  images_numbered = Helpers.array_to_numhash(comp_imgs.list_images)
  image_name = 'nil'

  # List available images in a numbered hash.
  puts "\nAvailable Images:"
  i_name_length = Helpers.hash_largest_value(images_numbered).length
  printf("%0s %-#{i_name_length}s\n", 'Id', 'Image')
  images_numbered.each do |id, i_name|
    printf("%0s %-#{i_name_length}s\n", "#{id}.", i_name)
  end

  # Loop input until existing image is selected
  print 'Which image should we use for this instance?: '
  until images_numbered.values.include?(image_name)
    image_name = gets.chomp

    if image_name =~ /^[0-9]*$/
      until images_numbered.keys.include?(image_name.to_i)
        print "#{image_name} is not a valid Id. Enter an Id from above: "
        image_name = gets.chomp
      end

      image_name = images_numbered[image_name.to_i]
    end

    image_check = images_numbered.values.include?(image_name)
    print "#{image_name} is not a valid image. Please enter an option from above: " if image_check == false
  end

  print "Image Name: #{image_name}\n"
  comp_imgs.get_image_by_name(image_name)
end

.keypair(comp_keys) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/danarchy_sys/cli/instance_manager/prompts_create_instance.rb', line 135

def self.keypair(comp_keys)
  keypairs = Helpers.objects_to_numhash(comp_keys.all_keypairs)
  keypair_name = 'nil'

  # List available keypairs
  puts "\nAvailable Keypairs:"
  print sprintf("%0s %-15s\n", 'Id', 'KeyPair Name')
  keypairs.each do |id, key|
    print sprintf("%0s %-15s\n", "#{id}.", key[:name])
  end

  # Loop input until existing flavor is selected or create a new one
  print 'Enter a keypair to use for this instance or enter a name for a new keypair : '
  keypair_check = false

  until keypair_check == true
    keypair_name = gets.chomp

    # Accept keypair Id as an entry
    if keypair_name =~ /^[0-9]*$/
      until keypairs.keys.include?(keypair_name.to_i)
        print "#{keypair_name} is not a valid Id.
Enter an Id from above, or \'return\' to restart keypair selection. : "
        keypair_name = gets.chomp
        return keypair(settings, compute) if keypair_name == 'return'
      end

      keypair_name = keypairs[keypair_name.to_i][:name]
    end

    keypair_check = Helpers.check_nested_hash_value(keypairs, :name, keypair_name)

    if keypair_check == false
      print "#{keypair_name} is not an existing keypair.
Should we create a new keypair named #{keypair_name}? (Y/N): "

      if gets.chomp =~ /^y(es)?$/i
        puts "Creating keypair: #{keypair_name}!"
        return comp_keys.create_keypair(keypair_name)
      else
        print 'Please enter an option from above: '
      end
    end
  end

  comp_keys.get_keypair(keypair_name)
end