-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshutdownSelf.py
More file actions
executable file
·33 lines (27 loc) · 868 Bytes
/
shutdownSelf.py
File metadata and controls
executable file
·33 lines (27 loc) · 868 Bytes
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
#!/usr/bin/env python
import boto3
import requests
from sys import exit
from os import path, remove
from subprocess import call
from datetime import datetime, timedelta
TIMESTAMP_FILE = 'shutdown.timestamp'
NOW = datetime.now()
def get_instance_id():
return requests.get('http://instance-data/latest/meta-data/instance-id').text
def shutdown_self():
ec2 = boto3.resource('ec2')
try:
server_start_time = datetime.fromtimestamp(path.getmtime(TIMESTAMP_FILE))
except:
call(['touch', TIMESTAMP_FILE])
call(['chmod', '666', TIMESTAMP_FILE])
exit()
if ((NOW - timedelta(minutes=110)) > server_start_time):
try:
remove(TIMESTAMP_FILE)
ec2.instances.filter(InstanceIds=[get_instance_id(),]).stop()
except OSError:
pass
if __name__ == "__main__":
shutdown_self()