<commit_before>#!/usr/bin/env python

## Copyright (c) 2014 Citrix Systems, Inc. All Rights Reserved.
## You may only reproduce, distribute, perform, display, or prepare derivative works of this file pursuant to a valid license from Citrix.

from ConfigParser import ConfigParser
from lib.hyperv import HyperV # the class
from lib.hyperv import hyperv # the connection
from lib.cloudstack import cs
import json
import hashlib
import ntpath
import os
import pprint
import sys
import time

# setup the conf object and set default values...
conf = ConfigParser()
conf.add_section('HYPERV')
conf.set('HYPERV', 'migration_input_file', './input/migrate_hyperv_input.json')
conf.set('HYPERV', 'pscp_exe', 'C:\pscp.exe')

# read in config files if they exists
conf.read(['./settings.conf', './running.conf'])

if not conf.has_section('STATE'):
	conf.add_section('STATE') # STATE config section to maintain state of the running process
if not conf.has_option('STATE', 'exported'):
	conf.set('STATE', 'exported', '[]') # parsed with: json.loads(conf.get('STATE', 'exported'))
if not conf.has_option('STATE', 'imported'):
	conf.set('STATE', 'imported', '[]') # parsed with: json.loads(conf.get('STATE', 'imported'))
if not conf.has_option('STATE', 'started'):
	conf.set('STATE', 'started', '[]') # parsed with: json.loads(conf.get('STATE', 'started'))
if not conf.has_option('STATE', 'vms'):
	conf.set('STATE', 'vms', '[]') # parsed with: json.loads(conf.get('STATE', 'vms'))


# copy vhd files to the file server
def copy_vhd_to_file_server(vhd_path, vhd_name):
	return hyperv.powershell('%s -l %s -pw %s "%s" %s:%s/%s' % (
		conf.get('HYPERV', 'pscp_exe'),
		conf.get('FILESERVER', 'username'),
		conf.get('FILESERVER', 'password'),
		vhd_path,
		conf.get('FILESERVER', 'host'),
		conf.get('FILESERVER', 'files_path'),
		vhd_name
	))


# run the actual migration
def do_migration():
	# comment out the following line to keep a history of the requests over multiple runs of this file.
	open(conf.get('HYPERV', 'log_file'), 'w').close() # cleans the powershell requests log before execution so it only includes this run.
	open(conf.get('CLOUDSTACK', 'log_file'), 'w').close() # cleans the cloudstack requests log before execution so it only includes this run.

	vm_input = []
	if os.path.exists(conf.get('HYPERV', 'migration_input_file')):
		with open(conf.get('HYPERV', 'migration_input_file'), 'r') as f:
			try:
				vm_input = json.load(f)
			except:
				print sys.exc_info()
				sys.exit("Error in the formatting of '%s'" % (conf.get('HYPERV', 'migration_input_file')))

	print('\n-----------------------\n-- RUNNING VM EXPORT --\n-----------------------')
	# collect data about the VMs from HyperV and populate a list of VMs

	# initialize the 'vms' variable from the existing config...
	vms = json.loads(conf.get('STATE', 'vms'))

	if vm_input: # make sure there is data in the file
		for vm_in in vm_input: # loop through the vms in the file
			# make sure the minimum fields were entered and they have not been processed already
			vm_id = hashlib.sha1(vm_in['hyperv_server']+"|"+vm_in['hyperv_vm_name']).hexdigest()
			if 'hyperv_vm_name' in vm_in and 'hyperv_server' in vm_in and vm_id not in json.loads(conf.get('STATE', 'exported')):
				objs, ok = hyperv.powershell('Get-VM -Name "%s" -Server "%s"' % (vm_in['hyperv_vm_name'], vm_in['hyperv_server']))
				if objs and ok: # make sure it found the specified VM
					print('\nEXPORTING %s\n%s' % (vm_in['hyperv_vm_name'], '----------'+'-'*len(vm_in['hyperv_vm_name'])))

					exported = False
					vm_raw = objs[0]
					vm_out = vm_in
					vm_out['id'] = vm_id
					
					# get cores
					cpu, ok = hyperv.powershell('Get-VMCPUCount -VM "%s" -Server "%s"' % (vm_in['hyperv_vm_name'], vm_in['hyperv_server']))
					if ok:
						vm_out['cores'] = int(cpu[0]['ProcessorsPerSocket']) * int(cpu[0]['SocketCount'])
					else:
						print('Get-VMCPUCount powershell command failed on %s' % (vm_in['hyperv_vm_name']))
						print('ERROR: Check the "%s" log for details' % (conf.get('HYPERV', 'log_file')))

					# get memory
					memory, ok = hyperv.powershell('Get-VMMemory -VM "%s" -Server "%s"' % (vm_in['hyperv_vm_name'], vm_in['hyperv_server']))
					if ok:
						vm_out['memory'] = int(memory[0]['Reservation'])
					else:
						print('Get-VMMemory powershell command failed on %s' % (vm_in['hyperv_vm_name']))
						print('ERROR: Check the "%s" log for details' % (conf.get('HYPERV', 'log_file')))

					# record their starting state and bring down if running
					if int(vm_raw['EnabledState']) == HyperV.VM_RUNNING:
						vm_out['state'] = 'running'
						print('VM %s is Running' % (vm_in['hyperv_vm_name']))
						status, ok = hyperv.powershell('Stop-VM -VM "%s" -Server "%s" -Wait -Force' % (vm_in['hyperv_vm_name'], vm_in['hyperv_server']))
						if ok:
							print('Stopped %s' % (vm_in['hyperv_vm_name']))
						else:
							print('Stop-VM powershell command failed on %s' % (vm_in['hyperv_vm_name']))
							print('ERROR: Check the "%s" log for details' % (conf.get('HYPERV', 'log_file')))
					elif int(vm_raw['EnabledState']) == HyperV.VM_STOPPED:
						vm_out['state'] = 'stopped'
						print('VM %s is Stopped' % (vm_in['hyperv_vm_name']))
					else: # this should be improved...
						vm_out['state'] = 'unknown'
						print('VM %s is in an Unknown state' % (vm_in['hyperv_vm_name']))

					if (vm_out['state'] == 'running' and ok) or vm_out['state'] == 'stopped':
						disks, ok = hyperv.powershell('Get-VMDisk -VM "%s"' % (vm_in['hyperv_vm_name']))
						if ok:
							vm_out['disks'] = []
							for disk in disks:
								if 'DriveName' in disk and disk['DriveName'] == 'Hard Drive' and 'DiskImage' in disk:
									vm_out['disks'].append({
										'name':ntpath.split(disk['DiskImage'])[1].replace(' ', '-').split('.')[0],
										'url':'%s://%s:%s%s%s' % (
											'https' if conf.get('FILESERVER', 'port') == '443' else 'http',
											conf.get('FILESERVER', 'host'),
											conf.get('FILESERVER', 'port'),
											conf.get('FILESERVER', 'base_uri'),
											ntpath.split(disk['DiskImage'])[1].replace(' ', '-')
											)
										})
									print('Copying drive %s' % (disk['DiskImage']))
									exported = True
									#result, ok = copy_vhd_to_file_server(disk['DiskImage'], ntpath.split(disk['DiskImage'])[1].replace(' ', '-'))
									#if ok:
									#	print('Finished copy...')
									#	exported = True
									#else:
									#	print('Copy failed...')
									#	print('ERROR: Check the "%s" log for details' % (conf.get('HYPERV', 'log_file')))
						else:
							print('Get-VMDisk powershell command failed on %s' % (vm_in['hyperv_vm_name']))
							print('ERROR: Check the "%s" log for details' % (conf.get('HYPERV', 'log_file')))

					# bring the machines back up that were running now that we copied their disks
					if vm_out['state'] == 'running':<commit_msg>uncomment out code that copies the vhd file to file server<commit_after>#!/usr/bin/env python

## Copyright (c) 2014 Citrix Systems, Inc. All Rights Reserved.
## You may only reproduce, distribute, perform, display, or prepare derivative works of this file pursuant to a valid license from Citrix.

from ConfigParser import ConfigParser
from lib.hyperv import HyperV # the class
from lib.hyperv import hyperv # the connection
from lib.cloudstack import cs
import json
import hashlib
import ntpath
import os
import pprint
import sys
import time

# setup the conf object and set default values...
conf = ConfigParser()
conf.add_section('HYPERV')
conf.set('HYPERV', 'migration_input_file', './input/migrate_hyperv_input.json')
conf.set('HYPERV', 'pscp_exe', 'C:\pscp.exe')

# read in config files if they exists
conf.read(['./settings.conf', './running.conf'])

if not conf.has_section('STATE'):
	conf.add_section('STATE') # STATE config section to maintain state of the running process
if not conf.has_option('STATE', 'exported'):
	conf.set('STATE', 'exported', '[]') # parsed with: json.loads(conf.get('STATE', 'exported'))
if not conf.has_option('STATE', 'imported'):
	conf.set('STATE', 'imported', '[]') # parsed with: json.loads(conf.get('STATE', 'imported'))
if not conf.has_option('STATE', 'started'):
	conf.set('STATE', 'started', '[]') # parsed with: json.loads(conf.get('STATE', 'started'))
if not conf.has_option('STATE', 'vms'):
	conf.set('STATE', 'vms', '[]') # parsed with: json.loads(conf.get('STATE', 'vms'))


# copy vhd files to the file server
def copy_vhd_to_file_server(vhd_path, vhd_name):
	return hyperv.powershell('%s -l %s -pw %s "%s" %s:%s/%s' % (
		conf.get('HYPERV', 'pscp_exe'),
		conf.get('FILESERVER', 'username'),
		conf.get('FILESERVER', 'password'),
		vhd_path,
		conf.get('FILESERVER', 'host'),
		conf.get('FILESERVER', 'files_path'),
		vhd_name
	))


# run the actual migration
def do_migration():
	# comment out the following line to keep a history of the requests over multiple runs of this file.
	open(conf.get('HYPERV', 'log_file'), 'w').close() # cleans the powershell requests log before execution so it only includes this run.
	open(conf.get('CLOUDSTACK', 'log_file'), 'w').close() # cleans the cloudstack requests log before execution so it only includes this run.

	vm_input = []
	if os.path.exists(conf.get('HYPERV', 'migration_input_file')):
		with open(conf.get('HYPERV', 'migration_input_file'), 'r') as f:
			try:
				vm_input = json.load(f)
			except:
				print sys.exc_info()
				sys.exit("Error in the formatting of '%s'" % (conf.get('HYPERV', 'migration_input_file')))

	print('\n-----------------------\n-- RUNNING VM EXPORT --\n-----------------------')
	# collect data about the VMs from HyperV and populate a list of VMs

	# initialize the 'vms' variable from the existing config...
	vms = json.loads(conf.get('STATE', 'vms'))

	if vm_input: # make sure there is data in the file
		for vm_in in vm_input: # loop through the vms in the file
			# make sure the minimum fields were entered and they have not been processed already
			vm_id = hashlib.sha1(vm_in['hyperv_server']+"|"+vm_in['hyperv_vm_name']).hexdigest()
			if 'hyperv_vm_name' in vm_in and 'hyperv_server' in vm_in and vm_id not in json.loads(conf.get('STATE', 'exported')):
				objs, ok = hyperv.powershell('Get-VM -Name "%s" -Server "%s"' % (vm_in['hyperv_vm_name'], vm_in['hyperv_server']))
				if objs and ok: # make sure it found the specified VM
					print('\nEXPORTING %s\n%s' % (vm_in['hyperv_vm_name'], '----------'+'-'*len(vm_in['hyperv_vm_name'])))

					exported = False
					vm_raw = objs[0]
					vm_out = vm_in
					vm_out['id'] = vm_id
					
					# get cores
					cpu, ok = hyperv.powershell('Get-VMCPUCount -VM "%s" -Server "%s"' % (vm_in['hyperv_vm_name'], vm_in['hyperv_server']))
					if ok:
						vm_out['cores'] = int(cpu[0]['ProcessorsPerSocket']) * int(cpu[0]['SocketCount'])
					else:
						print('Get-VMCPUCount powershell command failed on %s' % (vm_in['hyperv_vm_name']))
						print('ERROR: Check the "%s" log for details' % (conf.get('HYPERV', 'log_file')))

					# get memory
					memory, ok = hyperv.powershell('Get-VMMemory -VM "%s" -Server "%s"' % (vm_in['hyperv_vm_name'], vm_in['hyperv_server']))
					if ok:
						vm_out['memory'] = int(memory[0]['Reservation'])
					else:
						print('Get-VMMemory powershell command failed on %s' % (vm_in['hyperv_vm_name']))
						print('ERROR: Check the "%s" log for details' % (conf.get('HYPERV', 'log_file')))

					# record their starting state and bring down if running
					if int(vm_raw['EnabledState']) == HyperV.VM_RUNNING:
						vm_out['state'] = 'running'
						print('VM %s is Running' % (vm_in['hyperv_vm_name']))
						status, ok = hyperv.powershell('Stop-VM -VM "%s" -Server "%s" -Wait -Force' % (vm_in['hyperv_vm_name'], vm_in['hyperv_server']))
						if ok:
							print('Stopped %s' % (vm_in['hyperv_vm_name']))
						else:
							print('Stop-VM powershell command failed on %s' % (vm_in['hyperv_vm_name']))
							print('ERROR: Check the "%s" log for details' % (conf.get('HYPERV', 'log_file')))
					elif int(vm_raw['EnabledState']) == HyperV.VM_STOPPED:
						vm_out['state'] = 'stopped'
						print('VM %s is Stopped' % (vm_in['hyperv_vm_name']))
					else: # this should be improved...
						vm_out['state'] = 'unknown'
						print('VM %s is in an Unknown state' % (vm_in['hyperv_vm_name']))

					if (vm_out['state'] == 'running' and ok) or vm_out['state'] == 'stopped':
						disks, ok = hyperv.powershell('Get-VMDisk -VM "%s"' % (vm_in['hyperv_vm_name']))
						if ok:
							vm_out['disks'] = []
							for disk in disks:
								if 'DriveName' in disk and disk['DriveName'] == 'Hard Drive' and 'DiskImage' in disk:
									vm_out['disks'].append({
										'name':ntpath.split(disk['DiskImage'])[1].replace(' ', '-').split('.')[0],
										'url':'%s://%s:%s%s%s' % (
											'https' if conf.get('FILESERVER', 'port') == '443' else 'http',
											conf.get('FILESERVER', 'host'),
											conf.get('FILESERVER', 'port'),
											conf.get('FILESERVER', 'base_uri'),
											ntpath.split(disk['DiskImage'])[1].replace(' ', '-')
											)
										})
									print('Copying drive %s' % (disk['DiskImage']))
									exported = True
									result, ok = copy_vhd_to_file_server(disk['DiskImage'], ntpath.split(disk['DiskImage'])[1].replace(' ', '-'))
									if ok:
										print('Finished copy...')
										exported = True
									else:
										print('Copy failed...')
										print('ERROR: Check the "%s" log for details' % (conf.get('HYPERV', 'log_file')))
						else:
							print('Get-VMDisk powershell command failed on %s' % (vm_in['hyperv_vm_name']))
							print('ERROR: Check the "%s" log for details' % (conf.get('HYPERV', 'log_file')))

					# bring the machines back up that were running now that we copied their disks
					if vm_out['state'] == 'running':
{'fraction_non_alphanumeric': 0.17766019135981445, 'fraction_numerical': 0.002319512902290519, 'mean_word_length': 3.669035532994924, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3466559', 'n_tokens_mistral': 5584, 'n_tokens_neox': 4665, 'n_words': 1255}