Beispiel #1
0
def create_vm(**_):
    random_suffix_value = utils.random_suffix_generator()
    vm_name = ctx.node.properties[constants.VM_PREFIX]+random_suffix_value
    ctx.logger.info("Creating new virtual machine: {0}".format(vm_name))
    storage_account_name = ctx.instance.runtime_properties[constants.STORAGE_ACCOUNT_KEY]

    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]

    try:
        virtual_machine_params = get_virtual_machine_params(location, random_suffix_value, resource_group_name,
                                                            storage_account_name, subscription_id, vm_name)
        virtual_machine_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/Microsoft.Compute/virtualMachines/'+vm_name+'?validating=true&api-version='+constants.api_version
        response_vm = requests.put(url=virtual_machine_url, data=virtual_machine_params, headers=headers)
        if response_vm.text:
            ctx.logger.info("create_vm:{0} response_vm.text is {1}".format(vm_name, response_vm.text))
            if utils.request_failed("{0}:{1}".format('create_vm', vm_name), response_vm):
                raise NonRecoverableError("Virtual Machine {0} could not be created".format(vm_name))
        elif response_vm:
            ctx.logger.info("create_vm:{0} response_vm is {1}".format(vm_name, response_vm))
        else:
            ctx.logger.info("create_vm:{0} response_vm is empty".format(vm_name))
        ctx.instance.runtime_properties[constants.VM_KEY] = vm_name
    except:
        ctx.logger.info("Virtual Machine {0} could not be created".format(vm_name))
        raise NonRecoverableError("Virtual Machine {0} could not be created".format(vm_name))
Beispiel #2
0
def create_a_vm(**_):
    utils.set_runtime_properties_from_file()
    random_suffix_value = utils.random_suffix_generator()
    vm_name = ctx.node.properties[constants.VM_PREFIX] + random_suffix_value
    ctx.logger.info("Creating new virtual machine: {0}".format(vm_name))

    _set_ip_addresses_keys(vm_name)

    storage_account_name = ctx.instance.runtime_properties[
        constants.STORAGE_ACCOUNT_KEY]
    #availability_set_name = ctx.instance.runtime_properties[constants.AVAILABILITY_SET_KEY]
    availability_set_name = "To be developed by Pranjali and Vaidehi"
    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[
        constants.RESOURCE_GROUP_KEY]

    virtual_machine_params = _get_virtual_machine_params(
        location, random_suffix_value, resource_group_name,
        storage_account_name, subscription_id, vm_name, availability_set_name)
    virtual_machine_url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/Microsoft.Compute/virtualMachines/' + vm_name + '?validating=true&api-version=' + constants.api_version
    response_vm = requests.put(url=virtual_machine_url,
                               data=virtual_machine_params,
                               headers=headers)
    ctx.instance.runtime_properties[constants.VM_KEY] = vm_name
    return response_vm.status_code
def create_custom_script(**_):
    utils.set_runtime_properties_from_file()
    custom_script_name = constants.CUSTOM_SCRIPT_PREFIX+utils.random_suffix_generator()
    ctx.instance.runtime_properties[constants.CUSTOM_SCRIPT_KEY] = custom_script_name
    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    command = ctx.node.properties['custom_script_command']
    file_uri = ctx.node.properties['custom_script_path']
    vm_name = ctx.instance.runtime_properties[constants.VM_KEY]

    custom_script_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/Microsoft.Compute/virtualMachines/'+vm_name+'/extensions/'+custom_script_name+'?api-version='+constants.api_version
    custom_script_params = json.dumps({ 
       "id":"/subscriptions/"+subscription_id+"/resourceGroups/"+resource_group_name+"/providers/Microsoft.Compute/virtualMachines/"+vm_name+"/extensions/"+custom_script_name,
       "name": custom_script_name,
       "type": "Microsoft.Compute/virtualMachines/extensions",
       "location": location,
       "properties": {
         "publisher": "Microsoft.OSTCExtensions",
         "type": "CustomScriptForLinux",
         "typeHandlerVersion": "1.2",
         "settings": {
           "fileUris": [file_uri],
           "commandToExecute": command
         }
       }
    }
    )
    response_as = requests.put(url=custom_script_url, data=custom_script_params, headers=headers) 
    print response_as.text 
def create_a_nic(**_):
    utils.set_runtime_properties_from_file()
    _set_nic_subnet()
    if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[constants.USE_EXTERNAL_RESOURCE]:
        if constants.EXISTING_NIC_KEY in ctx.node.properties:
            existing_nic_name = ctx.node.properties[constants.EXISTING_NIC_KEY]
            if existing_nic_name:
                nic_exists = _get_nic_name(existing_nic_name)
                if not nic_exists:
                    raise NonRecoverableError("NIC {0} doesn't exist your Azure account".format(existing_nic_name))
            else:
                raise NonRecoverableError("The value of '{0}' in the input, is empty".format(constants.EXISTING_NIC_KEY))
        else:
            raise NonRecoverableError("'{0}' was specified, but '{1}' doesn't exist in the input".format(constants.USE_EXTERNAL_RESOURCE, constants.EXISTING_NIC_KEY))
        curr_nic_key = _get_nic_key()
        ctx.instance.runtime_properties[curr_nic_key] = ctx.node.properties[constants.EXISTING_NIC_KEY]
        return

    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
   
    vnet_name = ctx.instance.runtime_properties[constants.VNET_KEY]
    current_subnet_name = ctx.instance.runtime_properties[constants.SUBNET_KEY]
    nic_name = utils.key_in_runtime(constants.NIC_KEY, ends_with_key=False, starts_with_key=True, return_value=True)
    if not nic_name:
        random_suffix_value = utils.random_suffix_generator()
        nic_name = constants.NIC_PREFIX+random_suffix_value
        curr_nic_key = _get_nic_key()
        ctx.instance.runtime_properties[curr_nic_key] = nic_name

    ctx.logger.info("Creating new network interface card: {0}".format(nic_name))
    network_str, nic_params = _get_nic_params(current_subnet_name, location, resource_group_name, subscription_id, vnet_name)
    create_nic_url = constants.azure_url+network_str+"/networkInterfaces/"+nic_name+"?api-version="+constants.api_version_network
    return utils.create_resource(headers, nic_name, nic_params, create_nic_url, 'NIC')
def create_security_group(**_):
    utils.set_runtime_properties_from_file()
    security_group_name = utils.set_resource_name(_get_security_group_name, 'SECUIRTY_GROUP',
                                             constants.SECURITY_GROUP_KEY, constants.EXISTING_SECURITY_GROUP_KEY,
                                             constants.SECURITY_GROUP_PREFIX)
    if security_group_name is None:
        # Using an existing public ip, so don't create anything
        return

    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    if constants.SECURITY_GROUP_KEY in ctx.instance.runtime_properties:
        security_group_name = ctx.instance.runtime_properties[constants.SECURITY_GROUP_KEY]
    else:
        random_suffix_value = utils.random_suffix_generator()
        security_group_name = constants.SECURITY_GROUP_PREFIX+random_suffix_value

    security_group_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/Microsoft.Network/networkSecurityGroups/'+security_group_name+'?api-version='+constants.api_version_network

    ctx.logger.info("Creating a new security group: {0}".format(security_group_name))
    security_group_json = _get_security_group_json(location)
    security_group_params = _get_security_group_params(security_group_json)
    response_nsg = requests.put(url=security_group_url, data=security_group_params, headers=headers)
    ctx.instance.runtime_properties[constants.SECURITY_GROUP_KEY] = security_group_name
    return response_nsg.status_code
Beispiel #6
0
def create_a_nic(**_):
    utils.set_runtime_properties_from_file()
    _set_nic_subnet()
    if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[
            constants.USE_EXTERNAL_RESOURCE]:
        if constants.EXISTING_NIC_KEY in ctx.node.properties:
            existing_nic_name = ctx.node.properties[constants.EXISTING_NIC_KEY]
            if existing_nic_name:
                nic_exists = _get_nic_name(existing_nic_name)
                if not nic_exists:
                    raise NonRecoverableError(
                        "NIC {0} doesn't exist your Azure account".format(
                            existing_nic_name))
            else:
                raise NonRecoverableError(
                    "The value of '{0}' in the input, is empty".format(
                        constants.EXISTING_NIC_KEY))
        else:
            raise NonRecoverableError(
                "'{0}' was specified, but '{1}' doesn't exist in the input".
                format(constants.USE_EXTERNAL_RESOURCE,
                       constants.EXISTING_NIC_KEY))
        curr_nic_key = _get_nic_key()
        ctx.instance.runtime_properties[curr_nic_key] = ctx.node.properties[
            constants.EXISTING_NIC_KEY]
        return

    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[
        constants.RESOURCE_GROUP_KEY]

    vnet_name = ctx.instance.runtime_properties[constants.VNET_KEY]
    current_subnet_name = ctx.instance.runtime_properties[constants.SUBNET_KEY]
    nic_name = utils.key_in_runtime(constants.NIC_KEY,
                                    ends_with_key=False,
                                    starts_with_key=True,
                                    return_value=True)
    if not nic_name:
        random_suffix_value = utils.random_suffix_generator()
        nic_name = constants.NIC_PREFIX + random_suffix_value
        curr_nic_key = _get_nic_key()
        ctx.instance.runtime_properties[curr_nic_key] = nic_name

    ctx.logger.info(
        "Creating new network interface card: {0}".format(nic_name))
    network_str, nic_params = _get_nic_params(current_subnet_name, location,
                                              resource_group_name,
                                              subscription_id, vnet_name)
    create_nic_url = constants.azure_url + network_str + "/networkInterfaces/" + nic_name + "?api-version=" + constants.api_version_network
    return utils.create_resource(headers, nic_name, nic_params, create_nic_url,
                                 'NIC')
def create_disk(**_):
	luns = ctx.node.properties['lun']
	for x in range(0, luns):
		curr_disk_key = []
		curr_disk_key.append("{0}{1}{2}".format(constants.DATA_DISK_KEY, ctx.node.id, ctx.instance.id))
		#if curr_disk_key in ctx.instance.runtime_properties:
		#	ctx.logger.info("Using disk {0}".format(ctx.instance.runtime_properties[curr_disk_key]))
		#	return

		utils.set_runtime_properties_from_file()
		current_disk_name = constants.DATA_DISK_PREFIX+utils.random_suffix_generator()
		ctx.instance.runtime_properties[curr_disk_key[x]] = current_disk_name
		ctx.logger.info("create_disk: {0} is {1}".format(curr_disk_key[x], current_disk_name))
		ctx.instance.runtime_properties[constants.DATA_DISK_SIZE_KEY] = ctx.node.properties[constants.DATA_DISK_SIZE_KEY]
		ctx.logger.info("create_disk: {0} is {1}".format(current_disk_name, ctx.instance.runtime_properties[constants.DATA_DISK_SIZE_KEY]))
def create_a_vm(**_):
    utils.set_runtime_properties_from_file()
    random_suffix_value = utils.random_suffix_generator()
    vm_name = ctx.node.properties[constants.VM_PREFIX]
    ctx.logger.info("Creating new virtual machine: {0}".format(vm_name))

    _set_ip_addresses_keys(vm_name)

    storage_account_name = ctx.instance.runtime_properties[constants.STORAGE_ACCOUNT_KEY]
    #availability_set_name = ctx.instance.runtime_properties[constants.AVAILABILITY_SET_KEY]
    availability_set_name = "To be developed by Pranjali and Vaidehi"
    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]

    virtual_machine_params = _get_virtual_machine_params(location, random_suffix_value, resource_group_name,
                                                        storage_account_name, subscription_id, vm_name, availability_set_name)
    virtual_machine_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/Microsoft.Compute/virtualMachines/'+vm_name+'?validating=true&api-version='+constants.api_version
    response_vm = requests.put(url=virtual_machine_url, data=virtual_machine_params, headers=headers)
    ctx.instance.runtime_properties[constants.VM_KEY] = vm_name
    return response_vm.status_code
Beispiel #9
0
def create_custom_script(**_):
    utils.set_runtime_properties_from_file()
    custom_script_name = constants.CUSTOM_SCRIPT_PREFIX + utils.random_suffix_generator(
    )
    ctx.instance.runtime_properties[
        constants.CUSTOM_SCRIPT_KEY] = custom_script_name
    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[
        constants.RESOURCE_GROUP_KEY]
    command = ctx.node.properties['custom_script_command']
    file_uri = ctx.node.properties['custom_script_path']
    vm_name = ctx.instance.runtime_properties[constants.VM_KEY]

    custom_script_url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/Microsoft.Compute/virtualMachines/' + vm_name + '/extensions/' + custom_script_name + '?api-version=' + constants.api_version
    custom_script_params = json.dumps({
        "id":
        "/subscriptions/" + subscription_id + "/resourceGroups/" +
        resource_group_name + "/providers/Microsoft.Compute/virtualMachines/" +
        vm_name + "/extensions/" + custom_script_name,
        "name":
        custom_script_name,
        "type":
        "Microsoft.Compute/virtualMachines/extensions",
        "location":
        location,
        "properties": {
            "publisher": "Microsoft.OSTCExtensions",
            "type": "CustomScriptForLinux",
            "typeHandlerVersion": "1.2",
            "settings": {
                "fileUris": [file_uri],
                "commandToExecute": command
            }
        }
    })
    response_as = requests.put(url=custom_script_url,
                               data=custom_script_params,
                               headers=headers)
    print response_as.text
def create_disk(**_):
    luns = ctx.node.properties['lun']
    for x in range(0, luns):
        curr_disk_key = []
        curr_disk_key.append("{0}{1}{2}".format(constants.DATA_DISK_KEY,
                                                ctx.node.id, ctx.instance.id))
        #if curr_disk_key in ctx.instance.runtime_properties:
        #	ctx.logger.info("Using disk {0}".format(ctx.instance.runtime_properties[curr_disk_key]))
        #	return

        utils.set_runtime_properties_from_file()
        current_disk_name = constants.DATA_DISK_PREFIX + utils.random_suffix_generator(
        )
        ctx.instance.runtime_properties[curr_disk_key[x]] = current_disk_name
        ctx.logger.info("create_disk: {0} is {1}".format(
            curr_disk_key[x], current_disk_name))
        ctx.instance.runtime_properties[
            constants.DATA_DISK_SIZE_KEY] = ctx.node.properties[
                constants.DATA_DISK_SIZE_KEY]
        ctx.logger.info("create_disk: {0} is {1}".format(
            current_disk_name,
            ctx.instance.runtime_properties[constants.DATA_DISK_SIZE_KEY]))
Beispiel #11
0
def create_subnet(**_):
    curr_subnet_key = "{0}{1}{2}".format(constants.SUBNET_KEY, ctx.node.id,
                                         ctx.instance.id)
    if curr_subnet_key in ctx.instance.runtime_properties:
        ctx.logger.info("create_subnet: Using subnet {0}".format(
            ctx.instance.runtime_properties[curr_subnet_key]))
        return

    utils.set_runtime_properties_from_file()
    if constants.SUBNET_PROPERTY in ctx.node.properties and ctx.node.properties[
            constants.SUBNET_PROPERTY]:
        current_subnet_name = ctx.node.properties[constants.SUBNET_PROPERTY]
        ctx.logger.info(
            "Using an existing subnet {0}".format(current_subnet_name))
    else:
        current_subnet_name = constants.SUBNET_PREFIX + utils.random_suffix_generator(
        )
        ctx.logger.info(
            "Creating a new subnet {0}".format(current_subnet_name))

    ctx.instance.runtime_properties[curr_subnet_key] = current_subnet_name
    ctx.logger.info("In create_subnet: {0} is {1}".format(
        curr_subnet_key, current_subnet_name))
Beispiel #12
0
def create_vnet(**_):

    vnet_name = utils.set_resource_name(_get_vnet_name, 'VNET', constants.VNET_KEY, constants.EXISTING_VNET_KEY,
                                        constants.VNET_PREFIX)
    if vnet_name is None:
        # Using an existing VNET, so don't create anything
        return

    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]

    if constants.VNET_KEY in ctx.instance.runtime_properties:
        current_subnet_name = ctx.instance.runtime_properties[constants.SUBNET_KEY]
    else:
        ctx.instance.runtime_properties[constants.VNET_KEY] = vnet_name
        current_subnet_name = constants.SUBNET_PREFIX+utils.random_suffix_generator()
        ctx.instance.runtime_properties[constants.SUBNET_KEY] = current_subnet_name

    check_vnet_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/virtualNetworks/'+vnet_name+'?api-version='+constants.api_version
    create_vnet_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/virtualNetworks/'+vnet_name+'?api-version='+constants.api_version
    vnet_params = json.dumps({"name": vnet_name, "location": location, "properties": {"addressSpace": {"addressPrefixes": constants.vnet_address_prefixes},"subnets": [{"name": current_subnet_name, "properties": {"addressPrefix": constants.address_prefix}}]}})
    utils.check_or_create_resource(headers, vnet_name, vnet_params, check_vnet_url, create_vnet_url, 'VNET')

    ctx.logger.info("{0} is {1}".format(constants.VNET_KEY, vnet_name))
def create_security_group(**_):
    if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[constants.USE_EXTERNAL_RESOURCE]:
        if constants.EXISTING_SECURITY_GROUP_KEY in ctx.node.properties:
            existing_security_group_name = ctx.node.properties[constants.EXISTING_SECURITY_GROUP_KEY]
            if existing_security_group_name:
                security_group_exists = _get_security_group_name(existing_security_group_name)
                if not security_group_exists:
                    raise NonRecoverableError("Security group {} doesn't exist your Azure account".format(existing_security_group_name))
            else:
                raise NonRecoverableError("The value of '{}' in the input, is empty".format(constants.EXISTING_SECURITY_GROUP_KEY))
        else:
            raise NonRecoverableError("'{}' was specified, but '{}' doesn't exist in the input".format('use_external_resource',constants.EXISTING_SECURITY_GROUP_KEY))

        ctx.instance.runtime_properties[constants.SECURITY_GROUP_KEY] = ctx.node.properties[constants.EXISTING_SECURITY_GROUP_KEY]
        return
    
    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]

    if constants.SECURITY_GROUP_KEY in ctx.instance.runtime_properties:
        security_group_name = ctx.instance.runtime_properties[constants.SECURITY_GROUP_KEY]
    else:
        random_suffix_value = utils.random_suffix_generator()
        security_group_name = constants.SECURITY_GROUP_PREFIX+random_suffix_value

    security_group_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/networkSecurityGroups/'+security_group_name+'?api-version='+constants.api_version
    try:
        ctx.logger.info("Creating a new security group: {0}".format(security_group_name))
        security_group_params = json.dumps({
        "location": location,
        "tags": {
            "key": "value"
        },
        "properties": {
            "securityRules": [
                {
                    "name": constants.nsg_rule_name,
                    "properties": {
                        "description": constants.NSG_RULES_DESCRIPTION,
                        "protocol": ctx.node.properties['security_group_protocol'],
                        "sourcePortRange": ctx.node.properties['security_group_sourcePortRange'] ,
                        "destinationPortRange": ctx.node.properties['security_group_destinationPortRange'],
                        "sourceAddressPrefix": ctx.node.properties['security_group_sourceAddressPrefix'],
                        "destinationAddressPrefix": ctx.node.properties['security_group_destinationAddressPrefix'],
                        "access": ctx.node.properties['security_group_access'],
                        "priority": ctx.node.properties['security_group_priority'],
                        "direction": ctx.node.properties['security_group_direction']
                    }
                }
             ]
          }
        })
        response_nsg = requests.put(url=security_group_url, data=security_group_params, headers=headers)
        if response_nsg.text:
            ctx.logger.info("create_security_group {0} response_nsg.text is {1}".format(security_group_name, response_nsg.text))
            if utils.request_failed("{0}:{1}".format('create_security_group', security_group_name), response_nsg):
                raise NonRecoverableError("create_security_group {0} could not be created".format(security_group_name))
        ctx.instance.runtime_properties[constants.SECURITY_GROUP_KEY] = security_group_name
    except:
        ctx.logger.info("Security Group {0} could not be created".format(security_group_name))
        raise NonRecoverableError("Security Group {} could not be created".format(security_group_name))