diff options
author | k.kedron <k.kedron@partner.samsung.com> | 2020-06-04 10:46:47 +0200 |
---|---|---|
committer | k.kedron <k.kedron@partner.samsung.com> | 2020-06-24 11:16:53 +0200 |
commit | fca8a0b1af32083b8ea025135b120091aec9714f (patch) | |
tree | 4bb568ca92ad037801937d1cf2264b4dff923c52 | |
parent | 7027b33eb41947e6799392d392283b09110158d1 (diff) |
Refactoring the check backend, create consumers
Continue refactoring:
- added script for healthCheck, create new consumers
- also update chef recipes to use new script
Issue-ID: SDC-2784
Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com>
Change-Id: I158d816362f91f74b217fe85112cf7c14da8f1ec
13 files changed, 138 insertions, 174 deletions
diff --git a/catalog-be/.gitignore b/catalog-be/.gitignore index ff9fd70a4d..7646b9aebb 100644 --- a/catalog-be/.gitignore +++ b/catalog-be/.gitignore @@ -1,2 +1,2 @@ /sdc-backend/chef-repo/cookbooks/sdc-catalog-be/files/default/Artifact-Generator.properties -!/sdc-backend-init/scripts/** +/sdc-backend-init/scripts/** diff --git a/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/1_create_consumer_and_user.rb b/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/1_create_consumer_and_user.rb index bf8065b079..68cfcab6ea 100644 --- a/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/1_create_consumer_and_user.rb +++ b/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/1_create_consumer_and_user.rb @@ -15,29 +15,20 @@ else user_conf_dir = "" end -bash "executing-create_user" do +bash "executing-create_users" do code <<-EOH sdcuserinit -i #{node['Nodes']['BE']} -p #{be_port} #{user_conf_dir} #{https_flag} rc=$? if [[ $rc != 0 ]]; then exit $rc; fi EOH + returns [0] end -template "/var/tmp/consumers.py" do - source "consumers.py.erb" - sensitive true - mode 0755 - variables({ - :protocol => protocol, - :be_ip => node['Nodes']['BE'], - :be_port => be_port - }) -end - -bash "executing-consumers" do +bash "executing-create_consumers" do code <<-EOH - python /var/tmp/consumers.py + sdcconsumerinit -i #{node['Nodes']['BE']} -p #{be_port} #{https_flag} rc=$? if [[ $rc != 0 ]]; then exit $rc; fi EOH + returns [0] end diff --git a/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/2_check_Backend.rb b/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/2_check_Backend.rb index cd36fbd26b..e35caed5b1 100644 --- a/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/2_check_Backend.rb +++ b/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/2_check_Backend.rb @@ -1,27 +1,18 @@ if node['disableHttp'] protocol = "https" + https_flag = "--https" be_port = node['BE']['https_port'] else protocol = "http" + https_flag = "" be_port = node['BE']['http_port'] end -template "/var/tmp/check_Backend_Health.py" do - source "check_Backend_Health.py.erb" - sensitive true - mode 0755 - variables({ - :protocol => protocol, - :be_ip => node['Nodes']['BE'], - :be_port => be_port - }) -end - -bash "executing-check_Backend_Health" do +bash "executing-check_backend_health" do code <<-EOH - python /var/tmp/check_Backend_Health.py + sdccheckbackend -i #{node['Nodes']['BE']} -p #{be_port} #{https_flag} rc=$? if [[ $rc != 0 ]]; then exit $rc; fi EOH - returns [0] + returns [0] end
\ No newline at end of file diff --git a/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/3_import_Normatives.rb b/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/3_import_Normatives.rb index f59d91693d..79f880b146 100644 --- a/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/3_import_Normatives.rb +++ b/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/recipes/3_import_Normatives.rb @@ -46,4 +46,5 @@ bash "executing-import_Normatives" do if [[ $rc != 0 ]]; then exit $rc; fi fi EOH + returns [0] end diff --git a/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/templates/default/check_Backend_Health.py.erb b/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/templates/default/check_Backend_Health.py.erb deleted file mode 100644 index ebd8ea5c84..0000000000 --- a/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/templates/default/check_Backend_Health.py.erb +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/python -import subprocess -#from time import sleep -import time -from datetime import datetime -import sys - -beStat=0 -BE_IP="<%= @be_ip %>" -BE_PORT="<%= @be_port %>" -PROTOCOL="<%= @protocol %>" - -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 -k -s -o /dev/null -I -w \"%{http_code}\" -i "+PROTOCOL+"://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/jh0003" - - proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE ) - (out, err) = proc.communicate() - result = out.strip() - return str(result, "UTF-8") - -############################## -# Main -############################## - -for i in range(1,10): - myResult = checkBackend() - if myResult == '200': - print('[INFO]: Backend is up and running') - beStat=1 - break - else: - currentTime = datetime.now() - print('[ERROR]: ' + currentTime.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + ' 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 + 'Backend is DOWN :-(' + bcolors.ENDC - sys.exit(1)
\ No newline at end of file diff --git a/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/templates/default/consumers.py.erb b/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/templates/default/consumers.py.erb deleted file mode 100644 index 02ca9a8109..0000000000 --- a/catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/templates/default/consumers.py.erb +++ /dev/null @@ -1,93 +0,0 @@ -#!/usr/bin/python -import subprocess -#from time import sleep -import time -from datetime import datetime - -BE_IP="<%= @be_ip %>" -BE_PORT="<%= @be_port %>" -PROTOCOL="<%= @protocol %>" - -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 -k -s -o /dev/null -I -w \"%{http_code}\" -i "+PROTOCOL+"://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/jh0003" - - proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE ) - (out, err) = proc.communicate() - result = out.strip() - return str(result, "UTF-8") - - -def checkConsumer(consumerName): - 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 - - proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE ) - (out, err) = proc.communicate() - result = out.strip() - return str(result, "UTF-8") - - -def createConsumer( consumerName, consumerSalt, consumerPass ): - print('[INFO] ' + consumerName) - 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 + "'}'" - - proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE) - - (out, err) = proc.communicate() - result = out.strip() - return str(result, "UTF-8") - - - - -############################## -# Definitions -############################## -consumersList = [ "aai" , "appc" , "dcae" , "mso" , "sdnc" , "vid" , "cognita", "clamp" , "vfc" , "workflow" , "policy" , "pomba" , "multicloud", "cds", "modeling" ] -salt = "9cd4c3ad2a6f6ce3f3414e68b5157e63" -password = "35371c046f88c603ccba152cb3db34ec4475cb2e5713f2fc0a43bf18a5243495" -beStat=0 - - -############################## -# Main -############################## - -for i in range(1,10): - myResult = checkBackend() - if myResult == '200': - print('[INFO]: Backend is up and running') - beStat=1 - break - else: - currentTime = datetime.now() - print('[ERROR]: ' + currentTime.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + ' 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 + 'Backend is DOWN :-(' + bcolors.ENDC) - exit() - -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 + ']') diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/common/healthCheck.py b/catalog-be/src/main/resources/scripts/sdcBePy/common/healthCheck.py new file mode 100644 index 0000000000..7d8558d644 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/common/healthCheck.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +import sys +import time +from argparse import ArgumentParser +from datetime import datetime + +from sdcBePy.common.bColors import BColors +from sdcBePy.common.sdcBeProxy import SdcBeProxy + +colors = BColors() + +RETRY_TIME = 10 +RETRY_ATTEMPTS = 10 + + +def check_backend(sdc_be_proxy=None, reply_append_count=1, be_host=None, be_port=None, scheme=None, debug=False): + if sdc_be_proxy is None: + sdc_be_proxy = SdcBeProxy(be_host, be_port, scheme, debug=debug) + + for i in range(1, reply_append_count + 1): + if sdc_be_proxy.check_backend() == 200: + print('[INFO]: Backend is up and running') + return True + else: + print('[WARRING]: ' + datetime.now().strftime('%Y/%m/%d %H:%M:%S') + colors.FAIL + + ' Backend not responding, try #' + str(i) + colors.END_C) + time.sleep(RETRY_TIME) + + return False + + +def run(be_host, be_port, protocol): + if not check_backend(reply_append_count=RETRY_ATTEMPTS, be_host=be_host, be_port=be_port, scheme=protocol): + print('[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + colors.FAIL + ' Backend is DOWN :-(' + colors.END_C) + sys.exit() + + +def get_args(): + parser = ArgumentParser() + + parser.add_argument('-i', '--ip', required=True) + parser.add_argument('-p', '--port', required=True) + parser.add_argument('--https', action='store_true') + + args = parser.parse_args() + + return [args.ip, args.port, 'https' if args.https else 'http'] + + +def main(): + run(*get_args()) + + +if __name__ == '__main__': + main() diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/common/sdcBeProxy.py b/catalog-be/src/main/resources/scripts/sdcBePy/common/sdcBeProxy.py index 48261704f7..6fea657fab 100755 --- a/catalog-be/src/main/resources/scripts/sdcBePy/common/sdcBeProxy.py +++ b/catalog-be/src/main/resources/scripts/sdcBePy/common/sdcBeProxy.py @@ -35,6 +35,16 @@ class SdcBeProxy: 'role': role })) + def check_consumer(self, consumer_name): + return self.con.get("/sdc2/rest/v1/consumers/" + consumer_name) + + def create_consumer(self, consumer_name, slat, password): + return self.con.post("/sdc2/rest/v1/consumers/", json.dumps({ + 'consumerName': consumer_name, + 'consumerSalt': slat, + 'consumerPassword': password + })) + def post_file(self, path, multi_part_form_data): return self.con.post_file(path, multi_part_form_data) diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/consumers/__init__.py b/catalog-be/src/main/resources/scripts/sdcBePy/consumers/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/consumers/__init__.py diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/consumers/models/__init__.py b/catalog-be/src/main/resources/scripts/sdcBePy/consumers/models/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/consumers/models/__init__.py diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/consumers/models/consumerCandidate.py b/catalog-be/src/main/resources/scripts/sdcBePy/consumers/models/consumerCandidate.py new file mode 100644 index 0000000000..1fd2ad8929 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/consumers/models/consumerCandidate.py @@ -0,0 +1,15 @@ +from sdcBePy.common.helpers import check_arguments_not_none + + +class ConsumerCandidate: + + def __init__(self, consumer_name, slat, password): + if not check_arguments_not_none(consumer_name, slat, password): + raise AttributeError("The consumer_name, slat or password are missing!") + + self.consumer_name = consumer_name + self.slat = slat + self.password = password + + def get_parameters(self): + return self.consumer_name, self.slat, self.password diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/consumers/models/consumerCandidateList.py b/catalog-be/src/main/resources/scripts/sdcBePy/consumers/models/consumerCandidateList.py new file mode 100644 index 0000000000..b1904d5e48 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/consumers/models/consumerCandidateList.py @@ -0,0 +1,11 @@ +from sdcBePy.consumers.models.consumerCandidate import ConsumerCandidate + +consumersList = ["aai", "appc", "dcae", "mso", "sdnc", "vid", "cognita", + "clamp", "vfc", "workflow", "policy", "pomba", + "multicloud", "cds", "modeling"] +salt = "9cd4c3ad2a6f6ce3f3414e68b5157e63" +password = "35371c046f88c603ccba152cb3db34ec4475cb2e5713f2fc0a43bf18a5243495" + + +def get_consumers(): + return [ConsumerCandidate(name, slat=salt, password=password) for name in consumersList] diff --git a/catalog-be/src/main/resources/scripts/sdcBePy/consumers/run.py b/catalog-be/src/main/resources/scripts/sdcBePy/consumers/run.py new file mode 100644 index 0000000000..59a0b610d8 --- /dev/null +++ b/catalog-be/src/main/resources/scripts/sdcBePy/consumers/run.py @@ -0,0 +1,35 @@ +import time + +from sdcBePy.common.healthCheck import check_backend, RETRY_ATTEMPTS, get_args +from sdcBePy.common.sdcBeProxy import SdcBeProxy +from sdcBePy.consumers.models.consumerCandidateList import get_consumers +from sdcBePy.users.run import colors + + +def be_consumers_init(be_ip, be_port, protocol, consumer_candidate_list): + sdc_be_proxy = SdcBeProxy(be_ip, be_port, protocol) + if check_backend(sdc_be_proxy, RETRY_ATTEMPTS): + for consumer in consumer_candidate_list: + if sdc_be_proxy.check_user(consumer.consumer_name) != 200: + result = sdc_be_proxy.create_consumer(*consumer.get_parameters()) + if result == 201: + print('[INFO]: ' + consumer.consumer_name + + ' created, result: [' + str(result) + ']') + else: + print('[ERROR]: ' + colors.FAIL + consumer.consumer_name + colors.END_C + + ' error creating , result: [' + str(result) + ']') + else: + print('[INFO]: ' + consumer.consumer_name + ' already exists') + else: + print('[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + colors.FAIL + + ' Backend is DOWN :-(' + colors.END_C) + raise Exception("Cannot communicate with the backend!") + + +def main(): + be_ip, be_port, protocol = get_args() + be_consumers_init(be_ip, be_port, protocol, get_consumers()) + + +if __name__ == '__main__': + main() |