Skip to content

Commit

Permalink
vsphere: handle tunnel with console
Browse files Browse the repository at this point in the history
  • Loading branch information
karmab committed Jun 20, 2024
1 parent 114cac0 commit 5dbe3ad
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 19 deletions.
9 changes: 6 additions & 3 deletions kvirt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion kvirt/providers/aws/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion kvirt/providers/azure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 3 additions & 4 deletions kvirt/providers/gcp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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'])
Expand Down Expand Up @@ -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']}
Expand Down
2 changes: 1 addition & 1 deletion kvirt/providers/ibm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion kvirt/providers/kubevirt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion kvirt/providers/kvm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion kvirt/providers/openstack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion kvirt/providers/ovirt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion kvirt/providers/packet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions kvirt/providers/proxmox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
2 changes: 1 addition & 1 deletion kvirt/providers/sampleprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
17 changes: 15 additions & 2 deletions kvirt/providers/vsphere/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"):
Expand Down
2 changes: 1 addition & 1 deletion kvirt/providers/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5dbe3ad

Please sign in to comment.