Class: VagrantTeraTerm::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-teraterm/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



6
7
8
# File 'lib/vagrant-teraterm/command.rb', line 6

def self.synopsis
  "connects to machine via SSH using TeraTerm"
end

Instance Method Details

#absolute_winpath(vm, path) ⇒ Object



90
91
92
93
94
# File 'lib/vagrant-teraterm/command.rb', line 90

def absolute_winpath(vm, path)
  p = Pathname(path)
  return path.gsub(/\//, "\\") if p.absolute?
  return Pathname(vm.env.root_path).join(p).to_s.gsub(/\//, "\\")
end

#do_process(commands) ⇒ Object



96
97
98
99
# File 'lib/vagrant-teraterm/command.rb', line 96

def do_process(commands)
  pid = spawn(commands.join(" "))
  Process.detach(pid)
end

#executeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vagrant-teraterm/command.rb', line 10

def execute
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: vagrant teraterm [vm-name...]"

    opts.separator ""
  end

  argv = parse_options(opts)
  return -1 if !argv

  with_target_vms(argv, single_target: true) do |vm|
    @config = vm.config.teraterm

    ssh_info = vm.ssh_info
    @logger.debug("ssh_info is #{ssh_info}")
    # If ssh_info is nil, the machine is not ready for ssh.
    raise Vagrant::Errors::SSHNotReady if ssh_info.nil?

    exe_path = find_exe_path(@config.exe_path)
    return -1 if !exe_path

    commands = [
        "\"#{exe_path}\"",
        "#{ssh_info[:host]}",
        "#{ssh_info[:port]}",
        "/ssh",
        "/2",
        "/user=#{ssh_info[:username]}"
    ]


    if ssh_info.include?(:password)
      commands << "/passwd=#{ssh_info[:password]}"
      commands << "/auth=password"
    elsif ssh_info.include?(:private_key_path)
      Array(ssh_info[:private_key_path]).each do |p|
        commands << "/keyfile=#{File.expand_path(p)}"
      end
      commands << "/auth=publickey"
    end

    commands << "/ssh-A" if ssh_info[:forward_agent]
    commands << "/ssh-X" if ssh_info[:forward_x11]
    commands << "/f=\"#{absolute_winpath(vm, @config.ini_path)}\"" if not @config.ini_path.nil?
    commands << "/L=\"#{absolute_winpath(vm, @config.log_path)}\"" if not @config.log_path.nil?
    commands << "/M=\"#{absolute_winpath(vm, @config.macro_path)}\"" if not @config.macro_path.nil?
    commands << @config.extra_args if not @config.extra_args.nil?

    @logger.debug(commands)
    do_process(commands)
  end
end

#find_exe_path(path) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vagrant-teraterm/command.rb', line 63

def find_exe_path(path)
  if not path.nil?
    return path if File.executable?(path)

    # search in PATH
    ENV['PATH'].split(File::PATH_SEPARATOR).each do |p|
      _p = Pathname(p) + path
      return _p.to_s if _p.executable?
    end
  else
    # search in PATH
    ENV['PATH'].split(File::PATH_SEPARATOR).each do |p|
      _p = Pathname(p) + 'ttermpro.exe'
      return _p.to_s if _p.executable?
    end

    # Program Files
    ['C:\Program Files (x86)\teraterm\ttermpro.exe',
     'C:\Program Files\teraterm\ttermpro.exe'].each do |p|
       return p if File.executable?(p)
    end
  end

  @env.ui.error("File is not found or executable. => #{path}")
  nil
end