From fca8a0b1af32083b8ea025135b120091aec9714f Mon Sep 17 00:00:00 2001 From: "k.kedron" Date: Thu, 4 Jun 2020 10:46:47 +0200 Subject: 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 Change-Id: I158d816362f91f74b217fe85112cf7c14da8f1ec --- .../recipes/1_create_consumer_and_user.rb | 19 ++--- .../recipes/2_check_Backend.rb | 19 ++--- .../recipes/3_import_Normatives.rb | 1 + .../templates/default/check_Backend_Health.py.erb | 52 ------------ .../templates/default/consumers.py.erb | 93 ---------------------- 5 files changed, 11 insertions(+), 173 deletions(-) delete mode 100644 catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/templates/default/check_Backend_Health.py.erb delete mode 100644 catalog-be/sdc-backend-init/chef-repo/cookbooks/sdc-catalog-be-setup/templates/default/consumers.py.erb (limited to 'catalog-be/sdc-backend-init/chef-repo') 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 + ']') -- cgit 1.2.3-korg