-
Notifications
You must be signed in to change notification settings - Fork 2
/
cleanup.py
35 lines (31 loc) · 1.25 KB
/
cleanup.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
import datetime
import os
import sys
import time
# Clenup scirpt which will delete VM which is living longer than 1day
from novaclient.v1_1 import client
USER = os.environ.get('OS_USERNAME', '')
PASS = os.environ.get('OS_PASSWORD', '')
TENANT = os.environ.get('OS_TENANT_NAME', '')
AUTH_URL = os.environ.get('OS_AUTH_URL', '')
REGION_NAME = os.environ.get('OS_REGION_NAME', '')
client = client.Client(USER, PASS, TENANT, AUTH_URL,
endpoint_type="adminURL",
region_name=REGION_NAME)
now = datetime.datetime.now()
oneday = datetime.timedelta(days=1)
for server in client.servers.list(True, {'all_tenants':True}):
delta = now - datetime.datetime.strptime(server.created,
"%Y-%m-%dT%H:%M:%SZ")
if delta > oneday:
print "server_id:%s\ttenant_id:%s deleted" % (server.id,
server.tenant_id)
try:
server.delete()
time.sleep(1)
print "server_id:%s\ttenant_id:%s deleted" % (server.id,
server.tenant_id)
except:
print "[ERROR] failed to delete server_id:%s\ttenant_id:%s" % (
server.id,
server.tenant_id)