create_images.py: Add support for nested KVM on Linux and automatic rebooting during setup for Windows
diff --git a/buildkite/create_images.py b/buildkite/create_images.py
index b251294..9b4be33 100755
--- a/buildkite/create_images.py
+++ b/buildkite/create_images.py
@@ -44,6 +44,9 @@
             'install-python36.sh',
             'install-android-sdk.sh',
             'shutdown.sh'
+        ],
+        'licenses': [
+            'https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx'
         ]
     },
     'buildkite-ubuntu1604': {
@@ -61,6 +64,9 @@
             'install-python36.sh',
             'install-android-sdk.sh',
             'shutdown.sh'
+        ],
+        'licenses': [
+            'https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx'
         ]
     },
     # 'buildkite-windows2016': {
@@ -182,9 +188,14 @@
     run(['gcloud', 'compute', 'instances', 'delete', '--quiet', vm])
 
 
-def create_image(vm, target_image_family):
-    run(['gcloud', 'compute', 'images', 'create', vm, '--family',
-         target_image_family, '--source-disk', vm, '--source-disk-zone', LOCATION])
+def create_image(vm, params):
+    cmd = ['gcloud', 'compute', 'images', 'create', vm]
+    cmd.extend(['--family', params['target_image_family']])
+    cmd.extend(['--source-disk', vm])
+    cmd.extend(['--source-disk-zone', LOCATION])
+    for license in params.get('licenses', []):
+        cmd.extend(['--licenses', license])
+    run(cmd)
 
 
 # https://stackoverflow.com/a/25802742
@@ -209,6 +220,11 @@
         print('Use this password to connect to the Windows VM: ' + pw['password'])
         print('Please run the setup script C:\\setup.ps1 once you\'re logged in.')
 
+    # Wait until the VM reboots once, then open RDP again.
+    tail_serial_console(vm, until='Finished running startup scripts')
+    write_to_clipboard(pw['password'])
+    run(['open', rdp_file])
+
 
 def workflow(name, params):
     vm = "%s-image-%s" % (name, int(datetime.now().timestamp()))
@@ -221,7 +237,7 @@
 
         if 'windows' in vm:
             # Wait for VM to be ready, then print setup instructions.
-            tail_serial_console(vm, until='Instance setup finished.')
+            tail_serial_console(vm, until='Finished running startup scripts')
             print_windows_instructions(vm)
 
         # Continuously print the serial console.
@@ -231,7 +247,7 @@
         wait_for_vm(vm, 'TERMINATED')
 
         # Create a new image from our VM.
-        create_image(vm, params['target_image_family'])
+        create_image(vm, params)
     finally:
         delete_vm(vm)