|
| 1 | +# import os |
| 2 | +import argparse |
| 3 | +from dotenv import load_dotenv |
| 4 | +from azure.mgmt.compute import ComputeManagementClient |
| 5 | +# from azure.mgmt.compute.models import RunCommandInput |
| 6 | +from azure.identity import DefaultAzureCredential |
| 7 | +from resource_graph_query import run_azure_rg_query |
| 8 | + |
| 9 | +def run_commands_on_vmss(resource_group:str, subscription_id:str, vmss_name:str): |
| 10 | + """ |
| 11 | + Execute run commands on Azure using Compute Management Client class |
| 12 | + :return: |
| 13 | + """ |
| 14 | + credential = DefaultAzureCredential() |
| 15 | + compute_mgmt_client = ComputeManagementClient(credential=credential, subscription_id=subscription_id) |
| 16 | + |
| 17 | + # Get all VM instances in the VMSS |
| 18 | + vmss_instances = compute_mgmt_client.virtual_machine_scale_set_vms.list(resource_group_name=resource_group, |
| 19 | + virtual_machine_scale_set_name = vmss_name) |
| 20 | + instance_details = [] |
| 21 | + for instance in vmss_instances: |
| 22 | + # print(instance) |
| 23 | + instances = { |
| 24 | + 'name': instance.name, |
| 25 | + 'id': instance.id, |
| 26 | + 'instance_id': instance.instance_id |
| 27 | + } |
| 28 | + instance_details.append(instances) |
| 29 | + |
| 30 | + # print(f'instance details : {instance_details}') |
| 31 | + |
| 32 | + |
| 33 | + for vmss_in in instance_details: |
| 34 | + # Define the command parameters |
| 35 | + run_command_parameters = { |
| 36 | + 'command_id':'RunShellScript', |
| 37 | + 'script': ['df -hT'] |
| 38 | + } |
| 39 | + response = compute_mgmt_client.virtual_machine_scale_set_vms.begin_run_command( |
| 40 | + resource_group_name=resource_group, vm_scale_set_name=vmss_name, |
| 41 | + instance_id= vmss_in['instance_id'], parameters=run_command_parameters |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | + # Optionally, print the command output |
| 46 | + result = response.result() |
| 47 | + if result.value[0].code == "ProvisioningState/succeeded": |
| 48 | + # print(result.value[0].message) |
| 49 | + print(f"Run command execution in instance {vmss_in['name']} of {vmss_name} VMSS completed successfully") |
| 50 | + else: |
| 51 | + print('Something went wrong!!') |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +def main(): |
| 56 | + """ |
| 57 | + Execute program |
| 58 | + :return: |
| 59 | + """ |
| 60 | + load_dotenv() |
| 61 | + parser = argparse.ArgumentParser("Execute Run commands on an Azure VM") |
| 62 | + parser.add_argument("--subscription_name", help="Azuer Subscription name", required=True, type=str) |
| 63 | + parser.add_argument("--vmss_name", help="Azure VM name",required=True, type=str) |
| 64 | + parser.add_argument("--resource_group", help="Azure resource group name", type=str, required=True) |
| 65 | + |
| 66 | + args = parser.parse_args() |
| 67 | + |
| 68 | + subscription_name =args.subscription_name |
| 69 | + vmss_name = args.vmss_name |
| 70 | + resource_group = args.resource_group |
| 71 | + |
| 72 | + subscription_id = run_azure_rg_query(subscription_name=subscription_name) |
| 73 | + |
| 74 | + run_commands_on_vmss(subscription_id=subscription_id, resource_group=resource_group, vmss_name=vmss_name) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + main() |
| 79 | + |
0 commit comments