From 753a6463f1e49aac6877ebde55bf2b18196227ab Mon Sep 17 00:00:00 2001 From: hoangvv Date: Tue, 7 Jan 2025 14:40:30 +0700 Subject: [PATCH] update Jenkinfile add cleanup and gen config script --- Jenkinsfile | 34 ++++++++++++++-------- Makefile | 6 ++-- setup/clean_up.sh | 59 +++++++++++++++++++++++++++++++++++++ setup/gen-config.py | 71 ++++++++++++++++++++++++++++++++++----------- setup/init_conf.sh | 33 +++++++-------------- 5 files changed, 150 insertions(+), 53 deletions(-) create mode 100755 setup/clean_up.sh diff --git a/Jenkinsfile b/Jenkinsfile index d131cf521..f90830f5b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,16 +1,26 @@ -node('node'){ +node('Node-Dev-100163'){ currentBuild.result = "SUCCESS" - try { - stage('Checkout'){ - - checkout scm - } - stage('Test'){ - - } - - } catch (err) { - + try { + stage('Checkout'){ + checkout scm + } + stage('Setup'){ + steps { + sh 'make install' + } + } + stage('Testing'){ + steps { + sh 'make gen_test_config' + } + steps{ + sh 'make run_test' + } + steps{ + sh 'make clean_test' + } + } + } catch (err) { currentBuild.result = "FAILURE" throw err } diff --git a/Makefile b/Makefile index ca51f7fd6..c6659b551 100644 --- a/Makefile +++ b/Makefile @@ -15,10 +15,12 @@ CONFIG=odoo.conf install: sudo apt install python3-pip libldap2-dev libpq-dev libsasl2-dev && \ pip install -r requirements.txt -run_test: gen_test_config - ${PYTHON} odoo-bin -i all_modules --log-level=test --test-enable -d testdb --stop-after-init --config=${CONFIG} gen_test_config: ${PWD}/setup/init_conf.sh +run_test: + ${PYTHON} odoo-bin -i all_modules --log-level=test --test-enable -d testdb --stop-after-init --config=${CONFIG} +clean_test: + ${PWD}/setup/clean_up.sh gen_env: ${PWD}/setup/init_env.sh build-image: gen_env diff --git a/setup/clean_up.sh b/setup/clean_up.sh new file mode 100755 index 000000000..fec7e1fe7 --- /dev/null +++ b/setup/clean_up.sh @@ -0,0 +1,59 @@ +#!/usr/bin/bash + +export PATH=/usr/sbin:$PATH +export DEBIAN_FRONTEND=noninteractive + +set -euo pipefail +readonly COLOUR_RESET='\e[0m' +readonly aCOLOUR=( + '\e[38;5;154m' # green | Lines, bullets and separators + '\e[1m' # Bold white | Main descriptions + '\e[90m' # Grey | Credits + '\e[91m' # Red | Update notifications Alert + '\e[33m' # Yellow | Emphasis +) +trap 'onCtrlC' INT +onCtrlC() { + echo -e "${COLOUR_RESET}" + exit 1 +} + +Show() { + # OK + if (($1 == 0)); then + echo -e "${aCOLOUR[2]}[$COLOUR_RESET${aCOLOUR[0]} OK $COLOUR_RESET${aCOLOUR[2]}]$COLOUR_RESET $2" + # FAILED + elif (($1 == 1)); then + echo -e "${aCOLOUR[2]}[$COLOUR_RESET${aCOLOUR[3]}FAILED$COLOUR_RESET${aCOLOUR[2]}]$COLOUR_RESET $2" + exit 1 + # INFO + elif (($1 == 2)); then + echo -e "${aCOLOUR[2]}[$COLOUR_RESET${aCOLOUR[0]} INFO $COLOUR_RESET${aCOLOUR[2]}]$COLOUR_RESET $2" + # NOTICE + elif (($1 == 3)); then + echo -e "${aCOLOUR[2]}[$COLOUR_RESET${aCOLOUR[4]}NOTICE$COLOUR_RESET${aCOLOUR[2]}]$COLOUR_RESET $2" + fi +} + +Warn() { + echo -e "${aCOLOUR[3]}$1$COLOUR_RESET" +} + +GreyStart() { + echo -e "${aCOLOUR[2]}\c" +} + +ColorReset() { + echo -e "$COLOUR_RESET\c" +} + + +stop_test_db() { + cd "$(pwd)/testing_env" + Show 2 "Stopping containers..." + docker-compose down + rm -f "$(find . -type f \( -name "*.yml" -o -name "*.template" \) -not -name "docker-compose.yml" -not -name "env.template" )" + Show 0 "Test Server is offline" +} + +stop_test_db \ No newline at end of file diff --git a/setup/gen-config.py b/setup/gen-config.py index e7ce03737..7a19f7328 100755 --- a/setup/gen-config.py +++ b/setup/gen-config.py @@ -4,21 +4,58 @@ import shutil import os from dotenv import set_key from pathlib import Path +import socket +import secrets +import string -current = os.getcwd() -shutil.copyfile('../odoo.conf.sample', '../odoo.conf') -shutil.copyfile('../testing_env/env.template', '../testing_env/.env') -env_file_path = Path("../testing_env/.env") -config = configparser.ConfigParser() -config.read('../odoo.conf') -config['options']['db_host'] = 'localhost' -config['options']['db_user'] = 'nextzen' -config['options']['db_password'] = 'Smartyourlife123@*' -config['options']['db_port'] = '5432' -# Save some values to the file. -set_key(dotenv_path=env_file_path, key_to_set="PG_USER", value_to_set="nextzen") -set_key(dotenv_path=env_file_path, key_to_set="PG_PASS", value_to_set="Smartyourlife123@*") -set_key(dotenv_path=env_file_path, key_to_set="PG_PORT", value_to_set="5432") -# Write changes back to '../odoo.conf' -with open('../odoo.conf', 'w') as configfile: - config.write(configfile) \ No newline at end of file +def generate_password(length=16): + """Generates a random password of specified length.""" + alphabet = string.ascii_letters + string.digits + return ''.join(secrets.choice(alphabet) for _ in range(length)) + +def find_available_port(start_port=5432): + """Finds an available port starting from the given port.""" + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + while True: + try: + sock.bind(('localhost', start_port)) + return start_port + except OSError as e: + if e.errno == 98: # Address already in use + start_port += 1 + else: + raise + +def main(): + """ + Generates a random password and finds an available port. + Updates the Odoo configuration file and .env file with these values. + """ + db_port = find_available_port() + db_pass = generate_password(24) + db_user = "nextzen" + print(f"Available port found: {db_port}") + print(f"Database password generated: {db_pass}") + + # Copy template files + shutil.copyfile('odoo.conf.sample', 'odoo.conf') + shutil.copyfile('testing_env/env.template', 'testing_env/.env') + + # Update Odoo configuration file + config = configparser.ConfigParser() + config.read('odoo.conf') + config['options']['db_host'] = "localhost" + config['options']['db_user'] = db_user + config['options']['db_password'] = db_pass + config['options']['db_port'] = str(db_port) + with open('odoo.conf', 'w') as configfile: + config.write(configfile) + + # Update .env file + env_file_path = Path("testing_env/.env") + set_key(dotenv_path=env_file_path, key_to_set="PG_USER", value_to_set=db_user) + set_key(dotenv_path=env_file_path, key_to_set="PG_PASS", value_to_set=db_pass) + set_key(dotenv_path=env_file_path, key_to_set="PG_PORT", value_to_set=str(db_port)) + +if __name__ == "__main__": + main() diff --git a/setup/init_conf.sh b/setup/init_conf.sh index 97ddf6aea..524748a64 100755 --- a/setup/init_conf.sh +++ b/setup/init_conf.sh @@ -4,20 +4,7 @@ export PATH=/usr/sbin:$PATH export DEBIAN_FRONTEND=noninteractive set -euo pipefail -ODOO_IMAGE='hub.nextzenos.com/nexterp/odoo' -DEPLOY_PATH=$(pwd)/deployment -PG_DB=nexterp -PG_USER=nexterp -CURRENT_BRANCH=$(git symbolic-ref --short HEAD) -ODOO_ADDONS=./addons -ODOO_CONFIG=./etc -# System -DEPENDS_PACKAGE=('wget' 'curl' 'git' 'unzip' 'make' 'build-essential') -DEPENDS_COMMAND=('wget' 'curl' 'git' 'unzip' 'make') ((EUID)) && sudo_cmd="sudo" || sudo_cmd="" -readonly MINIMUM_DOCER_VERSION="20" -UNAME_U="$(uname -s)" -readonly UNAME_U readonly COLOUR_RESET='\e[0m' readonly aCOLOUR=( '\e[38;5;154m' # green | Lines, bullets and separators @@ -62,15 +49,17 @@ ColorReset() { } - - -Generate_Config(){ - python setup/gen-config.py - Show 0 'Generate Config Complete' +generate_config() { + python setup/gen-config.py + echo "Generate Config Complete" } -Run_Test_Server(){ -Show 0 'Test Server is online' +run_test_db() { + cd "$(pwd)/testing_env" + echo "Starting containers..." + docker-compose up -d + echo "Test Server is online" } -Generate_Config -Run_Test_Server \ No newline at end of file + +generate_config +run_test_db