Refactoring the sdc-BE-init python scripts
[sdc.git] / catalog-be / sdc-backend-init / chef-repo / cookbooks / sdc-catalog-be-setup / templates / default / consumers.py.erb
1 #!/usr/bin/python
2 import subprocess
3 #from time import sleep
4 import time
5 from datetime import datetime
6
7 BE_IP="<%= @be_ip %>"
8 BE_PORT="<%= @be_port %>"
9 PROTOCOL="<%= @protocol %>"
10
11 class bcolors:
12     HEADER    = '\033[95m'
13     OKBLUE    = '\033[94m'
14     OKGREEN   = '\033[92m'
15     WARNING   = '\033[93m'
16     FAIL      = '\033[91m'
17     ENDC      = '\033[0m'
18     BOLD      = '\033[1m'
19     UNDERLINE = '\033[4m'
20
21
22 ##############################
23 #    Functions
24 ##############################
25 def checkBackend():
26     command="curl -k -s -o /dev/null -I -w \"%{http_code}\" -i "+PROTOCOL+"://" + BE_IP  + ":" + BE_PORT + "/sdc2/rest/v1/user/jh0003"
27
28     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
29     (out, err) = proc.communicate()
30     result = out.strip()
31     return str(result, "UTF-8")
32
33
34 def checkConsumer(consumerName):
35     command="curl -k -s -o /dev/null -I -w \"%{http_code}\" -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\" "+PROTOCOL+"://" + BE_IP  + ":" + BE_PORT + "/sdc2/rest/v1/consumers/" + consumerName
36
37     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
38     (out, err) = proc.communicate()
39     result = out.strip()
40     return str(result, "UTF-8")
41
42
43 def createConsumer( consumerName, consumerSalt, consumerPass ):
44     print('[INFO] ' + consumerName)
45     command="curl -k -s -o /dev/null -w \"%{http_code}\" -X POST -i -H \"Accept: application/json; charset=UTF-8\" -H \"Content-Type: application/json\" -H \"USER_ID: jh0003\" "+PROTOCOL+"://"  + BE_IP  + ":" + BE_PORT + "/sdc2/rest/v1/consumers/ -d '{\"consumerName\": '" + consumerName + "', \"consumerSalt\": '" + consumerSalt + "',\"consumerPassword\": '" + consumerPass + "'}'"
46
47     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE)
48
49     (out, err) = proc.communicate()
50     result = out.strip()
51     return str(result, "UTF-8")
52
53
54
55
56 ##############################
57 #    Definitions
58 ##############################
59 consumersList = [ "aai" , "appc" , "dcae" , "mso" , "sdnc" , "vid" , "cognita", "clamp" , "vfc" , "workflow" , "policy" , "pomba" , "multicloud", "cds", "modeling" ]
60 salt = "9cd4c3ad2a6f6ce3f3414e68b5157e63"
61 password = "35371c046f88c603ccba152cb3db34ec4475cb2e5713f2fc0a43bf18a5243495"
62 beStat=0
63
64
65 ##############################
66 #    Main
67 ##############################
68
69 for i in range(1,10):
70     myResult = checkBackend()
71     if myResult == '200':
72         print('[INFO]: Backend is up and running')
73         beStat=1
74         break
75     else:
76         currentTime = datetime.now()
77         print('[ERROR]: ' + currentTime.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + ' Backend not responding, try #' + str(i) + bcolors.ENDC)
78         time.sleep(10)
79
80 if beStat == 0:
81     print('[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + 'Backend is DOWN :-(' + bcolors.ENDC)
82     exit()
83
84 for consumer in consumersList:
85     myResult = checkConsumer(consumer)
86     if myResult == '200':
87         print('[INFO]: ' + consumer + ' already exists')
88     else:
89         myResult = createConsumer( consumer, salt, password )
90         if myResult == '201':
91             print('[INFO]: ' + consumer + ' created, result: [' + myResult + ']')
92         else:
93             print('[ERROR]: ' + bcolors.FAIL + consumer + bcolors.ENDC + ' error creating , result: [' + myResult + ']')