Beispiel #1
0
def set_vmss_extension(resource_group_name,
                       vmss_name,
                       extension_name,
                       publisher,
                       version=None,
                       settings=None,
                       protected_settings=None,
                       no_auto_upgrade=False):
    '''create/update extensions for a VMSS in a resource group. You can use
    'extension image list' to get extension details
    :param vm_extension_name: the name of the extension
    :param publisher: the name of extension publisher
    :param version: the version of extension.
    :param settings: public settings or a file path with such contents
    :param protected_settings: protected settings or a file path with such contents
    :param no_auto_upgrade: by doing this, extension system will not pick the highest minor version
    for the specified version number, and will not auto update to the latest build/revision number
    on any scale set updates in future.
    '''
    client = _compute_client_factory()
    vmss = client.virtual_machine_scale_sets.get(resource_group_name,
                                                 vmss_name)

    from azure.mgmt.compute.models import VirtualMachineExtension

    protected_settings = load_json(
        protected_settings) if protected_settings else {}
    settings = load_json(settings) if settings else None

    #pylint: disable=no-member
    version = _normalize_extension_version(publisher, extension_name, version,
                                           vmss.location)

    ext = VirtualMachineScaleSetExtension(
        name=extension_name,
        publisher=publisher,
        type=extension_name,
        protected_settings=protected_settings,
        type_handler_version=version,
        settings=settings,
        auto_upgrade_minor_version=(not no_auto_upgrade))

    if not vmss.virtual_machine_profile.extension_profile:
        vmss.virtual_machine_profile.extension_profile = VirtualMachineScaleSetExtensionProfile(
            [])
    vmss.virtual_machine_profile.extension_profile.extensions.append(ext)

    return client.virtual_machine_scale_sets.create_or_update(
        resource_group_name, vmss_name, vmss)
def clone_vmss(vmScaleSetName, resourceGroupName, count):
    logging.info(
        "Collectiong Data from VM Scale Set {}".format(vmScaleSetName))
    vmss_data = computeclient.virtual_machine_scale_sets.get(
        resourceGroupName, vmScaleSetName)
    StorageAccount = vmss_data.virtual_machine_profile.extension_profile.extensions[
        0].settings['StorageAccount']
    logging.info(
        "Fetching  Data from Storage Account {}".format(StorageAccount))
    StorageAccountId = get_storage_account_info(resourceGroupName,
                                                StorageAccount)
    custom_data = get_user_data(StorageAccount)
    sku = Sku(name=vmss_data.sku.name, tier=vmss_data.sku.tier, capacity=0)
    os_profile = VirtualMachineScaleSetOSProfile(
        computer_name_prefix=vmss_data.virtual_machine_profile.os_profile.
        computer_name_prefix + str(count + 1),
        admin_username=vmss_data.virtual_machine_profile.os_profile.
        admin_username,
        windows_configuration=None,
        linux_configuration=vmss_data.virtual_machine_profile.os_profile.
        linux_configuration,
        custom_data=custom_data)
    virtual_machine_profile = VirtualMachineScaleSetVMProfile(
        os_profile=os_profile,
        storage_profile=vmss_data.virtual_machine_profile.storage_profile,
        #additional_capabilities=vmss_data.virtual_machine_profile.additional_capabilities,
        network_profile=vmss_data.virtual_machine_profile.network_profile,
        diagnostics_profile=vmss_data.virtual_machine_profile.
        diagnostics_profile)
    new_vmss_parameters = VirtualMachineScaleSet(
        location=vmss_data.location,
        tags=vmss_data.tags,
        sku=sku,
        plan=vmss_data.plan,
        upgrade_policy=vmss_data.upgrade_policy,
        virtual_machine_profile=virtual_machine_profile,
        overprovision=vmss_data.overprovision,
        do_not_run_extensions_on_overprovisioned_vms=vmss_data.
        do_not_run_extensions_on_overprovisioned_vms,
        single_placement_group=vmss_data.single_placement_group,
        zone_balance=vmss_data.zone_balance,
        platform_fault_domain_count=vmss_data.platform_fault_domain_count,
        identity=VirtualMachineScaleSetIdentity(type='SystemAssigned'),
        zones=vmss_data.zones)
    new_vm_scale_set = vmScaleSetName[:-1] + str(count + 1)
    logging.info(
        "Creating VM Scale Set with Name:  {}".format(new_vm_scale_set))
    new_vmss = computeclient.virtual_machine_scale_sets.create_or_update(
        resource_group_name=resourceGroupName,
        vm_scale_set_name=new_vm_scale_set,
        parameters=new_vmss_parameters)
    new_vmss.wait()
    new_vmss_data = new_vmss.result()
    new_vmss_id = new_vmss_data.id
    new_vmss_principal_id = new_vmss_data.identity.principal_id
    logging.info("Assiging Role - {} on Scope - {} for Prinicpal - {}".format(
        'Owner', new_vmss_id, new_vmss_principal_id))
    assign_role(new_vmss_id, 'Owner', new_vmss_principal_id)
    logging.info("Assiging Role - {} on Scope - {} for Prinicpal - {}".format(
        'Storage Blob Data Contributor', StorageAccountId,
        new_vmss_principal_id))
    assign_role(StorageAccountId, 'Storage Blob Data Contributor',
                new_vmss_principal_id)

    extension_settings = vmss_data.virtual_machine_profile.extension_profile.extensions[
        0].settings
    extension_settings['ladCfg']['diagnosticMonitorConfiguration']['metrics'][
        'resourceId'] = new_vmss_id
    logging.info(
        "Fetching  SAS from Storage Account {}".format(StorageAccount))
    sas_token = get_sas_token(resourceGroupName, StorageAccount)
    logging.info("SAS Token : {}".format(sas_token))
    protected_settings = {
        'storageAccountName': StorageAccount,
        'storageAccountSasToken': sas_token
    }

    extension_profile = VirtualMachineScaleSetExtensionProfile(extensions=[
        VirtualMachineScaleSetExtension(
            name=vmss_data.virtual_machine_profile.extension_profile.
            extensions[0].name,
            force_update_tag=vmss_data.virtual_machine_profile.
            extension_profile.extensions[0].force_update_tag,
            publisher=vmss_data.virtual_machine_profile.extension_profile.
            extensions[0].publisher,
            #type1=vmss_data.virtual_machine_profile.extension_profile.extensions[0].type,
            type=vmss_data.virtual_machine_profile.extension_profile.
            extensions[0].type,
            type_handler_version=vmss_data.virtual_machine_profile.
            extension_profile.extensions[0].type_handler_version,
            auto_upgrade_minor_version=vmss_data.virtual_machine_profile.
            extension_profile.extensions[0].auto_upgrade_minor_version,
            settings=extension_settings,
            protected_settings=protected_settings,
            provision_after_extensions=vmss_data.virtual_machine_profile.
            extension_profile.extensions[0].provision_after_extensions)
    ])
    update_virtual_machine_profile = VirtualMachineScaleSetVMProfile(
        extension_profile=extension_profile)
    update_parameters = VirtualMachineScaleSet(
        location=vmss_data.location,
        virtual_machine_profile=update_virtual_machine_profile)
    logging.info(
        "Setting Linux Diagnositc Extension on  VM Scale Set :  {}".format(
            new_vm_scale_set))
    new_vmss_update = computeclient.virtual_machine_scale_sets.create_or_update(
        resource_group_name=resourceGroupName,
        vm_scale_set_name=new_vm_scale_set,
        parameters=update_parameters)
    new_vmss_update.wait()

    #############
    """
    updatedsku = Sku(
        name = vmss_data.sku.name,
        tier = vmss_data.sku.tier,
        capacity = 2
    )
    updated_vmss_parameters = VirtualMachineScaleSet(
        location=vmss_data.location,
        sku=updatedsku
    )
    new_vmss = computeclient.virtual_machine_scale_sets.create_or_update(
        resource_group_name=resourceGroupName,
        vm_scale_set_name=new_vm_scale_set,
        parameters=updated_vmss_parameters
        )
    new_vmss.wait()
    """
    #############
    logging.info("Creating AutoScale Setting for   VM Scale Set :  {}".format(
        new_vm_scale_set))
    create_autoscaling_settings(vmScaleSetName, new_vmss_id, resourceGroupName)

    logging.info("Creating Activity Log Alert for VM Scale Set :  {}".format(
        new_vm_scale_set))
    clone_activity_log(resourceGroupName, new_vmss_id)
    logging.info(
        "Finished Creating Activity Log Alert for VM Scale Set :  {}".format(
            new_vm_scale_set))

    logging.info("Successfully Cloned VM ScaleSet {} to create {}".format(
        vmScaleSetName, new_vm_scale_set))