Class: Host::Default
- Inherits:
-
Object
- Object
- Host::Default
- Defined in:
- lib/host/default.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#alias ⇒ Object
readonly
Returns the value of attribute alias.
-
#host_name ⇒ Object
readonly
Returns the value of attribute host_name.
-
#ssh_pem ⇒ Object
readonly
Returns the value of attribute ssh_pem.
-
#tags ⇒ Object
Returns the value of attribute tags.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(host = 'unspecified', info = {}, opts = {}) ⇒ Default
constructor
A new instance of Default.
- #matches?(tags) ⇒ Boolean
- #scp_to_host(src, dest) ⇒ Object
- #shell!(opts = nil) ⇒ Object
- #shell_exec!(command, opts = {}) ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(host = 'unspecified', info = {}, opts = {}) ⇒ Default
Returns a new instance of Default.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/host/default.rb', line 6 def initialize( host = 'unspecified', info = {}, opts = {} ) @alias = host @host_name = info[ "HostName" ] || nil @user = info[ "User" ] || Etc.getlogin @ssh_pem = info[ "IdentityFile" ] || nil @ssh_port = info[ "Port" ] || nil @type = info[ "Type" ] || :default = info[ "Tags" ] || {} @pem_dirs = opts[ "IdentityLocations" ] || nil end |
Instance Attribute Details
#alias ⇒ Object (readonly)
Returns the value of attribute alias.
4 5 6 |
# File 'lib/host/default.rb', line 4 def alias @alias end |
#host_name ⇒ Object (readonly)
Returns the value of attribute host_name.
4 5 6 |
# File 'lib/host/default.rb', line 4 def host_name @host_name end |
#ssh_pem ⇒ Object (readonly)
Returns the value of attribute ssh_pem.
4 5 6 |
# File 'lib/host/default.rb', line 4 def ssh_pem @ssh_pem end |
#tags ⇒ Object
Returns the value of attribute tags.
4 5 6 |
# File 'lib/host/default.rb', line 4 def end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
4 5 6 |
# File 'lib/host/default.rb', line 4 def user @user end |
Instance Method Details
#matches?(tags) ⇒ Boolean
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/host/default.rb', line 27 def matches?( ) .each do | tag, value | is_regex = value.match( /^\/(.*)\/$/ ) if is_regex regex = Regexp.new( is_regex[ 1 ] ) unless .has_key?( tag ) && [ tag ].match( regex ) return false end else unless .has_key?( tag ) && [ tag ] == value return false end end end true end |
#scp_to_host(src, dest) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/host/default.rb', line 85 def scp_to_host( src, dest ) scp_cmd = [ "scp" ] raise( IOError, "Error: HostName invalid." ) if @host_name.nil? scp_cmd << [ "-i", ssh_pem ] unless ssh_pem.nil? scp_cmd << "#{ src } #{ @user }@#{ @host_name }:#{ dest }" begin `#{ scp_cmd.join(" ") }` rescue => e raise( IOError, "Could not call scp. #{ e.message }" ) end end |
#shell!(opts = nil) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/host/default.rb', line 67 def shell!( opts = nil ) ssh_cmd = [ "ssh" ] raise( IOError, "Error: HostName invalid." ) if @host_name.nil? ssh_cmd << [ "-l", @user ] ssh_cmd << @host_name pem = ssh_pem ssh_cmd << [ "-i", pem ] unless pem.nil? begin exec( ssh_cmd.join(" ") ) rescue => e raise( IOError, "Could not call ssh." ) end end |
#shell_exec!(command, opts = {}) ⇒ Object
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/host/default.rb', line 100 def shell_exec!( command, opts = {} ) = { :keys => ssh_pem } color = Color.random_color begin Net::SSH.start( host_name, user, ) do | s | channel = s.open_channel do |ch| channel.request_pty do |ch, success| if success puts "pty successfully obtained" else puts "could not obtain pty" end end if opts.has_key? :pty ch.exec( command ) do | ch, success | raise( IOError, "#{ host_name } > could not execute command" ) unless success ch.on_data do | c, data | data.split("\n").each do | line | puts "#{ Color.print( self.alias, [ :bold, color ] ) } > #{ line }" end end ch.on_extended_data do |c, type, data| data.split("\n").each do | line | puts "#{ Color.print( self.alias, [ :bold, color ] ) } > #{ line }" end end ch.on_close do puts "#{ Color.print( self.alias, [ :bold, color ] ) } > COMMAND finished" end end end end rescue => e puts "Error: #{ self.alias } > #{ e.message }".error end end |
#type ⇒ Object
18 19 20 |
# File 'lib/host/default.rb', line 18 def type @type.to_sym end |