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