Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- ### usage
- # virt-viewer-client.py name_of_the_vm
- import os
- import ovirtsdk.api
- import ovirtsdk.xml
- import subprocess
- import tempfile
- import urllib
- import sys
- import time
- # The parameters to connect to the engine:
- engine_host = "self-hosted"
- engine_port = 443
- engine_user = "admin@internal"
- engine_password = "PassworD"
- engine_cer_file = "ca.pem"
- # The name of the vm to connect
- vm_name = sys.argv[1]
- # A template to generate the viewer configuration file:
- config_template = """\
- [virt-viewer]
- type={type}
- host={host}
- port={port}
- password={password}
- tls-port={tls_port}
- fullscreen=2
- title={title}
- enable-smartcard=0
- enable-usbredir=1
- enable-usb-autoshare=1
- usb-filter=-1,-1,-1,-1,-1
- delete-this-file=1
- tls-ciphers=DEFAULT
- host-subject={tls_subject}
- ca={ca}
- toggle-fullscreen=shift+f11
- release-cursor=shift+f12
- secure-channels=main;inputs;cursor;playback;record;display;usbredir;smartcard
- """
- # Download the CA certificate, as we need to pass this to the viewer so that it
- # will trust the SSL certificate of the host:
- ca_url = "http://cert/ca.pem"
- #ca_url = "https://{host}:{port}/ovirt-engine/services/pki-resource?resource=ca-#certificate&format=X509-PEM-CA".format(
- # host=engine_host,
- # port=engine_port
- #)
- ca_path, _ = urllib.urlretrieve(ca_url)
- with open(ca_path, "r") as ca_file:
- ca_content = ca_file.read()
- ca_content = ca_content.replace("\n", "\\n")
- ## Bajo e Certificado CA para que la API funque OK!
- ## y la dejo en la variable que tengo configurada antes
- urllib.urlretrieve(ca_url,engine_cer_file)
- # Connect to the API:
- api_url = "https://{host}:{port}/ovirt-engine/api".format(
- host=engine_host,
- port=engine_port
- )
- api = ovirtsdk.api.API(
- url=api_url,
- username=engine_user,
- password=engine_password,
- ca_file=engine_cer_file,
- insecure=False,
- #filter=True,
- debug=True
- )
- # Find the VM and get the display details:
- vm = api.vms.get(name=vm_name, all_content=True)
- #vm = api.vms.get(name=vm_name, all_content=False)
- display = vm.get_display()
- try:
- if vm.status.state != 'up':
- print 'Starting VM'
- vm.start()
- print 'Waiting for VM to reach Up status...'
- while vm.status.state == 'down':
- time.sleep(10)
- vm = api.vms.get(name=vm_name, all_content=True)
- display = vm.get_display()
- else:
- print 'VM already up'
- except Exception as e:
- print 'Failed to Start VM:\n%s' % str(e)
- # Request a ticket for the display of the VM:
- ticket_result = vm.ticket()
- ticket = ticket_result.get_ticket()
- # Create the viewer configuration:
- config_content = config_template.format(
- type=display.get_type(),
- host=display.get_address(),
- port=display.get_port(),
- password=ticket.get_value(),
- tls_port=display.get_secure_port(),
- title=vm_name,
- tls_subject=display.get_certificate().get_subject(),
- ca=ca_content
- )
- config_fd, config_path = tempfile.mkstemp()
- with os.fdopen(config_fd, "w") as config_file:
- config_file.write(config_content)
- #subprocess.call(["remote-viewer", config_path])
- time.sleep(5)
- # Run the viewer:
- #subprocess.call(["remote-viewer", config_path])
- subprocess.call(["remote-viewer", "-k", "--kiosk-quit=on-disconnect", config_path])
- subprocess.call("poweroff")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement