Ejemplo n.º 1
0
def get_provisioning_state(**_):
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    ctx.logger.info("Searching for resource group {0}".format(resource_group_name))
    headers, location, subscription_id = auth.get_credentials()

    resource_group_url = "{0}/subscriptions/{1}/resourceGroups/{2}?api-version={3}".format(constants.azure_url, subscription_id, resource_group_name, constants.api_version_resource_group)
    return azurerequests.get_provisioning_state(headers, resource_group_name, resource_group_url)
Ejemplo n.º 2
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')
Ejemplo n.º 3
0
def create_resource_group(**_):
    resource_group_name = utils.set_resource_name(_get_resource_group_name, 'Resource group',
                                                  constants.RESOURCE_GROUP_KEY, constants.EXISTING_RESOURCE_GROUP_KEY,
                                                  constants.RESOURCE_GROUP_PREFIX)
    if resource_group_name is None:
        # Using an existing resource group, so don't create anything
        return

    headers, location, subscription_id = auth.get_credentials()

    resource_group_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'?api-version='+constants.api_version_resource_group

    try:
        ctx.logger.info("Creating a new Resource group: {}".format(resource_group_name))
        resource_group_params = json.dumps({"name": resource_group_name, "location": location})
        response_rg = requests.put(url=resource_group_url, data=resource_group_params, headers=headers)
        if response_rg.text:
            ctx.logger.info("create_resource_group {0} response_rg.text is {1}".format(resource_group_name, response_rg.text))
            if utils.request_failed("{0}:{1}".format('create_resource_group', resource_group_name), response_rg):
                raise NonRecoverableError("Resource group {0} could not be created".format(resource_group_name))

        ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY] = resource_group_name
    except:
        ctx.logger.info("Resource Group {0} could not be created".format(resource_group_name))
        raise NonRecoverableError("Resource Group {0} could not be created".format(resource_group_name))
Ejemplo n.º 4
0
def check_if_vm_started(start_retry_interval, **kwargs):
    resource_group_name = ctx.instance.runtime_properties[
        constants.RESOURCE_GROUP_KEY]
    vm_name = ctx.instance.runtime_properties[constants.VM_KEY]

    start_vm_succeeded = ctx.instance.runtime_properties[
        constants.SERVER_STARTED]
    headers, location, subscription_id = auth.get_credentials()
    if not start_vm_succeeded:
        start_vm_succeeded, status_code = _start_vm_call(
            headers, vm_name, subscription_id, resource_group_name)
        ctx.logger.info(
            "set_ips: start_vm_succeeded is {0}, status code is {1}".format(
                start_vm_succeeded, status_code))
        if start_vm_succeeded:
            ctx.instance.runtime_properties[constants.SERVER_STARTED] = True
            ctx.logger.info("set_ips: vm has started")
        else:
            return ctx.operation.retry(
                message='Waiting for the server ({0}) to be started'.format(
                    vm_name),
                retry_after=start_retry_interval)
    else:
        ctx.logger.info("set_ips: vm has already started")

    _set_public_ip(subscription_id, resource_group_name, headers)
    return constants.OK_STATUS_CODE
Ejemplo n.º 5
0
def create_storage_account(**_):
    utils.set_runtime_properties_from_file()
    storage_account_name = utils.set_resource_name(
        _get_storage_account_name, 'Storage account',
        constants.STORAGE_ACCOUNT_KEY, constants.EXISTING_STORAGE_ACCOUNT_KEY,
        constants.STORAGE_ACCOUNT_PREFIX)
    if storage_account_name is None:
        # Using an existing storage account, so don't create anything
        return constants.ACCEPTED_STATUS_CODE

    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[
        constants.RESOURCE_GROUP_KEY]
    if constants.STORAGE_ACCOUNT_KEY not in ctx.instance.runtime_properties:
        ctx.instance.runtime_properties[
            constants.STORAGE_ACCOUNT_KEY] = storage_account_name

    ctx.logger.info(
        "Creating a new storage account: {0}".format(storage_account_name))
    storage_account_url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/Microsoft.Storage/storageAccounts/' + storage_account_name + '?api-version=' + constants.api_version
    storage_account_params = json.dumps({
        "properties": {
            "accountType": constants.storage_account_type,
        },
        "location": location
    })
    status_code = utils.create_resource(headers, storage_account_name,
                                        storage_account_params,
                                        storage_account_url,
                                        constants.STORAGE_ACCOUNT)
    ctx.logger.info("{0} is {1}".format(constants.STORAGE_ACCOUNT_KEY,
                                        storage_account_name))
    return status_code
Ejemplo n.º 6
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 
Ejemplo n.º 7
0
def create_vnet(**_):
    utils.set_runtime_properties_from_file()
    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 not in ctx.instance.runtime_properties:
        ctx.instance.runtime_properties[constants.VNET_KEY] = vnet_name

    create_vnet_url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/microsoft.network/virtualNetworks/' + vnet_name + '?api-version=' + constants.api_version_network
    vnet_json = _get_vnet_json(vnet_name, location, subscription_id,
                               resource_group_name)
    vnet_params = json.dumps(vnet_json)

    status_code = utils.create_resource(headers, vnet_name, vnet_params,
                                        create_vnet_url, 'VNET')
    ctx.logger.info("{0} is {1}".format(constants.VNET_KEY, vnet_name))
    return status_code
Ejemplo n.º 8
0
def delete_current_vnet(start_retry_interval=30, **kwargs):
    if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[
            constants.USE_EXTERNAL_RESOURCE]:
        ctx.logger.info(
            "An existing VNET was used, so there's no need to delete")
        return

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

    try:
        ctx.logger.info("Deleting the virtual network: {0}".format(vnet_name))
        vnet_url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/microsoft.network/virtualNetworks/' + vnet_name + '?api-version=' + constants.api_version_network
        response_vnet = requests.delete(url=vnet_url, headers=headers)
        return azurerequests.check_delete_response(response_vnet,
                                                   start_retry_interval,
                                                   'delete_current_vnet',
                                                   vnet_name, 'VNET')
    except:
        ctx.logger.info(
            "Virtual Network {0} could not be deleted.".format(vnet_name))
        raise NonRecoverableError(
            "Virtual Network {0} could not be created.".format(vnet_name))
Ejemplo n.º 9
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
Ejemplo n.º 10
0
def stop_a_vm(**_):
    headers, location, subscription_id = auth.get_credentials()
    vm_name = ctx.instance.runtime_properties[constants.VM_KEY]
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    stop_vm_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/Microsoft.Compute/virtualMachines/'+vm_name+'/start?api-version='+constants.api_version
    response_stop_vm = requests.post(stop_vm_url,headers=headers)
    print (response_stop_vm.text)
Ejemplo n.º 11
0
def main():

    print('script started')
    """
    google api code
    """
    credentials = auth.get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('drive', 'v3', http=http)
    """
    download file list
    """
    if config.DOWNLOAD_METADATA:
        downloadMetadata.downloadFileList(service)
    """
    create tree
    """
    nodeList = None
    if config.CREATE_TREE:
        nodeList = tree.createTree()
    """
    download files
    """
    if config.CREATE_STRUCTURE:
        createFileStructure.createStructure(nodeList, service)

    print('')
    print('script finished')
Ejemplo n.º 12
0
def delete_current_nic(start_retry_interval=30, **kwargs):
    if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[
            constants.USE_EXTERNAL_RESOURCE]:
        ctx.logger.info(
            "An existing NIC was used, so there's no need to delete")
        return

    ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True
    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[
        constants.RESOURCE_GROUP_KEY]
    nic_name = utils.key_in_runtime(constants.NIC_KEY,
                                    ends_with_key=False,
                                    starts_with_key=True,
                                    return_value=True)

    try:
        ctx.logger.info("Deleting NIC {0}".format(nic_name))
        nic_url = constants.azure_url + "/subscriptions/" + subscription_id + "/resourceGroups/" + resource_group_name + "/providers/microsoft.network/networkInterfaces/" + nic_name + "?api-version=" + constants.api_version
        response_nic = requests.delete(url=nic_url, headers=headers)
        return azurerequests.check_delete_response(response_nic,
                                                   start_retry_interval,
                                                   'delete_current_nic',
                                                   nic_name, 'NIC')
    except:
        ctx.logger.info(
            "Network Interface Card {0} could not be deleted.".format(
                nic_name))
Ejemplo n.º 13
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))
Ejemplo n.º 14
0
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
Ejemplo n.º 15
0
def delete_current_storage_account(start_retry_interval=30, **kwargs):
    if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[
            constants.USE_EXTERNAL_RESOURCE]:
        ctx.logger.info(
            "An existing storage_account was used, so there's no need to delete"
        )
        return constants.ACCEPTED_STATUS_CODE

    ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True
    resource_group_name = ctx.instance.runtime_properties[
        constants.RESOURCE_GROUP_KEY]
    headers, location, subscription_id = auth.get_credentials()
    storage_account_name = ctx.instance.runtime_properties[
        constants.STORAGE_ACCOUNT_KEY]
    ctx.logger.info(
        "Deleting Storage Account {0}".format(storage_account_name))
    try:
        storage_account_url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/Microsoft.Storage/storageAccounts/' + storage_account_name + '?api-version=' + constants.api_version
        response_sa = requests.delete(url=storage_account_url, headers=headers)
        return azurerequests.check_delete_response(
            response_sa, start_retry_interval,
            'delete_current_storage_account', storage_account_name,
            'storage_account')
    except:
        ctx.logger.info("Storage Account {0} could not be deleted.".format(
            storage_account_name))
    return constants.FAILURE_CODE
Ejemplo n.º 16
0
def send_email(subject, text):
    print("sending email")

    credentials = auth.get_credentials()
    gmail_service = auth.get_gmail_service(credentials)

    message = mail.create_message("*****@*****.**", subject, text)
    mail.send_message(gmail_service, message)
Ejemplo n.º 17
0
def stop_a_vm(**_):
    headers, location, subscription_id = auth.get_credentials()
    vm_name = ctx.instance.runtime_properties[constants.VM_KEY]
    resource_group_name = ctx.instance.runtime_properties[
        constants.RESOURCE_GROUP_KEY]
    stop_vm_url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/Microsoft.Compute/virtualMachines/' + vm_name + '/start?api-version=' + constants.api_version
    response_stop_vm = requests.post(stop_vm_url, headers=headers)
    print(response_stop_vm.text)
Ejemplo n.º 18
0
def setService():
    global service

    credentials = auth.get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('drive', 'v3', http=http)

    return True
Ejemplo n.º 19
0
def main(flags,company_name,position_name,description):
    credentials = auth.get_credentials(flags,CLIENT_SECRET_PATH,SCOPES,CREDENTIALS_PATH)
    service = driveutils.get_drive_service(credentials)

    if position_name:
        createPosition(service,company_name,position_name,description)
    else:
        createCompany(service,company_name)
Ejemplo n.º 20
0
def main(flags, company_name, position_name, description):
    credentials = auth.get_credentials(flags, CLIENT_SECRET_PATH, SCOPES,
                                       CREDENTIALS_PATH)
    service = driveutils.get_drive_service(credentials)

    if position_name:
        createPosition(service, company_name, position_name, description)
    else:
        createCompany(service, company_name)
Ejemplo n.º 21
0
def _get_nic_name(nic_name):
    if constants.RESOURCE_GROUP_KEY in ctx.instance.runtime_properties:
        resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    else:
        raise RecoverableError("{0} is not in nic runtime_properties yet".format(constants.RESOURCE_GROUP_KEY))
    headers, location, subscription_id = auth.get_credentials()
    nic_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/networkInterfaces?api-version='+constants.api_version
    response_get_nic_name = requests.get(url=nic_url,headers=headers)
    if nic_name in response_get_nic_name.text:
        return True
    return False
Ejemplo n.º 22
0
def _get_public_ip_name(public_ip_name):
    if constants.RESOURCE_GROUP_KEY in ctx.instance.runtime_properties:
        resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    else:
        raise RecoverableError("{0} is not in public ip runtime_properties yet.".format(constants.RESOURCE_GROUP_KEY))
    headers, location, subscription_id = auth.get_credentials()
    pip_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/publicIPAddresses?api-version='+constants.api_version
    response_get_pip = requests.get(url=pip_url,headers=headers)
    if public_ip_name in response_get_pip.text:
        return True
    else:
        return False
Ejemplo n.º 23
0
def _get_public_ip_name(public_ip_name):
    if constants.RESOURCE_GROUP_KEY in ctx.instance.runtime_properties:
        resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    else:
        raise RecoverableError("{0} is not in public ip runtime_properties yet.".format(constants.RESOURCE_GROUP_KEY))
    headers, location, subscription_id = auth.get_credentials()
    pip_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/publicIPAddresses?api-version='+constants.api_version_network
    response_get_pip = requests.get(url=pip_url,headers=headers)
    if public_ip_name in response_get_pip.text:
        return True
    else:
        return False
Ejemplo n.º 24
0
def _get_resource_group_name(resource_group_name):
    ctx.logger.info("In _get_resource_group_name looking for {0}".format(resource_group_name))
    credentials = 'Bearer ' + auth.get_auth_token()
    ctx.logger.info("In _get_resource_group_name credentials is {0}".format(credentials))
    headers, location, subscription_id = auth.get_credentials()
    list_resource_group_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourcegroups?api-version='+constants.api_version_resource_group
    response_get_resource_group = requests.get(url=list_resource_group_url, headers=headers)
    ctx.logger.info("response_get_resource_group.text {0} ".format(response_get_resource_group.text))
    if resource_group_name in response_get_resource_group.text:
        return True
    else:
        return False
Ejemplo n.º 25
0
def get_provisioning_state(**_):
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]

    vnet_name = ctx.instance.runtime_properties[constants.VNET_KEY]

    ctx.logger.info("Searching for VNET {0}".format(vnet_name))
    headers, location, subscription_id = auth.get_credentials()

    check_vnet_url = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.network/" \
                          "virtualNetworks/{3}?api-version={4}".format(constants.azure_url,
                                subscription_id, resource_group_name, vnet_name, constants.api_version_network)
    return azurerequests.get_provisioning_state(headers, resource_group_name, check_vnet_url)
Ejemplo n.º 26
0
def get_provisioning_state(**_):
    current_instance = utils.get_instance_or_source_instance()
    resource_group_name = current_instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    vm_name = current_instance.runtime_properties[constants.VM_KEY]

    ctx.logger.info("Searching for {0}".format(vm_name))
    headers, location, subscription_id = auth.get_credentials()

    virtual_machine_url = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.Compute/" \
                          "virtualMachines/{3}?validating=true&api-version={4}".format(constants.azure_url,
                                subscription_id, resource_group_name, vm_name, constants.api_version)
    return azurerequests.get_provisioning_state(headers, resource_group_name, virtual_machine_url)
def get_provisioning_state(**_):
    utils.validate_node_properties(constants.STORAGE_ACCOUNT_REQUIRED_PROPERTIES, ctx.node.properties)

    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    storage_account_name = ctx.instance.runtime_properties[constants.STORAGE_ACCOUNT_KEY]
    ctx.logger.info("Searching for storage account {0} in resource group {1}".format(storage_account_name, resource_group_name))
    headers, location, subscription_id = auth.get_credentials()

    storage_account_url = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/" \
                          "Microsoft.Storage/storageAccounts/{3}?api-version={4}".\
        format(constants.azure_url, subscription_id, resource_group_name, storage_account_name, constants.api_version)
    return azurerequests.get_provisioning_state(headers, storage_account_name, storage_account_url)
Ejemplo n.º 28
0
def get_provisioning_state(**_):
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]

    public_ip_name = ctx.instance.runtime_properties[constants.PUBLIC_IP_KEY]

    ctx.logger.info("Searching for public ip {0}".format(public_ip_name))
    headers, location, subscription_id = auth.get_credentials()

    check_public_ip_url = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.network/" \
                          "publicIPAddresses/{3}?api-version={4}".format(constants.azure_url,
                                subscription_id, resource_group_name, public_ip_name, constants.api_version_network)

    return azurerequests.get_provisioning_state(headers, resource_group_name, check_public_ip_url)
Ejemplo n.º 29
0
def glanceList():
    print "glance image-list"
    credentials = auth.get_credentials()
    keystone = ksclient.Client(**credentials)
    token = keystone.auth_token

    print "auth_token = " + token

    glance_endpoint = keystone.service_catalog.url_for(service_type='image')
    glance = glclient.Client(glance_endpoint, token=token)

    images = glance.images.list()
    print list(images)
Ejemplo n.º 30
0
def glanceList():
    print "glance image-list"
    credentials = auth.get_credentials()
    keystone = ksclient.Client(**credentials)
    token = keystone.auth_token

    print "auth_token = " + token

    glance_endpoint = keystone.service_catalog.url_for(service_type='image')
    glance = glclient.Client(glance_endpoint, token=token)

    images = glance.images.list()
    print list(images)
Ejemplo n.º 31
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')
Ejemplo n.º 32
0
def _get_security_group_name(security_group_name):  
    if constants.RESOURCE_GROUP_KEY in ctx.instance.runtime_properties:
        resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    else:
        raise RecoverableError("{} is not in public ip runtime_properties yet.".format(constants.RESOURCE_GROUP_KEY))
    headers, location, subscription_id = auth.get_credentials()
    list_security_group_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/networkSecurityGroups?api-version='+constants.api_version
    response_get_security_group = requests.get(url=list_security_group_url, headers=headers)
   
    if security_group_name in response_get_security_group.text:
        return True
    else:
        ctx.logger.info("Security group {} does not exist".format(security_group_name))
        return False
Ejemplo n.º 33
0
def get_provisioning_state(**_):
    current_instance = utils.get_instance_or_source_instance()
    resource_group_name = current_instance.runtime_properties[
        constants.RESOURCE_GROUP_KEY]
    vm_name = current_instance.runtime_properties[constants.VM_KEY]

    ctx.logger.info("Searching for {0}".format(vm_name))
    headers, location, subscription_id = auth.get_credentials()

    virtual_machine_url = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.Compute/" \
                          "virtualMachines/{3}?validating=true&api-version={4}".format(constants.azure_url,
                                subscription_id, resource_group_name, vm_name, constants.api_version)
    return azurerequests.get_provisioning_state(headers, resource_group_name,
                                                virtual_machine_url)
Ejemplo n.º 34
0
def _get_security_group_name(security_group_name):  
    if constants.RESOURCE_GROUP_KEY in ctx.instance.runtime_properties:
        resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    else:
        raise RecoverableError("{} is not in public ip runtime_properties yet.".format(constants.RESOURCE_GROUP_KEY))
    headers, location, subscription_id = auth.get_credentials()
    list_security_group_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/networkSecurityGroups?api-version='+constants.api_version
    response_get_security_group = requests.get(url=list_security_group_url, headers=headers)
   
    if security_group_name in response_get_security_group.text:
        return True
    else:
        ctx.logger.info("Security group {} does not exist".format(security_group_name))
        return False
Ejemplo n.º 35
0
def delete_current_security_group(**_):
    if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[constants.USE_EXTERNAL_RESOURCE]:
        ctx.logger.info("An existing security group was used, so there's no need to delete")
        return

    security_group_name = ctx.instance.runtime_properties[constants.SECURITY_GROUP_KEY]
    headers, location, subscription_id = auth.get_credentials()
    try:
        ctx.logger.info("Deleting Security Group: {0}".format(security_group_name))
        security_group_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+security_group_name+'/providers/microsoft.network/networkSecurityGroups/'+security_group_name+'?api-version='+constants.api_version
        response_nsg = requests.delete(url=security_group_url, headers=headers)
        print(response_nsg.text)
    except:
        ctx.logger.info("Security Group {0} could not be deleted.".format(security_group_name))
Ejemplo n.º 36
0
def delete_current_virtual_machine(start_retry_interval=30, **kwargs):
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    headers, location, subscription_id = auth.get_credentials()
    vm_name = ctx.instance.runtime_properties[constants.VM_KEY]
    ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True

    try:
        ctx.logger.info("Deleting the virtual machine: {0}".format(vm_name))
        vm_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.delete(url=vm_url, headers=headers)
        return azurerequests.check_delete_response(response_vm, start_retry_interval,
                                                   'delete_current_virtual_machine', vm_name, 'VM')
    except:
        ctx.logger.info("Virtual Machine {0} could not be deleted".format(vm_name))
Ejemplo n.º 37
0
def _delete_os_disk(start_retry_interval):
    headers, location, subscription_id = auth.get_credentials()

    os_disk_name = ctx.instance.runtime_properties[constants.OS_DISK_NAME]
    delete_os_disk_url = ctx.instance.runtime_properties[constants.OS_DISK_URI]
    ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True

    try:
        ctx.logger.info("Deleting the OS Disk {0}: {1}".format(os_disk_name, delete_os_disk_url))
        response_delete_os_disk = requests.delete(url=delete_os_disk_url, headers=headers)
        return azurerequests.check_delete_response(response_delete_os_disk, start_retry_interval,
                                                   '_delete_os_disk', os_disk_name, constants.OS_DISK)
    except:
        ctx.logger.info("OS disk {0} could not be deleted".format(delete_os_disk_url))
Ejemplo n.º 38
0
def get_provisioning_state(**_):
    resource_group_name = ctx.instance.runtime_properties[
        constants.RESOURCE_GROUP_KEY]

    vnet_name = ctx.instance.runtime_properties[constants.VNET_KEY]

    ctx.logger.info("Searching for VNET {0}".format(vnet_name))
    headers, location, subscription_id = auth.get_credentials()

    check_vnet_url = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.network/" \
                          "virtualNetworks/{3}?api-version={4}".format(constants.azure_url,
                                subscription_id, resource_group_name, vnet_name, constants.api_version_network)
    return azurerequests.get_provisioning_state(headers, resource_group_name,
                                                check_vnet_url)
Ejemplo n.º 39
0
def _get_nic_name(nic_name):
    if constants.RESOURCE_GROUP_KEY in ctx.instance.runtime_properties:
        resource_group_name = ctx.instance.runtime_properties[
            constants.RESOURCE_GROUP_KEY]
    else:
        raise RecoverableError(
            "{0} is not in nic runtime_properties yet".format(
                constants.RESOURCE_GROUP_KEY))
    headers, location, subscription_id = auth.get_credentials()
    nic_url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/microsoft.network/networkInterfaces?api-version=' + constants.api_version
    response_get_nic_name = requests.get(url=nic_url, headers=headers)
    if nic_name in response_get_nic_name.text:
        return True
    return False
Ejemplo n.º 40
0
def _get_vnet_name(vnet_name):
    ctx.logger.info("In _get_vnet_name looking for {0} ".format(vnet_name))
    if constants.RESOURCE_GROUP_KEY in ctx.instance.runtime_properties:
        resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    else:
        raise RecoverableError("{0} is not in vnet runtime_properties yet".format(constants.RESOURCE_GROUP_KEY))
    headers, location, subscription_id = auth.get_credentials()
    url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/virtualnetworks?api-version='+constants.api_version
    response_list = requests.get(url, headers=headers)
    ctx.logger.info("VNET response_list.text {0}".format(response_list.text))
    if vnet_name in response_list.text:
        return True
    else:
        ctx.logger.info("Virtual Network {0} does not exist".format(vnet_name))
        return False
Ejemplo n.º 41
0
def get_provisioning_state(**_):
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]

    vm_name = ctx.instance.runtime_properties[constants.VM_KEY]
    custom_script_name = ctx.instance.runtime_properties[constants.CUSTOM_SCRIPT_KEY]

    ctx.logger.info("Searching for custom script {0} for VM {1}".format(custom_script_name, vm_name))
    headers, location, subscription_id = auth.get_credentials()

    custom_script_url = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.Compute/" \
                          "virtualMachines/{3}/extensions/{4}?api-version={5}".format(constants.azure_url,
                                subscription_id, resource_group_name, vm_name, custom_script_name,
                                constants.api_version)

    return azurerequests.get_provisioning_state(headers, resource_group_name, custom_script_url)
Ejemplo n.º 42
0
def get_provisioning_state(**_):
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]

    nic_key = _get_nic_key()
    nic_name = ctx.instance.runtime_properties[nic_key]

    ctx.logger.info("Searching for NIC {0}".format(nic_name))
    headers, location, subscription_id = auth.get_credentials()

    network_str = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/".format(subscription_id,
                                                                                              resource_group_name)
    check_nic_url = "{0}{1}/networkInterfaces/{2}?api-version={3}".format(constants.azure_url, network_str, nic_name,
                                                                          constants.api_version_network)
    return azurerequests.get_provisioning_state(headers, resource_group_name, check_nic_url,
                                                save_successful_response=True)
Ejemplo n.º 43
0
def delete_current_resource_group(**_):
    if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[constants.USE_EXTERNAL_RESOURCE]:
        ctx.logger.info("An existing resource group was used, so there's no need to delete")
        return

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

    try:
        ctx.logger.info("Deleting Resource Group: {0}".format(resource_group_name))
        resource_group_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'?api-version='+constants.api_version_resource_group
        response_rg = requests.delete(url=resource_group_url, headers=headers)
        print(response_rg.text)
    except:
        ctx.logger.info("Resource Group {0} could not be deleted.".format(resource_group_name))
def delete_current_availability_set(**_):
    if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[constants.USE_EXTERNAL_RESOURCE]:
        ctx.logger.info("An existing availabilty set was used, so there's no need to delete")
        return
    
    availability_set_name = ctx.instance.runtime_properties[constants.AVAILABILTY_SET_KEY]
    headers, location, subscription_id = auth.get_credentials()
    try:
        ctx.logger.info("Deleting Availabilty set: {0}".format(availability_set_name))
        resource_group_name = "modify later"
        delete_url = 'https://management.azure.com/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/Microsoft.Compute/availabilitySets/'+availability_set_name+'?api-version=2015-05-01-preview'
        response_as = requests.delete(url=delete_url, headers=headers)
        print(response_as.text)
    except:
        ctx.logger.info("Availability set {0} could not be deleted.".format(availability_set_name))
Ejemplo n.º 45
0
def main():
    """Shows basic usage of the Gmail API.

        Creates a Gmail API service object and outputs a list of label names
        of the user's Gmail account.
    """
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('gmail', 'v1', http=http)
    user_id = 'me'

    get_all_labels(service,user_id)
    fetch_and_store_in_db(service,user_id)
    
    execute_rules()
Ejemplo n.º 46
0
def delete_current_virtual_machine(**_):
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    headers, location, subscription_id = auth.get_credentials()
    vm_name = ctx.instance.runtime_properties[constants.VM_KEY]

    try:
        ctx.logger.info("Deleting the virtual machine: {}".format(vm_name))
        vm_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.delete(url=vm_url, headers=headers)
        if response_vm.text:
            ctx.logger.info("delete_current_virtual_machine response is {0}".format(response_vm.text))
        else:
            ctx.logger.info("delete_current_virtual_machine status code is {0}".format(response_vm.status_code))
    except:
        ctx.logger.info("Virtual Machine {} could not be deleted".format(vm_name))
Ejemplo n.º 47
0
def main():

    # initialize display during boot
    display.clear()
    display.print_number_str("0000")
    display.write_display()

    # 40 second delay for network to come up during auto start at boot
    sleep(40)

    assignment_dict = {}

    print("Attempting to get credentials...")

    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('classroom', 'v1', http=http)

    print("Credentials seem to have been obtained...")

    print("Application is now running...")

    global_count = 0

    while True:

        sleep(1)

        print("Pre check assignment dict", assignment_dict)

        for course in courses:

            count = tally(service, course, assignment_dict)

            print("Post check assignment dict", assignment_dict)

        if count != global_count:

            buzzer.on()
            sleep(0.40)
            buzzer.off()
            global_count = count

        display.clear()

        display.print_number_str(str(global_count))

        display.write_display()
Ejemplo n.º 48
0
def create_availability_set(**_):
    utils.set_runtime_properties_from_file()
    availability_set_name = ''
    if availability_set_name is None:
        return

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

    availability_set_url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/Microsoft.Compute/availabilitySets/' + availability_set_name + '?api-version='

    try:
        ctx.logger.info(
            "Creating new availability set: {0}".format(availability_set_name))
        availability_set_params = json.dumps({
            "name": availability_set_name,
            "type": "Microsoft.Compute/availabilitySets",
            "location": location
        })

        response_as = requests.put(url=availability_set_url,
                                   data=availability_set_params,
                                   headers=headers)
        if response_as.text:
            ctx.logger.info(
                "create_availability_set {0} response_as.text is {1}".format(
                    availability_set_name, response_as.text))
            if utils.request_failed(
                    "{0}:{1}".format('create_availability_set',
                                     availability_set_name), response_as):
                raise NonRecoverableError(
                    "create_availabilty_set {0} could not be created".format(
                        availability_set_name))
        ctx.instance.runtime_properties[
            constants.AVAILABILTY_SET_KEY] = availability_set_name
    except:
        ctx.logger.info("Availabilty set {0} could not be created".format(
            availability_set_name))
        raise NonRecoverableError(
            "Availabilty Set {} could not be created".format(
                availability_set_name))
Ejemplo n.º 49
0
def _delete_data_disks(start_retry_interval):
    headers, location, subscription_id = auth.get_credentials()
    storage_account_name = ctx.instance.runtime_properties[constants.STORAGE_ACCOUNT_KEY]
    for curr_key in ctx.instance.runtime_properties:
        if curr_key.startswith(constants.DATA_DISK_KEY):
            disk_name = ctx.instance.runtime_properties[curr_key]
            ctx.logger.info("_delete_data_disks : disk_name is {0}".format(disk_name))
            delete_data_disk_url = "https://{0}.blob.core.windows.net/vhds/{1}.vhd".format(storage_account_name, disk_name)
            ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True    
            try:
                ctx.logger.info("Deleting the data Disk {0}: {1}".format(disk_name, delete_data_disk_url))
                response_delete_data_disk = requests.delete(url=delete_data_disk_url, headers=headers)
                return azurerequests.check_delete_response(response_delete_data_disk, start_retry_interval,
                                                   '_delete_data_disks', disk_name, constants.OS_DISK)
            except:
                ctx.logger.info("Data disk {0} could not be deleted".format(delete_data_disk_url))
Ejemplo n.º 50
0
def delete_current_nic(**_):
    if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[constants.USE_EXTERNAL_RESOURCE]:
        ctx.logger.info("An existing NIC was used, so there's no need to delete")
        return

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

    try:
        ctx.logger.info("Deleting NIC {0}".format(nic_name))
        nic_url = constants.azure_url+"/subscriptions/"+subscription_id+"/resourceGroups/"+resource_group_name+"/providers/microsoft.network/networkInterfaces/"+nic_name+"?api-version="+constants.api_version
        response_nic = requests.delete(url=nic_url, headers=headers)
        print(response_nic.text)
    except:
        ctx.logger.info("Network Interface Card {0} could not be deleted.".format(nic_name))
Ejemplo n.º 51
0
def start_a_vm(start_retry_interval, **kwargs):

    resource_group_name = ctx.source.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    vm_name = ctx.source.instance.runtime_properties[constants.VM_KEY]

    curr_status = get_provisioning_state()
    if curr_status != constants.SUCCEEDED:
        return ctx.operation.retry(
            message='Waiting for the server ({0}) to be provisioned'.format(vm_name),
            retry_after=start_retry_interval)

    headers, location, subscription_id = auth.get_credentials()
    start_vm_succeeded, status_code = _start_vm_call(headers, vm_name, subscription_id, resource_group_name)
    ctx.logger.info("start_a_vm: start_vm_succeeded is {0}, status code is {1}".format(start_vm_succeeded, status_code))
    ctx.source.instance.runtime_properties[constants.SERVER_STARTED] = start_vm_succeeded
    return constants.OK_STATUS_CODE
def get_storageaccount_access_keys(**_):

    ctx.logger.info("In get_storageaccount_access_keys")
    headers, location, subscription_id = auth.get_credentials()
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    ctx.logger.info("In get_storageaccount_access_keys resource group is {0}".format(resource_group_name))
    storage_account_name = ctx.instance.runtime_properties[constants.STORAGE_ACCOUNT_KEY]

    api_version = constants.api_version
    keys_url = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.Storage/" \
                  "storageAccounts/{3}/listKeys?api-version={4}"\
                    .format(constants.azure_url, subscription_id, resource_group_name,
                    storage_account_name, api_version)

    response = requests.post(url=keys_url, data="{}", headers=headers)
    result = response.json()
    return [result['key1'], result['key2']]
Ejemplo n.º 53
0
def delete_current_public_ip(**_):
    if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[constants.USE_EXTERNAL_RESOURCE]:
        ctx.logger.info("An existing Public IP was used, so there's no need to delete")
        return

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

    try:
        ctx.logger.info("Deleting Public IP")
        public_ip_url = constants.azure_url+'/subscriptions/'+subscription_id+'/resourceGroups/'+resource_group_name+'/providers/microsoft.network/ publicIPAddresses/'+public_ip_name+'?api-version='+constants.api_version
        response_pip = requests.delete(url=public_ip_url, headers=headers)
        print(response_pip.text)

    except:
        ctx.logger.info("Public IP {0} could not be deleted.".format(public_ip_name))
Ejemplo n.º 54
0
def delete_current_virtual_machine(start_retry_interval=30, **kwargs):
    resource_group_name = ctx.instance.runtime_properties[
        constants.RESOURCE_GROUP_KEY]
    headers, location, subscription_id = auth.get_credentials()
    vm_name = ctx.instance.runtime_properties[constants.VM_KEY]
    ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True

    try:
        ctx.logger.info("Deleting the virtual machine: {0}".format(vm_name))
        vm_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.delete(url=vm_url, headers=headers)
        return azurerequests.check_delete_response(
            response_vm, start_retry_interval,
            'delete_current_virtual_machine', vm_name, 'VM')
    except:
        ctx.logger.info(
            "Virtual Machine {0} could not be deleted".format(vm_name))
Ejemplo n.º 55
0
def _get_vnet_name(vnet_name):
    ctx.logger.info("In _get_vnet_name looking for {0} ".format(vnet_name))
    if constants.RESOURCE_GROUP_KEY in ctx.instance.runtime_properties:
        resource_group_name = ctx.instance.runtime_properties[
            constants.RESOURCE_GROUP_KEY]
    else:
        raise RecoverableError(
            "{0} is not in vnet runtime_properties yet".format(
                constants.RESOURCE_GROUP_KEY))
    headers, location, subscription_id = auth.get_credentials()
    url = constants.azure_url + '/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group_name + '/providers/microsoft.network/virtualnetworks?api-version=' + constants.api_version_network
    response_list = requests.get(url, headers=headers)
    ctx.logger.info("VNET response_list.text {0}".format(response_list.text))
    if vnet_name in response_list.text:
        return True
    else:
        ctx.logger.info("Virtual Network {0} does not exist".format(vnet_name))
        return False
Ejemplo n.º 56
0
def delete_current_security_group(start_retry_interval=30, **kwargs):
    if constants.USE_EXTERNAL_RESOURCE in ctx.node.properties and ctx.node.properties[constants.USE_EXTERNAL_RESOURCE]:
        ctx.logger.info("An existing security group was used, so there's no need to delete")
        return

    ctx.instance.runtime_properties[constants.RESOURCE_NOT_DELETED] = True
    resource_group_name = ctx.instance.runtime_properties[constants.RESOURCE_GROUP_KEY]
    security_group_name = ctx.instance.runtime_properties[constants.SECURITY_GROUP_KEY]
    headers, location, subscription_id = auth.get_credentials()
    try:
        ctx.logger.info("Deleting Security Group: {0}".format(security_group_name))
        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
        response_nsg = requests.delete(url=security_group_url, headers=headers)
        return azurerequests.check_delete_response(response_nsg, start_retry_interval,
                                                   'delete_current_security_group', security_group_name,
                                                   'security_group')
    except:
        ctx.logger.info("Security Group {0} could not be deleted.".format(security_group_name))
Ejemplo n.º 57
0
def get_provisioning_state(**_):
    utils.validate_node_properties(
        constants.STORAGE_ACCOUNT_REQUIRED_PROPERTIES, ctx.node.properties)

    resource_group_name = ctx.instance.runtime_properties[
        constants.RESOURCE_GROUP_KEY]
    storage_account_name = ctx.instance.runtime_properties[
        constants.STORAGE_ACCOUNT_KEY]
    ctx.logger.info(
        "Searching for storage account {0} in resource group {1}".format(
            storage_account_name, resource_group_name))
    headers, location, subscription_id = auth.get_credentials()

    storage_account_url = "{0}/subscriptions/{1}/resourceGroups/{2}/providers/" \
                          "Microsoft.Storage/storageAccounts/{3}?api-version={4}".\
        format(constants.azure_url, subscription_id, resource_group_name, storage_account_name, constants.api_version)
    return azurerequests.get_provisioning_state(headers, storage_account_name,
                                                storage_account_url)