예제 #1
0
 def update_model(self, newsku, newversion, newvmsize):
     changes = 0
     if self.sku != newsku:
         if self.image_type == 'platform':  # sku not relevant for custom image
             changes += 1
             self.model['properties']['virtualMachineProfile']['storageProfile']['imageReference']['sku'] = newsku
             self.sku = newsku
         else:
             self.status = 'You cannot change sku setting for custom image'
     if self.version != newversion:
         changes += 1
         self.version = newversion
         if self.image_type == 'platform':
             self.model['properties']['virtualMachineProfile']['storageProfile']['imageReference']['version'] = newversion
         else:
             self.model['properties']['virtualMachineProfile']['storageProfile']['osDisk']['image']['uri'] = newversion
     if self.vmsize != newvmsize:
         changes += 1
         self.model['sku']['name'] = newvmsize # to do - add a check that the new vm size matches the tier
         self.vmsize = newvmsize
     if changes == 0:
         self.status = 'VMSS model is unchanged, skipping update'
     else:
         # put the vmss model
         updateresult = azurerm.update_vmss(self.access_token, self.sub_id, self.rgname, self.name,
                                            json.dumps(self.model))
         self.status = updateresult
예제 #2
0
파일: vmss.py 프로젝트: praneethr/vmsstools
 def update_extns(self, extnprofile):
     self.model['properties']['virtualMachineProfile'][
         'extensionProfile'] = extnprofile
     # put the vmss model
     updateresult = azurerm.update_vmss(self.access_token, self.sub_id,
                                        self.rgname, self.name,
                                        json.dumps(self.model))
     self.status = updateresult
예제 #3
0
    def update_model(self, newsku, newversion, newvmsize):
        '''update the VMSS model with any updated properties'''
        changes = 0
        if self.sku != newsku:
            if self.image_type == 'platform':  # sku not relevant for custom image
                changes += 1
                self.model['properties']['virtualMachineProfile'][
                    'storageProfile']['imageReference']['sku'] = newsku
                self.sku = newsku
            else:
                self.status = 'You cannot change sku setting for custom image'
        if self.version != newversion:
            changes += 1
            self.version = newversion
            if self.image_type == 'platform':  # for platform image modify image reference
                self.model['properties']['virtualMachineProfile'][
                    'storageProfile']['imageReference']['version'] = newversion
            else:
                # check for managed disk
                if 'imageReference' in self.model['properties'][
                        'virtualMachineProfile']['storageProfile']:
                    self.model['properties']['virtualMachineProfile'][
                        'storageProfile']['imageReference'][
                            'id'] = self.image_resource_id + '.Compute/' + newversion
                else:
                    # unmanaged custom image - has a URI which points directly
                    # to image blob
                    self.model['properties']['virtualMachineProfile'][
                        'storageProfile']['osDisk']['image'][
                            'uri'] = newversion

        if self.vmsize != newvmsize:
            changes += 1
            # to do - add a check that the new vm size matches the tier
            self.model['sku']['name'] = newvmsize
            self.vmsize = newvmsize
        if changes == 0:
            self.status = 'VMSS model is unchanged, skipping update'
        else:
            # put the vmss model
            updateresult = azurerm.update_vmss(self.access_token, self.sub_id,
                                               self.rgname, self.name,
                                               json.dumps(self.model))
            self.status = updateresult
예제 #4
0
def updatevmss():
    newversion = versiontext.get()
    vmssname = vmsstext.get()
    resource_group = selectedrg.get()
    try:
        vmssmodel = azurerm.get_vmss(access_token, subscription_id, resource_group, vmssname)
        imagereference = vmssmodel['properties']['virtualMachineProfile']['storageProfile']['imageReference']
    except KeyError:
        statusmsg('KeyError: azurerm.get_vmss() returned: ' + vmssmodel)
        return

    if imagereference['version'] != newversion:
        vmssmodel['properties']['virtualMachineProfile']['storageProfile']['imageReference']['version'] = newversion
        # put the vmss model
        updateresult = azurerm.update_vmss(access_token, subscription_id, resource_group, vmssname,
                                           json.dumps(vmssmodel))
        statusmsg(updateresult)
    else:
        statusmsg('Versions are the same, skipping update')
예제 #5
0
파일: vmss.py 프로젝트: gbowerman/vmsstools
 def update_model(self, newversion, newvmsize):
     changes = 0
     if self.version != newversion:
         changes += 1
         if self.image_type == 'platform':
             self.model['properties']['virtualMachineProfile']['storageProfile']['imageReference']['version'] = newversion
         else:
             self.model['properties']['virtualMachineProfile']['storageProfile']['osDisk']['image']['uri'] = newversion
     if self.vmsize != newvmsize:
         changes += 1
         self.model['sku']['name'] = newvmsize # to do - add a check that the new vm size matches the tier
         self.version = newversion
         self.vmsize = newvmsize
     if changes == 0:
         self.status = 'VMSS model is unchanged, skipping update'
     else:
         # put the vmss model
         updateresult = azurerm.update_vmss(self.access_token, self.sub_id, self.rgname, self.name,
                                            json.dumps(self.model))
         self.status = updateresult
예제 #6
0
    def update_model(self, newsku, newversion, newvmsize):
        '''update the VMSS model with any updated properties'''
        changes = 0
        if self.sku != newsku:
            if self.image_type == 'platform':  # sku not relevant for custom image
                changes += 1
                self.model['properties']['virtualMachineProfile']['storageProfile']['imageReference']['sku'] = newsku
                self.sku = newsku
            else:
                self.status = 'You cannot change sku setting for custom image'
        if self.version != newversion:
            changes += 1
            self.version = newversion
            if self.image_type == 'platform':  # for platform image modify image reference
                self.model['properties']['virtualMachineProfile']['storageProfile']['imageReference']['version'] = newversion
            else:
                # check for managed disk
                if 'imageReference' in self.model['properties']['virtualMachineProfile']['storageProfile']:
                    self.model['properties']['virtualMachineProfile']['storageProfile'][
                        'imageReference']['id'] = self.image_resource_id + '.Compute/' + newversion
                else:
                    # unmanaged custom image - has a URI which points directly
                    # to image blob
                    self.model['properties']['virtualMachineProfile']['storageProfile']['osDisk']['image']['uri'] = newversion

        if self.vmsize != newvmsize:
            changes += 1
            # to do - add a check that the new vm size matches the tier
            self.model['sku']['name'] = newvmsize
            self.vmsize = newvmsize
        if changes == 0:
            self.status = 'VMSS model is unchanged, skipping update'
        else:
            # put the vmss model
            updateresult = azurerm.update_vmss(self.access_token, self.sub_id, self.rgname,
                                               self.name, json.dumps(self.model))
            self.status = updateresult
예제 #7
0
def main():
    # create parser
    argParser = argparse.ArgumentParser()

    argParser.add_argument('--vmssname',
                           '-s',
                           required=True,
                           action='store',
                           help='VM Scale Set name')
    argParser.add_argument('--resourcegroup',
                           '-r',
                           required=True,
                           dest='resource_group',
                           action='store',
                           help='Resource group name')
    argParser.add_argument('--newversion',
                           '-n',
                           dest='newversion',
                           action='store',
                           help='New platform image version string')
    argParser.add_argument('--customuri',
                           '-c',
                           dest='customuri',
                           action='store',
                           help='New custom image URI string')
    argParser.add_argument('--updatedomain',
                           '-u',
                           dest='updatedomain',
                           action='store',
                           type=int,
                           help='Update domain (int)')
    argParser.add_argument('--vmid',
                           '-i',
                           dest='vmid',
                           action='store',
                           type=int,
                           help='Single VM ID (int)')
    argParser.add_argument('--vmlist',
                           '-l',
                           dest='vmlist',
                           action='store',
                           help='List of VM IDs e.g. "["1", "2"]"')
    argParser.add_argument('--nowait',
                           '-w',
                           action='store_true',
                           default=False,
                           help='Start upgrades and then exit without waiting')
    argParser.add_argument('--verbose',
                           '-v',
                           action='store_true',
                           default=False,
                           help='Show additional information')
    argParser.add_argument('-y',
                           dest='noprompt',
                           action='store_true',
                           default=False,
                           help='Do not prompt for confirmation')

    args = argParser.parse_args()

    # switches to determine program behavior
    noprompt = args.noprompt  # go ahead and upgrade without waiting for confirmation when True
    nowait = args.nowait  # don't loop waiting for upgrade provisioning to complete when True
    verbose = args.verbose  # print extra status information when True

    vmssname = args.vmssname
    resource_group = args.resource_group
    if args.newversion is not None:
        newversion = args.newversion
        storagemode = 'platform'
    elif args.customuri is not None:
        customuri = args.customuri
        storagemode = 'custom'
    else:
        argParser.error(
            'You must specify a new version for platform images or a custom uri for custom images'
        )

    if args.updatedomain is not None:
        updatedomain = args.updatedomain
        upgrademode = 'updatedomain'
    elif args.vmid is not None:
        vmid = args.vmid
        upgrademode = 'vmid'
    elif args.vmlist is not None:
        vmlist = args.vmlist
        upgrademode = 'vmlist'
    else:
        argParser.error(
            'You must specify an update domain, a vm id, or a vm list')

    # Load Azure app defaults
    try:
        with open('vmssconfig.json') as configFile:
            configdata = json.load(configFile)
    except FileNotFoundError:
        print("Error: Expecting vmssconfig.json in current folder")
        sys.exit()

    tenant_id = configdata['tenantId']
    app_id = configdata['appId']
    app_secret = configdata['appSecret']
    subscription_id = configdata['subscriptionId']

    access_token = azurerm.get_access_token(tenant_id, app_id, app_secret)

    # get the vmss model
    vmssmodel = azurerm.get_vmss(access_token, subscription_id, resource_group,
                                 vmssname)
    # print(json.dumps(vmssmodel, sort_keys=False, indent=2, separators=(',', ': ')))

    if storagemode == 'platform':
        # check current version
        imagereference = vmssmodel['properties']['virtualMachineProfile'][
            'storageProfile']['imageReference']
        print('Current image reference in Scale Set model:')
        print(
            json.dumps(imagereference,
                       sort_keys=False,
                       indent=2,
                       separators=(',', ': ')))

        # compare current version with new version
        if imagereference['version'] == newversion:
            print('Scale Set model version is already set to ' + newversion +
                  ', skipping model update.')
        else:
            if not noprompt:
                response = input('Confirm version upgrade to: ' + newversion +
                                 ' (y/n)')
                if response.lower() != 'y':
                    sys.exit(1)
            # change the version
            vmssmodel['properties']['virtualMachineProfile']['storageProfile'][
                'imageReference']['version'] = newversion
            # put the vmss model
            updateresult = azurerm.update_vmss(access_token, subscription_id,
                                               resource_group, vmssname,
                                               json.dumps(vmssmodel))
            if verbose:
                print(updateresult)
            print('OS version updated to ' + newversion +
                  ' in model for VM Scale Set: ' + vmssname)
    else:  # storagemode = custom
        # check current uri
        oldimageuri = vmssmodel['properties']['virtualMachineProfile'][
            'storageProfile']['osDisk']['image']['uri']
        print('Current image URI in Scale Set model:' + oldimageuri)

        # compare current uri with new uri
        if oldimageuri == customuri:
            print('Scale Set model version is already set to ' + customuri +
                  ', skipping model update.')
        else:
            if not noprompt:
                response = input('Confirm uri upgrade to: ' + customuri +
                                 ' (y/n)')
                if response.lower() != 'y':
                    sys.exit(1)
            # change the version
            vmssmodel['properties']['virtualMachineProfile']['storageProfile'][
                'osDisk']['image']['uri'] = customuri
            # put the vmss model
            updateresult = azurerm.update_vmss(access_token, subscription_id,
                                               resource_group, vmssname,
                                               json.dumps(vmssmodel))
            if verbose:
                print(updateresult)
            print('Image URI updated to ' + customuri +
                  ' in model for VM Scale Set: ' + vmssname)

    # build the list of VMs to upgrade depending on the upgrademode setting
    if upgrademode == 'updatedomain':
        # list the VMSS VM instance views to determine their update domains
        print('Examining the scale set..')
        udinstancelist = get_vm_ids_by_ud(access_token, subscription_id,
                                          resource_group, vmssname,
                                          updatedomain)
        print('VM instances in UD: ' + str(updatedomain) + ' to upgrade:')
        print(udinstancelist)
        vmids = json.dumps(udinstancelist)
        print('Upgrading VMs in UD: ' + str(updatedomain))
    elif upgrademode == 'vmid':
        vmids = json.dumps([str(vmid)])
        print('Upgrading VM ID: ' + str(vmid))
    else:  # upgrademode = vmlist
        vmids = vmlist
        print('Upgrading VM IDs: ' + vmlist)

    # do manualupgrade on the VMs in the list
    upgraderesult = azurerm.upgrade_vmss_vms(access_token, subscription_id,
                                             resource_group, vmssname, vmids)
    print(upgraderesult)

    # now wait for upgrade to complete
    # query VM scale set instance view
    if not nowait:
        updatecomplete = False
        provisioningstate = ''
        while not updatecomplete:
            vmssinstanceview = azurerm.get_vmss_instance_view(
                access_token, subscription_id, resource_group, vmssname)
            for status in vmssinstanceview['statuses']:
                provisioningstate = status['code']
                if provisioningstate == 'ProvisioningState/succeeded':
                    updatecomplete = True
            if verbose:
                print(provisioningstate)
            time.sleep(5)
        print(status['code'])
    else:
        print(
            'Check Scale Set provisioning state to determine when upgrade is complete.'
        )
예제 #8
0
def main():
    # create parser
    argParser = argparse.ArgumentParser()

    argParser.add_argument('--vmssname', '-s', required=True, action='store', help='VM Scale Set name')
    argParser.add_argument('--resourcegroup', '-r', required=True, dest='resource_group', action='store',
                           help='Resource group name')
    argParser.add_argument('--newversion', '-n', dest='newversion', action='store',
                           help='New platform image version string')
    argParser.add_argument('--customuri', '-c', dest='customuri', action='store', help='New custom image URI string')
    argParser.add_argument('--updatedomain', '-u', dest='updatedomain', action='store', type=int,
                           help='Update domain (int)')
    argParser.add_argument('--vmid', '-i', dest='vmid', action='store', type=int, help='Single VM ID (int)')
    argParser.add_argument('--vmlist', '-l', dest='vmlist', action='store', help='List of VM IDs e.g. "["1", "2"]"')
    argParser.add_argument('--nowait', '-w', action='store_true', default=False,
                           help='Start upgrades and then exit without waiting')
    argParser.add_argument('--verbose', '-v', action='store_true', default=False, help='Show additional information')
    argParser.add_argument('-y', dest='noprompt', action='store_true', default=False,
                           help='Do not prompt for confirmation')

    args = argParser.parse_args()

    # switches to determine program behavior
    noprompt = args.noprompt  # go ahead and upgrade without waiting for confirmation when True
    nowait = args.nowait  # don't loop waiting for upgrade provisioning to complete when True
    verbose = args.verbose  # print extra status information when True

    vmssname = args.vmssname
    resource_group = args.resource_group
    if args.newversion is not None:
        newversion = args.newversion
        storagemode = 'platform'
    elif args.customuri is not None:
        customuri = args.customuri
        storagemode = 'custom'
    else:
        argParser.error('You must specify a new version for platform images or a custom uri for custom images')

    if args.updatedomain is not None:
        updatedomain = args.updatedomain
        upgrademode = 'updatedomain'
    elif args.vmid is not None:
        vmid = args.vmid
        upgrademode = 'vmid'
    elif args.vmlist is not None:
        vmlist = args.vmlist
        upgrademode = 'vmlist'
    else:
        argParser.error('You must specify an update domain, a vm id, or a vm list')

    # Load Azure app defaults
    try:
        with open('vmssconfig.json') as configFile:
            configdata = json.load(configFile)
    except FileNotFoundError:
        print("Error: Expecting vmssconfig.json in current folder")
        sys.exit()

    tenant_id = configdata['tenantId']
    app_id = configdata['appId']
    app_secret = configdata['appSecret']
    subscription_id = configdata['subscriptionId']

    access_token = azurerm.get_access_token(tenant_id, app_id, app_secret)

    # get the vmss model
    vmssmodel = azurerm.get_vmss(access_token, subscription_id, resource_group, vmssname)
    # print(json.dumps(vmssmodel, sort_keys=False, indent=2, separators=(',', ': ')))

    if storagemode == 'platform':
        # check current version
        imagereference = vmssmodel['properties']['virtualMachineProfile']['storageProfile']['imageReference']
        print('Current image reference in Scale Set model:')
        print(json.dumps(imagereference, sort_keys=False, indent=2, separators=(',', ': ')))

        # compare current version with new version
        if imagereference['version'] == newversion:
            print('Scale Set model version is already set to ' + newversion + ', skipping model update.')
        else:
            if not noprompt:
                response = input('Confirm version upgrade to: ' + newversion + ' (y/n)')
                if response.lower() != 'y':
                    sys.exit(1)
            # change the version
            vmssmodel['properties']['virtualMachineProfile']['storageProfile']['imageReference']['version'] = newversion
            # put the vmss model
            updateresult = azurerm.update_vmss(access_token, subscription_id, resource_group, vmssname,
                                               json.dumps(vmssmodel))
            if verbose:
                print(updateresult)
            print('OS version updated to ' + newversion + ' in model for VM Scale Set: ' + vmssname)
    else:  # storagemode = custom
        # check current uri
        oldimageuri = vmssmodel['properties']['virtualMachineProfile']['storageProfile']['osDisk']['image']['uri']
        print('Current image URI in Scale Set model:' + oldimageuri)

        # compare current uri with new uri
        if oldimageuri == customuri:
            print('Scale Set model version is already set to ' + customuri + ', skipping model update.')
        else:
            if not noprompt:
                response = input('Confirm uri upgrade to: ' + customuri + ' (y/n)')
                if response.lower() != 'y':
                    sys.exit(1)
            # change the version
            vmssmodel['properties']['virtualMachineProfile']['storageProfile']['osDisk']['image']['uri'] = customuri
            # put the vmss model
            updateresult = azurerm.update_vmss(access_token, subscription_id, resource_group, vmssname,
                                               json.dumps(vmssmodel))
            if verbose:
                print(updateresult)
            print('Image URI updated to ' + customuri + ' in model for VM Scale Set: ' + vmssname)

    # build the list of VMs to upgrade depending on the upgrademode setting
    if upgrademode == 'updatedomain':
        # list the VMSS VM instance views to determine their update domains
        print('Examining the scale set..')
        udinstancelist = get_vm_ids_by_ud(access_token, subscription_id, resource_group, vmssname, updatedomain)
        print('VM instances in UD: ' + str(updatedomain) + ' to upgrade:')
        print(udinstancelist)
        vmids = json.dumps(udinstancelist)
        print('Upgrading VMs in UD: ' + str(updatedomain))
    elif upgrademode == 'vmid':
        vmids = json.dumps([str(vmid)])
        print('Upgrading VM ID: ' + str(vmid))
    else:  # upgrademode = vmlist
        vmids = vmlist
        print('Upgrading VM IDs: ' + vmlist)

    # do manualupgrade on the VMs in the list
    upgraderesult = azurerm.upgrade_vmss_vms(access_token, subscription_id, resource_group, vmssname, vmids)
    print(upgraderesult)

    # now wait for upgrade to complete
    # query VM scale set instance view
    if not nowait:
        updatecomplete = False
        provisioningstate = ''
        while not updatecomplete:
            vmssinstanceview = azurerm.get_vmss_instance_view(access_token, subscription_id, resource_group, vmssname)
            for status in vmssinstanceview['statuses']:
                provisioningstate = status['code']
                if provisioningstate == 'ProvisioningState/succeeded':
                    updatecomplete = True
            if verbose:
                print(provisioningstate)
            time.sleep(5)
        print(status['code'])
    else:
        print('Check Scale Set provisioning state to determine when upgrade is complete.')
예제 #9
0
파일: vmss.py 프로젝트: gbowerman/vmsstools
 def update_extns(self, extnprofile):
     self.model['properties']['virtualMachineProfile']['extensionProfile'] = extnprofile
     # put the vmss model
     updateresult = azurerm.update_vmss(self.access_token, self.sub_id, self.rgname, self.name,
                                        json.dumps(self.model))
     self.status = updateresult