0
点赞
收藏
分享

微信扫一扫

练手python脚本分享

分享一个最近练手的脚本,没什么实际意义,纯粹就是练手用的,最近准备把python拿出来再练练,所以随意找了点东西研究下,脚本里主要用到了ttkbootstrap来做GUI,以及非常非常基础的azure python的SDK

import datetime
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap import utility

from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient



class Sample(ttk.Frame):
    def __init__(self, master):
        super().__init__(master, padding=15)
        self.pack(fill=BOTH, expand=YES) 
        option_text = "Virtual Machines in Subscription"
        self.option_lf = ttk.Labelframe(self, text=option_text, padding=15)
        self.option_lf.pack(fill=X, expand=YES, anchor=N)
        self.vm_client=VirtualMachines(subscription_id="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx")
        self.vms=self.vm_client.get_all_vms()
        self.create_tree_view()
        self.create_button()

    
    def create_tree_view(self):
        self.resultview = ttk.Treeview(
            master=self.option_lf, 
            bootstyle=INFO, 
            columns=[0, 1, 2, 3, 4],
            show=HEADINGS
        )
        self.resultview.pack(fill=BOTH, expand=YES, pady=10)
        self.resultview.heading(0, text='VM Name', anchor=W)
        self.resultview.heading(1, text='Resource Group Name', anchor=W)
        self.resultview.heading(2, text='Location', anchor=E)
        self.resultview.heading(3, text='OS', anchor=E)
        self.resultview.heading(4, text='Size', anchor=W)
        
        self.resultview.column(
            column=0, 
            anchor=W, 
            width=utility.scale_size(self, 125), 
            stretch=False
        )
        self.resultview.column(
            column=1, 
            anchor=W, 
            width=utility.scale_size(self, 140), 
            stretch=False
        )
        self.resultview.column(
            column=2, 
            anchor=E, 
            width=utility.scale_size(self, 50), 
            stretch=False
        )
        self.resultview.column(
            column=3, 
            anchor=E, 
            width=utility.scale_size(self, 50), 
            stretch=False
        )
        self.resultview.column(
            column=4, 
            anchor=W, 
            width=utility.scale_size(self, 300)
        )
    
    def create_button(self):
        self.button=ttk.Button(
            master=self, 
            text='Show VM',
            command=self.on_click
        )
        self.button.pack(fill=X, expand=YES, pady=10)
    
    def on_click(self):

        for vm in self.vms:
            vm_rg=vm.id.split("/")[4]
            vm_os_fun=lambda x: "Windows" if x.os_profile.linux_configuration == None else "Linux"
            vm_os=vm_os_fun(vm)
            vm_size=vm.hardware_profile.vm_size
            iid = self.resultview.insert(
            parent='', 
            index=END, 
            values=(vm.name, vm_rg, vm.location, vm_os, vm_size)
    )
        self.resultview.selection_set(iid)
        self.resultview.see(iid)

class VirtualMachines:
    def __init__(self, subscription_id, cloud="Global"):
        self.subscription_id = subscription_id
        if cloud == "Global":
            self.compute_client=ComputeManagementClient(
                subscription_id=self.subscription_id,
                credential=DefaultAzureCredential()
                                            )
        elif cloud == "China":
            self.compute_client=ComputeManagementClient(
                subscription_id=self.subscription_id,
                credential=DefaultAzureCredential(),
                base_url="https://management.chinacloudapi.cn"
                
        )
        else:
            raise "unsupport cloud"

    def get_all_vms(self):
        return self.compute_client.virtual_machines.list_all()


if __name__ == '__main__':
    app = ttk.Window("Virtual Machine Engine", "journal")
    Sample(app)
    app.mainloop()

要用的话,首先要有az cli,然后先用cli登录,因为脚本里是直接用defaultcredential,所以需要用环境变量或者直接用cli登录,登录之后,会有个GUI显示出来,点击show VM的button,会读出来subscription下的VM

练手python脚本分享_云

基本就是这么个东西,没有实际意义,纯粹就是练手~

举报

相关推荐

0 条评论