Creating a Simple OS Installation ProgramIn this section, we will write a simple program that can be used to automatically install a hypothetical operating system inside a brand new virtual machine. Step 1 - Preparation First, we have to capture all screens that the OS installation wizard displays to the user.
Step 2 - Writing the automated OS installation program Now that we have screenshots and interaction instructions, we can write the program that will automatically install the OS on any blank virtual machine.
Bit-by-bit comparison notes In theory, the screen comparison procedure described above (comparing two full screens) should work. In reality, it is virtually impossible to achieve a state when an individual screen remains absolutely static. Such things as blinking cursor, messages from the program vendor, and other animations will make each capture different from another. Therefore, instead of comparing an entire screen, the following approach can be used:
Example The following sample program illustrates the implementation of the steps above. Please note that this is not a complete working program. The program does not include the implementation of the algorithm described in the Bit-by-bit comparison subsection (above). An implementation of the algorithm depends on the image format used and in any case should be simple and straightforward. Some of the steps in the program are simplified, specifically the import sys import prlsdkapi
consts = prlsdkapi.prlsdk.consts
if len(sys.argv) != 3: print "Usage: install_os <VM_name> <path_to_iso>" exit()
# Initialize the Parallels API library. prlsdkapi.init_server_sdk()
# Create a server object. server = prlsdkapi.Server()
# Log in. try: "10.30.18.99", "root", "qawsed", consts.PSL_NORMAL_SECURITY result = server.login("10.30.18.99", "root", "qawsed", '', 0, 0, consts.PSL_HIGH_SECURITY).wait() except prlsdkapi.prlsdk.PrlSDKError, e: print "Login error: %s" % e exit()
# Get a list of virtual machines. # Find the specified virtual machine and # obtain an object identifying it. try: result = server.get_vm_list().wait() except prlsdkapi.PrlSDKError, e: print "Error: %s" % e exit()
found = False for i in range(result.get_params_count()): VM = result.get_param_by_index(i) if VM.get_name() == sys.argv[1]: found = True break
if found == False: print "Specified virtual machine not found." exit()
# Obtain an object identifying the # CD/DVD drive cdrom = VM.get_optical_disk(0)
# Begin the virtual machine editing operation. VM.begin_edit()
# Mount the OS installation ISO image. cdrom.set_emulated_type(consts.PDT_USE_IMAGE_FILE) cdrom.set_sys_name(sys.argv[2]) cdrom.set_image_path(sys.argv[2])
# Commit the changes to the virtual machine. VM.commit()
# Start the virtual machine. VM.start().wait()
# Instantiate the prlsdkapi.VmIO class. vm_io = prlsdkapi.VmIO()
# Begin a Remote Desktop Access session. try: vm_io.connect_to_vm(VM).wait() except prlsdkapi.PrlSDKError, e: print "Error: %s" % e exit()
# Define the name of the file to save the # captured screen data to. current_screen = "current.bmp"
# Set the reference screenshot count to 0. ref_count = 0
# Define the names of files containing # reference screenshots. ref_files = ['1.bmp', '2.bmp', '3.bmp']
# Define the keyboard keys that will be used # to interact with the remote desktop. keys = ['enter', 'f1', 'enter']
# Capture the virtual machine screen, find the matching # screen in the list of reference files and send the # appropriate keyboard command to the virtual machine. # Repeat for all screens. while True: # Get a reference file name. ref_screen = ref_files[ref_count]
# Capture the current virtual machine desktop screen and save it # into a file as a BMP image. # The parameters are: # Target file name # X coordinate # Y coordinate # Width (-1 for full screen) # Height (-1 for full screen) # Image format vm_io.sync_capture_screen_region_to_file(VM, current_screen, \ consts.PIF_BMP)
# Do a bit-by-bit comparison of the captured screen and # the reference screen for this iteration. # The actual comparison procedure depends on the data format used. # The bb_cmp() function DOES NOT exist in this sample program. # You will have to implement it yourself. if bb_cmp(current_screen, ref_screen): print "%d screen valid" % ref_count
# Press the appropriate key. press = consts.PKE_PRESS release = consts.PKE_RELEASE key = keys[ref_count] key = key.upper()
# Determine the key scan code based on its name. # The codes are defined in the ScanCodesList constant. # For the complete list of codes, start Python from the command line, # import the prlsdkapi module, and issue the # "print prlsdkapi.prlsdk.consts.ScanCodesList" statement. scan_code = consts.ScanCodesList[key]
# Send the key command to the virtual machine. vm_io.send_key_event(VM, scan_code, press) vm_io.send_key_event(VM, scan_code, release)
# If still have reference files to process, continue, otherwise, exit. if ref_count < (len(ref_files) - 1): ref_count = ref_count + 1 else: print "Os is installed." break
# End the Remote Desktop Access session. vm_io.disconnect_from_vm(VM)
# Stop the virtual machine. VM.stop().wait()
# Logoff and deinitialize the library. server.logoff() prlsdkapi.deinit_sdk() |
|||
|