-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
50 lines (43 loc) · 1.55 KB
/
handler.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
from scaleway_lib_fix import server_action, update_server
from scaleway import Client
from scaleway.instance.v1 import InstanceV1API
from scaleway.instance.v1.types import ServerAction
client = Client.from_env()
def stopSchedule(event, context):
instance_api = InstanceV1API(client)
servers = instance_api.list_servers_all()
for server in servers:
if "on-schedule" in server.tags:
# Set the tag "scheduled-off" to the server and stop the instance
result = server_action(
instance_api, server_id=server.id, action=ServerAction.POWEROFF
)
result = update_server(
instance_api, server_id=server.id, tags=server.tags + ["scheduled-off"]
)
return {
"body": "Done",
"headers": {
"Content-Type": ["text/plain"],
},
}
def startSchedule(event, context):
instance_api = InstanceV1API(client)
servers = instance_api.list_servers_all()
for server in servers:
if "scheduled-off" in server.tags:
# Start the server and remove the scheduled-off tag
result = server_action(
instance_api, server_id=server.id, action=ServerAction.POWERON
)
tags = server.tags
tags.remove("scheduled-off")
result = update_server(instance_api, server_id=server.id, tags=tags)
return {
"body": "Done",
"headers": {
"Content-Type": ["text/plain"],
},
}
if __name__ == "__main__":
stopSchedule(None, None)