/
plan_query.py
108 lines (79 loc) · 2.62 KB
/
plan_query.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
A module that defines how to manipulate the plan config db.
By richardxx, 2014.1
"""
__author__ = 'richardxx'
import time
import tingDB_service
import plan_composer
def start_plan(plan_name, _type="servers", try_times = 5):
"""
_type can only be servers and clusters.
"""
while try_times > 0:
response = tingDB_service.send_request_to_op(plan_name, "op-start", _type)
if response != "":
return response
time.sleep(1)
try_times -= 1
return ""
def stop_plan(plan_name, _type="servers", try_times = 5):
while try_times > 0:
response = tingDB_service.send_request_to_op(plan_name, "op-stop", _type)
if response != "":
return response
time.sleep(1)
try_times -= 1
return ""
def change_plan_status(plan_name, new_state, plan_type = ""):
"""
Turn plan on and off.
"""
plans_collection = tingDB_service.get_config_collection("plans")
if plan_type == "":
# We get the type ourselves
plan_cursor = plans_collection.find({"plan_name": plan_name})
plan_type = plan_cursor[0]["type"]
doc_col = plan_composer.get_collection_name_for_plan_type(plan_type)
if new_state == "online":
ret_data = start_plan(plan_name, doc_col)
elif new_state == "offline":
ret_data = stop_plan(plan_name, doc_col)
else:
return False
if ret_data != "":
plans_collection.update({"plan_name": plan_name},
{"$set": {"status": new_state}})
return True
def get_plans_for_user(username):
"""
Returns all the plans for a given user.
"""
plans_collection = tingDB_service.get_config_collection("plans")
plans_cursor = plans_collection.find({"owner": username})
plans = []
for plan in plans_cursor:
plans.append(plan)
return plans
def get_URIs_for_user(username):
"""
Retrieve the URIs for all the plans for a given user.
"""
plans = get_plans_for_user(username)
URIs = []
for plan in plans:
plan_name = plan["_id"]
print plan_name
uri = tingDB_service.send_request_to_op(plan_name, "op-geturi")
URIs.append(uri)
return URIs
def __get_server_for_plan(plan):
"""
Get server descriptor from plan descriptor.
"""
plan_name = plan["_id"]
plan_type = plan["type"]
col_name = plan_composer.get_collection_name_for_plan_type(plan_type)
servers_collection = tingDB_service.get_config_collection(col_name)
server_cursor = servers_collection.find({"_id": plan_name})
return server_cursor[0]