Class: HostManager
- Inherits:
-
Object
- Object
- HostManager
- Defined in:
- lib/hostmanager.rb
Overview
Hostmanager
Summary
A class and command line utility that allows simple management of ssh hosts and remote folders. E.g. logging in to a remote server becomes as easy as ‘hm l y’.
Examples
hm add m myname@myhost
hm x m
hm list
hm scp m:my_file h:.
hm rsync -o av m:source/path h:dest/path
Command Manual
Commands
hm add <letter> [email protected] --- Add a host to be referred to with <letter>
hm list -- List all hosts
hm l <letter> -- Login to a host with ssh
hm x <letter> -- Login to a host with ssh -X
hm scp <letter1>:<file1> <letter2>:<file2> -- Copy file1 from host letter1 to host letter2
hm rsync <letter1>:<path1> <letter2>:<path2> -- Sync path1 on host
letter1 to path2 on host letter2 using rsync
hm sshfs <letter>:<path> <localpath> -- Mount path on host letter to localpath using sshfs
Options
-z --- host is a zero conf host
-o <optionstring> -- Add optionstring to command
-T -- 'test': print out the resulting command without running it
Instance Attribute Summary collapse
-
#content_list ⇒ Object
Returns the value of attribute content_list.
-
#forwards ⇒ Object
Returns the value of attribute forwards.
-
#host_list ⇒ Object
Returns the value of attribute host_list.
-
#hosts ⇒ Object
Returns the value of attribute hosts.
-
#option_string ⇒ Object
Returns the value of attribute option_string.
Instance Method Summary collapse
- #add(letter, hosturl) ⇒ Object
- #both_remote?(*letters) ⇒ Boolean
- #host(letter) ⇒ Object
-
#initialize(hash = {}) ⇒ HostManager
constructor
A new instance of HostManager.
- #inspect ⇒ Object
- #login(letter) ⇒ Object
- #port_forward(via_letter, destination_letter) ⇒ Object
- #rsync(from_letter, from_folder, to_letter, to_folder) ⇒ Object
- #rsync_scp(letter) ⇒ Object
- #scp(from_letter, from_folder, to_letter, to_folder) ⇒ Object
- #ssh(letter) ⇒ Object
- #sshfs(letter, remote_folder = nil, local_folder) ⇒ Object
- #user_name(letter) ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ HostManager
Returns a new instance of HostManager.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/hostmanager.rb', line 164 def initialize(hash={}) hash.each do |key, value| self.class.send(:attr_accessor, key) set(key, value) end @host_list ||= {} @host_list['h'] = '' @host_list['l'] = ENV['USER'] + '@localhost' @host_list.each do |letter, string| string.sub!(/:$/, '') end @forwards ||= {} @content_list ||= {} @hosts = {} @start_port ||= 29800 @host_list.each do |letter, host_string| if @forwards[letter] @hosts[letter] = Host::Forwarded.from_string(host_string, @forwards[letter]) unless (string = "#{@hosts[letter].ssh} cd . 2>/dev/null"; system string) # puts 'port failed' @forwards.delete(letter) @hosts[letter] = Host.from_string(host_string) else # puts 'port passed' end # exit elsif host_string == nil host_string = @host_list[letter] = "" redo else @hosts[letter] = Host.from_string(host_string) end end end |
Instance Attribute Details
#content_list ⇒ Object
Returns the value of attribute content_list.
163 164 165 |
# File 'lib/hostmanager.rb', line 163 def content_list @content_list end |
#forwards ⇒ Object
Returns the value of attribute forwards.
163 164 165 |
# File 'lib/hostmanager.rb', line 163 def forwards @forwards end |
#host_list ⇒ Object
Returns the value of attribute host_list.
163 164 165 |
# File 'lib/hostmanager.rb', line 163 def host_list @host_list end |
#hosts ⇒ Object
Returns the value of attribute hosts.
163 164 165 |
# File 'lib/hostmanager.rb', line 163 def hosts @hosts end |
#option_string ⇒ Object
Returns the value of attribute option_string.
163 164 165 |
# File 'lib/hostmanager.rb', line 163 def option_string @option_string end |
Instance Method Details
#add(letter, hosturl) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/hostmanager.rb', line 198 def add(letter, hosturl) raise 'A host label can only be one letter or symbol' unless letter.length == 1 case letter when "h" puts "h can only be used to mean 'home', i.e. an empty string" return when "l" puts "l can only be used to mean $USER@localhost" return else (puts 'This host already exists. Do you want to replace it? Press enter to replace it or Ctrl + C to cancel.'; $stdin.gets) if host_list[letter] host_list[letter] = hosturl end end |
#both_remote?(*letters) ⇒ Boolean
226 227 228 |
# File 'lib/hostmanager.rb', line 226 def both_remote?(*letters) return letters.inject(true){|bool, letter| bool and @hosts[letter].remote?} end |
#host(letter) ⇒ Object
248 249 250 |
# File 'lib/hostmanager.rb', line 248 def host(letter) return @hosts[letter].host end |
#inspect ⇒ Object
213 214 215 216 217 218 219 220 221 |
# File 'lib/hostmanager.rb', line 213 def inspect @hosts = "Don't edit this variable" hash = instance_variables.inject({}) do |hash, var| name = var.to_s.sub(/@/, '').to_sym hash[name] = instance_variable_get(var) hash end return "HostManager.new(\n#{hash.pretty_inspect.gsub(/\n/, "\n\t")}\n)" end |
#login(letter) ⇒ Object
222 223 224 225 |
# File 'lib/hostmanager.rb', line 222 def login(letter) Host.ssh_option_string = @option_string return @hosts[letter].ssh end |
#port_forward(via_letter, destination_letter) ⇒ Object
243 244 245 246 247 |
# File 'lib/hostmanager.rb', line 243 def port_forward(via_letter, destination_letter) raise "Port forwarding already active for #{destination_letter}" if @forwards[destination_letter] @forwards[destination_letter] = @start_port + destination_letter.ord %[#{@hosts[via_letter].ssh} -L #{@forwards[destination_letter]}:#{@hosts[destination_letter].host}:22] end |
#rsync(from_letter, from_folder, to_letter, to_folder) ⇒ Object
236 237 238 239 240 241 242 |
# File 'lib/hostmanager.rb', line 236 def rsync(from_letter, from_folder, to_letter, to_folder) if both_remote?(from_letter, to_letter) return "#{@hosts[to_letter].ssh} rsync #@option_string #{@hosts[from_letter].rsync_scp_remote}#{from_folder} #{@hosts[to_letter].rsync_scp_local}#{to_folder}" else return "rsync #@option_string #{@hosts[from_letter].rsync_scp_remote}#{from_folder} #{@hosts[to_letter].rsync_scp_remote}#{to_folder}" end end |
#rsync_scp(letter) ⇒ Object
260 261 262 |
# File 'lib/hostmanager.rb', line 260 def rsync_scp(letter) return @hosts[letter].rsync_scp_remote end |
#scp(from_letter, from_folder, to_letter, to_folder) ⇒ Object
229 230 231 232 233 234 235 |
# File 'lib/hostmanager.rb', line 229 def scp(from_letter, from_folder, to_letter, to_folder) if both_remote?(from_letter, to_letter) return "#{@hosts[to_letter].ssh} scp #@option_string #{@hosts[from_letter].rsync_scp_remote}#{from_folder} #{@hosts[to_letter].rsync_scp_local}#{to_folder}" else return "scp #@option_string #{@hosts[from_letter].rsync_scp_remote}#{from_folder} #{@hosts[to_letter].rsync_scp_remote}#{to_folder}" end end |
#ssh(letter) ⇒ Object
257 258 259 |
# File 'lib/hostmanager.rb', line 257 def ssh(letter) return @hosts[letter].ssh end |
#sshfs(letter, remote_folder = nil, local_folder) ⇒ Object
254 255 256 |
# File 'lib/hostmanager.rb', line 254 def sshfs(letter, remote_folder = nil, local_folder) return @hosts[letter].sshfs + "#{remote_folder} #{local_folder}" end |
#user_name(letter) ⇒ Object
251 252 253 |
# File 'lib/hostmanager.rb', line 251 def user_name(letter) return @hosts[letter].user_name end |