Class: RSpecSystem::NodeSet::Base Abstract
- Inherits:
-
Object
- Object
- RSpecSystem::NodeSet::Base
- Defined in:
- lib/rspec-system/node_set/base.rb
Overview
Subclass and override methods to create a new NodeSet variant.
Base class for a NodeSet driver. If you want to create a new driver, you should sub-class this and override all the methods below.
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#destroy ⇒ Object
readonly
Returns the value of attribute destroy.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#setname ⇒ Object
readonly
Returns the value of attribute setname.
Instance Method Summary collapse
-
#default_node ⇒ RSpecSystem::Node
Return default node.
-
#env_type ⇒ Object
Return environment type.
-
#initialize(setname, config, options) ⇒ Base
constructor
Create new NodeSet, populating necessary data structures.
-
#randmac ⇒ String
Return a random mac address.
-
#random_string(length = 50) ⇒ String
Return a random string of chars, used for temp dir creation.
-
#rcp(options) ⇒ Object
Copy a file to the host in the NodeSet.
-
#run(options) ⇒ Object
Run a command on a host in the NodeSet.
-
#setup ⇒ Object
Setup the NodeSet by starting all nodes.
-
#ssh_exec!(ssh, command) ⇒ Hash
Execute command via SSH.
-
#teardown ⇒ Object
Shutdown the NodeSet by shutting down or pausing all nodes.
-
#tmppath ⇒ String
Generates a random string for use in remote transfers.
Constructor Details
#initialize(setname, config, options) ⇒ Base
Create new NodeSet, populating necessary data structures.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rspec-system/node_set/base.rb', line 13 def initialize(setname, config, ) @setname = setname @config = config @destroy = [:destroy] @nodes = {} config['nodes'].each do |k,v| @nodes[k] = RSpecSystem::Node.node_from_yaml(self, k, v) end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/rspec-system/node_set/base.rb', line 7 def config @config end |
#destroy ⇒ Object (readonly)
Returns the value of attribute destroy.
10 11 12 |
# File 'lib/rspec-system/node_set/base.rb', line 10 def destroy @destroy end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
9 10 11 |
# File 'lib/rspec-system/node_set/base.rb', line 9 def nodes @nodes end |
#setname ⇒ Object (readonly)
Returns the value of attribute setname.
8 9 10 |
# File 'lib/rspec-system/node_set/base.rb', line 8 def setname @setname end |
Instance Method Details
#default_node ⇒ RSpecSystem::Node
Return default node
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rspec-system/node_set/base.rb', line 52 def default_node dn = config['default_node'] if dn.nil? if nodes.length == 1 dn = nodes.first[1] return dn else raise "No default node" end else return nodes[dn] end end |
#env_type ⇒ Object
Return environment type
45 46 47 |
# File 'lib/rspec-system/node_set/base.rb', line 45 def env_type self.class::ENV_TYPE end |
#randmac ⇒ String
Return a random mac address
136 137 138 |
# File 'lib/rspec-system/node_set/base.rb', line 136 def randmac "080027" + (1..3).map{"%0.2X"%rand(256)}.join end |
#random_string(length = 50) ⇒ String
Return a random string of chars, used for temp dir creation
69 70 71 72 |
# File 'lib/rspec-system/node_set/base.rb', line 69 def random_string(length = 50) o = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten (0...length).map{ o[rand(o.length)] }.join end |
#rcp(options) ⇒ Object
Copy a file to the host in the NodeSet.
40 41 42 |
# File 'lib/rspec-system/node_set/base.rb', line 40 def rcp() raise "Unimplemented method #rcp" end |
#run(options) ⇒ Object
Run a command on a host in the NodeSet.
35 36 37 |
# File 'lib/rspec-system/node_set/base.rb', line 35 def run() raise "Unimplemented method #run" end |
#setup ⇒ Object
Setup the NodeSet by starting all nodes.
25 26 27 |
# File 'lib/rspec-system/node_set/base.rb', line 25 def setup raise "Unimplemented method #setup" end |
#ssh_exec!(ssh, command) ⇒ Hash
Execute command via SSH.
A special version of exec! from Net::SSH that returns exit code and exit signal as well. This method is blocking.
91 92 93 94 95 96 97 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 |
# File 'lib/rspec-system/node_set/base.rb', line 91 def ssh_exec!(ssh, command) r = { :stdout => '', :stderr => '', :exit_code => nil, :exit_signal => nil, } ssh.open_channel do |channel| channel.exec(command) do |ch, success| unless success abort "FAILED: couldn't execute command (ssh.channel.exec)" end channel.on_data do |ch,data| d = data print d r[:stdout]+=d end channel.on_extended_data do |ch,type,data| d = data print d r[:stderr]+=d end channel.on_request("exit-status") do |ch,data| c = data.read_long puts "Exit code: #{c}" r[:exit_code] = c end channel.on_request("exit-signal") do |ch, data| s = data.read_string puts "Exit signal: #{s}" r[:exit_signal] = s end end end ssh.loop r end |
#teardown ⇒ Object
Shutdown the NodeSet by shutting down or pausing all nodes.
30 31 32 |
# File 'lib/rspec-system/node_set/base.rb', line 30 def teardown raise "Unimplemented method #teardown" end |
#tmppath ⇒ String
Very Linux dependant, probably need to consider OS X and Windows at least.
Generates a random string for use in remote transfers.
79 80 81 |
# File 'lib/rspec-system/node_set/base.rb', line 79 def tmppath '/tmp/' + random_string end |