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