8 from pprint import pprint
11 CAMUNDA="http://localhost:8085/engine-rest/"
13 def get_current_deployments(key = 'sentiment-analysis'):
14 res = requests.get(CAMUNDA + "deployment")
15 #return [proc for proc in res.json() if proc['name'] == key]
18 def get_current_process_instances(key = 'sentiment-analysis'):
19 res = requests.get(CAMUNDA + 'process-instance')
23 return [instance for instance in res.json() if instance['definitionId'].startswith(key + ":")]
25 def cleanup_old_deployments(key='sentiment-analysis'):
26 print ("Cleaning up old deployments")
27 for deployment in get_current_deployments(key):
28 res = requests.delete(CAMUNDA + "deployment/" + deployment['id'] + "?cascade=true&skipCustomListeners=true")
29 if (res.status_code == 204):
30 print ("Cleaned up deployment {}".format(deployment['id']))
32 print ("Error cleaning old deployment {}: Code: {}".format(deployment['id'], res.status_code))
39 res = requests.get(CAMUNDA + "user")
43 res = requests.get(CAMUNDA + "group")
46 def cleanup_users(keep = ["demo"]):
47 for user in get_users():
49 if user['id'] in keep:
52 res = requests.delete(CAMUNDA + "user/" + user['id'])
53 if (res.status_code == 204):
54 print ("Cleaned up user {}".format(user['id']))
56 print ("Error cleaning user {}: Code: {}".format(user['id'], res.status_code))
63 for group in get_groups():
64 res = requests.delete(CAMUNDA + "group/" + group['id'])
65 if (res.status_code == 204):
66 print ("Cleaned up group {}".format(group['id']))
68 print ("Error cleaning group {}: Code: {}".format(group['id'], res.status_code))
74 def get_authorizations():
75 res = requests.get(CAMUNDA + "authorization")
78 def create_admin_user():
85 "email": "aic@dsg.tuwien.ac.at"
91 res = requests.post(CAMUNDA + "user/create", json=params)
92 if (res.status_code == 204):
93 print ("Created user admin")
95 print ("Error creating user admin: Code: {}".format(res.status_code))
102 # clean all authorizations
103 for auth in get_authorizations():
104 res = requests.delete(CAMUNDA + "authorization/" + auth['id'])
105 if (res.status_code == 204):
106 print ("Cleaned up authorization {}".format(auth['id']))
108 print ("Error cleaning authorization {}: Code: {}".format(auth['id'], res.status_code))
115 # set all permissions for modifying users
118 "permissions": ["ALL"],
122 "resourceId": "admin"
124 res = requests.post(CAMUNDA + "authorization/create", json=params)
125 if (res.status_code == 200):
126 print ("Set users permissions for admin users")
128 print ("Error setting users permissions for admin user: Code: {}".format(res.status_code))
136 "id": "camunda-admin",
137 "name": "camunda BPM Administrators",
140 res = requests.post(CAMUNDA + "group/create", json=params)
141 if (res.status_code == 204):
142 print ("Created user admin")
144 print ("Error creating group camunda-admin: Code: {}".format(res.status_code))
150 # add admin user to camunda-admin
151 res = requests.put(CAMUNDA + "group/camunda-admin/members/admin")
152 if (res.status_code == 204):
153 print ("Added admin to camunda-admin")
155 print ("Error adding user admin to group camunda-admin: Code: {}".format(res.status_code))
161 # set all permissions for admin group
162 for resourceType in range(0, 16):
165 "permissions": ["ALL"],
167 "groupId": "camunda-admin",
168 "resourceType": resourceType,
171 res = requests.post(CAMUNDA + "authorization/create", json=params)
172 if (res.status_code == 200):
173 print ("Set group permissions for id {}".format(resourceType))
175 print ("Error setting application permissions for admin user: Code: {}".format(res.status_code))
182 res = requests.get(CAMUNDA + "filter")
185 def cleanup_filters(keep = ["All Tasks"]):
186 for filter in get_filters():
187 # keep filter "All Tasks"
188 if filter['name'] in keep:
191 res = requests.delete(CAMUNDA + "filter/" + filter['id'])
192 if (res.status_code == 204):
193 print ("Cleaned up filter {}".format(filter['name']))
195 print ("Error cleaning filter {} ({}): Code: {}".format(filter['name'], filter['id'], res.status_code))
201 def create_deployment(cleanup=False):
203 ("deployment-name", "sentiment-analysis"),
204 #("enable-duplicate-filtering", "true"),
205 #("deploy-changed-only", "true"),
206 ("file1", open("sentiment-analysis.bpmn")),
207 ("file2", open("forms/input-terms.html")),
208 ("file3", open("forms/download-pdf.html")),
211 cleanup_old_deployments()
212 res = requests.post(CAMUNDA + "deployment/create", files=parameters)
213 if (res.status_code == 200):
214 print ("Successfully deployed Sentiment Analysis")
216 pprint ("Status Code: {}".format(res.status_code))
222 def submit_terms(terms):
223 termlist = [{'term': term} for term in terms]
228 'value': json.dumps(termlist),
231 'serializationDataFormat': "application/json",
236 res = requests.post(CAMUNDA + "process-definition/key/sentiment-analysis/start", json=params)
237 if (res.status_code == 200):
238 print ("Successfully started Sentiment Analysis")
240 pprint ("Status Code: {}".format(res.status_code))
245 import xml.dom.minidom
246 content = res.content.decode('utf-8').replace('<!doctype html>', '')
247 print(xml.dom.minidom.parseString(content).toprettyxml())
252 instances = get_current_process_instances()
253 if len(instances) == 0:
254 print ("Error: no running instance found.")
256 instance = instances[0]['id']
257 res = requests.get(CAMUNDA + 'process-instance/' + instance + '/variables')
264 parser = argparse.ArgumentParser()
265 parser.add_argument('--no-deploy', dest='deploy', default=True, action='store_false', help="Do not run the deployment step")
266 parser.add_argument('--no-cleanup', dest='cleanup', default=True, action='store_false', help="Initial deploy does not need cleanup")
267 parser.add_argument('--autoclick', dest='autoclick', type=int, default=0, help="How many steps to autoclick")
268 parser.add_argument('--terms', dest='terms', type=str, nargs='*', default=["aic", "TU Wien"], help="How many steps to autoclick")
269 args = parser.parse_args()
272 # initialize camunda process
273 create_deployment(cleanup=args.cleanup)
280 if args.autoclick >= 1:
282 submit_terms(args.terms)
284 if args.autoclick >= 2:
288 if __name__ == "__main__":
294 except KeyboardInterrupt:
297 print("looks bad - retry in 10 sec - "+str(sys.exc_info()[0]))