danarchy_sys 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5c3ad8faf86550ad951bf1bb6b8f4e25220264628373ba1da95fe0b1fac942f
4
- data.tar.gz: 78d4d8bebebd00e899aa25840a6ce96b6d70f0bef9e3a8cd0611dddc90e396c1
3
+ metadata.gz: 425cdec809b087727a5dd9c9f6f4bf6752d1e23f29021921f5350c22201f8e46
4
+ data.tar.gz: a0a09bd3bca283330e9597d3ebbb1e1b753dff3d5e956ae14f3e427c5bc395ec
5
5
  SHA512:
6
- metadata.gz: 5f8e7e628ac733cf73a28295f73e821541e8f2ce1769351b6151abf2327e4a575809abe0bb1af33b755afc2b72deac7570a2972303e3869b3bda174a9d3ad5f0
7
- data.tar.gz: 6de2f4c30e92333830e293a5b5e0f08f7602561f9e540c7457c0a4201549b37104bedc1a6c175596f264dc18278ee3e7e0f1cab022d71aa0c1bdbb41223a774b
6
+ metadata.gz: 04a1a995b50c94b7237aabdcae302767835282189cf94c95c703442f4cc8a6f4e97d6f29a448d4521a63544ef4eae9b5b5200eb24c026cf3e7bd5ab56c1b6522
7
+ data.tar.gz: c2f02632a979e7b6381103fa6c191e6c6ccb3e53d0bc87a8d1f95908095ebf1f5fa7b5e29638d360e2765de5fed3773038e945575c00c330e06e75738fffef0b
data/bin/danarchy_sys CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ require_relative '../lib/danarchy_sys'
2
3
  require_relative '../lib/danarchy_sys/cli'
3
4
 
4
5
  DanarchySys::CLI.new
@@ -46,7 +46,11 @@ class InstanceManager
46
46
  end
47
47
  elsif cmd == 'status'
48
48
  instance = @os_compute.instances.get_instance(instance.name)
49
- printf("%#{instance.name.size}s %0s %0s\n", instance.name, ' => ', instance.state)
49
+ if instance.state == 'ACTIVE' && @os_compute.ssh(instance, 'uptime')[:stderr]
50
+ printf("%#{instance.name.size}s %0s %0s\n", instance.name, ' => ', 'WAITING')
51
+ else
52
+ printf("%#{instance.name.size}s %0s %0s\n", instance.name, ' => ', instance.state)
53
+ end
50
54
  elsif %w(pause unpause suspend resume start stop).include?(cmd.to_s)
51
55
  status = instance.state
52
56
 
@@ -74,15 +78,14 @@ class InstanceManager
74
78
  print "Should we rebuild #{instance.name} with image: #{image.name}? (Y/N): "
75
79
  if gets.chomp =~ /^y(es)?$/i
76
80
  puts "Rebuilding #{instance.name} with #{image.name}"
77
- @os_compute.instances.rebuild_instance(instance, image)
78
- instance = @os_compute.instances.get_instance(instance.name)
81
+ instance = @os_compute.instances.rebuild_instance(instance, image)
79
82
  puts "\nRebuild in progress!"
80
83
  else
81
84
  puts "Not rebuilding #{instance.name} at this time."
82
85
  end
83
86
  elsif cmd == 'connect'
84
87
  if instance.state == 'ACTIVE'
85
- @os_compute.ssh(instance.name)
88
+ @os_compute.ssh(instance.name)
86
89
  else
87
90
  puts "Unable to connect: #{instance.name} is not running!"
88
91
  end
@@ -1,14 +1,11 @@
1
-
2
- require_relative '../danarchy_sys'
1
+ require_relative 'cli/menus'
2
+ require_relative 'cli/accounts'
3
+ require_relative 'cli/instance_manager'
4
+ require_relative 'cli/keypair_manager'
3
5
 
4
6
  module DanarchySys
5
7
  class CLI
6
8
  def initialize
7
- require_relative 'cli/menus'
8
- require_relative 'cli/accounts'
9
- require_relative 'cli/instance_manager'
10
- require_relative 'cli/keypair_manager'
11
-
12
9
  danarchysys_config = DanarchySys::ConfigManager::Config.new
13
10
  account = Accounts.chooser(danarchysys_config)
14
11
  connection = danarchysys_config[:accounts][account]
@@ -110,18 +110,6 @@ class ComputeInstances
110
110
  instance
111
111
  end
112
112
 
113
- def rebuild_instance(instance, image)
114
- instance = get_instance(instance) if instance.class == String
115
-
116
- instance.rebuild(image.id, instance.name)
117
- addrs = [get_public_addresses(instance),
118
- get_private_addresses(instance)].flatten.compact!
119
- addrs.each { |addr| system("ssh-keygen -R #{addr} &>/dev/null") }
120
-
121
- # instance.wait_for { ready? }
122
- instance
123
- end
124
-
125
113
  def delete_instance(instance)
126
114
  instance = get_instance(instance) if instance.class == String
127
115
  return 1 if instance == false
@@ -142,4 +130,24 @@ class ComputeInstances
142
130
 
143
131
  return true
144
132
  end
133
+
134
+ def rebuild_instance(instance, image)
135
+ instance = get_instance(instance) if instance.class == String
136
+
137
+ instance.rebuild(image.id, instance.name)
138
+ addrs = [get_public_addresses(instance),
139
+ get_private_addresses(instance)].flatten.compact!
140
+ addrs.each { |addr| system("ssh-keygen -R #{addr} &>/dev/null") }
141
+
142
+ # instance.wait_for { ready? }
143
+ get_instance(instance.name)
144
+ end
145
+
146
+ def ssh_connector(instance)
147
+ addrs = get_public_addresses(instance.name)
148
+ { ipv4: addrs.grep(/\./).first,
149
+ ipv6: addrs.grep(/:/).first,
150
+ ssh_user: user,
151
+ ssh_key: pemfile }
152
+ end
145
153
  end
@@ -2,6 +2,7 @@ require_relative 'compute/instances'
2
2
  require_relative 'compute/keypairs'
3
3
  require_relative 'compute/images'
4
4
  require_relative 'compute/flavors'
5
+ require 'shellwords'
5
6
 
6
7
  module DanarchySys
7
8
  module OpenStack
@@ -34,19 +35,17 @@ module DanarchySys
34
35
  ComputeSecgroups.new(@compute)
35
36
  end
36
37
 
37
- def ssh(instance_name)
38
- (comp_inst, comp_kp, comp_img) = instances, keypairs, images
39
- instance = comp_inst.get_instance(instance_name)
40
- keypair_name = instance.key_name
41
- pemfile = comp_kp.pemfile_path(keypair_name)
42
-
43
- addrs = comp_inst.get_public_addresses(instance_name)
44
- ipv4 = addrs.grep(/\./).first
45
- ipv6 = addrs.grep(/:/).first
38
+ def ssh(instance, *cmd)
39
+ instance = instances.get_instance(instance) if instance.class == String
40
+ opts = { quiet: true }
41
+ opts[:command] = cmd ? cmd.shift : nil
42
+ pemfile = keypairs.pemfile_path(instance.key_name)
43
+ addrs = instances.get_public_addresses(instance)
44
+ # (instances, keypairs, images) = instances, keypairs, images
45
+ # instance = instances.get_instance(instance)
46
46
 
47
47
  # Define user by image_id
48
- image_id = instance.image['id']
49
- image = comp_img.get_image_by_id(image_id)
48
+ image = images.get_image_by_id(instance.image['id'])
50
49
 
51
50
  ssh, user = nil
52
51
  if image == nil
@@ -63,12 +62,16 @@ module DanarchySys
63
62
 
64
63
  return if !user
65
64
 
66
- print "Connecting as user: #{user} @ #{ipv4} " + cmd + "\n"
67
- connect = "/usr/bin/ssh -o ConnectTimeout=15 -o StrictHostKeyChecking=no -o PasswordAuthentication=no -i '#{pemfile}' #{user}@#{ipv4}"
68
- system(connect)
65
+ connector = { ipv4: addrs.grep(/\./).first,
66
+ ipv6: addrs.grep(/:/).first,
67
+ ssh_user: user,
68
+ ssh_key: pemfile }
69
+
70
+ SSH.new(connector, opts)
69
71
  end
70
72
 
71
73
  def fallback_ssh(ipv4, pemfile)
74
+ # This needs some updating to utilize the SSH module as above.
72
75
  users = %w[debian ubuntu centos fedora core]
73
76
  ssh, user = nil
74
77
 
@@ -1,4 +1,3 @@
1
-
2
1
  require 'fog/openstack'
3
2
 
4
3
  module DanarchySys
@@ -0,0 +1,35 @@
1
+ require 'open3'
2
+ require 'shellwords'
3
+
4
+ class SSH
5
+ def self.new(connector, opts = {})
6
+ # connector: { ipv4: "str", ssh_user: "str", ssh_key: "str" }
7
+ # options: { command: "str", timeout: int, quiet: true|false }
8
+ opts[:timeout] ||= 30
9
+ opts[:quiet] ||= false
10
+ pid, stdout, stderr = nil
11
+ ssh = "ssh -i '#{connector[:ssh_key]}' #{connector[:ssh_user]}@#{connector[:ipv4]} "
12
+ ssh += "-o StrictHostKeyChecking=no "
13
+ ssh += "-o ConnectTimeout=#{opts[:timeout]} " if opts[:timeout]
14
+
15
+ if opts[:command]
16
+ puts "Running '#{opts[:command]}' on '#{connector[:ipv4]}'" unless opts[:quiet]
17
+ ssh += Shellwords.shellescape(opts[:command])
18
+
19
+ Open3.popen3(ssh) do |i, o, e, t|
20
+ pid = t.pid
21
+ (out, err) = o.read, e.read
22
+ stdout = !out.empty? ? out : nil
23
+ stderr = !err.empty? ? err : nil
24
+ end
25
+ else
26
+ return system(ssh)
27
+ end
28
+
29
+ if opts[:quiet] == false
30
+ puts "------\nErrored at: #{caller_locations.first.label} Line: #{caller_locations.first.lineno}\nSTDERR: ", stderr, '------' if stderr
31
+ puts "------\nSTDOUT: ", stdout, '------' if stdout
32
+ end
33
+ { pid: pid, stdout: stdout, stderr: stderr }
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module DanarchySys
2
- VERSION = '0.5.2'
2
+ VERSION = '0.5.3'
3
3
  end
data/lib/danarchy_sys.rb CHANGED
@@ -1,7 +1,6 @@
1
-
2
- require 'fog/openstack'
3
1
  require_relative 'danarchy_sys/version'
4
2
  require_relative 'danarchy_sys/helpers'
3
+ require_relative 'danarchy_sys/ssh'
5
4
  require_relative 'danarchy_sys/config_manager'
6
5
  require_relative 'danarchy_sys/printformats'
7
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danarchy_sys
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan James
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-18 00:00:00.000000000 Z
11
+ date: 2018-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fog-openstack
@@ -114,6 +114,7 @@ files:
114
114
  - lib/danarchy_sys/openstack/compute/tests/keypairs_test.rb
115
115
  - lib/danarchy_sys/openstack/compute/tests/manage_test.rb
116
116
  - lib/danarchy_sys/printformats.rb
117
+ - lib/danarchy_sys/ssh.rb
117
118
  - lib/danarchy_sys/version.rb
118
119
  homepage: https://github.com/danarchy85/danarchy_sys
119
120
  licenses: