2014-04-12 22:40:27 +00:00
|
|
|
require "optparse"
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module CommandRDP
|
|
|
|
class Command < Vagrant.plugin("2", :command)
|
|
|
|
def self.synopsis
|
|
|
|
"connects to machine via RDP"
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2014-05-08 01:32:20 +00:00
|
|
|
options = {}
|
|
|
|
|
2014-04-12 22:40:27 +00:00
|
|
|
opts = OptionParser.new do |o|
|
2014-05-08 01:32:20 +00:00
|
|
|
o.banner = "Usage: vagrant rdp [options] [name] [-- extra args]"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Parse out the extra args to send to the RDP client, which
|
|
|
|
# is everything after the "--"
|
|
|
|
split_index = @argv.index("--")
|
|
|
|
if split_index
|
|
|
|
options[:extra_args] = @argv.drop(split_index + 1)
|
|
|
|
@argv = @argv.take(split_index)
|
2014-04-12 22:40:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Parse the options and return if we don't have any target.
|
|
|
|
argv = parse_options(opts)
|
|
|
|
return if !argv
|
|
|
|
|
|
|
|
# Check if the host even supports RDP
|
|
|
|
raise Errors::HostUnsupported if !@env.host.capability?(:rdp_client)
|
|
|
|
|
|
|
|
# Execute RDP if we can
|
|
|
|
with_target_vms(argv, single_target: true) do |machine|
|
2014-04-12 22:51:07 +00:00
|
|
|
if !machine.communicate.ready?
|
|
|
|
raise Vagrant::Errors::VMNotCreatedError
|
|
|
|
end
|
|
|
|
|
2014-04-12 23:25:31 +00:00
|
|
|
machine.ui.output(I18n.t("vagrant_rdp.detecting"))
|
2014-04-12 23:16:57 +00:00
|
|
|
rdp_info = get_rdp_info(machine)
|
2014-04-12 23:22:35 +00:00
|
|
|
raise Errors::RDPUndetected if !rdp_info
|
2014-04-12 23:14:58 +00:00
|
|
|
|
2014-05-08 01:32:20 +00:00
|
|
|
# Extra arguments if we have any
|
|
|
|
rdp_info[:extra_args] = options[:extra_args]
|
|
|
|
|
2014-04-12 23:25:31 +00:00
|
|
|
machine.ui.detail(
|
|
|
|
"Address: #{rdp_info[:host]}:#{rdp_info[:port]}")
|
|
|
|
machine.ui.detail("Username: #{rdp_info[:username]}")
|
|
|
|
|
|
|
|
machine.ui.success(I18n.t("vagrant_rdp.connecting"))
|
2014-04-12 23:14:58 +00:00
|
|
|
@env.host.capability(:rdp_client, rdp_info)
|
2014-04-12 22:40:27 +00:00
|
|
|
end
|
|
|
|
end
|
2014-04-12 23:16:57 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def get_rdp_info(machine)
|
2014-05-21 03:17:21 +00:00
|
|
|
rdp_info = {}
|
|
|
|
if machine.provider.capability?(:rdp_info)
|
|
|
|
rdp_info = machine.provider.capability(:rdp_info)
|
2014-05-21 03:18:15 +00:00
|
|
|
rdp_info ||= {}
|
2014-05-21 03:17:21 +00:00
|
|
|
end
|
|
|
|
|
2014-04-12 23:16:57 +00:00
|
|
|
ssh_info = machine.ssh_info
|
2014-05-21 03:17:21 +00:00
|
|
|
|
|
|
|
if !rdp_info[:username]
|
|
|
|
username = ssh_info[:username]
|
|
|
|
if machine.config.vm.communicator == :winrm
|
|
|
|
username = machine.config.winrm.username
|
|
|
|
end
|
|
|
|
rdp_info[:username] = username
|
2014-04-12 23:16:57 +00:00
|
|
|
end
|
2014-10-27 17:21:02 +00:00
|
|
|
|
|
|
|
if !rdp_info[:password]
|
2014-10-27 17:23:00 +00:00
|
|
|
password = ssh_info[:password]
|
2014-10-27 17:21:02 +00:00
|
|
|
if machine.config.vm.communicator == :winrm
|
2014-10-27 17:23:00 +00:00
|
|
|
password = machine.config.winrm.password
|
2014-10-27 17:21:02 +00:00
|
|
|
end
|
2014-10-27 17:23:00 +00:00
|
|
|
rdp_info[:password] = password
|
2014-10-27 17:21:02 +00:00
|
|
|
end
|
|
|
|
|
2014-05-21 03:17:21 +00:00
|
|
|
rdp_info[:host] ||= ssh_info[:host]
|
|
|
|
rdp_info[:port] ||= machine.config.rdp.port
|
2014-04-12 23:16:57 +00:00
|
|
|
|
2014-05-21 03:17:21 +00:00
|
|
|
if rdp_info[:host] == "127.0.0.1"
|
2014-04-12 23:16:57 +00:00
|
|
|
# We need to find a forwarded port...
|
2014-04-12 23:37:44 +00:00
|
|
|
search_port = machine.config.rdp.search_port
|
2014-04-12 23:16:57 +00:00
|
|
|
ports = nil
|
|
|
|
if machine.provider.capability?(:forwarded_ports)
|
|
|
|
ports = machine.provider.capability(:forwarded_ports)
|
|
|
|
else
|
|
|
|
ports = {}.tap do |result|
|
|
|
|
machine.config.vm.networks.each do |type, netopts|
|
|
|
|
next if type != :forwarded_port
|
|
|
|
next if !netopts[:host]
|
|
|
|
result[netopts[:host]] = netopts[:guest]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ports = ports.invert
|
|
|
|
port = ports[search_port]
|
2014-05-21 03:17:21 +00:00
|
|
|
rdp_info[:port] = port
|
2014-04-12 23:16:57 +00:00
|
|
|
return nil if !port
|
|
|
|
end
|
|
|
|
|
2014-05-21 03:17:21 +00:00
|
|
|
return rdp_info
|
2014-04-12 23:16:57 +00:00
|
|
|
end
|
2014-04-12 22:40:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|