-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcleaner.py
More file actions
41 lines (34 loc) · 1.37 KB
/
cleaner.py
File metadata and controls
41 lines (34 loc) · 1.37 KB
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
import os
import time
import logging
import shutil
from openbrokerapi.service_broker import OperationState
from .config import INSTANCES_PATH
from .tasks import deprovision
from .utils import get_instance_file, load_instance_meta
logger = logging.getLogger(__name__)
def clean_instance():
if not os.path.exists(INSTANCES_PATH):
return
for instance_id in os.listdir(INSTANCES_PATH):
if os.path.exists(get_instance_file(instance_id)):
data = load_instance_meta(instance_id)
interval = time.time() - data["last_modified_time"]
state = data["last_operation"]["state"]
operation = data["last_operation"]["operation"]
if interval > 3600 * 24 and (
operation == "deprovision"
and state != OperationState.SUCCEEDED):
deprovision.delay(instance_id)
if operation == "deprovision":
if state == OperationState.SUCCEEDED or (
interval > 3600 * 24
and state != OperationState.SUCCEEDED):
shutil.rmtree(
os.path.join(INSTANCES_PATH, instance_id),
ignore_errors=True)
else:
shutil.rmtree(
os.path.join(INSTANCES_PATH, instance_id), ignore_errors=True)
if __name__ == "__main__":
clean_instance()