Example #1
0
 def __init__(self, component_full_name, flag_values=None, **kwargs):
     super(_VmGroupSpec, self).__init__(component_full_name,
                                        flag_values=flag_values,
                                        **kwargs)
     providers.LoadProvider(self.cloud.lower())
     if self.disk_spec:
         disk_config = getattr(self.disk_spec, self.cloud, None)
         if disk_config is None:
             raise errors.Config.MissingOption(
                 '{0}.cloud is "{1}", but {0}.disk_spec does not contain a '
                 'configuration for "{1}".'.format(component_full_name,
                                                   self.cloud))
         disk_spec_class = disk.GetDiskSpecClass(self.cloud)
         self.disk_spec = disk_spec_class('{0}.disk_spec.{1}'.format(
             component_full_name, self.cloud),
                                          flag_values=flag_values,
                                          **disk_config)
     vm_config = getattr(self.vm_spec, self.cloud, None)
     if vm_config is None:
         raise errors.Config.MissingOption(
             '{0}.cloud is "{1}", but {0}.vm_spec does not contain a '
             'configuration for "{1}".'.format(component_full_name,
                                               self.cloud))
     vm_spec_class = virtual_machine.GetVmSpecClass(self.cloud)
     self.vm_spec = vm_spec_class('{0}.vm_spec.{1}'.format(
         component_full_name, self.cloud),
                                  flag_values=flag_values,
                                  **vm_config)
    def __init__(self, component_full_name, flag_values=None, **kwargs):
        super(_ManagedRelationalDbSpec, self).__init__(component_full_name,
                                                       flag_values=flag_values,
                                                       **kwargs)
        # TODO(ferneyhough): This is a lot of boilerplate, and is repeated
        # below in VmGroupSpec. See if some can be consolidated. Maybe we can
        # specify a VmGroupSpec instead of both vm_spec and disk_spec.
        ignore_package_requirements = (getattr(
            flag_values, 'ignore_package_requirements', True)
                                       if flag_values else True)
        providers.LoadProvider(self.cloud, ignore_package_requirements)

        if self.disk_spec:
            disk_config = getattr(self.disk_spec, self.cloud, None)
            if disk_config is None:
                raise errors.Config.MissingOption(
                    '{0}.cloud is "{1}", but {0}.disk_spec does not contain a '
                    'configuration for "{1}".'.format(component_full_name,
                                                      self.cloud))
            disk_spec_class = disk.GetDiskSpecClass(self.cloud)
            self.disk_spec = disk_spec_class('{0}.disk_spec.{1}'.format(
                component_full_name, self.cloud),
                                             flag_values=flag_values,
                                             **disk_config)

        vm_config = getattr(self.vm_spec, self.cloud, None)
        if vm_config is None:
            raise errors.Config.MissingOption(
                '{0}.cloud is "{1}", but {0}.vm_spec does not contain a '
                'configuration for "{1}".'.format(component_full_name,
                                                  self.cloud))
        vm_spec_class = virtual_machine.GetVmSpecClass(self.cloud)
        self.vm_spec = vm_spec_class('{0}.vm_spec.{1}'.format(
            component_full_name, self.cloud),
                                     flag_values=flag_values,
                                     **vm_config)

        # Set defaults that were not able to be set in
        # GetOptionDecoderConstructions()
        if not self.engine_version:
            managed_db_class = managed_relational_db.GetManagedRelationalDbClass(
                self.cloud)
            self.engine_version = managed_db_class.GetDefaultEngineVersion(
                self.engine)
        if not self.database_name:
            self.database_name = 'pkb-db-%s' % flag_values.run_uri
        if not self.database_username:
            self.database_username = '******' % flag_values.run_uri
        if not self.database_password:
            self.database_password = managed_relational_db.GenerateRandomDbPassword(
            )
    def ConstructVirtualMachines(self):
        """Constructs the BenchmarkSpec's VirtualMachine objects."""
        vm_group_specs = self.config[VM_GROUPS]

        zone_index = 0
        for group_name, group_spec in vm_group_specs.iteritems():
            vms = []
            vm_count = group_spec.get(VM_COUNT, DEFAULT_COUNT)
            if vm_count is None:
                vm_count = FLAGS.num_vms
            disk_count = group_spec.get(DISK_COUNT, DEFAULT_COUNT)

            try:
                # First create the Static VMs.
                if STATIC_VMS in group_spec:
                    static_vm_specs = group_spec[STATIC_VMS][:vm_count]
                    for static_vm_spec_index, spec_kwargs in enumerate(
                            static_vm_specs):
                        vm_spec = static_vm.StaticVmSpec(
                            '{0}.{1}.{2}.{3}[{4}]'.format(
                                self.name, VM_GROUPS, group_name, STATIC_VMS,
                                static_vm_spec_index), **spec_kwargs)
                        static_vm_class = static_vm.GetStaticVmClass(
                            vm_spec.os_type)
                        vms.append(static_vm_class(vm_spec))

                os_type = self._GetOsTypeForGroup(group_name)
                cloud = self._GetCloudForGroup(group_name)
                providers.LoadProvider(cloud.lower())

                # This throws an exception if the benchmark is not
                # supported.
                self._CheckBenchmarkSupport(cloud)

                # Then create a VmSpec and possibly a DiskSpec which we can
                # use to create the remaining VMs.
                vm_spec_class = virtual_machine.GetVmSpecClass(cloud)
                vm_spec = vm_spec_class(
                    '.'.join(
                        (self.name, VM_GROUPS, group_name, VM_SPEC, cloud)),
                    FLAGS, **group_spec[VM_SPEC][cloud])

                if DISK_SPEC in group_spec:
                    disk_spec_class = disk.GetDiskSpecClass(cloud)
                    disk_spec = disk_spec_class(**group_spec[DISK_SPEC][cloud])
                    disk_spec.ApplyFlags(FLAGS)
                    # disk_spec.disk_type may contain legacy values that were
                    # copied from FLAGS.scratch_disk_type into
                    # FLAGS.data_disk_type at the beginning of the run. We
                    # translate them here, rather than earlier, because here is
                    # where we know what cloud we're using and therefore we're
                    # able to pick the right translation table.
                    disk_spec.disk_type = disk.WarnAndTranslateDiskTypes(
                        disk_spec.disk_type, cloud)
                else:
                    disk_spec = None

            except TypeError as e:
                # This is what we get if one of the kwargs passed into a spec's
                # __init__ method was unexpected.
                raise ValueError(
                    'Config contained an unexpected parameter. Error message:\n%s'
                    % e)

            # Create the remaining VMs using the specs we created earlier.
            for _ in xrange(vm_count - len(vms)):
                # Assign a zone to each VM sequentially from the --zones flag.
                if FLAGS.zones:
                    vm_spec.zone = FLAGS.zones[zone_index]
                    zone_index = (zone_index + 1
                                  if zone_index < len(FLAGS.zones) - 1 else 0)
                vm = self._CreateVirtualMachine(vm_spec, os_type, cloud)
                if disk_spec:
                    vm.disk_specs = [
                        copy.copy(disk_spec) for _ in xrange(disk_count)
                    ]
                    # In the event that we need to create multiple disks from the same
                    # DiskSpec, we need to ensure that they have different mount points.
                    if (disk_count > 1 and disk_spec.mount_point):
                        for i, spec in enumerate(vm.disk_specs):
                            spec.mount_point += str(i)
                vms.append(vm)

            self.vm_groups[group_name] = vms
            self.vms.extend(vms)