Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move defaults from benchmark_spec to vm classes #342

Merged
merged 1 commit into from
Jun 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions perfkitbenchmarker/aws/aws_virtual_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from perfkitbenchmarker import virtual_machine
from perfkitbenchmarker import vm_util
from perfkitbenchmarker.aws import aws_disk
from perfkitbenchmarker.aws import aws_network
from perfkitbenchmarker.aws import util

FLAGS = flags.FLAGS
Expand All @@ -49,7 +50,7 @@
AP_SOUTHEAST_1 = 'ap-southeast-1'
AP_SOUTHEAST_2 = 'ap-southeast-2'
SA_EAST_1 = 'sa-east-1'
AMIS = {
UBUNTU_AMIS = {
HVM: {
US_EAST_1: 'ami-acff23c4',
US_WEST_1: 'ami-05717d40',
Expand Down Expand Up @@ -113,15 +114,6 @@ def GetBlockDeviceMap(machine_type):
return None


def GetImage(machine_type, region):
"""Gets an ami compatible with the machine type and zone."""
prefix = machine_type.split('.')[0]
if prefix in NON_HVM_PREFIXES:
return AMIS[PV][region]
else:
return AMIS[HVM][region]


def IsPlacementGroupCompatible(machine_type):
"""Returns True if VMs of 'machine_type' can be put in a placement group."""
prefix = machine_type.split('.')[0]
Expand All @@ -131,6 +123,9 @@ def IsPlacementGroupCompatible(machine_type):
class AwsVirtualMachine(virtual_machine.BaseVirtualMachine):
"""Object representing an AWS Virtual Machine."""

DEFAULT_ZONE = 'us-east-1a'
DEFAULT_MACHINE_TYPE = 'm3.medium'

_lock = threading.Lock()
imported_keyfile_set = set()
deleted_keyfile_set = set()
Expand All @@ -143,11 +138,30 @@ def __init__(self, vm_spec):
"""
super(AwsVirtualMachine, self).__init__(vm_spec)
self.region = self.zone[:-1]
self.image = self.image or GetImage(self.machine_type, self.region)
self.user_name = FLAGS.aws_user_name
self.network = aws_network.AwsNetwork.GetNetwork(self.zone)
if self.machine_type in NUM_LOCAL_VOLUMES:
self.max_local_disks = NUM_LOCAL_VOLUMES[self.machine_type]

@classmethod
def SetVmSpecDefaults(cls, vm_spec):
"""Updates the VM spec with cloud specific defaults."""
if vm_spec.machine_type is None:
vm_spec.machine_type = cls.DEFAULT_MACHINE_TYPE
if vm_spec.zone is None:
vm_spec.zone = cls.DEFAULT_ZONE
if vm_spec.image is None:
region = vm_spec.zone[:-1]
vm_spec.image = cls._GetDefaultImage(vm_spec.machine_type, region)

@staticmethod
def _GetDefaultImage(machine_type, region):
"""Returns the default image given the machine type and region.

If no default is configured, this will return None.
"""
return None

def ImportKeyfile(self):
"""Imports the public keyfile to AWS."""
with self._lock:
Expand Down Expand Up @@ -297,7 +311,18 @@ def AddMetadata(self, **kwargs):

class DebianBasedAwsVirtualMachine(AwsVirtualMachine,
linux_virtual_machine.DebianMixin):
pass

@staticmethod
def _GetDefaultImage(machine_type, region):
"""Returns the default image given the machine type and region.

If no default is configured, this will return None.
"""
prefix = machine_type.split('.')[0]
if prefix in NON_HVM_PREFIXES:
return UBUNTU_AMIS[PV][region]
else:
return UBUNTU_AMIS[HVM][region]


class RhelBasedAwsVirtualMachine(AwsVirtualMachine,
Expand Down
25 changes: 23 additions & 2 deletions perfkitbenchmarker/azure/azure_virtual_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@
from perfkitbenchmarker import virtual_machine
from perfkitbenchmarker import vm_util
from perfkitbenchmarker.azure import azure_disk
from perfkitbenchmarker.azure import azure_network

FLAGS = flags.FLAGS

AZURE_PATH = 'azure'
UBUNTU_IMAGE = ('b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-'
'14_04_1-LTS-amd64-server-20150123-en-us-30GB')
CENTOS_IMAGE = ('0b11de9248dd4d87b18621318e037d37__RightImage-'
'CentOS-7.0-x64-v14.2.1')


class AzureService(resource.BaseResource):
Expand Down Expand Up @@ -85,20 +90,36 @@ def _Exists(self):
class AzureVirtualMachine(virtual_machine.BaseVirtualMachine):
"""Object representing an Azure Virtual Machine."""

DEFAULT_ZONE = 'East US'
DEFAULT_MACHINE_TYPE = 'Small'
# Subclasses should override the default image.
DEFAULT_IMAGE = None

def __init__(self, vm_spec):
"""Initialize a Azure virtual machine.

Args:
vm_spec: virtual_machine.BaseVirtualMachineSpec object of the vm.
"""
super(AzureVirtualMachine, self).__init__(vm_spec)
self.network = azure_network.AzureNetwork.GetNetwork(self.zone)
self.service = AzureService(self.name,
self.network.affinity_group.name)
disk_spec = disk.BaseDiskSpec(None, None, None)
self.os_disk = azure_disk.AzureDisk(disk_spec, self.name)
self.max_local_disks = 1
self.local_disk_counter = 0

@classmethod
def SetVmSpecDefaults(cls, vm_spec):
"""Updates the VM spec with cloud specific defaults."""
if vm_spec.machine_type is None:
vm_spec.machine_type = cls.DEFAULT_MACHINE_TYPE
if vm_spec.zone is None:
vm_spec.zone = cls.DEFAULT_ZONE
if vm_spec.image is None:
vm_spec.image = cls.DEFAULT_IMAGE

def _CreateDependencies(self):
"""Create VM dependencies."""
self.service.Create()
Expand Down Expand Up @@ -190,9 +211,9 @@ def GetLocalDisks(self):

class DebianBasedAzureVirtualMachine(AzureVirtualMachine,
linux_virtual_machine.DebianMixin):
pass
DEFAULT_IMAGE = UBUNTU_IMAGE


class RhelBasedAzureVirtualMachine(AzureVirtualMachine,
linux_virtual_machine.RhelMixin):
pass
DEFAULT_IMAGE = CENTOS_IMAGE
67 changes: 18 additions & 49 deletions perfkitbenchmarker/benchmark_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from perfkitbenchmarker import disk
from perfkitbenchmarker import flags
from perfkitbenchmarker import network
from perfkitbenchmarker import static_virtual_machine
from perfkitbenchmarker import virtual_machine
from perfkitbenchmarker import vm_util
Expand All @@ -45,52 +46,26 @@
VIRTUAL_MACHINE = 'virtual_machine'
NETWORK = 'network'
FIREWALL = 'firewall'
DEFAULTS = {
GCP: {
IMAGE: 'ubuntu-14-04',
MACHINE_TYPE: 'n1-standard-1',
ZONE: 'us-central1-a',
},
AZURE: {
IMAGE: ('b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-'
'14_04_1-LTS-amd64-server-20150123-en-us-30GB'),
MACHINE_TYPE: 'Small',
ZONE: 'East US',
},
AWS: {
IMAGE: None,
MACHINE_TYPE: 'm3.medium',
ZONE: 'us-east-1a'
},
DIGITALOCEAN: {
IMAGE: 'ubuntu-14-04-x64',
MACHINE_TYPE: '2gb',
ZONE: 'sfo1'
}
}
CLASSES = {
GCP: {
VIRTUAL_MACHINE: {
DEBIAN: gce_virtual_machine.DebianBasedGceVirtualMachine,
RHEL: gce_virtual_machine.RhelBasedGceVirtualMachine
},
NETWORK: gce_network.GceNetwork,
FIREWALL: gce_network.GceFirewall
},
AZURE: {
VIRTUAL_MACHINE: {
DEBIAN: azure_virtual_machine.DebianBasedAzureVirtualMachine,
RHEL: azure_virtual_machine.RhelBasedAzureVirtualMachine
},
NETWORK: azure_network.AzureNetwork,
FIREWALL: azure_network.AzureFirewall
},
AWS: {
VIRTUAL_MACHINE: {
DEBIAN: aws_virtual_machine.DebianBasedAwsVirtualMachine,
RHEL: aws_virtual_machine.RhelBasedAwsVirtualMachine
},
NETWORK: aws_network.AwsNetwork,
FIREWALL: aws_network.AwsFirewall
},
DIGITALOCEAN: {
Expand All @@ -100,7 +75,6 @@
RHEL:
digitalocean_virtual_machine.RhelBasedDigitalOceanVirtualMachine,
},
NETWORK: digitalocean_network.DigitalOceanNetwork,
FIREWALL: digitalocean_network.DigitalOceanFirewall
},
}
Expand Down Expand Up @@ -130,7 +104,6 @@ def __init__(self, benchmark_info):
FLAGS.benchmark_config_pair[benchmark_info['name']])
self.vms = []
self.vm_dict = {'default': []}
self.networks = {}
self.benchmark_name = benchmark_info['name']
if hasattr(self, 'config'):
config_dict = {}
Expand All @@ -155,11 +128,9 @@ def __init__(self, benchmark_info):
else:
self.cloud = FLAGS.cloud
self.project = FLAGS.project
defaults = DEFAULTS[self.cloud]
self.zones = FLAGS.zones or [defaults[ZONE]]
self.image = FLAGS.image or defaults[IMAGE]
self.machine_type = FLAGS.machine_type or defaults[
MACHINE_TYPE]
self.zones = FLAGS.zones
self.image = FLAGS.image
self.machine_type = FLAGS.machine_type
if benchmark_info['num_machines'] is None:
self.num_vms = FLAGS.num_vms
else:
Expand Down Expand Up @@ -199,9 +170,9 @@ def __init__(self, benchmark_info):

def Prepare(self):
"""Prepares the VMs and networks necessary for the benchmark to run."""
if self.networks:
prepare_args = [self.networks[zone] for zone in self.networks]
vm_util.RunThreaded(self.PrepareNetwork, prepare_args)
prepare_args = network.BaseNetwork.networks.values()
vm_util.RunThreaded(self.PrepareNetwork, prepare_args)

if self.vms:
prepare_args = [((vm, self.firewall), {}) for vm in self.vms]
vm_util.RunThreaded(self.PrepareVm, prepare_args)
Expand All @@ -222,9 +193,9 @@ def Delete(self):
except Exception:
logging.exception('Got an exception disabling firewalls. '
'Attempting to continue tearing down.')
for zone in self.networks:
for net in network.BaseNetwork.networks.itervalues():
try:
self.networks[zone].Delete()
net.Delete()
except Exception:
logging.exception('Got an exception deleting networks. '
'Attempting to continue tearing down.')
Expand All @@ -234,12 +205,12 @@ def PrepareNetwork(self, network):
"""Initialize the network."""
network.Create()

def CreateVirtualMachine(self, opt_zone=None):
def CreateVirtualMachine(self, zone):
"""Create a vm in zone.

Args:
opt_zone: The zone in which the vm will be created. If not provided,
FLAGS.zone or the revelant zone from DEFAULT will be used.
zone: The zone in which the vm will be created. If zone is None,
the VM class's DEFAULT_ZONE will be used instead.
Returns:
A vm object.
"""
Expand All @@ -248,14 +219,12 @@ def CreateVirtualMachine(self, opt_zone=None):
return vm

vm_class = CLASSES[self.cloud][VIRTUAL_MACHINE][FLAGS.os_type]
zone = opt_zone or self.zones[0]
if zone not in self.networks:
network_class = CLASSES[self.cloud][NETWORK]
self.networks[zone] = network_class(zone)
self.vm_spec = virtual_machine.BaseVirtualMachineSpec(
self.project, zone, self.machine_type, self.image,
self.networks[zone])
return vm_class(self.vm_spec)

vm_spec = virtual_machine.BaseVirtualMachineSpec(
self.project, zone, self.machine_type, self.image)
vm_class.SetVmSpecDefaults(vm_spec)

return vm_class(vm_spec)

def CreateVirtualMachineFromNodeSection(self, node_section, node_name):
"""Create a VirtualMachine object from NodeSection.
Expand Down
18 changes: 17 additions & 1 deletion perfkitbenchmarker/digitalocean/digitalocean_virtual_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

FLAGS = flags.FLAGS

UBUNTU_IMAGE = 'ubuntu-14-04-x64'
CLOUD_CONFIG_TEMPLATE = '''#cloud-config
users:
- name: {0}
Expand Down Expand Up @@ -60,6 +61,11 @@ def GetErrorMessage(stdout):
class DigitalOceanVirtualMachine(virtual_machine.BaseVirtualMachine):
"""Object representing a DigitalOcean Virtual Machine (Droplet)."""

DEFAULT_ZONE = 'sfo1'
DEFAULT_MACHINE_TYPE = '2gb'
# Subclasses should override the default image.
DEFAULT_IMAGE = None

def __init__(self, vm_spec):
"""Initialize a DigitalOcean virtual machine.

Expand All @@ -71,6 +77,16 @@ def __init__(self, vm_spec):
self.max_local_disks = 1
self.local_disk_counter = 0

@classmethod
def SetVmSpecDefaults(cls, vm_spec):
"""Updates the VM spec with cloud specific defaults."""
if vm_spec.machine_type is None:
vm_spec.machine_type = cls.DEFAULT_MACHINE_TYPE
if vm_spec.zone is None:
vm_spec.zone = cls.DEFAULT_ZONE
if vm_spec.image is None:
vm_spec.image = cls.DEFAULT_IMAGE

def _Create(self):
"""Create a DigitalOcean VM instance (droplet)."""
with open(self.ssh_public_key) as f:
Expand Down Expand Up @@ -206,7 +222,7 @@ def CreateScratchDisk(self, disk_spec):

class DebianBasedDigitalOceanVirtualMachine(DigitalOceanVirtualMachine,
linux_virtual_machine.DebianMixin):
pass
DEFAULT_IMAGE = UBUNTU_IMAGE


class RhelBasedDigitalOceanVirtualMachine(DigitalOceanVirtualMachine,
Expand Down
Loading