-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
77 lines (68 loc) · 3.04 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import functions_framework
import base64
import logging
from google.api_core import exceptions
from google.auth import default
from google.cloud import compute_v1
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@functions_framework.cloud_event
def start_stop_vm(cloud_event):
try:
data = cloud_event.data
message_bytes = base64.b64decode(data["message"]["data"]).decode("utf-8")
vm_name, zone, action = message_bytes.split(":")
credentials, project = default()
compute_client = compute_v1.InstancesClient(credentials=credentials)
if action == "start":
request = compute_v1.StartInstanceRequest(project=project, zone=zone, instance=vm_name)
compute_client.start(request=request)
logger.info(f"Started VM {vm_name} in {zone}")
elif action == "stop":
request = compute_v1.StopInstanceRequest(project=project, zone=zone, instance=vm_name)
compute_client.stop(request=request)
logger.info(f"Stopped VM {vm_name} in {zone}")
elif action == "create":
create_vm(compute_client, project, zone, vm_name)
else:
logger.error(f"Invalid action: {action}")
return
except ValueError:
logger.error("Invalid message format. Expected: vm_name:zone:action")
except exceptions.NotFound:
logger.error(f"VM not found.")
except exceptions.PermissionDenied:
logger.error(f"Permission denied.")
except Exception as e:
logger.error(f"An error occurred: {e}")
def create_vm(compute_client, project, zone, vm_name):
try:
image_client = compute_v1.ImagesClient()
image = image_client.get_from_family(project="debian-cloud", family="debian-11")
source_disk_image = image.self_link
machine_type = f"zones/{zone}/machineTypes/e2-small"
config = compute_v1.Instance(
name=vm_name,
machine_type=machine_type,
disks=[
compute_v1.AttachedDisk(
boot=True,
auto_delete=True,
initialize_params=compute_v1.AttachedDiskInitializeParams(source_image=source_disk_image),
)
],
network_interfaces=[
compute_v1.NetworkInterface(
network="global/networks/default",
access_configs=[compute_v1.AccessConfig(name="External NAT")], # Removed TypeValue
)
],
service_accounts=[compute_v1.ServiceAccount(email="default", scopes=["https://www.googleapis.com/auth/devstorage.read_write", "https://www.googleapis.com/auth/logging.write"])],
)
request = compute_v1.InsertInstanceRequest(project=project, zone=zone, instance_resource=config)
compute_client.insert(request=request)
logger.info(f"Created VM {vm_name} in {zone}")
except exceptions.NotFound:
logger.error(f"Image not found.")
except Exception as e:
logger.error(f"An error occurred: {e}")