Class: Freighthop::CLI::SSH

Inherits:
Object
  • Object
show all
Defined in:
lib/freighthop/cli/ssh.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ SSH

Returns a new instance of SSH.



8
9
10
# File 'lib/freighthop/cli/ssh.rb', line 8

def initialize(*args)
  @args = args
end

Class Method Details

.match?(*args) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/freighthop/cli/ssh.rb', line 4

def self.match?(*args)
  !args.empty? # we handle guest passthrough
end

Instance Method Details

#app_nameObject



65
66
67
# File 'lib/freighthop/cli/ssh.rb', line 65

def app_name
  Freighthop.app_name
end

#configObject



29
30
31
32
33
34
35
# File 'lib/freighthop/cli/ssh.rb', line 29

def config
  config_path.tap do |conf|
    if !conf.exist? || (Time.now - conf.mtime) > 86400
      File.write(conf, ssh_config)
    end
  end
end

#config_pathObject



57
58
59
# File 'lib/freighthop/cli/ssh.rb', line 57

def config_path
  Pathname("/tmp/freighthop.#{app_name}.ssh-config")
end

#guest_rootObject



69
70
71
# File 'lib/freighthop/cli/ssh.rb', line 69

def guest_root
  Freighthop.guest_root
end

#hostnameObject



61
62
63
# File 'lib/freighthop/cli/ssh.rb', line 61

def hostname
  Freighthop.hostname
end

#runObject



12
13
14
15
16
17
18
19
# File 'lib/freighthop/cli/ssh.rb', line 12

def run
  Freighthop::CLI::Checks.ensure_config_exists!
  if shell?
    ssh('-i')
  else
    ssh(%Q(-c "#{Shellwords.join(@args)}"))
  end
end

#shell?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/freighthop/cli/ssh.rb', line 21

def shell?
  @args.first == 'ssh'
end

#ssh(cmd) ⇒ Object



25
26
27
# File 'lib/freighthop/cli/ssh.rb', line 25

def ssh(cmd)
  exec %Q(ssh -q -t -F #{config} #{app_name} 'cd #{guest_root}; /bin/bash -l #{cmd}')
end

#ssh_configObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/freighthop/cli/ssh.rb', line 37

def ssh_config
  <<-SSH_CONFIG.gsub(/^      /, '')
    Host #{app_name}
      HostName #{hostname}
      User vagrant
      Port 22
      UserKnownHostsFile /dev/null
      StrictHostKeyChecking no
      PasswordAuthentication no
      IdentityFile #{vagrant_ssh_key_path}
      IdentitiesOnly yes
      LogLevel FATAL
      ForwardAgent yes
  SSH_CONFIG
end

#vagrant_ssh_key_pathObject



53
54
55
# File 'lib/freighthop/cli/ssh.rb', line 53

def vagrant_ssh_key_path
  Pathname('~/.vagrant.d/insecure_private_key').expand_path
end