summaryrefslogtreecommitdiffstats
path: root/docker/docker_be/chef-solo/cookbooks/Deploy-DCAE/templates/default/consumers.py.erb
blob: ca58bc6ae68b3da9a70e864e8464dbda0dda7553 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python
import sys
import subprocess
#from time import sleep
import time
from datetime import datetime

class bcolors:
    HEADER    = '\033[95m'
    OKBLUE    = '\033[94m'
    OKGREEN   = '\033[92m'
    WARNING   = '\033[93m'
    FAIL      = '\033[91m'
    ENDC      = '\033[0m'
    BOLD      = '\033[1m'
    UNDERLINE = '\033[4m'


##############################
#    Functions
##############################
def checkBackend():
    command="curl -s -o /dev/null -I -w \"%{http_code}\" -i <%= @protocol %>://<%= @catalog_ip %>:<%= @catalog_port %>/sdc2/rest/v1/user/jh0003"

    proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
    (out, err) = proc.communicate()
    result = out.strip()
    return result


def checkConsumer(consumerName):
    command="curl -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 %>://<%= @catalog_ip %>:<%= @catalog_port %>/sdc2/rest/v1/consumers/" + consumerName

    proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
    (out, err) = proc.communicate()
    result = out.strip()
    return result


def createConsumer( consumerName, consumerSalt, consumerPass ):
    print '[INFO] ' + consumerName
    command="curl -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 %>://<%= @catalog_ip %>:<%= @catalog_port %>/sdc2/rest/v1/consumers/ -d '{\"consumerName\": '" + consumerName + "', \"consumerSalt\": '" + consumerSalt + "',\"consumerPassword\": '" + consumerPass + "'}'"

    proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE)

    (out, err) = proc.communicate()
    result = out.strip()
    return result




##############################
#    Definitions
##############################
consumersList = [ "<%= @consumerName %>" ]
salt = "67fbde1c142bb25c7d6086252d6ab08d"
password = "d6e61a6859456cf4ded84f641ae59301ebf19d56cd5fc8a8f15b7cc54d3b6429"
beStat=0


##############################
#    Main
##############################

for i in range(1,10):
    myResult = checkBackend()
    if myResult == '200':
        print '[INFO]: SDC Backend is up and running'
        beStat=1
        break
    else:
        currentTime = datetime.now()
        print '[ERROR]: ' + currentTime.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + ' SDC Backend not responding, try #' + str(i) + bcolors.ENDC
        time.sleep(10)

if beStat == 0:
    print '[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + 'SDC Backend is DOWN :-(' + bcolors.ENDC
    sys.exit(0)

for consumer in consumersList:
    myResult = checkConsumer(consumer)
    if myResult == '200':
        print '[INFO]: ' + consumer + ' already exists'
    else:
        myResult = createConsumer( consumer, salt, password )
        if myResult == '201':
            print '[INFO]: ' + consumer + ' created, result: [' + myResult + ']'
        else:
            print '[ERROR]: ' + bcolors.FAIL + consumer + bcolors.ENDC + ' error creating , result: [' + myResult + ']'