Skip to main content

py_az_vm_controller

Introduction

This is simple library for doing remote turn on and off azure virtual machine.

The library requires the user to have

  • Azure Subscription with Entra ID (Entra ID's app id as tenantID) for register new app (registered app) for oauth login
    • App ID as clientId
    • Creating Credential as (clientSecret)
  • The registered app configured to be able to perform
    • Microsoft.Compute/virtualMachines/read
    • Microsoft.Compute/virtualMachines/instanceView/read
    • Microsoft.Compute/virtualMachines/start/action
    • Microsoft.Compute/virtualMachines/powerOff/action

Execution

This is a library rather then a executable problem. The execution will trigger the demo only

Run through python interpreter:

python -m py_az_vm_controller

Demo Code

import os
os.environ["using-j-vault-rest-server"]="localhost,7910,false,harden"
import py_az_vm_controller as project
from j_vault_http_client_xethhung12 import client

# Make sure you defined the system variables:
#     * tenantId              <--- The microsoft Entra Id
#     * clientId              <--- The client id of the app registered
#     * clientSecret          <--- The client secret of the app registered 
#     * subscriptionId        <--- The subscription id of the VM
#     * resourceGroupName     <--- The resource group name of the VM
#     * vmName                <--- The name of the virtual machine

import time
def main():
    client.load_to_env()
    tenantId = os.getenv("tenantId")
    clientId = os.getenv("clientId")
    clientSecret = os.getenv("clientSecret")
    subscriptionId = os.getenv("subscriptionId")
    resourceGroupName = os.getenv("resourceGroupName")
    vmName = os.getenv("vmName")
    print(tenantId, clientId, clientSecret, subscriptionId, resourceGroupName, vmName)
    azOAuth = project.AzOAuth(tenantId)
    session = azOAuth.get_session(clientId, clientSecret)
    vm = session.vm(subscriptionId, resourceGroupName, vmName)
    if vm.isVMStopped():
        print("Try power on the machine")
        vm.powerOn()
    while(True):
        time.sleep(5)
        print(vm.instanceState())
        if vm.isVMRunning():
            print("running finally")
            break
        
    time.sleep(10)

    print("stopping machine")
    vm.powerOff()
    while(True):
        time.sleep(5)
        print(vm.instanceState())
        if vm.isVMStopped():
            print("stopped finally")
            break

Development

The project requires python (3+ version) installed and pip ready for use on adding manage dependencies

Tools

Name Platform Type Description
install-dependencies.sh shell script The scripts for installing depencies required
build.sh shell script The scripts for build the package
build-and-deploy.sh shell script The scripts for build and deploy the package
  • install-dependencies.sh The script will install dependencies listed in dev-requirements.txt and requirements.txt. The first requirement file contains the dependencies for development like build and deploy tools. The second requirement file defined all required dependencies for the making the package works (actual dependencies).

Useful Scripts

Project Versioning

For version update in pyproject.toml. This project use package xh-py-project-versioning to manipulate the project version.

Simple usage includes:
Base on current version, update the patch number with dev id python -m xh_py_project_versioning --patch
In case current version is 0.0.1, the updated version will be 0.0.2-dev+000

To prompt the dev version to official version use command. python -m xh_py_project_versioning -r. Through the command, version 0.0.2-dev+000 will be prompt to 0.0.2 official versioning.

Base on current version, update the patch number directly python -m xh_py_project_versioning --patch -d
In case current version is 0.0.1, the updated version will be 0.0.2

Base on current version, update the minor number directly python -m xh_py_project_versioning --minor -d
In case current version is 0.0.1, the updated version will be 0.1.0

Base on current version, update the minor number directly python -m xh_py_project_versioning --minor -d
In case current version is 0.0.1, the updated version will be 1.0.0

Metadata

Release files for py-az-vm-controller-xethhung12 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-az-vm-controller-xethhung12 0.3.0
File Size Uploaded Provenance Actions
py_az_vm_controller_xethhung12-0.3.0.tar.gz 20.7 kB Without provenance Details for py_az_vm_controller_xethhung12-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for py-az-vm-controller-xethhung12 0.3.0
File Interpreter ABI Platform Actions
py_az_vm_controller_xethhung12-0.3.0-py3-none-any.whl Python 3 none any Details for py_az_vm_controller_xethhung12-0.3.0-py3-none-any.whl

Total release size: 42.9 kB

Release files / py_az_vm_controller_xethhung12-0.3.0.tar.gz

Download URL py_az_vm_controller_xethhung12-0.3.0.tar.gz
Size 20.7 kB
Tags Source
SHA-256 checksum
How to use checksums
95843f8309965b6efda7552f5cfa259d54e1cf59f707fd9b36b5ad153070356a
BLAKE2b-256 checksum
How to use checksums
285c220a604cc5f0508cf8e1b41e4ae2b714e8c1c036eb74fddc49fdef0c1503
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Release files / py_az_vm_controller_xethhung12-0.3.0-py3-none-any.whl

Download URL py_az_vm_controller_xethhung12-0.3.0-py3-none-any.whl
Size 22.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ebdd5e78c27e2fcfc52eef5e63d7d12b3a78773b5388e58b26fbbc327162042d
BLAKE2b-256 checksum
How to use checksums
0ebdf60f038687174c9e0892ce8acd4622b16186835c635b6a9c56a28bc6cd80
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.3

Security for py-az-vm-controller-xethhung12 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page