Start / stop GCE from python

python 3.7

requiremets.txt

google-api-python-client==1.7.9 
oauth2client==4.1.3

main.py

import time

from googleapiclient.discovery import build, Resource
from oauth2client.client import GoogleCredentials


def start(project, zone, instance_name):
    credentials: GoogleCredentials = GoogleCredentials.get_application_default()
    compute: Resource = build('compute', 'v1', credentials=credentials)
    instance: dict = compute.instances().get(project=project, zone=zone, instance=instance_name).execute()
    result: dict = compute.instances().start(project=project, zone=zone, instance=instance['name']).execute()
    # Wait Running
    while True:
        instance: dict = compute.instances().get(project=project, zone=zone, instance=instance_name).execute()
        print(instance["status"])
        # TERMINATED -> STAGING -> RUNNING
        if instance["status"] == "RUNNING":
            break
        time.sleep(5)


def stop(project, zone, instance_name):
    credentials: GoogleCredentials = GoogleCredentials.get_application_default()
    compute: Resource = build('compute', 'v1', credentials=credentials)
    instance: dict = compute.instances().get(project=project, zone=zone, instance=instance_name).execute()
    result: dict = compute.instances().stop(project=project, zone=zone, instance=instance['name']).execute()
    # Wait Terminated
    while True:
        instance: dict = compute.instances().get(project=project, zone=zone, instance=instance_name).execute()
        print(instance["status"])
        #  RUNNING -> STOPPING -> TERMINATED
        if instance["status"] == "TERMINATED":
            break
        time.sleep(5)


if __name__ == "__main__":
    project = 'xxx'
    zone = 'yyy'
    instance_name = "zzz"
    start(project, zone, instance_name)
    stop(project, zone, instance_name)

Recommended Posts

Start / stop GCE from python
Stop Omxplayer from Python code
Start python
Start using Python
Python quick start
sql from python
[Python] Start studying
MeCab from Python
[Python] Start diary from today Atcorder ABC058-B
Use thingsspeak from python
Touch MySQL from Python 3
Operate Filemaker from Python
Use fluentd from python
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Python from or import
Use MySQL from Python
Run python from excel
Install python from source
Operate neutron from Python!
Use MySQL from Python
Operate LXC from Python
Manipulate riak from python
Force Python from Fortran
Use BigQuery from python.
Execute command from python
[Python] Read From Stdin
Use mecab-ipadic-neologd from python
Stop EC2 for specified time + start with Lambda (python)
Let's start Python from Excel. I don't use VBA.
Working with Azure CosmosDB from Python (quick start digging)
[Python] Start a batch file from Python and pass variables.
Flatten using Python yield from
Call CPLEX from Python (DO cplex)
Deep Python learned from DEAP
Pseudo stop / start NAT gateway
Post from Python to Slack
Grammar features added from Python3.6
Cheating from PHP to Python
Make MeCab available from Python3
Information obtained from tweet_id (Python)
OCR from PDF in Python
Run illustrator script from python
Use MySQL from Anaconda (python)
I want to start a lot of processes from python
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Study from Python Hour4: Object-oriented ②
Query Athena from Lambda Python
Access Oracle DB from Python
Study from Python Hour3: Functions
Start to Selenium using python
[Gimp] Start scripting in Python
3 Reasons Beginners to Start Python
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
Install pyenv from Homebrew, install Python from pyenv
Study from Python Hour4: Object-oriented ①
Python naming convention (from PEP8)
With skype, notify with skype from python!
Use e-Stat API from Python