Skip to content

Commit 1d92ded

Browse files
author
lijianguo
committed
Merge branch 'main' of github.com:drycc/helmbroker into main
2 parents f99a2a4 + 0a36372 commit 1d92ded

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')
@@ -69,7 +69,8 @@ def update(instance_id: str, details: UpdateDetails):
6969
data['details']['service_id'] = details.parameters
7070
data['last_operation'] = {
7171
"state": OperationState.IN_PROGRESS.value,
72-
"description": "update %s in progress at %s" % (instance_id, time.time()) # noqa
72+
"description": (
73+
"update %s in progress at %s" % (instance_id, time.time()))
7374
}
7475
dump_instance_meta(instance_id, data)
7576
chart_path = get_chart_path(instance_id)
@@ -93,10 +94,12 @@ def update(instance_id: str, details: UpdateDetails):
9394
status, output = command("helm", *args)
9495
if status != 0:
9596
data["last_operation"]["state"] = OperationState.FAILED.value
96-
data["last_operation"]["description"] = "update %s failed: %s" % (instance_id, output) # noqa
97+
data["last_operation"]["description"] = (
98+
"update %s failed: %s" % (instance_id, output))
9799
else:
98100
data["last_operation"]["state"] = OperationState.SUCCEEDED.value
99-
data["last_operation"]["description"] = "update %s succeeded at %s" % (instance_id, time.time()) # noqa
101+
data["last_operation"]["description"] = (
102+
"update %s succeeded at %s" % (instance_id, time.time()))
100103
dump_instance_meta(instance_id, data)
101104

102105

@@ -111,7 +114,8 @@ def bind(instance_id: str,
111114
"credentials": {},
112115
"last_operation": {
113116
"state": OperationState.IN_PROGRESS.value,
114-
"description": "binding %s in progress at %s" % (binding_id, time.time()) # noqa
117+
"description": (
118+
"binding %s in progress at %s" % (binding_id, time.time()))
115119
}
116120
}
117121
dump_binding_meta(instance_id, data)
@@ -166,9 +170,14 @@ def bind(instance_id: str,
166170
@app.task()
167171
def deprovision(instance_id: str):
168172
with InstanceLock(instance_id):
173+
shutil.copy(get_instance_file(instance_id), "%s.%s" % (
174+
get_instance_file(instance_id), time.time()
175+
))
169176
data = load_instance_meta(instance_id)
177+
data["last_operation"]["operation"] = "deprovision"
170178
data["last_operation"]["state"] = OperationState.IN_PROGRESS.value
171-
data["last_operation"]["description"] = "deprovision %s in progress at %s" % (instance_id, time.time()) # noqa
179+
data["last_operation"]["description"] = (
180+
"deprovision %s in progress at %s" % (instance_id, time.time()))
172181
dump_instance_meta(instance_id, data)
173182
status, output = command(
174183
"helm",
@@ -181,13 +190,9 @@ def deprovision(instance_id: str):
181190
data["last_operation"]["state"] = OperationState.FAILED.value
182191
data["last_operation"]["description"] = (
183192
"deprovision error:\n%s" % output)
184-
shutil.copy(get_instance_file(instance_id), "%s.%s" % (
185-
get_instance_file(instance_id),
186-
time.time()
187-
))
188193
else:
189-
data["last_operation"]["state"] = OperationState.SUCCEEDED.value
194+
data["last_operation"]["state"] = (
195+
OperationState.SUCCEEDED.value)
190196
data["last_operation"]["description"] = (
191197
"deprovision succeeded at %s" % time.time())
192-
os.remove(get_instance_file(instance_id))
193198
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)