Skip to content

Commit 0a36372

Browse files
committed
fix(helmbroker): dead loop delete bug
1 parent 4ac5871 commit 0a36372

4 files changed

Lines changed: 49 additions & 28 deletions

File tree

rootfs/helmbroker/broker.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
from .utils import get_instance_path, get_chart_path, get_plan_path, \
1717
get_addon_path, get_addon_updateable, get_addon_bindable, InstanceLock, \
18-
get_instance_file, load_instance_meta, load_binding_meta, \
19-
dump_instance_meta, load_addons_meta
18+
load_instance_meta, load_binding_meta, dump_instance_meta, \
19+
load_addons_meta
2020
from .tasks import provision, bind, deprovision, update
2121

2222

@@ -60,6 +60,7 @@ def provision(self,
6060
},
6161
"last_operation": {
6262
"state": OperationState.IN_PROGRESS.value,
63+
"operation": "provision",
6364
"description": (
6465
"provision %s in progress at %s" % (
6566
instance_id, time.time()))
@@ -154,16 +155,18 @@ def deprovision(self,
154155
details: DeprovisionDetails,
155156
async_allowed: bool,
156157
**kwargs) -> DeprovisionServiceSpec:
157-
instance_path = get_instance_path(instance_id)
158-
if os.path.exists(instance_path):
159-
if not os.path.exists(get_instance_file(instance_id)):
160-
return DeprovisionServiceSpec(
161-
is_async=False, operation=OperationState.SUCCEEDED)
162-
else:
158+
if not os.path.exists(get_instance_path(instance_id)):
163159
raise ErrInstanceDoesNotExist()
164-
if not async_allowed:
165-
raise ErrAsyncRequired()
166-
deprovision.delay(instance_id)
160+
with InstanceLock(instance_id):
161+
data = load_instance_meta(instance_id)
162+
operation = data["last_operation"]["operation"]
163+
if operation == "provision":
164+
if not async_allowed:
165+
raise ErrAsyncRequired()
166+
deprovision.delay(instance_id)
167+
elif operation == "deprovision":
168+
return DeprovisionServiceSpec(
169+
is_async=True, operation=OperationState(operation))
167170
return DeprovisionServiceSpec(is_async=True)
168171

169172
def last_operation(self,

rootfs/helmbroker/cleaner.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,25 @@
1414

1515
def clean_instance():
1616
for instance_id in os.listdir(INSTANCES_PATH):
17-
if os.path.exists(get_instance_file(instance_id)): # noqa
17+
if os.path.exists(get_instance_file(instance_id)):
1818
data = load_instance_meta(instance_id)
1919
interval = time.time() - data["last_modified_time"]
20-
if interval > 3600 * 24 and data["last_operation"]["state"] != OperationState.SUCCEEDED: # noqa
20+
state = data["last_operation"]["state"]
21+
operation = data["last_operation"]["operation"]
22+
if interval > 3600 * 24 and (
23+
operation == "deprovision"
24+
and state != OperationState.SUCCEEDED):
2125
deprovision.delay(instance_id)
26+
if operation == "deprovision":
27+
if state == OperationState.SUCCEEDED or (
28+
interval > 3600 * 24
29+
and state != OperationState.SUCCEEDED):
30+
shutil.rmtree(
31+
os.path.join(INSTANCES_PATH, instance_id),
32+
ignore_errors=True)
2233
else:
23-
shutil.rmtree(os.path.join(INSTANCES_PATH, instance_id), ignore_errors=True) # noqa
34+
shutil.rmtree(
35+
os.path.join(INSTANCES_PATH, instance_id), ignore_errors=True)
2436

2537

2638
if __name__ == "__main__":

rootfs/helmbroker/tasks.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import os
2-
import shutil
32
import time
3+
import shutil
44
import yaml
55

66
from openbrokerapi.service_broker import ProvisionDetails, OperationState, \
77
UpdateDetails, BindDetails
88

99
from .celery import app
1010
from .utils import command, get_plan_path, get_chart_path, get_cred_value, \
11-
InstanceLock, get_instance_file, dump_instance_meta, dump_binding_meta, \
12-
load_instance_meta
11+
InstanceLock, dump_instance_meta, dump_binding_meta, load_instance_meta, \
12+
get_instance_file
1313

1414

1515
@app.task(serializer='pickle')
@@ -70,7 +70,8 @@ def update(instance_id: str, details: UpdateDetails):
7070
data['details']['service_id'] = details.parameters
7171
data['last_operation'] = {
7272
"state": OperationState.IN_PROGRESS.value,
73-
"description": "update %s in progress at %s" % (instance_id, time.time()) # noqa
73+
"description": (
74+
"update %s in progress at %s" % (instance_id, time.time()))
7475
}
7576
dump_instance_meta(instance_id, data)
7677
chart_path = get_chart_path(instance_id)
@@ -94,10 +95,12 @@ def update(instance_id: str, details: UpdateDetails):
9495
status, output = command("helm", *args)
9596
if status != 0:
9697
data["last_operation"]["state"] = OperationState.FAILED.value
97-
data["last_operation"]["description"] = "update %s failed: %s" % (instance_id, output) # noqa
98+
data["last_operation"]["description"] = (
99+
"update %s failed: %s" % (instance_id, output))
98100
else:
99101
data["last_operation"]["state"] = OperationState.SUCCEEDED.value
100-
data["last_operation"]["description"] = "update %s succeeded at %s" % (instance_id, time.time()) # noqa
102+
data["last_operation"]["description"] = (
103+
"update %s succeeded at %s" % (instance_id, time.time()))
101104
dump_instance_meta(instance_id, data)
102105

103106

@@ -112,7 +115,8 @@ def bind(instance_id: str,
112115
"credentials": {},
113116
"last_operation": {
114117
"state": OperationState.IN_PROGRESS.value,
115-
"description": "binding %s in progress at %s" % (binding_id, time.time()) # noqa
118+
"description": (
119+
"binding %s in progress at %s" % (binding_id, time.time()))
116120
}
117121
}
118122
dump_binding_meta(instance_id, data)
@@ -167,9 +171,14 @@ def bind(instance_id: str,
167171
@app.task()
168172
def deprovision(instance_id: str):
169173
with InstanceLock(instance_id):
174+
shutil.copy(get_instance_file(instance_id), "%s.%s" % (
175+
get_instance_file(instance_id), time.time()
176+
))
170177
data = load_instance_meta(instance_id)
178+
data["last_operation"]["operation"] = "deprovision"
171179
data["last_operation"]["state"] = OperationState.IN_PROGRESS.value
172-
data["last_operation"]["description"] = "deprovision %s in progress at %s" % (instance_id, time.time()) # noqa
180+
data["last_operation"]["description"] = (
181+
"deprovision %s in progress at %s" % (instance_id, time.time()))
173182
dump_instance_meta(instance_id, data)
174183
status, output = command(
175184
"helm",
@@ -182,13 +191,9 @@ def deprovision(instance_id: str):
182191
data["last_operation"]["state"] = OperationState.FAILED.value
183192
data["last_operation"]["description"] = (
184193
"deprovision error:\n%s" % output)
185-
shutil.copy(get_instance_file(instance_id), "%s.%s" % (
186-
get_instance_file(instance_id),
187-
time.time()
188-
))
189194
else:
190-
data["last_operation"]["state"] = OperationState.SUCCEEDED.value
195+
data["last_operation"]["state"] = (
196+
OperationState.SUCCEEDED.value)
191197
data["last_operation"]["description"] = (
192198
"deprovision succeeded at %s" % time.time())
193-
os.remove(get_instance_file(instance_id))
194199
dump_instance_meta(instance_id, data)

rootfs/helmbroker/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def command(cmd, *args, output_type="text"):
4747
"type": "object",
4848
"properties": {
4949
"state": {"type": "string"},
50+
"operation": {"type": "string"},
5051
"description": {"type": "string"}
5152
}
5253
},

0 commit comments

Comments
 (0)