]> git.somenet.org - pub/jan/aic18.git/blob - camunda-overlay/camunda.py
Merge branch '54-add-required-information-to-readme' into 'master'
[pub/jan/aic18.git] / camunda-overlay / camunda.py
1 #!/usr/bin/env python3
2
3 import sys
4 import os
5 import json
6 import requests
7 import argparse
8 from pprint import pprint
9 import time
10
11 CAMUNDA="http://localhost:8085/engine-rest/"
12
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]
16     return res.json()
17
18 def get_current_process_instances(key = 'sentiment-analysis'):
19     res = requests.get(CAMUNDA + 'process-instance')
20     if (key is None):
21         return res.json()
22     else:
23         return [instance for instance in res.json() if instance['definitionId'].startswith(key + ":")]
24
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']))
31         else:
32             print ("Error cleaning old deployment {}: Code: {}".format(deployment['id'], res.status_code))
33             try:
34                 pprint(res.json())
35             except:
36                 pprint(res.content)
37
38 def get_users():
39     res = requests.get(CAMUNDA + "user")
40     return res.json()
41
42 def get_groups():
43     res = requests.get(CAMUNDA + "group")
44     return res.json()
45
46 def cleanup_users(keep = ["demo"]):
47     for user in get_users():
48         # keep user "demo"
49         if user['id'] in keep:
50             continue
51
52         res = requests.delete(CAMUNDA + "user/" + user['id'])
53         if (res.status_code == 204):
54             print ("Cleaned up user {}".format(user['id']))
55         else:
56             print ("Error cleaning user {}: Code: {}".format(user['id'], res.status_code))
57             try:
58                 pprint(res.json())
59             except:
60                 pprint(res.content)
61
62 def cleanup_groups():
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']))
67         else:
68             print ("Error cleaning group {}: Code: {}".format(group['id'], res.status_code))
69             try:
70                 pprint(res.json())
71             except:
72                 pprint(res.content)
73
74 def get_authorizations():
75     res = requests.get(CAMUNDA + "authorization")
76     return res.json()
77
78 def create_admin_user():
79     # create user
80     params = {
81             "profile": {
82                 "id": "admin",
83                 "firstName": "Admin",
84                 "lastName": "User",
85                 "email": "aic@dsg.tuwien.ac.at"
86                 },
87             "credentials": {
88                 "password": "admin"
89                 }
90             }
91     res = requests.post(CAMUNDA + "user/create", json=params)
92     if (res.status_code == 204):
93         print ("Created user admin")
94     else:
95         print ("Error creating user admin: Code: {}".format(res.status_code))
96         try:
97             pprint(res.json())
98         except:
99             pprint(res.content)
100
101
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']))
107         else:
108             print ("Error cleaning authorization {}: Code: {}".format(auth['id'], res.status_code))
109             try:
110                 pprint(res.json())
111             except:
112                 pprint(res.content)
113
114
115     # set all permissions for modifying users
116     params = {
117             "type": 1,
118             "permissions": ["ALL"],
119             "userId": "admin",
120             "groupId": None,
121             "resourceType": 1,
122             "resourceId": "admin"
123             }
124     res = requests.post(CAMUNDA + "authorization/create", json=params)
125     if (res.status_code == 200):
126         print ("Set users permissions for admin users")
127     else:
128         print ("Error setting users permissions for admin user: Code: {}".format(res.status_code))
129         try:
130             pprint(res.json())
131         except:
132             pprint(res.content)
133
134     # create admin group
135     params = {
136             "id": "camunda-admin",
137             "name": "camunda BPM Administrators",
138             "type": "SYSTEM"
139             }
140     res = requests.post(CAMUNDA + "group/create", json=params)
141     if (res.status_code == 204):
142         print ("Created user admin")
143     else:
144         print ("Error creating group camunda-admin: Code: {}".format(res.status_code))
145         try:
146             pprint(res.json())
147         except:
148             pprint(res.content)
149
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")
154     else:
155         print ("Error adding user admin to group camunda-admin: Code: {}".format(res.status_code))
156         try:
157             pprint(res.json())
158         except:
159             pprint(res.content)
160
161     # set all permissions for admin group
162     for resourceType in range(0, 16):
163         params = {
164                 "type": 1,
165                 "permissions": ["ALL"],
166                 "userId": None,
167                 "groupId": "camunda-admin",
168                 "resourceType": resourceType,
169                 "resourceId": "*"
170                 }
171         res = requests.post(CAMUNDA + "authorization/create", json=params)
172         if (res.status_code == 200):
173             print ("Set group permissions for id {}".format(resourceType))
174         else:
175             print ("Error setting application permissions for admin user: Code: {}".format(res.status_code))
176             try:
177                 pprint(res.json())
178             except:
179                 pprint(res.content)
180
181 def get_filters():
182     res = requests.get(CAMUNDA + "filter")
183     return res.json()
184
185 def cleanup_filters(keep = ["All Tasks"]):
186     for filter in get_filters():
187         # keep filter "All Tasks"
188         if filter['name'] in keep:
189             continue
190
191         res = requests.delete(CAMUNDA + "filter/" + filter['id'])
192         if (res.status_code == 204):
193             print ("Cleaned up filter {}".format(filter['name']))
194         else:
195             print ("Error cleaning filter {} ({}): Code: {}".format(filter['name'], filter['id'], res.status_code))
196             try:
197                 pprint(res.json())
198             except:
199                 pprint(res.content)
200
201 def create_deployment(cleanup=False):
202     parameters = [
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")),
209             ]
210     if cleanup:
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")
215     else:
216         pprint ("Status Code: {}".format(res.status_code))
217         try:
218             pprint(res.json())
219         except:
220             pprint(res.content)
221
222 def submit_terms(terms):
223     termlist = [{'term': term} for term in terms]
224     # submit to camunda
225     params = {
226             'variables': {
227                 'terms': {
228                     'value': json.dumps(termlist),
229                     'type': "json",
230                     'valueInfo': {
231                         'serializationDataFormat': "application/json",
232                         }
233                     }
234                 }
235             }
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")
239     else:
240         pprint ("Status Code: {}".format(res.status_code))
241         try:
242             pprint(res.json())
243         except:
244             try:
245                 import xml.dom.minidom
246                 content = res.content.decode('utf-8').replace('<!doctype html>', '')
247                 print(xml.dom.minidom.parseString(content).toprettyxml())
248             except:
249                 pprint(res.content)
250
251 def download_pdf():
252     instances = get_current_process_instances()
253     if len(instances) == 0:
254         print ("Error: no running instance found.")
255         return
256     instance = instances[0]['id']
257     res = requests.get(CAMUNDA + 'process-instance/' + instance + '/variables')
258     try:
259         pprint(res.json())
260     except:
261         pprint(res.content)
262
263 def main():
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()
270
271     if args.deploy:
272         # initialize camunda process
273         create_deployment(cleanup=args.cleanup)
274         if args.cleanup:
275             cleanup_users()
276             cleanup_groups()
277             cleanup_filters()
278             create_admin_user()
279
280     if args.autoclick >= 1:
281         # start clicking
282         submit_terms(args.terms)
283
284     if args.autoclick >= 2:
285         download_pdf()
286
287
288 if __name__ == "__main__":
289     loop_main = True
290     while loop_main:
291         try:
292             main()
293             loop_main = False
294         except KeyboardInterrupt:
295             raise
296         except:
297             print("looks bad - retry in 10 sec - "+str(sys.exc_info()[0]))
298             time.sleep(10)