Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. ### usage
  4. # virt-viewer-client.py name_of_the_vm
  5.  
  6. import os
  7. import ovirtsdk.api
  8. import ovirtsdk.xml
  9. import subprocess
  10. import tempfile
  11. import urllib
  12. import sys
  13. import time
  14.  
  15.  
  16. # The parameters to connect to the engine:
  17. engine_host = "self-hosted"
  18. engine_port = 443
  19. engine_user = "admin@internal"
  20. engine_password = "PassworD"
  21. engine_cer_file = "ca.pem"
  22.  
  23. # The name of the vm to connect
  24. vm_name = sys.argv[1]
  25.  
  26. # A template to generate the viewer configuration file:
  27. config_template = """\
  28. [virt-viewer]
  29. type={type}
  30. host={host}
  31. port={port}
  32. password={password}
  33. tls-port={tls_port}
  34. fullscreen=2
  35. title={title}
  36. enable-smartcard=0
  37. enable-usbredir=1
  38. enable-usb-autoshare=1
  39. usb-filter=-1,-1,-1,-1,-1
  40. delete-this-file=1
  41. tls-ciphers=DEFAULT
  42. host-subject={tls_subject}
  43. ca={ca}
  44. toggle-fullscreen=shift+f11
  45. release-cursor=shift+f12
  46. secure-channels=main;inputs;cursor;playback;record;display;usbredir;smartcard
  47. """
  48.  
  49. # Download the CA certificate, as we need to pass this to the viewer so that it
  50. # will trust the SSL certificate of the host:
  51.  
  52. ca_url = "http://cert/ca.pem"
  53. #ca_url = "https://{host}:{port}/ovirt-engine/services/pki-resource?resource=ca-#certificate&format=X509-PEM-CA".format(
  54. #    host=engine_host,
  55. #    port=engine_port
  56. #)
  57. ca_path, _ = urllib.urlretrieve(ca_url)
  58. with open(ca_path, "r") as ca_file:
  59.     ca_content = ca_file.read()
  60. ca_content = ca_content.replace("\n", "\\n")
  61.  
  62. ## Bajo e Certificado CA para que la API funque OK!
  63. ## y la dejo en la variable que tengo configurada antes
  64. urllib.urlretrieve(ca_url,engine_cer_file)
  65.  
  66. # Connect to the API:
  67. api_url = "https://{host}:{port}/ovirt-engine/api".format(
  68.     host=engine_host,
  69.     port=engine_port
  70. )
  71. api = ovirtsdk.api.API(
  72.   url=api_url,
  73.   username=engine_user,
  74.   password=engine_password,
  75.   ca_file=engine_cer_file,
  76.   insecure=False,
  77.   #filter=True,
  78.   debug=True
  79. )
  80.  
  81.  
  82. # Find the VM and get the display details:
  83. vm = api.vms.get(name=vm_name, all_content=True)
  84. #vm = api.vms.get(name=vm_name, all_content=False)
  85. display = vm.get_display()
  86.  
  87. try:
  88.     if vm.status.state != 'up':
  89.         print 'Starting VM'
  90.         vm.start()
  91.         print 'Waiting for VM to reach Up status...'
  92.         while vm.status.state == 'down':
  93.             time.sleep(10)
  94.         vm = api.vms.get(name=vm_name, all_content=True)
  95.         display = vm.get_display()
  96.     else:
  97.         print 'VM already up'
  98. except Exception as e:
  99.     print 'Failed to Start VM:\n%s' % str(e)
  100.  
  101. # Request a ticket for the display of the VM:
  102. ticket_result = vm.ticket()
  103. ticket = ticket_result.get_ticket()
  104.  
  105. # Create the viewer configuration:
  106. config_content = config_template.format(
  107.     type=display.get_type(),
  108.     host=display.get_address(),
  109.     port=display.get_port(),
  110.     password=ticket.get_value(),
  111.     tls_port=display.get_secure_port(),
  112.     title=vm_name,
  113.     tls_subject=display.get_certificate().get_subject(),
  114.     ca=ca_content
  115. )
  116. config_fd, config_path = tempfile.mkstemp()
  117. with os.fdopen(config_fd, "w") as config_file:
  118.     config_file.write(config_content)
  119. #subprocess.call(["remote-viewer", config_path])
  120.  
  121. time.sleep(5)
  122. # Run the viewer:
  123. #subprocess.call(["remote-viewer", config_path])
  124. subprocess.call(["remote-viewer", "-k", "--kiosk-quit=on-disconnect", config_path])
  125. subprocess.call("poweroff")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement