danarchy_sys 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +2 -0
  6. data/LICENSE +21 -0
  7. data/README.txt +86 -0
  8. data/Rakefile +6 -0
  9. data/bin/console +6 -0
  10. data/bin/danarchy_sys +4 -0
  11. data/bin/setup +18 -0
  12. data/bin/setup.rb +86 -0
  13. data/danarchy_sys.gemspec +38 -0
  14. data/lib/danarchy_sys.rb +16 -0
  15. data/lib/danarchy_sys/aws.rb +9 -0
  16. data/lib/danarchy_sys/aws/compute.rb +13 -0
  17. data/lib/danarchy_sys/cli.rb +63 -0
  18. data/lib/danarchy_sys/cli/instance_manager.rb +130 -0
  19. data/lib/danarchy_sys/cli/instance_manager/instance_status.rb +50 -0
  20. data/lib/danarchy_sys/cli/instance_manager/prompts_create_instance.rb +182 -0
  21. data/lib/danarchy_sys/cli/menus.rb +60 -0
  22. data/lib/danarchy_sys/cli/providers.rb +33 -0
  23. data/lib/danarchy_sys/config.rb +66 -0
  24. data/lib/danarchy_sys/helpers.rb +61 -0
  25. data/lib/danarchy_sys/openstack.rb +9 -0
  26. data/lib/danarchy_sys/openstack/compute.rb +59 -0
  27. data/lib/danarchy_sys/openstack/compute/flavors.rb +47 -0
  28. data/lib/danarchy_sys/openstack/compute/images.rb +52 -0
  29. data/lib/danarchy_sys/openstack/compute/instances.rb +120 -0
  30. data/lib/danarchy_sys/openstack/compute/keypairs.rb +84 -0
  31. data/lib/danarchy_sys/openstack/compute/tests/answer_yes.rb +7 -0
  32. data/lib/danarchy_sys/openstack/compute/tests/flavors_test.rb +10 -0
  33. data/lib/danarchy_sys/openstack/compute/tests/images_test.rb +14 -0
  34. data/lib/danarchy_sys/openstack/compute/tests/instance_create_test.rb +14 -0
  35. data/lib/danarchy_sys/openstack/compute/tests/instance_manage_test.rb +24 -0
  36. data/lib/danarchy_sys/openstack/compute/tests/keypairs_prompt_test.rb +12 -0
  37. data/lib/danarchy_sys/openstack/compute/tests/keypairs_test.rb +38 -0
  38. data/lib/danarchy_sys/openstack/compute/tests/manage_test.rb +16 -0
  39. data/lib/danarchy_sys/printformats.rb +49 -0
  40. data/lib/danarchy_sys/version.rb +3 -0
  41. metadata +151 -0
@@ -0,0 +1,9 @@
1
+
2
+ require 'fog/openstack'
3
+
4
+ module DanarchySys
5
+ module OpenStack
6
+ # Load OpenStack compute requirements
7
+ require_relative 'openstack/compute'
8
+ end
9
+ end
@@ -0,0 +1,59 @@
1
+ require_relative 'compute/instances'
2
+ require_relative 'compute/keypairs'
3
+ require_relative 'compute/images'
4
+ require_relative 'compute/flavors'
5
+
6
+ module DanarchySys
7
+ module OpenStack
8
+ class Compute
9
+ def initialize(provider)
10
+ config = ConfigMgr.new
11
+ danarchysys_config = config.load
12
+ connection = danarchysys_config[:connections][provider]
13
+ @settings = danarchysys_config[:settings]
14
+ @compute = Fog::Compute::OpenStack.new(connection)
15
+ end
16
+
17
+ def compute_instances
18
+ ComputeInstances.new(@compute, @settings)
19
+ end
20
+
21
+ def compute_keypairs
22
+ ComputeKeypairs.new(@compute, @settings)
23
+ end
24
+
25
+ def compute_images
26
+ ComputeImages.new(@compute)
27
+ end
28
+
29
+ def compute_flavors
30
+ ComputeFlavors.new(@compute)
31
+ end
32
+
33
+ def compute_ssh(instance_name)
34
+ (comp_inst, comp_kp, comp_img) = compute_instances, compute_keypairs, compute_images
35
+ instance = comp_inst.get_instance(instance_name)
36
+ keypair_name = instance.key_name
37
+ pemfile = comp_kp.pemfile_path(keypair_name)
38
+
39
+ # Define user by image_id
40
+ image_info = instance.image
41
+ image_id = image_info['id']
42
+ image = comp_img.get_image_by_id(image_id)
43
+
44
+ # CoreOS is an exception with user as simply 'core' and not 'coreos'
45
+ user = ''
46
+ if image.name =~ /CoreOS/i
47
+ user = 'core'
48
+ else
49
+ user = image.name.downcase.split('-')[0]
50
+ end
51
+
52
+ ipv4 = comp_inst.get_addresses(instance_name)[1]['addr']
53
+
54
+ ssh = "ssh #{user}@#{ipv4} -i '#{pemfile}'"
55
+ system(ssh)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,47 @@
1
+
2
+ # OpenStack Flavor Management
3
+ class ComputeFlavors
4
+ def initialize(compute)
5
+ @compute = compute
6
+ end
7
+
8
+ def all_flavors
9
+ @compute.flavors
10
+ end
11
+
12
+ def list_flavors
13
+ flavors = all_flavors
14
+ flavor_list = []
15
+
16
+ # Get flavor names into array
17
+ flavors.each do |i|
18
+ flavor_list.push(i.name.split('.')[1])
19
+ end
20
+
21
+ flavor_list
22
+ end
23
+
24
+ def get_flavor(flavor_name)
25
+ flavors = all_flavors
26
+
27
+ # Get flavor object based on input flavor_name.
28
+ flavor = 'nil'
29
+ flavors.each do |f|
30
+ flavor = f if f.name.end_with?(flavor_name)
31
+ end
32
+
33
+ flavor
34
+ end
35
+
36
+ def get_flavor_by_id(flavor_id)
37
+ flavors = all_flavors
38
+
39
+ # Get flavor based on input flavor_id.
40
+ flavor = 'nil'
41
+ flavors.each do |i|
42
+ flavor = i if i.id == flavor_id
43
+ end
44
+
45
+ flavor
46
+ end
47
+ end
@@ -0,0 +1,52 @@
1
+
2
+ # OpenStack Image Management
3
+ class ComputeImages
4
+ def initialize(compute)
5
+ @compute = compute
6
+ end
7
+
8
+ def all_images
9
+ @compute.images
10
+ end
11
+
12
+ def list_images
13
+ images = all_images
14
+ image_list = []
15
+
16
+ # Get image names into array
17
+ images.each do |i|
18
+ next unless i.status == 'ACTIVE'
19
+ image_list.push(i.name)
20
+ end
21
+
22
+ image_list
23
+ end
24
+
25
+ def get_image_by_name(image_name)
26
+ images = all_images
27
+
28
+ # Get image based on input image_name.
29
+ image = 'nil'
30
+ images.each do |i|
31
+ next unless i.name == image_name
32
+ next unless i.status == 'ACTIVE'
33
+ image = i
34
+ end
35
+
36
+ image
37
+ end
38
+
39
+ def get_image_by_id(image_id)
40
+ images = all_images
41
+
42
+ # Get image based on input image_id.
43
+ image = 'nil'
44
+ images.each do |i|
45
+ next unless i.id == image_id
46
+ next unless i.status == 'ACTIVE'
47
+ image = i
48
+ end
49
+
50
+ image
51
+ end
52
+ end
@@ -0,0 +1,120 @@
1
+
2
+ # OpenStack Instance Management
3
+ class ComputeInstances
4
+ def initialize(compute, settings)
5
+ @compute = compute
6
+ @settings = settings
7
+ end
8
+
9
+ def all_instances
10
+ @compute.servers
11
+ end
12
+
13
+ def list_all_instances
14
+ instances = all_instances
15
+ instances.map(&:name)
16
+ end
17
+
18
+ def list_active_instances
19
+ instances = all_instances
20
+ instance_list = []
21
+
22
+ instances.each do |i|
23
+ instance_list.push(i.name) if i.state == 'ACTIVE'
24
+ end
25
+
26
+ instance_list
27
+ end
28
+
29
+ def check_instance(instance_name)
30
+ instances = list_all_instances
31
+
32
+ return true if instances.include?(instance_name)
33
+ false
34
+ end
35
+
36
+ def get_instance(instance_name)
37
+ servers = all_instances
38
+
39
+ # Get servers ID based on input instance_name
40
+ instance = 'nil'
41
+ servers.each do |i|
42
+ instance = i if i.name.end_with?(instance_name)
43
+ end
44
+
45
+ return false unless instance
46
+ instance
47
+ end
48
+
49
+ def get_addresses(instance_name)
50
+ instance = get_instance(instance_name)
51
+
52
+ addresses = instance.addresses['public']
53
+ ipv6, ipv4 = addresses[0], addresses[1]
54
+ end
55
+
56
+ def pause(instance_name)
57
+ instance = get_instance(instance_name)
58
+ return false unless instance.state == 'ACTIVE'
59
+ @compute.pause_server(instance.id)
60
+ end
61
+
62
+ def unpause(instance_name)
63
+ instance = get_instance(instance_name)
64
+ return false unless instance.state == 'PAUSED'
65
+ @compute.unpause_server(instance.id)
66
+ end
67
+
68
+ def suspend(instance_name)
69
+ instance = get_instance(instance_name)
70
+ return false unless instance.state == 'ACTIVE'
71
+ @compute.suspend_server(instance.id)
72
+ end
73
+
74
+ def resume(instance_name)
75
+ instance = get_instance(instance_name)
76
+ return false unless instance.state == 'SUSPENDED'
77
+ @compute.resume_server(instance.id)
78
+ end
79
+
80
+ def start(instance_name)
81
+ instance = get_instance(instance_name)
82
+ return false unless instance.state == 'SHUTOFF'
83
+ @compute.start_server(instance.id)
84
+ end
85
+
86
+ def stop(instance_name)
87
+ instance = get_instance(instance_name)
88
+ return false unless instance.state == 'ACTIVE'
89
+ @compute.stop_server(instance.id)
90
+ end
91
+
92
+ def create_instance(instance_name, image_id, flavor_id, keypair_name)
93
+ instance = @compute.servers.create(name: instance_name,
94
+ image_ref: image_id,
95
+ flavor_ref: flavor_id,
96
+ key_name: keypair_name)
97
+ # add security_group
98
+
99
+ # Put error handling from instance_prompts here
100
+
101
+ instance.wait_for { ready? }
102
+ instance
103
+ end
104
+
105
+ def delete_instance(instance_name)
106
+ # check for and delete instance
107
+ instance = get_instance(instance_name)
108
+ return 1 if instance == false
109
+ @compute.delete_server(instance.id)
110
+
111
+ attempt = 1
112
+ until check_instance(instance_name) == false
113
+ return false if attempt == 5
114
+ sleep(5)
115
+ attempt += 1
116
+ end
117
+
118
+ return true
119
+ end
120
+ end
@@ -0,0 +1,84 @@
1
+
2
+ # OpenStack Keypair Management
3
+ class ComputeKeypairs
4
+ def initialize(compute, settings)
5
+ @compute = compute
6
+ @settings = settings
7
+ end
8
+
9
+ def pemfile_path(keypair_name)
10
+ "#{@settings[:ssh_key_path]}/fog_#{keypair_name}.pem"
11
+ end
12
+
13
+ def pemfile_check(pemfile)
14
+ return true if File.exist?(pemfile)
15
+ false
16
+ end
17
+
18
+ def all_keypairs
19
+ @compute.key_pairs
20
+ end
21
+
22
+ def list_keypairs
23
+ keypair_list = []
24
+ keypairs = all_keypairs
25
+
26
+ keypairs.each do |kp|
27
+ keypair_list.push(kp.name)
28
+ end
29
+
30
+ keypair_list
31
+ end
32
+
33
+ def check_keypair(keypair_name)
34
+ keypairs = list_keypairs
35
+
36
+ return true if keypairs.include?(keypair_name)
37
+ false
38
+ end
39
+
40
+ def get_keypair(keypair_name)
41
+ keypairs = all_keypairs
42
+ keypair = 'nil'
43
+
44
+ keypairs.each do |kp|
45
+ keypair = kp if kp.name == keypair_name
46
+ end
47
+
48
+ keypair
49
+ end
50
+
51
+ def create_keypair(keypair_name)
52
+ keypair = @compute.create_key_pair(keypair_name)
53
+ pemfile = pemfile_path(keypair_name)
54
+
55
+ # create pemfile at pemfile_path
56
+ keyhash = keypair.body['keypair']
57
+ private_key = keyhash['private_key']
58
+ File.open(pemfile, 'w') do |f|
59
+ f.puts(private_key)
60
+ f.chmod(0600)
61
+ end
62
+
63
+ # Verify and return keypair & pemfile
64
+ keypair_check = check_keypair(keypair_name)
65
+ pemfile_check = pemfile_check(pemfile)
66
+ if keypair_check == true && pemfile_check == true
67
+ puts "Created keypair and pemfile for #{keypair_name}!"
68
+ return get_keypair(keypair_name)
69
+ else
70
+ abort("Error: Could not create keypair: #{keypair}") if keypair_check == false
71
+ abort("Error: Could not create pemfile: #{pemfile}") if pemfile_check == false
72
+ end
73
+ end
74
+
75
+ def delete_keypair(keypair_name)
76
+ # check for and delete key and .pem file
77
+ pemfile = pemfile_path(keypair_name)
78
+ pem_check = pemfile_check(pemfile)
79
+ kp_check = check_keypair(keypair_name)
80
+
81
+ @compute.delete_key_pair(keypair_name) if kp_check == true
82
+ File.delete(pemfile) if pem_check == true
83
+ end
84
+ end
@@ -0,0 +1,7 @@
1
+ answer = gets.chomp
2
+
3
+ if answer =~ /^y(es)?$/i
4
+ puts 'yes'
5
+ else
6
+ puts 'no'
7
+ end
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fog/openstack'
3
+ require '../../../../../config/danarchysys.config'
4
+ require '../../../helpers'
5
+ require '../prompts'
6
+
7
+ os_connection_params = Connection.os_dreamcompute
8
+ compute = Fog::Compute::OpenStack.new(os_connection_params)
9
+
10
+ p ComputePrompts.flavor(compute)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fog/openstack'
3
+ require '../../connection'
4
+ require_relative '../images'
5
+
6
+ @os_connection_params = Connection.os_dreamcompute
7
+ @compute = Fog::Compute::OpenStack.new(@os_connection_params)
8
+
9
+ p ComputeImages.image_list(@compute)
10
+
11
+ print "Which image should we test? : "
12
+ image_name = gets.chomp
13
+
14
+ p ComputeImages.image_get(@compute, image_name)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fog/openstack'
3
+ require '../../connection'
4
+ require '../instance_prompts'
5
+ require '../instance_params'
6
+ require '../instance_manage'
7
+
8
+ @os_connection_params = Connection.os_danarchy
9
+ @compute = Fog::Compute::OpenStack.new(@os_connection_params)
10
+
11
+ prompts = InstancePrompts.new
12
+ instance = prompts.create_instance(@compute)
13
+
14
+ p instance
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ require 'fog/openstack'
3
+ require '../../../../danarchy_sys'
4
+ require '../instances'
5
+
6
+ os_connection_params = Connection.os_dreamcompute
7
+ compute = Fog::Compute::OpenStack.new os_connection_params
8
+
9
+ p ComputeInstances.all_instances compute
10
+
11
+ puts 'Existing Instances:', ComputeInstances.list_all_instances(compute)
12
+ print 'Which instance should we check?: '
13
+ instance_name = gets.chomp
14
+
15
+ puts "Checking: #{instance_name}", ComputeInstances.check_instance(compute, instance_name)
16
+
17
+ puts "Getting: #{instance_name}"
18
+ instance = ComputeInstances.get_instance(compute, instance_name)
19
+
20
+ addresses = instance.addresses['public']
21
+ ipv6, ipv4 = addresses[0], addresses[1]
22
+ #ipv4 = addresses[1]
23
+ print 'ipv6: ', ipv6['addr'], "\n"
24
+ print 'ipv4: ', ipv4['addr'], "\n"