Skip to content

Commit 58d1c2c

Browse files
committed
add cleaner
1 parent 46ede22 commit 58d1c2c

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

rootfs/helmbroker/broker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def deprovision(self,
116116
if not async_allowed:
117117
raise ErrAsyncRequired()
118118

119-
deprovision.delay(instance_id, details)
119+
deprovision.delay(instance_id)
120120
return DeprovisionServiceSpec(is_async=True)
121121

122122
def last_operation(self,

rootfs/helmbroker/cleaner.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
import time
3+
import logging
4+
import shutil
5+
from .config import INSTANCES_PATH
6+
from .meta import load_instance_meta
7+
from .tasks import deprovision
8+
from openbrokerapi.service_broker import *
9+
10+
11+
logger = logging.getLogger(__name__)
12+
13+
def clean_instance():
14+
for instance_id in os.listdir(INSTANCES_PATH):
15+
if os.path.exists(os.path.join(INSTANCES_PATH, instance_id, "instance.json")):
16+
data = load_instance_meta(instance_id)
17+
interval = time.time() - data["last_modified_time"]
18+
if interval > 3600 * 24 and data["last_operation"]["state"] != OperationState.SUCCEEDED:
19+
deprovision.delay(instance_id)
20+
else:
21+
shutil.rmtree(os.path.join(INSTANCES_PATH, instance_id), ignore_errors=True)
22+
23+
24+
if __name__ == "__main__":
25+
clean_instance()

rootfs/helmbroker/meta.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import json
3+
import time
34
from jsonschema import validate
45
from .config import INSTANCES_PATH
56

@@ -23,7 +24,8 @@
2324
"state": {"type": "string"},
2425
"description": {"type": "string"}
2526
}
26-
}
27+
},
28+
"last_modified_time": {"type": "number"}
2729
},
2830
}
2931

@@ -37,6 +39,7 @@ def load_instance_meta(instance_id):
3739

3840

3941
def dump_instance_meta(instance_id, data):
42+
data["last_modified_time "] = time.time()
4043
file = os.path.join(INSTANCES_PATH, instance_id, "instance.json")
4144
validate(instance=data, schema=INSTANCE_META_SCHEMA)
4245
with open(file, "w") as f:
@@ -56,7 +59,8 @@ def dump_instance_meta(instance_id, data):
5659
"state": {"type": "string"},
5760
"description": {"type": "string"}
5861
}
59-
}
62+
},
63+
"last_modified_time": {"type": "number"}
6064
}
6165
}
6266

@@ -70,6 +74,7 @@ def load_binding_meta(instance_id):
7074

7175

7276
def dump_binding_meta(instance_id, data):
77+
data["last_modified_time "] = time.time()
7378
file = os.path.join(INSTANCES_PATH, instance_id, "binding.json")
7479
validate(instance=data, schema=INSTANCE_META_SCHEMA)
7580
with open(file, "w") as f:

rootfs/helmbroker/tasks.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
22
import time
33
import yaml
4+
import shutil
45

6+
from .config import INSTANCES_PATH
57
from .utils import command, get_plan_path, get_chart_path, get_cred_value
68
from .meta import dump_instance_meta, dump_binding_meta, load_instance_meta
79
from openbrokerapi.service_broker import *
@@ -137,7 +139,7 @@ def bind(instance_id: str,
137139
dump_binding_meta(instance_id, data)
138140

139141

140-
def deprovision(instance_id: str, details: DeprovisionDetails):
142+
def deprovision(instance_id: str):
141143
data = load_instance_meta(instance_id)
142144
data["last_operation"]["state"] = OperationState.IN_PROGRESS
143145
data["last_operation"]["description"] = "deprovision %s in progress at %s" % (instance_id, time.time())
@@ -155,4 +157,5 @@ def deprovision(instance_id: str, details: DeprovisionDetails):
155157
data["last_operation"]["description"] = "deprovision error:\n%s" % output
156158
else:
157159
data["last_operation"]["state"] = OperationState.SUCCEEDED
158-
data["last_operation"]["description"] = "deprovision succeeded at %s" % time.time()
160+
data["last_operation"]["description"] = "deprovision succeeded at %s" % time.time()
161+
shutil.rmtree(os.path.join(INSTANCES_PATH, instance_id), ignore_errors=True)

0 commit comments

Comments
 (0)