def _get_public_ips(self, resource_group=None): result = azurerm.list_public_ips(self.access_token, self.subscription_id, resource_group=resource_group) if 'error' not in result and 'value' in result: return [str(i['properties']['ipAddress']) for i in result['value']]
def main(): global clickLoopCount global showScaleIn, showScaleOut global ipaddr, dns # get an access token for Azure authentication access_token = azurerm.get_access_token(tenant_id, app_id, app_secret) # get public ip address for resource group (don't need to query this in a loop) # this gets the first ip address - modify this if your RG has multiple ips ips = azurerm.list_public_ips(access_token, subscription_id, rgname) dns = ips['value'][0]['properties']['dnsSettings']['fqdn'] ipaddr = ips['value'][0]['properties']['ipAddress'] # start a timer in order to refresh the access token in 10 minutes start_time = time.time() # start a VMSS monitoring thread vmss_thread = threading.Thread(target=get_vmss_properties, args=(access_token, subscription_id)) vmss_thread.start() # Loop until the user clicks the close button. done = False clock = pygame.time.Clock() pygame.key.set_repeat(1,2) # keyboard monitoring [pause before repeat, repeats/sec] while not done: screen.blit(background, [0, 0]) # set how many times per second to run the loop - if you reduce this value, then reduce clickLoopCount # reduce it too low and clicking on things becomes less responsive clock.tick(20) mousepos = pygame.mouse.get_pos() for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0]: process_click(event.pos, access_token) # refresh screen if VMSS and VM details are available if len(vmssProperties) > 0: draw_vmss() if len(vmssVmProperties) > 0: draw_vmssvms() # loop counters to show transient messages for a few moments if showScaleIn == True: clickLoopCount -= 1 if clickLoopCount < 1: showScaleIn = False if showScaleOut == True: clickLoopCount -= 1 if clickLoopCount < 1: showScaleOut = False # update display pygame.display.flip() # renew Azure access token before timeout (600 secs), then reset timer if int(start_time - time.time()) > 600: access_token = azurerm.get_access_token(tenant_id, app_id, app_secret) start_time = time.time() pygame.quit()
# list load balancers in resource group print('\nLoad Balancers in resource group: ' + resource_group) lbs = azurerm.list_load_balancers_rg(access_token, subscription_id, resource_group) for lb in lbs['value']: print(lb['name'] + ', ' + lb['location']) # get details for a load balancer lb_name = resource_group + 'lb' print('\nLoad balancer ' + lb_name + ' details: ') lb = azurerm.get_load_balancer(access_token, subscription_id, resource_group, lb_name) #print(json.dumps(lb, sort_keys=False, indent=2, separators=(',', ': '))) # list the public ip addresses in a resource group print('\nPublic IPs in Resource Group ' + resource_group + ': ') ips = azurerm.list_public_ips(access_token, subscription_id, resource_group) #print(json.dumps(ips, sort_keys=False, indent=2, separators=(',', ': '))) for ip in ips['value']: dns = ip['properties']['dnsSettings']['fqdn'] if 'ipAddress' in ip['properties']: ipaddr = ip['properties']['ipAddress'] else: ipaddr = 'no ip address' print(dns + ' (' + ipaddr + ')\n') # get a public ip ip = azurerm.get_public_ip(access_token, subscription_id, resource_group, resource_group + 'pip') print(json.dumps(ip, sort_keys=False, indent=2, separators=(',', ': '))) dns = ip['properties']['dnsSettings']['fqdn'] if 'ipAddress' in ip['properties']:
def main(): global clickLoopCount global showScaleIn, showScaleOut global ipaddr, dns # get an access token for Azure authentication access_token = azurerm.get_access_token(tenant_id, app_id, app_secret) # get public ip address for resource group (don't need to query this in a loop) # this gets the first ip address - modify this if your RG has multiple ips ips = azurerm.list_public_ips(access_token, subscription_id, rgname) # print(ips) if len(ips['value']) > 0: dns = ips['value'][0]['properties']['dnsSettings']['fqdn'] ipaddr = ips['value'][0]['properties']['ipAddress'] # start a timer in order to refresh the access token in 10 minutes start_time = time.time() # start a VMSS monitoring thread vmss_thread = threading.Thread(target=get_vmss_properties, args=(access_token, subscription_id)) vmss_thread.start() # Loop until the user clicks the close button. done = False clock = pygame.time.Clock() pygame.key.set_repeat( 1, 2) # keyboard monitoring [pause before repeat, repeats/sec] while not done: screen.blit(background, [0, 0]) # set how many times per second to run the loop - if you reduce this value, then reduce clickLoopCount # reduce it too low and clicking on things becomes less responsive clock.tick(20) mousepos = pygame.mouse.get_pos() for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed( )[0]: process_click(event.pos, access_token) # refresh screen if VMSS and VM details are available if len(vmssProperties) > 0: draw_vmss() if len(vmssVmProperties) > 0: draw_vmssvms() # loop counters to show transient messages for a few moments if showScaleIn == True: clickLoopCount -= 1 if clickLoopCount < 1: showScaleIn = False if showScaleOut == True: clickLoopCount -= 1 if clickLoopCount < 1: showScaleOut = False # update display pygame.display.flip() # renew Azure access token before timeout (600 secs), then reset timer if int(start_time - time.time()) > 600: access_token = azurerm.get_access_token(tenant_id, app_id, app_secret) start_time = time.time() pygame.quit()
def get_vmss_properties(access_token, run_event, window_information, panel_information, window_continents, panel_continents): global vmssProperties, vmssVmProperties, countery, capacity, region, tier, vmsku, vm_selected, window_vm, panel_vm, instances_deployed, vm_details, vm_nic, page; ROOM = 5; DEPLOYED = 0; #VM's destination... destx = 22; desty = 4; XS =50; YS = 4; init_coords = (XS, YS); window_dc = 0; #Our window_information arrays... panel_vm = []; window_vm = []; #Our thread loop... while run_event.is_set(): try: #Timestamp... ourtime = time.strftime("%H:%M:%S"); write_str(window_information['status'], 1, 13, ourtime); #Clean Forms... clean_forms(window_information); #Get VMSS details vmssget = azurerm.get_vmss(access_token, subscription_id, rgname, vmssname); # Get public ip address for RG (First IP) - modify this if your RG has multiple ips net = azurerm.list_public_ips(access_token, subscription_id, rgname); #Clean Info and Sys Windows... clean_infoandsys(window_information); #Fill the information... fill_vmss_info(window_information, vmssget, net); #Set VMSS variables... (name, capacity, location, offer, sku, provisioningState, dns, ipaddr) = set_vmss_variables(vmssget, net); #Set the current and old location... old_location = region; if (old_location != ""): continent_old_location = get_continent_dc(old_location); #New region = location; continent_location = get_continent_dc(location); #Quota... quota = azurerm.get_compute_usage(access_token, subscription_id, location); fill_quota_info(window_information, quota); #Mark Datacenter where VMSS is deployed... if (old_location != ""): if (old_location != location): #Now switch the datacenter mark on map... new_window_dc = mark_vmss_dc(continent_old_location, window_continents[continent_old_location], old_location, window_continents[continent_location], location, window_dc); window_dc = new_window_dc; else: new_window_dc = mark_vmss_dc(continent_location, window_continents[continent_location], location, window_continents[continent_location], location, window_dc); window_dc = new_window_dc; #Our arrays... vmssProperties = [name, capacity, location, rgname, offer, sku, provisioningState, dns, ipaddr]; vmssvms = azurerm.list_vmss_vms(access_token, subscription_id, rgname, vmssname); vmssVmProperties = []; #All VMs are created in the following coordinates... qtd = vmssvms['value'].__len__(); factor = (vmssvms['value'].__len__() / 100); write_str(window_information['system'], 3, 22, qtd); step = qtd / 10; if (step < 1): step = 1; #We take more time on our VM effect depending on how many VMs we are talking about... if (qtd < 10): ts = 0.01; elif (qtd < 25): ts = 0.005; elif (qtd < 50): ts = 0.002; elif (qtd < 100): ts = 0.0005; else: ts = 0; counter = 1; counter_page = 0; nr_pages = 1; snap_page = page; page_top = (snap_page * 100); page_base = ((snap_page - 1) * 100); if (vm_selected[1] == 999998): #Clean VM Info... clean_vm(window_information); #Loop each VM... for vm in vmssvms['value']: instanceId = vm['instanceId']; write_str(window_information['monitor'], 1, 30, instanceId); wrefresh(window_information['monitor']); vmsel = 0; vmName = vm['name']; provisioningState = vm['properties']['provisioningState']; vmssVmProperties.append([instanceId, vmName, provisioningState]); if (counter > DEPLOYED): window_vm.append(DEPLOYED); panel_vm.append(DEPLOYED); instances_deployed.append(DEPLOYED); instances_deployed[DEPLOYED] = int(instanceId); #Prepare the place for the VM icon... if countery < 10: countery += 1; else: destx += 3; desty = 4; countery = 1; if (counter_page > 99): destx = 22; counter_page = 0; nr_pages += 1; cur_page = "%02d" % snap_page; tot_pages = "%02d" % nr_pages; update_vm_footer(window_information, cur_page, tot_pages); else: counter_page += 1; window_vm[DEPLOYED] = create_window(3, 5, init_coords[0], init_coords[1]); panel_vm[DEPLOYED] = new_panel(window_vm[DEPLOYED]); #Show only VM's that are on the visible window... if (page_top > DEPLOYED and DEPLOYED >= page_base): show_panel(panel_vm[DEPLOYED]); else: hide_panel(panel_vm[DEPLOYED]); box(window_vm[DEPLOYED]); #Creation of the VM icon, in this flow we never have a VM selected... draw_vm(int(instanceId), window_vm[DEPLOYED], provisioningState, vmsel); vm_animation(panel_vm[DEPLOYED], init_coords, destx, desty, 1, ts); desty += ROOM; DEPLOYED += 1; else: instances_deployed[counter - 1] = int(instanceId); #Remove the old mark... vmsel = deselect_vm(window_vm, panel_information, instanceId, counter); #Show only VM's that are on the visible window... if (page_top > (counter - 1) and (counter - 1) >= page_base): show_panel(panel_vm[counter -1]); else: hide_panel(panel_vm[counter -1]); #Creation of the VM icon... draw_vm(int(instanceId), window_vm[counter - 1], provisioningState, vmsel); #If a VM is selected, fill the details... if (vm_selected[0] == int(instanceId) and vm_selected[1] != 999998): vm_details = azurerm.get_vmss_vm_instance_view(access_token, subscription_id, rgname, vmssname, vm_selected[0]); vm_nic = azurerm.get_vmss_vm_nics(access_token, subscription_id, rgname, vmssname, vm_selected[0]); clean_vm(window_information); if (vm_details != "" and vm_nic != ""): fill_vm_details(window_information, instanceId, vmName, provisioningState); update_panels(); doupdate(); counter += 1; do_update_bar(window_information['status'], step, 0); step += step; #Last mile... write_str(window_information['monitor'], 1, 30, "Done."); do_update_bar(window_information['status'], step, 1); #Remove destroyed VMs... counter_page = 0; if (DEPLOYED >= counter): time.sleep(0.5); write_str_color(window_information['monitor'], 1, 30, "Removing VM's...", 7, 0); wrefresh(window_information['monitor']); time.sleep(1); clean_monitor_form(window_information); while (DEPLOYED >= counter): write_str(window_information['monitor'], 1, 30, DEPLOYED); lastvm = window_vm.__len__() - 1; vm_coords = getbegyx(window_vm[lastvm]); vm_animation(panel_vm[lastvm], vm_coords, init_coords[0], init_coords[1], 0, ts); if (countery > 0): desty -= ROOM; countery -= 1; elif (destx > 22): destx -= 3; desty = 49; countery = 9; if (counter_page > 99): destx = 52; counter_page = 0; nr_pages -= 1; tot_pages = "%02d" % nr_pages; cur_page = "%02d" % page; update_vm_footer(window_information, cur_page, tot_pages); else: counter_page += 1; #Clean VM Info... if (vm_selected[0] == instances_deployed[lastvm]): clean_vm(window_information); #Free up some memory... del_panel(panel_vm[lastvm]); delwin(window_vm[lastvm]); wobj = panel_vm[lastvm]; panel_vm.remove(wobj); wobj = window_vm[lastvm]; window_vm.remove(wobj); wobj = instances_deployed[lastvm]; instances_deployed.remove(wobj); DEPLOYED -= 1; update_panels(); doupdate(); write_str(window_information['monitor'], 1, 30, "Done."); ourtime = time.strftime("%H:%M:%S"); do_update_bar(window_information['status'], step, 1); write_str(window_information['status'], 1, 13, ourtime); write_str_color(window_information['status'], 1, 22, " OK ", 6, 0); update_panels(); doupdate(); # sleep before each loop to avoid throttling... time.sleep(interval); except: logging.exception("Getting VMSS Information...") write_str(window_information['error'], 1, 24, "Let's sleep for 30 seconds and try to refresh the dashboard again..."); show_panel(panel_information['error']); update_panels(); doupdate(); ## break out of loop when an error is encountered #break time.sleep(30); hide_panel(panel_information['error']);
def get_vmss_properties(access_token, run_event, window_information, panel_information, window_continents, panel_continents): global vmssProperties, vmssVmProperties, countery, capacity, region, tier, vmsku, vm_selected, window_vm, panel_vm, instances_deployed, vm_details, vm_nic, page; ROOM = 5; DEPLOYED = 0; #VM's destination... destx = 22; desty = 4; XS =50; YS = 4; init_coords = (XS, YS); window_dc = 0; #Our window_information arrays... panel_vm = []; window_vm = []; #Our thread loop... while run_event.is_set(): try: #Timestamp... ourtime = time.strftime("%H:%M:%S"); write_str(window_information['status'], 1, 13, ourtime); #Clean Forms... clean_forms(window_information); #Get VMSS details vmssget = azurerm.get_vmss(access_token, subscription_id, rgname, vmssname); # Get public ip address for RG (First IP) - modify this if your RG has multiple ips net = azurerm.list_public_ips(access_token, subscription_id, rgname); #Clean Info and Sys Windows... clean_infoandsys(window_information); #Fill the information... fill_vmss_info(window_information, vmssget, net); #Set VMSS variables... (name, capacity, location, offer, sku, provisioningState, dns, ipaddr) = set_vmss_variables(vmssget, net); #Set the current and old location... old_location = region; if (old_location != ""): continent_old_location = get_continent_dc(old_location); #New region = location; continent_location = get_continent_dc(location); #Quota... quota = azurerm.get_compute_usage(access_token, subscription_id, location); fill_quota_info(window_information, quota); #Mark Datacenter where VMSS is deployed... if (old_location != ""): if (old_location != location): #Now switch the datacenter mark on map... new_window_dc = mark_vmss_dc(continent_old_location, window_continents[continent_old_location], old_location, window_continents[continent_location], location, window_dc); window_dc = new_window_dc; else: new_window_dc = mark_vmss_dc(continent_location, window_continents[continent_location], location, window_continents[continent_location], location, window_dc); window_dc = new_window_dc; #Our arrays... vmssProperties = [name, capacity, location, rgname, offer, sku, provisioningState, dns, ipaddr]; vmssvms = azurerm.list_vmss_vms(access_token, subscription_id, rgname, vmssname); vmssVmProperties = []; #All VMs are created in the following coordinates... qtd = vmssvms['value'].__len__(); factor = (vmssvms['value'].__len__() / 100); write_str(window_information['system'], 3, 22, qtd); step = qtd / 10; if (step < 1): step = 1; #We take more time on our VM effects depending on how many VMs we are talking about... if (qtd < 10): ts = 0.01; elif (qtd < 25): ts = 0.004; elif (qtd < 50): ts = 0.0008; elif (qtd < 100): ts = 0.0004; else: ts = 0; counter = 1; counter_page = 0; nr_pages = 1; snap_page = page; page_top = (snap_page * 100); page_base = ((snap_page - 1) * 100); if (vm_selected[1] == 999998): #Clean VM Info... clean_vm(window_information); #Loop each VM... for vm in vmssvms['value']: instanceId = vm['instanceId']; write_str(window_information['monitor'], 1, 30, instanceId); wrefresh(window_information['monitor']); vmsel = 0; vmName = vm['name']; provisioningState = vm['properties']['provisioningState']; vmssVmProperties.append([instanceId, vmName, provisioningState]); if (counter > DEPLOYED): window_vm.append(DEPLOYED); panel_vm.append(DEPLOYED); instances_deployed.append(DEPLOYED); instances_deployed[DEPLOYED] = int(instanceId); #Prepare the place for the VM icon... if countery < 10: countery += 1; else: destx += 3; desty = 4; countery = 1; if (counter_page > 99): destx = 22; counter_page = 0; nr_pages += 1; cur_page = "%02d" % snap_page; tot_pages = "%02d" % nr_pages; update_vm_footer(window_information, cur_page, tot_pages); else: counter_page += 1; window_vm[DEPLOYED] = create_window(3, 5, init_coords[0], init_coords[1]); panel_vm[DEPLOYED] = new_panel(window_vm[DEPLOYED]); #Show only VM's that are on the visible window... if (page_top > DEPLOYED and DEPLOYED >= page_base): show_panel(panel_vm[DEPLOYED]); else: hide_panel(panel_vm[DEPLOYED]); box(window_vm[DEPLOYED]); #Creation of the VM icon, in this flow we never have a VM selected... draw_vm(int(instanceId), window_vm[DEPLOYED], provisioningState, vmsel); vm_animation(panel_vm[DEPLOYED], init_coords, destx, desty, 1, ts); desty += ROOM; DEPLOYED += 1; else: instances_deployed[counter - 1] = int(instanceId); #Remove the old mark... vmsel = deselect_vm(window_vm, panel_information, instanceId, counter); #Show only VM's that are on the visible window... if (page_top > (counter - 1) and (counter - 1) >= page_base): show_panel(panel_vm[counter -1]); else: hide_panel(panel_vm[counter -1]); #Creation of the VM icon... draw_vm(int(instanceId), window_vm[counter - 1], provisioningState, vmsel); #If a VM is selected, fill the details... if (vm_selected[0] == int(instanceId) and vm_selected[1] != 999998): vm_details = azurerm.get_vmss_vm_instance_view(access_token, subscription_id, rgname, vmssname, vm_selected[0]); vm_nic = azurerm.get_vmss_vm_nics(access_token, subscription_id, rgname, vmssname, vm_selected[0]); clean_vm(window_information); if (vm_details != "" and vm_nic != ""): fill_vm_details(window_information, instanceId, vmName, provisioningState); update_panels(); doupdate(); counter += 1; do_update_bar(window_information['status'], step, 0); step += step; #Last mile... write_str(window_information['monitor'], 1, 30, "Done."); do_update_bar(window_information['status'], step, 1); #Remove destroyed VMs... counter_page = 0; if (DEPLOYED >= counter): time.sleep(0.5); write_str_color(window_information['monitor'], 1, 30, "Removing VM's...", 7, 0); wrefresh(window_information['monitor']); time.sleep(1); clean_monitor_form(window_information); while (DEPLOYED >= counter): write_str(window_information['monitor'], 1, 30, DEPLOYED); lastvm = window_vm.__len__() - 1; vm_coords = getbegyx(window_vm[lastvm]); vm_animation(panel_vm[lastvm], vm_coords, init_coords[0], init_coords[1], 0, ts); if (countery > 0): desty -= ROOM; countery -= 1; elif (destx > 22): destx -= 3; desty = 49; countery = 9; if (counter_page > 99): destx = 52; counter_page = 0; nr_pages -= 1; tot_pages = "%02d" % nr_pages; cur_page = "%02d" % page; update_vm_footer(window_information, cur_page, tot_pages); else: counter_page += 1; #Clean VM Info... if (vm_selected[0] == instances_deployed[lastvm]): clean_vm(window_information); #Free up some memory... del_panel(panel_vm[lastvm]); delwin(window_vm[lastvm]); wobj = panel_vm[lastvm]; panel_vm.remove(wobj); wobj = window_vm[lastvm]; window_vm.remove(wobj); wobj = instances_deployed[lastvm]; instances_deployed.remove(wobj); DEPLOYED -= 1; update_panels(); doupdate(); write_str(window_information['monitor'], 1, 30, "Done."); ourtime = time.strftime("%H:%M:%S"); do_update_bar(window_information['status'], step, 1); write_str(window_information['status'], 1, 13, ourtime); write_str_color(window_information['status'], 1, 22, " OK ", 6, 0); update_panels(); doupdate(); # sleep before each loop to avoid throttling... time.sleep(interval); except: logging.exception("Getting VMSS Information...") write_str(window_information['error'], 1, 24, "Let's sleep for 30 seconds and try to refresh the dashboard again..."); show_panel(panel_information['error']); update_panels(); doupdate(); ## break out of loop when an error is encountered #break time.sleep(30); hide_panel(panel_information['error']);