class Object
Public Class Methods
new(node)
click to toggle source
Calls superclass method
# File lib/inprovise/channel/ssh.rb, line 14 def initialize(node) super(node) @connection = nil @sftp = nil end
Public Instance Methods
admin_user()
click to toggle source
platform properties
# File lib/inprovise/helper/cygwin.rb, line 16 def admin_user 'administrator' end
binary_exists?(bin)
click to toggle source
# File lib/inprovise/helper/linux.rb, line 166 def binary_exists?(bin) exec("which #{bin}") =~ /\/#{bin}/ ? true : false end
cat(path)
click to toggle source
# File lib/inprovise/helper/linux.rb, line 66 def cat(path) path = real_path(path) begin @channel.content(path) rescue exec("cat #{path}") end end
close()
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 20 def close disconnect end
connection()
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 154 def connection return @connection if @connection && !@connection.closed? @connection = Net::SSH.start(@node.host, @node.user, options_for_ssh) end
content(path)
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 73 def content(path) @node.log.remote("SFTP.READ: #{path}") if Inprovise.verbosity > 1 sftp.file.open(path) do |io| return io.read end end
copy(from, to)
click to toggle source
# File lib/inprovise/helper/linux.rb, line 112 def copy(from, to) exec("cp #{real_path(from)} #{real_path(to)}") end
cwd()
click to toggle source
# File lib/inprovise/helper/linux.rb, line 26 def cwd @cwd || plain_run('pwd').chomp end
delete(path)
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 80 def delete(path) @node.log.remote("SFTP.DELETE: #{path}") if Inprovise.verbosity > 1 sftp.delete!(path) if exists?(path) end
directory?(path)
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 68 def directory?(path) @node.log.remote("SFTP.DIRECTORY?: #{path}") if Inprovise.verbosity > 1 path_check(path, :directory) end
disconnect()
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 159 def disconnect @connection.close if @connection && !@connection.closed? end
download(from, to)
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 37 def download(from, to) @node.log.remote("SFTP.DOWNLOAD: #{to} <= #{from}") if Inprovise.verbosity > 1 sftp.download!(from, to) end
echo(arg)
click to toggle source
basic commands
# File lib/inprovise/helper/linux.rb, line 62 def echo(arg) exec("echo #{arg}") end
env_reference(varname)
click to toggle source
# File lib/inprovise/helper/cygwin.rb, line 20 def env_reference(varname) "\$#{varname}" end
execute(cmd, forcelog=false)
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 138 def execute(cmd, forcelog=false) @node.log.remote("SSH: #{cmd}") if Inprovise.verbosity > 1 || forcelog output = '' begin connection.exec! cmd do |_channel, stream, data| output << data if stream == :stdout @node.log.send(stream, data, forcelog) if Inprovise.verbosity > 1 || forcelog end rescue Net::SSH::Exception => ex raise Inprovise::CmdChannel::Exception, "#{ex.message}" ensure @node.log.flush_all if Inprovise.verbosity > 1 || forcelog end output end
exists?(path)
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 58 def exists?(path) @node.log.remote("SFTP.EXISTS?: #{path}") if Inprovise.verbosity > 1 path_check(path) end
file?(path)
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 63 def file?(path) @node.log.remote("SFTP.FILE?: #{path}") if Inprovise.verbosity > 1 path_check(path, :regular) end
hash_for(path)
click to toggle source
# File lib/inprovise/helper/linux.rb, line 75 def hash_for(path) path = real_path(path) exec("sha1sum #{path}")[0...40] end
mkdir(path)
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 42 def mkdir(path) @node.log.remote("SFTP.MKDIR: #{path}") if Inprovise.verbosity > 1 sftp.mkdir!(path) end
move(from, to)
click to toggle source
# File lib/inprovise/helper/linux.rb, line 116 def move(from, to) exec("mv #{real_path(from)} #{real_path(to)}") end
options_for_ssh()
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 120 def options_for_ssh opts = [ :auth_methods, :compression, :compression_level, :config, :encryption , :forward_agent , :global_known_hosts_file , :hmac , :host_key , :host_key_alias , :host_name, :kex , :keys , :key_data , :keys_only , :logger , :paranoid , :passphrase , :password , :port , :properties , :proxy , :rekey_blocks_limit , :rekey_limit , :rekey_packet_limit , :timeout , :user , :user_known_hosts_file , :verbose, :verify_host_key ] ssh_cfg = (@node.config[:ssh] || {}).reduce({}) do |hsh, (k,v)| if opts.include?(k) hsh[k] = (k == :verify_host_key ? v.to_sym : v) end hsh end (@node.config[:credentials] || {}).reduce(ssh_cfg) do |hsh, (k,v)| hsh[k] = v if k == :password || k == :passphrase hsh end end
owner(path)
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 100 def owner(path) @node.log.remote("SFTP.OWNER: #{path}") if Inprovise.verbosity > 1 begin result = sftp.stat!(path) {:user => result.owner, :group => result.group} rescue Net::SFTP::StatusException => ex raise ex unless ex.code == Net::SFTP::Response::FX_NO_SUCH_FILE nil end end
path_check(path, type=nil)
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 47 def path_check(path, type=nil) begin stat = sftp.stat!(path) stat != nil && (type.nil? || stat.symbolic_type == type) rescue Net::SFTP::StatusException => ex raise ex unless ex.code == Net::SFTP::Response::FX_NO_SUCH_FILE false end end
permissions(path)
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 85 def permissions(path) @node.log.remote("SFTP.PERMISSIONS: #{path}") if Inprovise.verbosity > 1 begin sftp.stat!(path).permissions & 0x0FFF rescue Net::SFTP::StatusException => ex raise ex unless ex.code == Net::SFTP::Response::FX_NO_SUCH_FILE 0 end end
plain_run(cmd, forcelog=false)
click to toggle source
# File lib/inprovise/helper/linux.rb, line 181 def plain_run(cmd, forcelog=false) @channel.run(cmd, forcelog) end
real_path(path)
click to toggle source
# File lib/inprovise/helper/linux.rb, line 172 def real_path(path) return File.expand_path(path, @cwd) if @cwd path end
run(command, forcelog=false)
click to toggle source
command execution
# File lib/inprovise/channel/ssh.rb, line 26 def run(command, forcelog=false) execute(command, forcelog) end
script(name, &definition)
click to toggle source
# File lib/inprovise/script.rb, line 139 def script(name, &definition) Inprovise.log.local("Adding provisioning script #{name}") if Inprovise.verbosity > 1 Inprovise.add_script(Inprovise::Script.new(name)) do |script| Inprovise::Script::DSL.new(script).instance_eval(&definition) end end
set_cwd(path)
click to toggle source
# File lib/inprovise/helper/linux.rb, line 30 def set_cwd(path) old_cwd = @cwd @cwd = path ? File.expand_path(path, self.cwd) : path old_cwd end
set_owner(path, user, group=nil)
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 111 def set_owner(path, user, group=nil) @node.log.remote("SFTP.SET_OWNER: #{path} #{user} #{group}") if Inprovise.verbosity > 1 attrs = { :owner => user } attrs.merge({ :group => group }) if group sftp.setstat!(path, attrs) end
set_permissions(path, perm)
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 95 def set_permissions(path, perm) @node.log.remote("SFTP.SETPERMISSIONS: #{path} #{'%o' % perm}") if Inprovise.verbosity > 1 sftp.setstat!(path, :permissions => perm) end
sftp()
click to toggle source
# File lib/inprovise/channel/ssh.rb, line 163 def sftp @sftp ||= connection.sftp.connect end
sudo()
click to toggle source
generic command execution
# File lib/inprovise/helper/cygwin.rb, line 26 def sudo return self if channel.node.user == admin_user unless @sudo @sudo = channel.node.for_user(admin_user, "sudo:#{channel.node.user}").helper end @sudo.set_cwd(self.cwd) @sudo.channel.node.log_to(channel.node.log.clone_for_node(@sudo.channel.node)) @sudo end
sudo_run(cmd, forcelog=false)
click to toggle source
# File lib/inprovise/helper/linux.rb, line 185 def sudo_run(cmd, forcelog=false) @channel.run(%{sudo sh -c "#{cmd}"}, forcelog) end
upload(from, to)
click to toggle source
file management
# File lib/inprovise/channel/ssh.rb, line 32 def upload(from, to) @node.log.remote("SFTP.UPLOAD: #{from} => #{to}") if Inprovise.verbosity > 1 sftp.upload!(from, to) end
Private Instance Methods
exec(cmd, forcelog=false)
click to toggle source
# File lib/inprovise/helper/linux.rb, line 177 def exec(cmd, forcelog=false) send(@exec, cmd, forcelog) end