in perfkitbenchmarker/static_virtual_machine.py [0:0]
def ReadStaticVirtualMachineFile(cls, file_obj):
"""Read a file describing the static VMs to use.
This function will read the static VM information from the provided file,
instantiate VMs corresponding to the info, and add the VMs to the static
VM pool. The provided file should contain a single array in JSON-format.
Each element in the array must be an object with required format:
ip_address: string.
user_name: string.
keyfile_path: string.
ssh_port: integer, optional. Default 22
internal_ip: string, optional.
zone: string, optional.
local_disks: array of strings, optional.
scratch_disk_mountpoints: array of strings, optional
os_type: string, optional (see package_managers)
install_packages: bool, optional
Args:
file_obj: An open handle to a file containing the static VM info.
Raises:
ValueError: On missing required keys, or invalid keys.
"""
vm_arr = json.load(file_obj)
if not isinstance(vm_arr, list):
raise ValueError(
'Invalid static VM file. Expected array, got: %s.' % type(vm_arr)
)
required_keys = frozenset(['ip_address', 'user_name'])
linux_required_keys = required_keys | frozenset(['keyfile_path'])
required_keys_by_os = {
os_types.WINDOWS: required_keys | frozenset(['password']),
os_types.DEBIAN: linux_required_keys,
os_types.RHEL: linux_required_keys,
os_types.CLEAR: linux_required_keys,
}
# assume linux_required_keys for unknown os_type
required_keys = required_keys_by_os.get(FLAGS.os_type, linux_required_keys)
optional_keys = frozenset([
'internal_ip',
'zone',
'local_disks',
'scratch_disk_mountpoints',
'os_type',
'ssh_port',
'install_packages',
])
allowed_keys = required_keys | optional_keys
def VerifyItemFormat(item):
"""Verify that the decoded JSON object matches the required schema."""
item_keys = frozenset(item)
extra_keys = sorted(item_keys - allowed_keys)
missing_keys = required_keys - item_keys
if extra_keys:
raise ValueError('Unexpected keys: {}'.format(', '.join(extra_keys)))
elif missing_keys:
raise ValueError(
'Missing required keys: {}'.format(', '.join(missing_keys))
)
for item in vm_arr:
VerifyItemFormat(item)
ip_address = item['ip_address']
user_name = item['user_name']
keyfile_path = item.get('keyfile_path')
internal_ip = item.get('internal_ip')
zone = item.get('zone')
local_disks = item.get('local_disks', [])
password = item.get('password')
if not isinstance(local_disks, list):
raise ValueError(
'Expected a list of local disks, got: {}'.format(local_disks)
)
scratch_disk_mountpoints = item.get('scratch_disk_mountpoints', [])
if not isinstance(scratch_disk_mountpoints, list):
raise ValueError(
'Expected a list of disk mount points, got: {}'.format(
scratch_disk_mountpoints
)
)
ssh_port = item.get('ssh_port', 22)
os_type = item.get('os_type')
install_packages = item.get('install_packages', True)
if (
os_type == os_types.WINDOWS and FLAGS.os_type != os_types.WINDOWS
) or (os_type != os_types.WINDOWS and FLAGS.os_type == os_types.WINDOWS):
raise ValueError(
'Please only use Windows VMs when using '
'--os_type=windows and vice versa.'
)
disk_kwargs_list = []
for path in scratch_disk_mountpoints:
disk_kwargs_list.append({'mount_point': path})
for local_disk in local_disks:
disk_kwargs_list.append({'device_path': local_disk})
vm_spec = StaticVmSpec(
'static_vm_file',
ip_address=ip_address,
user_name=user_name,
ssh_port=ssh_port,
install_packages=install_packages,
ssh_private_key=keyfile_path,
internal_ip=internal_ip,
zone=zone,
disk_specs=disk_kwargs_list,
password=password,
flag_values=flags.FLAGS,
)
vm_class = GetStaticVmClass(os_type)
vm = vm_class(vm_spec) # pytype: disable=not-instantiable
cls.vm_pool.append(vm)