]> git.somenet.org - pub/jan/aic18.git/blob - camunda-overlay/camunda.py
und bugfix jetzt in schön
[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
10 CAMUNDA="http://localhost:8080/engine-rest/"
11
12 def get_current_deployments(key = 'sentiment-analysis'):
13     res = requests.get(CAMUNDA + "deployment")
14     #return [proc for proc in res.json() if proc['name'] == key]
15     return res.json()
16
17 def cleanup_old_deployments(key='sentiment-analysis'):
18     print ("Cleaning up old deployments")
19     for deployment in get_current_deployments(key):
20         res = requests.delete(CAMUNDA + "deployment/" + deployment['id'] + "?cascade=true&skipCustomListeners=true")
21         if (res.status_code == 204):
22             print ("Cleaned up deployment {}".format(deployment['id']))
23         else:
24             print ("Error cleaning old deployment {}: Code: {}".format(deployment['id'], res.status_code))
25             try:
26                 pprint(res.json())
27             except:
28                 pprint(res.content)
29
30 def create_deployment(cleanup=False):
31     parameters = [
32             ("deployment-name", "sentiment-analysis"),
33             #("enable-duplicate-filtering", "true"),
34             #("deploy-changed-only", "true"),
35             ("file1", open("sentiment-analysis.bpmn")),
36             ("file2", open("forms/input-terms.html")),
37             ("file3", open("forms/download-pdf.html")),
38             ]
39     if cleanup:
40         cleanup_old_deployments()
41     res = requests.post(CAMUNDA + "deployment/create", files=parameters)
42     if (res.status_code == 200):
43         print ("Successfully deployed Sentiment Analysis")
44     else:
45         pprint ("Status Code: {}".format(res.status_code))
46         try:
47             pprint(res.json())
48         except:
49             pprint(res.content)
50
51 def submit_terms(terms):
52     termlist = [{'term': term} for term in terms]
53     # submit to camunda
54     params = {
55             'variables': {
56                 'terms': {
57                     'value': json.dumps(termlist),
58                     'type': "json",
59                     'valueInfo': {
60                         'serializationDataFormat': "application/json",
61                         }
62                     }
63                 }
64             }
65     res = requests.post(CAMUNDA + "process-definition/key/sentiment-analysis/start", json=params)
66     if (res.status_code == 200):
67         print ("Successfully started Sentiment Analysis")
68     else:
69         pprint ("Status Code: {}".format(res.status_code))
70         try:
71             pprint(res.json())
72         except:
73             try:
74                 import xml.dom.minidom
75                 content = res.content.decode('utf-8').replace('<!doctype html>', '')
76                 print(xml.dom.minidom.parseString(content).toprettyxml())
77             except:
78                 pprint(res.content)
79
80 if __name__ == "__main__":
81     parser = argparse.ArgumentParser()
82     parser.add_argument('--deploy', dest='deploy', default=True, action='store_false', help="Do deployment step")
83     parser.add_argument('--cleanup', dest='cleanup', default=True, action='store_false', help="Initial deploy needs cleanup")
84     parser.add_argument('--autoclick', dest='autoclick', type=int, default=0, help="How many steps to autoclick")
85     args = parser.parse_args()
86
87     if args.deploy:
88         # initialize camunda process
89         create_deployment(cleanup=args.cleanup)
90
91     if args.autoclick >= 1:
92         # start clicking
93         submit_terms(["voting", "phonegate", "35c3"])