diff --git a/kvirt/cli.py b/kvirt/cli.py index d34269ebf..493ceedac 100755 --- a/kvirt/cli.py +++ b/kvirt/cli.py @@ -312,6 +312,9 @@ def console_vm(args): name = common.get_lastvm(config.client) if not args.name else args.name k = config.k tunnel = config.tunnel + tunnelhost = config.tunnelhost + tunnelport = config.tunnelport or 22 + tunneluser = config.tunneluser or 'root' if serial: k.serialconsole(name) elif web: @@ -320,7 +323,7 @@ def console_vm(args): sys.exit(1) config.webconsole(name) else: - k.console(name=name, tunnel=tunnel) + k.console(name=name, tunnel=tunnel, tunnelhost=tunnelhost, tunnelport=tunnelport, tunneluser=tunneluser) def console_container(args): @@ -2721,8 +2724,8 @@ def ssh_vm(args): name = [common.get_lastvm(baseconfig.client)] if not args.name else args.name tunnel = baseconfig.tunnel tunnelhost = baseconfig.tunnelhost - tunneluser = baseconfig.tunneluser tunnelport = baseconfig.tunnelport + tunneluser = baseconfig.tunneluser if tunnel and tunnelhost is None and baseconfig.type != 'kubevirt': error("Tunnel requested but no tunnelhost defined") sys.exit(1) @@ -2783,8 +2786,8 @@ def scp_vm(args): baseconfig = Kbaseconfig(client=args.client, debug=args.debug, quiet=True) tunnel = baseconfig.tunnel tunnelhost = baseconfig.tunnelhost - tunneluser = baseconfig.tunneluser tunnelport = baseconfig.tunnelport + tunneluser = baseconfig.tunneluser if tunnel and tunnelhost is None: error("Tunnel requested but no tunnelhost defined") sys.exit(1) diff --git a/kvirt/providers/aws/__init__.py b/kvirt/providers/aws/__init__.py index 4d6bb5937..764d6aef5 100644 --- a/kvirt/providers/aws/__init__.py +++ b/kvirt/providers/aws/__init__.py @@ -518,7 +518,7 @@ def list(self): continue return sorted(vms, key=lambda x: x['name']) - def console(self, name, tunnel=False, web=False): + def console(self, name, tunnel=False, tunnelhost=None, tunnelport=22, tunneluser='root', web=False): df = {'InstanceIds': [name]} if name.startswith('i-') else {'Filters': [{'Name': "tag:Name", 'Values': [name]}]} try: vm = self.conn.describe_instances(**df)['Reservations'][0]['Instances'][0] diff --git a/kvirt/providers/azure/__init__.py b/kvirt/providers/azure/__init__.py index ce7d4fdc9..ffb76a0ed 100644 --- a/kvirt/providers/azure/__init__.py +++ b/kvirt/providers/azure/__init__.py @@ -436,7 +436,7 @@ def list(self): return [] return sorted(vms, key=lambda x: x['name']) - def console(self, name, tunnel=False, web=False): + def console(self, name, tunnel=False, tunnelhost=None, tunnelport=22, tunneluser='root', web=False): self.serialconsole(name, web=web) def serialconsole(self, name, web=False): diff --git a/kvirt/providers/gcp/__init__.py b/kvirt/providers/gcp/__init__.py index 6540fb6bf..794f3ec91 100644 --- a/kvirt/providers/gcp/__init__.py +++ b/kvirt/providers/gcp/__init__.py @@ -560,7 +560,7 @@ def list(self): return [] return sorted(vms, key=lambda x: x['name']) - def console(self, name, tunnel=False, web=False): + def console(self, name, tunnel=False, tunnelhost=None, tunnelport=22, tunneluser='root', web=False): project = self.project zone = self.zone resource = build('cloudresourcemanager', 'v1') @@ -672,7 +672,6 @@ def info(self, name, vm=None, debug=False): disks = [] for index, disk in enumerate(vm['disks']): devname = disk['deviceName'] - diskname = os.path.basename(disk['source']) diskformat = disk['interface'] drivertype = disk['type'] path = os.path.basename(disk['source']) @@ -1479,8 +1478,8 @@ def info_flavor(self, name): project = self.project zone = self.zone if name in self.machine_flavor_cache: - flavor = self.machine_flavor_cache[name] - else: + flavor = self.machine_flavor_cache[name] + else: flavor = conn.machineTypes().get(project=project, zone=zone, machineType=name).execute() self.machine_flavor_cache[name] = flavor return {'cpus': flavor['guestCpus'], 'memory': flavor['memoryMb']} diff --git a/kvirt/providers/ibm/__init__.py b/kvirt/providers/ibm/__init__.py index 8a48298ea..b14b78101 100644 --- a/kvirt/providers/ibm/__init__.py +++ b/kvirt/providers/ibm/__init__.py @@ -363,7 +363,7 @@ def list(self): continue return sorted(vms, key=lambda x: x['name']) - def console(self, name, tunnel=False, web=False): + def console(self, name, tunnel=False, tunnelhost=None, tunnelport=22, tunneluser='root', web=False): try: vm = self._get_vm(name) if vm is None: diff --git a/kvirt/providers/kubevirt/__init__.py b/kvirt/providers/kubevirt/__init__.py index 5900dc732..78bb38bfa 100644 --- a/kvirt/providers/kubevirt/__init__.py +++ b/kvirt/providers/kubevirt/__init__.py @@ -621,7 +621,7 @@ def list(self): continue return sorted(vms, key=lambda x: x['name']) - def console(self, name, tunnel=False, web=False): + def console(self, name, tunnel=False, tunnelhost=None, tunnelport=22, tunneluser='root', web=False): if os.path.exists("/i_am_a_container"): error("This functionality is not supported in container mode") return diff --git a/kvirt/providers/kvm/__init__.py b/kvirt/providers/kvm/__init__.py index 2b474b98a..2a1091ac8 100644 --- a/kvirt/providers/kvm/__init__.py +++ b/kvirt/providers/kvm/__init__.py @@ -1632,7 +1632,7 @@ def list(self): continue return sorted(vms, key=lambda x: x['name']) - def console(self, name, tunnel=False, web=False): + def console(self, name, tunnel=False, tunnelhost=None, tunnelport=22, tunneluser='root', web=False): conn = self.conn try: vm = conn.lookupByName(name) diff --git a/kvirt/providers/openstack/__init__.py b/kvirt/providers/openstack/__init__.py index c7e23430b..fdf934d00 100644 --- a/kvirt/providers/openstack/__init__.py +++ b/kvirt/providers/openstack/__init__.py @@ -339,7 +339,7 @@ def list(self): continue return sorted(vms, key=lambda x: x['name']) - def console(self, name, tunnel=False, web=False): + def console(self, name, tunnel=False, tunnelhost=None, tunnelport=22, tunneluser='root', web=False): nova = self.nova try: vm = nova.servers.find(name=name) diff --git a/kvirt/providers/ovirt/__init__.py b/kvirt/providers/ovirt/__init__.py index 4e3f8c86c..bf1b7085f 100644 --- a/kvirt/providers/ovirt/__init__.py +++ b/kvirt/providers/ovirt/__init__.py @@ -449,7 +449,7 @@ def list(self): continue return sorted(vms, key=lambda x: x['name']) - def console(self, name, tunnel=False, web=False): + def console(self, name, tunnel=False, tunnelhost=None, tunnelport=22, tunneluser='root', web=False): connectiondetails = None vmsearch = self.vms_service.list(search=f'name={name}') if not vmsearch: diff --git a/kvirt/providers/packet/__init__.py b/kvirt/providers/packet/__init__.py index 6a17f782b..f367f027b 100644 --- a/kvirt/providers/packet/__init__.py +++ b/kvirt/providers/packet/__init__.py @@ -306,7 +306,7 @@ def list(self): continue return sorted(vms, key=lambda x: x['name']) - def console(self, name, tunnel=False, web=False): + def console(self, name, tunnel=False, tunnelhost=None, tunnelport=22, tunneluser='root', web=False): self.serialconsole(name, web=web) return diff --git a/kvirt/providers/proxmox/__init__.py b/kvirt/providers/proxmox/__init__.py index d4191cdca..33ccbabe9 100644 --- a/kvirt/providers/proxmox/__init__.py +++ b/kvirt/providers/proxmox/__init__.py @@ -1083,3 +1083,9 @@ def delete_security_group(self, name): def update_security_group(self, name, overrides={}): print("not implemented") return {'result': 'success'} + + def console(self, name, tunnel=False, tunnelhost=None, tunnelport=22, tunneluser='root', web=False): + print("not implemented") + + def serialconsole(self, name, web=False): + print("not implemented") diff --git a/kvirt/providers/sampleprovider.py b/kvirt/providers/sampleprovider.py index b92890cac..5cbad3891 100644 --- a/kvirt/providers/sampleprovider.py +++ b/kvirt/providers/sampleprovider.py @@ -82,7 +82,7 @@ def list(self): print("not implemented") return [] - def console(self, name, tunnel=False, web=False): + def console(self, name, tunnel=False, tunnelhost=None, tunnelport=22, tunneluser='root', web=False): print("not implemented") def serialconsole(self, name, web=False): diff --git a/kvirt/providers/vsphere/__init__.py b/kvirt/providers/vsphere/__init__.py index 48826da13..eb24b9f0c 100644 --- a/kvirt/providers/vsphere/__init__.py +++ b/kvirt/providers/vsphere/__init__.py @@ -667,7 +667,7 @@ def serialconsole(self, name, web=False): if not os.path.exists("/i_am_a_container"): call(consolecommand, shell=True) - def console(self, name, tunnel=False, web=False): + def console(self, name, tunnel=False, tunnelhost=None, tunnelport=22, tunneluser='root', web=False): si = self.si vcip = self.vcip vmFolder = self.basefolder @@ -691,9 +691,22 @@ def console(self, name, tunnel=False, web=False): else: continue if vncfound: + consolecommand = '' host = runtime.host.name + if tunnel and tunnelhost is not None: + localport = common.get_free_port() + consolecommand += "ssh -o LogLevel=QUIET -f -p " + consolecommand += f"{tunnelport} -L {localport}:{host}:{vncport} {tunneluser}@{tunnelhost} sleep 10;" + host = '127.0.0.1' + vncport = localport url = f"vnc://{host}:{vncport}" - consolecommand = f"remote-viewer {url} &" + if os.path.exists('/Applications'): + if os.path.exists('/Applications/VNC Viewer.app'): + consolecommand += f"open -a 'VNC Viewer' --args {url.replace('vnc://', '')} &" + else: + consolecommand += f"open -a 'Screen Sharing' {url} &" + else: + consolecommand += f"remote-viewer {url} &" if web: return url if self.debug or os.path.exists("/i_am_a_container"): diff --git a/kvirt/providers/web/__init__.py b/kvirt/providers/web/__init__.py index f30e5d018..0aca66157 100644 --- a/kvirt/providers/web/__init__.py +++ b/kvirt/providers/web/__init__.py @@ -128,7 +128,7 @@ def list(self): vms = response['vms'] return vms - def console(self, name, tunnel=False, web=False): + def console(self, name, tunnel=False, tunnelhost=None, tunnelport=22, tunneluser='root', web=False): url = f'{self.base}/vmconsole/{name}' pprint(f"Opening web console {url}") webbrowser.open(url, new=2, autoraise=True)