diff --git a/pmm_qa/Dockerfile b/pmm_qa/Dockerfile new file mode 100644 index 0000000..83a5c65 --- /dev/null +++ b/pmm_qa/Dockerfile @@ -0,0 +1,16 @@ +FROM php:7.4-alpine + +RUN set -ex \ + && apk --no-cache add postgresql-libs postgresql-dev \ + && docker-php-ext-install pgsql pdo_pgsql \ + && docker-php-ext-install mysqli && docker-php-ext-enable mysqli \ + && apk del postgresql-dev + +RUN apk --update add --virtual build-dependencies build-base openssl-dev autoconf \ + && pecl install mongodb \ + && docker-php-ext-enable mongodb \ + && apk del build-dependencies build-base openssl-dev autoconf \ + && rm -rf /var/cache/apk/* + +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer +RUN composer require mongodb/mongodb diff --git a/pmm_qa/mlaunch_setup.sh b/pmm_qa/mlaunch_setup.sh new file mode 100755 index 0000000..a3ee14a --- /dev/null +++ b/pmm_qa/mlaunch_setup.sh @@ -0,0 +1,111 @@ +#!/bin/sh + +while [ $# -gt 0 ]; do + if [[ $1 == *"--"* ]]; then + param="${1/--/}" + declare $param="$2" + fi + shift +done + +if [ -z "$mongodb_version" ]; then + export mongodb_version=6.0 +fi + +if [ -z "$mongdb_setup" ]; then + export mongdb_setup=pss +fi + +if [ -z "$metrics_mode" ]; then + export metrics_mode=push +fi + +# Mongo user credtials for the replicat set cluster +export user="dba" +export pwd="test1234" + +# Install the dependencies +source ~/.bash_profile || true; +apt-get update +apt-get -y install wget curl jq git gnupg2 lsb-release +apt-get -y install libreadline6-dev systemtap-sdt-dev zlib1g-dev libssl-dev libpam0g-dev python-dev bison make flex libipc-run-perl +sleep 10 + +#wget https://raw.githubusercontent.com/Percona-QA/percona-qa/master/mongo_startup.sh +#chmod +x mongo_startup.sh +export SERVICE_RANDOM_NUMBER=$(echo $((1 + $RANDOM % 9999))) + +### Detect latest tarball link for specified mongodb_version: 7.0 | 6.0 | 5.0 | 4.4 | 4.2 at the moment +# psmdb_latest=$(wget -q --post-data "version=percona-server-mongodb-${mongodb_version}" https://www.percona.com/products-api.php -O - | grep -oP "(?<=value\=\")[^\"]*" | sort -V | tail -1) +psmdb_tarball=$(wget -q --post-data "version_files=percona-server-mongodb-${mongodb_version}&software_files=binary" https://www.percona.com/products-api.php -O - | jq -r '.[] | select(.link | contains("sha") | not) | .link' | grep glibc2\.17-minimal) + +echo "Downloading ${mongodb_version} ..." +wget -O percona_server_mongodb.tar.gz ${psmdb_tarball} +tar -xvf percona_server_mongodb.tar.gz + +export extracted_folder_name=$(ls | grep percona-server-mongodb) +echo "Extracted folder name ${extracted_folder_name}" +mv ${extracted_folder_name} psmdb_${mongodb_version} + +# TODO: refactor if to match range of versions 6.0+ +if [[ "$mongodb_version" == "6.0" || "$mongodb_version" == "7.0" ]]; then + ### PSMDB 6+ requires "percona-mongodb-mongosh" additionally + echo "Downloading mongosh ..." + mongosh_link=$(wget -q --post-data "version_files=percona-server-mongodb-${mongodb_version}&software_files=binary" https://www.percona.com/products-api.php -O - | jq -r '.[] | select(.link | contains("sha") | not) | .link' | grep mongosh) + wget -O mongosh.tar.gz ${mongosh_link} + tar -xvf mongosh.tar.gz + mv percona-mongodb-mongosh* mongosh + cp mongosh/bin/mongosh ./psmdb_${mongodb_version}/bin/mongo + rm mongosh.tar.gz +fi +rm percona_server_mongodb.tar.gz* + +# For mongodb dependency in Debian +wget http://http.us.debian.org/debian/pool/main/o/openldap/libldap-2.4-2_2.4.47+dfsg-3+deb10u7_amd64.deb +apt install -y ./libldap-2.4-2_2.4.47+dfsg-3+deb10u7_amd64.deb + +if [ "$mongodb_setup" == "pss" ]; then + mlaunch init --bind_ip 0.0.0.0 --binarypath "./psmdb_${mongodb_version}/bin" --replicaset --name rs1 --nodes 3 + sleep 20 + pmm-admin remove mongodb mongodb_rs1_1_${SERVICE_RANDOM_NUMBER} || true; pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=rs1 --environment=mongodb_rs_node --metrics-mode=$metrics_mode mongodb_rs1_1_${SERVICE_RANDOM_NUMBER} --debug 127.0.0.1:27017 + sleep 2 + pmm-admin remove mongodb mongodb_rs1_2_${SERVICE_RANDOM_NUMBER} || true; pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=rs1 --environment=mongodb_rs_node --metrics-mode=$metrics_mode mongodb_rs1_2_${SERVICE_RANDOM_NUMBER} --debug 127.0.0.1:27018 + sleep 2 + pmm-admin remove mongodb mongodb_rs1_3_${SERVICE_RANDOM_NUMBER} || true; pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=rs1 --environment=mongodb_rs_node --metrics-mode=$metrics_mode mongodb_rs1_3_${SERVICE_RANDOM_NUMBER} --debug 127.0.0.1:27019 + sleep 20 +fi + +if [ "$mongodb_setup" == "psa" ]; then + mlaunch init --bind_ip 0.0.0.0 --binarypath "./psmdb_${mongodb_version}/bin" --replicaset --name rs1 --nodes 2 --arbiter + sleep 20 + pmm-admin remove mongodb mongodb_rs1_1_${SERVICE_RANDOM_NUMBER} || true; pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=rs1 --environment=mongodb_rs_node --metrics-mode=$metrics_mode mongodb_rs1_1_${SERVICE_RANDOM_NUMBER} --debug 127.0.0.1:27017 + sleep 2 + pmm-admin remove mongodb mongodb_rs1_2_${SERVICE_RANDOM_NUMBER} || true; pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=rs1 --environment=mongodb_rs_node --metrics-mode=$metrics_mode mongodb_rs1_2_${SERVICE_RANDOM_NUMBER} --debug 127.0.0.1:27018 + sleep 2 + pmm-admin remove mongodb mongodb_rs1_3_${SERVICE_RANDOM_NUMBER} || true; pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=rs1 --environment=mongodb_rs_node --metrics-mode=$metrics_mode mongodb_rs1_3_${SERVICE_RANDOM_NUMBER} --debug 127.0.0.1:27019 + sleep 20 +fi + +if [ "$mongodb_setup" == "sharded" ]; then + mlaunch init --bind_ip 0.0.0.0 --binarypath "./psmdb_${mongodb_version}/bin" --replicaset --sharded rs1 rs2 --config 3 + pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --environment=mongos_shraded_node mongos_shraded_node_${SERVICE_RANDOM_NUMBER} --metrics-mode=$metrics_mode --debug 127.0.0.1:27017 + sleep 2 + pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=config --environment=mongodb_config_node mongodb_config_1_${SERVICE_RANDOM_NUMBER} --metrics-mode=$metrics_mode --debug 127.0.0.1:27024 + sleep 2 + pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=config --environment=mongodb_config_node mongodb_config_2_${SERVICE_RANDOM_NUMBER} --metrics-mode=$metrics_mode --debug 127.0.0.1:27025 + sleep 2 + pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=config --environment=mongodb_config_node mongodb_config_3_${SERVICE_RANDOM_NUMBER} --metrics-mode=$metrics_mode --debug 127.0.0.1:27026 + sleep 2 + pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=rs1 --environment=mongodb_rs_node mongodb_rs1_1_${SERVICE_RANDOM_NUMBER} --metrics-mode=$metrics_mode --debug 127.0.0.1:27018 + sleep 2 + pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=rs1 --environment=mongodb_rs_node mongodb_rs1_2_${SERVICE_RANDOM_NUMBER} --metrics-mode=$metrics_mode --debug 127.0.0.1:27019 + sleep 2 + pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=rs1 --environment=mongodb_rs_node mongodb_rs1_3_${SERVICE_RANDOM_NUMBER} --metrics-mode=$metrics_mode --debug 127.0.0.1:27020 + sleep 2 + pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=rs2 --environment=mongodb_rs_node mongodb_rs2_1_${SERVICE_RANDOM_NUMBER} --metrics-mode=$metrics_mode --debug 127.0.0.1:27021 + sleep 2 + pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=rs2 --environment=mongodb_rs_node mongodb_rs2_2_${SERVICE_RANDOM_NUMBER} --metrics-mode=$metrics_mode --debug 127.0.0.1:27022 + sleep 2 + pmm-admin add mongodb --enable-all-collectors --cluster mongodb_node_cluster --replication-set=rs2 --environment=mongodb_rs_node mongodb_rs2_3_${SERVICE_RANDOM_NUMBER} --metrics-mode=$metrics_mode --debug 127.0.0.1:27023 + sleep 20 +fi diff --git a/pmm_qa/mlaunch_setup.yml b/pmm_qa/mlaunch_setup.yml new file mode 100644 index 0000000..b42ff62 --- /dev/null +++ b/pmm_qa/mlaunch_setup.yml @@ -0,0 +1,85 @@ +--- + +- hosts: all + become: true + become_method: sudo + vars: + psmdb_version: "{{ lookup('vars', 'extra_psmdb_version', default=lookup('env','PSMDB_VERSION') | default('4.4', true) ) }}" + psmdb_tarball: "{{ lookup('vars', 'extra_psmdb_tarball', default=lookup('env','PSMDB_TARBALL') | default('', true) ) }}" + psmdb_setup: "{{ lookup('vars', 'extra_psmdb_setup', default=lookup('env','PSMDB_SETUP') | default('pss', true) ) }}" + psmdb_container: "{{ lookup('vars', 'extra_psmdb_container', default=lookup('env','PSMDB_CONTAINER') | default('psmdb', true) ) }}" + pmm_server_ip: "{{ lookup('vars', 'extra_pmm_server_ip', default=lookup('env','PMM_SERVER_IP') | default('127.0.0.1', true) ) }}" + client_version: "{{ lookup('vars', 'extra_client_version', default=lookup('env','CLIENT_VERSION') | default('dev-latest', true) ) }}" + admin_password: "{{ lookup('vars', 'extra_admin_password', default=lookup('env','ADMIN_PASSWORD') | default('admin', true) ) }}" + pmm_qa_branch: "{{ lookup('vars', 'extra_pmm_qa_branch', default=lookup('env','PMM_QA_GIT_BRANCH') | default('main', true) ) }}" + + tasks: + - name: cleanup container for client and DB setup + shell: > + docker ps -a --filter "name={{ psmdb_container }}" | grep -q . && docker stop {{ psmdb_container }} && docker rm -fv {{ psmdb_container }} + ignore_errors: true + tags: + - cleanup + + - name: Create pmm-qa network if not exist + shell: docker network create pmm-qa + ignore_errors: true + + - name: Prepare Container for PSMDB + shell: > + docker run -d --name={{ psmdb_container }} + -p 27017:27017 + phusion/baseimage:jammy-1.0.1 + + - name: Copy all required Artifacts to the docker psmdb_container + shell: "{{ item }}" + with_items: + - docker cp ./mlaunch_setup.sh {{ psmdb_container }}:/ + - docker cp ./pmm3-client-setup.sh {{ psmdb_container }}:/ + - docker exec {{ psmdb_container }} apt-get update + - docker exec {{ psmdb_container }} apt-get -y install wget curl git gnupg2 lsb-release jq python3 pip + + - name: Install required software's to the docker psmdb_container + shell: "{{ item }}" + with_items: + - docker exec {{ psmdb_container }} python3 -m pip install --upgrade pip + - docker exec {{ psmdb_container }} pip3 install 'mtools[all]' + + - name: Install pmm2-client on the psmdb_container + shell: "{{ item }}" + with_items: + - docker network connect pmm-qa {{ psmdb_container }} + - docker exec {{ psmdb_container }} bash -x ./pmm3-client-setup.sh --pmm_server_ip {{ pmm_server_ip }} --client_version {{ client_version }} --admin_password {{ admin_password }} --use_metrics_mode no + + - name: Setup psmdb for monitoring + shell: "{{ item }}" + with_items: + - docker exec {{ psmdb_container }} bash -x ./mlaunch_setup.sh --mongodb_version {{ psmdb_version }} --mongodb_setup {{ psmdb_setup }} > setup_psmdb_{{ psmdb_version }}_{{ psmdb_setup }}.log + + - name: Setup Load Running Docker Container + shell: "{{ item }}" + with_items: + - rm -rf ~/psmdb_{{ psmdb_version }} || true; mkdir -p ~/psmdb_{{ psmdb_version }} + - wget -P ~/psmdb_{{ psmdb_version }}/ "https://raw.githubusercontent.com/Percona-Lab/qa-integration/pmm3-mongo-mlaunch/pmm_qa/Dockerfile" + - wget -P ~/psmdb_{{ psmdb_version }}/ "https://raw.githubusercontent.com/Percona-Lab/qa-integration/pmm3-mongo-mlaunch/pmm_qa/mongodb_query.php" + - docker build --tag php-db ~/psmdb_{{ psmdb_version }}/ > ~/docker-build_mongodb_load_{{ psmdb_version }}_{{ psmdb_setup }}.log || true + - docker rm mongodb_load_{{ psmdb_version }}_{{ psmdb_setup }} || true + - docker run --rm --name mongodb_load_{{ psmdb_version }}_{{ psmdb_setup }} --network=pmm-qa -v $(pwd):/usr/src/myapp -w /usr/src/myapp php-db composer require mongodb/mongodb || true + + - name: Run load on Replica Set Master(PSS) + shell: "{{ item }}" + with_items: + - docker run --name mongodb_load_{{ psmdb_version }}_{{ psmdb_setup }} -d -e MONGODB_HOST={{ psmdb_container }} -e MONGODB_PORT=27017 -e TEST_TARGET_QPS=10 -e TEST_COLLECTION=10 -e TEST_DB=30 --network=pmm-qa -v $(pwd):/usr/src/myapp -w /usr/src/myapp php-db php mongodb_query.php >> setup_psmdb_{{ psmdb_version }}_{{ psmdb_setup }}.log + when: psmdb_setup == "pss" + + - name: Run load on Replica Set Master(PSA) + shell: "{{ item }}" + with_items: + - docker run --name mongodb_load_{{ psmdb_version }}_{{ psmdb_setup }} -d -e MONGODB_HOST={{ psmdb_container }} -e MONGODB_PORT=27017 -e TEST_TARGET_QPS=10 -e TEST_COLLECTION=10 -e TEST_DB=30 --network=pmm-qa -v $(pwd):/usr/src/myapp -w /usr/src/myapp php-db php mongodb_query.php >> setup_psmdb_{{ psmdb_version }}_{{ psmdb_setup }}.log + when: psmdb_setup == "psa" + + - name: Run load on Sharded Clusters Master + shell: "{{ item }}" + with_items: + - docker run --name mongodb_load_{{ psmdb_version }}_{{ psmdb_setup }} -d -e MONGODB_HOST={{ psmdb_container }} -e MONGODB_PORT=27017 -e TEST_TARGET_QPS=10 -e TEST_COLLECTION=10 -e TEST_DB=30 --network=pmm-qa -v $(pwd):/usr/src/myapp -w /usr/src/myapp php-db php mongodb_query.php >> setup_psmdb_{{ psmdb_version }}_{{ psmdb_setup }}.log + when: psmdb_setup == "sharded" \ No newline at end of file diff --git a/pmm_qa/mongodb_query.php b/pmm_qa/mongodb_query.php new file mode 100644 index 0000000..494e8a7 --- /dev/null +++ b/pmm_qa/mongodb_query.php @@ -0,0 +1,108 @@ + false]); + +function run_query($db,$collection) +{ + global $client; + $collectionName = "beers" . $collection; + $dbName = "demo" . $db; + $collectionObj = $client->$dbName->$collectionName; + //read + $cursor = $collectionObj->find(); + //update + $collectionObj->updateMany(array("a"=>"a"), + array('$set'=>array("a"=>"a_u"))); + + //count + $collectionObj->count(); + + //distinct + $collectionObj->distinct("a"); + + //aggregate + $collectionObj->aggregate([array('$match' =>array("a"=>"a_u"))]); + + //findAndModify + $collectionObj->findOneAndUpdate(array("a"=>"a_u"), array('$set'=>array("a"=>"a_m"))); + + //delete + $collectionObj->deleteOne(array("a"=>"a_m")); + //create + $result = $collectionObj->insertOne( [ 'a' => 'a', 'b' => 'B', 'c' => $i ] ); +} + +echo("Running Queries...\n"); + +//lets create all db's and data +for($i = 1; $i <= $db; $i++) +{ + $dbName = "demo" . $i; + for ($j = 1; $j <= $collection; $j++) + { + $collectionName = "beers" . $j; + $collectionObj = $client->$dbName->$collectionName; + $result = $collectionObj->insertOne( [ 'a' => 'a', 'b' => 'B', 'c' => $j ] ); + echo "Inserted with Object ID '{$result->getInsertedId()}'"; + } +} + +/* How long we want target to take */ +$target_round_time=1/$target_qps; + +while(1) +{ + $start=microtime(1); + $dbNumber=skewed_rnd(1,$db); + $collectionNumber=skewed_rnd(1,$collection); + run_query($dbNumber,$collectionNumber); + $end=microtime(1); + $round_time=$end-$start; +# echo("Round Took: $round_time\n"); + if($round_time<$target_round_time) /* Went faster than needed */ + { + $sleep=($target_round_time-$round_time)*1000000; +# echo("Sleeping $sleep microseconds\n"); + usleep($sleep); + } +} +?> diff --git a/pmm_qa/pmm-framework.py b/pmm_qa/pmm-framework.py index a9ce849..f7d218c 100755 --- a/pmm_qa/pmm-framework.py +++ b/pmm_qa/pmm-framework.py @@ -13,6 +13,15 @@ "configurations": {"CLIENT_VERSION": "3-dev-latest", "SETUP_TYPE": "pss", "COMPOSE_PROFILES": "classic", "TARBALL": ""} }, + "MLAUNCH": { + "versions": ["4.4", "5.0", "6.0", "7.0"], + "configurations": {"CLIENT_VERSION": "3-dev-latest", "SETUP_TYPE": "pss", "TARBALL": ""} + }, + "SSL_MLAUNCH": { + "versions": ["4.4", "5.0", "6.0", "7.0"], + "configurations": {"CLIENT_VERSION": "3-dev-latest", "SETUP_TYPE": "pss", "COMPOSE_PROFILES": "classic", + "TARBALL": ""} + }, "SSL_PSMDB": { "versions": ["4.4", "5.0", "6.0", "7.0", "latest"], "configurations": {"CLIENT_VERSION": "3-dev-latest", "SETUP_TYPE": "pss", "COMPOSE_PROFILES": "classic", @@ -86,7 +95,8 @@ def run_ansible_playbook(playbook_filename, env_vars, args): playbook=playbook_path, inventory='127.0.0.1', cmdline='-l localhost, --connection=local', - envvars=env_vars + envvars=env_vars, + suppress_env_files=True, ) print(f'{playbook_filename} playbook execution {r.status}') @@ -403,6 +413,35 @@ def setup_external(db_type, db_version=None, db_config=None, args=None): run_ansible_playbook(playbook_filename, env_vars, args) +def setup_mlaunch(db_type, db_version=None, db_config=None, args=None): + # Check if PMM server is running + container_name = get_running_container_name() + if container_name is None and args.pmm_server_ip is None: + print(f"Check if PMM Server is Up and Running..Exiting") + exit() + + # Gather Version details + psmdb_version = os.getenv('PSMDB_VERSION') or get_latest_psmdb_version(db_version) or \ + database_configs[db_type]["versions"][-1] + + # Define environment variables for playbook + env_vars = { + 'PSMDB_VERSION': psmdb_version, + 'PMM_SERVER_IP': args.pmm_server_ip or container_name or '127.0.0.1', + 'PSMDB_CONTAINER': 'psmdb_pmm_' + str(psmdb_version), + 'PSMDB_SETUP': get_value('SETUP_TYPE', db_type, args, db_config), + 'CLIENT_VERSION': get_value('CLIENT_VERSION', db_type, args, db_config), + 'ADMIN_PASSWORD': os.getenv('ADMIN_PASSWORD') or args.pmm_server_password or 'admin', + 'PMM_QA_GIT_BRANCH': os.getenv('PMM_QA_GIT_BRANCH') or 'v3' + } + + # Ansible playbook filename + playbook_filename = 'mlaunch_setup.yml' + + # Call the function to run the Ansible playbook + run_ansible_playbook(playbook_filename, env_vars, args) + + def execute_shell_scripts(shell_scripts, project_relative_scripts_dir, env_vars, args): # Get script directory current_directory = os.getcwd() @@ -653,6 +692,34 @@ def setup_ssl_psmdb(db_type, db_version=None, db_config=None, args=None): execute_shell_scripts(shell_scripts, scripts_folder, env_vars, args) +def setup_ssl_mlaunch(db_type, db_version=None, db_config=None, args=None): + # Check if PMM server is running + container_name = get_running_container_name() + if container_name is None and args.pmm_server_ip is None: + print(f"Check if PMM Server is Up and Running...Exiting") + exit(1) + + # Gather Version details + psmdb_version = os.getenv('PSMDB_VERSION') or get_latest_psmdb_version(db_version) or \ + database_configs[db_type]["versions"][-1] + + # Define environment variables for playbook + env_vars = { + 'MONGODB_VERSION': psmdb_version, + 'PMM_SERVER_IP': args.pmm_server_ip or container_name or '127.0.0.1', + 'MONGODB_SSL_CONTAINER': 'psmdb_ssl_pmm_' + str(psmdb_version), + 'CLIENT_VERSION': get_value('CLIENT_VERSION', db_type, args, db_config), + 'ADMIN_PASSWORD': os.getenv('ADMIN_PASSWORD') or args.pmm_server_password or 'admin', + 'PMM_QA_GIT_BRANCH': os.getenv('PMM_QA_GIT_BRANCH') or 'v3' + } + + # Ansible playbook filename + playbook_filename = 'tls-ssl-setup/mlaunch_tls_setup.yml' + + # Call the function to run the Ansible playbook + run_ansible_playbook(playbook_filename, env_vars, args) + + def setup_pxc_proxysql(db_type, db_version=None, db_config=None, args=None): # Check if PMM server is running container_name = get_running_container_name() @@ -735,6 +802,11 @@ def setup_database(db_type, db_version=None, db_config=None, args=None): setup_ssl_pdpgsql(db_type, db_version, db_config, args) elif db_type == 'SSL_PSMDB': setup_ssl_psmdb(db_type, db_version, db_config, args) + elif db_type == 'MLAUNCH': + setup_mlaunch(db_type, db_version, db_config, args) + elif db_type == 'SSL_MLAUNCH': + setup_ssl_mlaunch(db_type, db_version, db_config, args) + else: print(f"Database type {db_type} is not recognised, Exiting...") exit(1) @@ -751,7 +823,7 @@ def setup_database(db_type, db_version=None, db_config=None, args=None): for db_type, options in database_configs.items(): db_parser = subparsers.add_parser(db_type.lower()) for config, value in options['configurations'].items(): - db_parser.add_argument(f'{config}',metavar='', help=f'{config} for {db_type} (default: {value})') + db_parser.add_argument(f'{config}', metavar='', help=f'{config} for {db_type} (default: {value})') # Add arguments parser.add_argument("--database", action='append', nargs=1, diff --git a/pmm_qa/tls-ssl-setup/create_certs.sh b/pmm_qa/tls-ssl-setup/create_certs.sh index dcb6d94..809723f 100644 --- a/pmm_qa/tls-ssl-setup/create_certs.sh +++ b/pmm_qa/tls-ssl-setup/create_certs.sh @@ -4,7 +4,7 @@ export PWD=$(pwd) export HOST=localhost mkdir -p certificates pushd certificates -echo -e "\n=== Generating SSL certificates in ${PWD} ===" +echo -e "\n=== Generating SSL certificates in ${PWD}/certificates ===" # Generate self signed root CA cert openssl req -nodes -x509 -newkey rsa:4096 -keyout ca.key -out ca.crt -subj "/C=US/ST=California/L=San Francisco/O=Percona/OU=root/CN=${HOST}/emailAddress=test@percona.com" # Generate server cert to be signed diff --git a/pmm_qa/tls-ssl-setup/mlaunch_tls_setup.yml b/pmm_qa/tls-ssl-setup/mlaunch_tls_setup.yml new file mode 100644 index 0000000..f47d885 --- /dev/null +++ b/pmm_qa/tls-ssl-setup/mlaunch_tls_setup.yml @@ -0,0 +1,94 @@ +--- + +- hosts: all + become: true + become_method: sudo + vars: + mongodb_version: "{{ lookup('vars', 'extra_mongodb_version', default=lookup('env','MONGODB_VERSION') | default('4.4', true) ) }}" + mongodb_ssl_container: "{{ lookup('vars', 'extra_mongodb_ssl_container', default=lookup('env','MONGODB_SSL_CONTAINER') | default('mongodb_ssl', true) ) }}" + pmm_server_ip: "{{ lookup('vars', 'extra_pmm_server_ip', default=lookup('env','PMM_SERVER_IP') | default('127.0.0.1', true) ) }}" + client_version: "{{ lookup('vars', 'extra_client_version', default=lookup('env','CLIENT_VERSION') | default('dev-latest', true) ) }}" + admin_password: "{{ lookup('vars', 'extra_admin_password', default=lookup('env','ADMIN_PASSWORD') | default('admin', true) ) }}" + pmm_qa_branch: "{{ lookup('vars', 'extra_pmm_qa_branch', default=lookup('env','PMM_QA_GIT_BRANCH') | default('main', true) ) }}" + + tasks: + - name: Cleanup Docker container for client and DB setup + shell: > + docker ps -a --filter "name={{ mongodb_ssl_container }}" | grep -q . && docker stop {{ mongodb_ssl_container }} && docker rm -fv {{ mongodb_ssl_container }} + ignore_errors: true + tags: + - cleanup + - name: delete network if exist + shell: docker network rm "{{ mongodb_ssl_container }}_network" + ignore_errors: true + tags: + - cleanup + + - name: Create a network + shell: docker network create "{{ mongodb_ssl_container }}_network" + + - name: Create pmm-qa network if not exist + shell: docker network create pmm-qa + ignore_errors: true + + - name: Prepare Container for mongodb ssl container + shell: > + docker run -d --name={{ mongodb_ssl_container }} + --network "{{ mongodb_ssl_container }}_network" + phusion/baseimage:jammy-1.0.1 + + - name: Copy all required Artifacts to the docker mongodb_ssl_container + shell: "{{ item }}" + with_items: + - docker exec {{ mongodb_ssl_container }} mkdir -p artifacts + - docker cp ./mongodb/mlaunch_ssl_setup.sh {{ mongodb_ssl_container }}:/ + - docker cp ./create_certs.sh {{ mongodb_ssl_container }}:/ + - docker cp ../pmm3-client-setup.sh {{ mongodb_ssl_container }}:/ + + - name: Install required software's to the docker psmdb_container + shell: "{{ item }}" + with_items: + - docker exec {{ mongodb_ssl_container }} apt-get update + - docker exec {{ mongodb_ssl_container }} apt-get -y install wget curl git gnupg2 lsb-release jq python3 pip + - docker exec {{ mongodb_ssl_container }} python3 -m pip install --upgrade pip + - docker exec {{ mongodb_ssl_container }} pip3 install 'mtools[all]' + + - name: Execute Certs Script inside the mongodb mongodb_ssl_container + shell: "{{ item }}" + with_items: + - docker exec {{ mongodb_ssl_container }} bash -xe ./create_certs.sh > mongodb/setup_mongodb_ssl_{{ mongodb_version }}.log + + - name: Execute Setup script inside the mongodb mongodb_ssl_container + shell: "{{ item }}" + with_items: + - docker exec {{ mongodb_ssl_container }} bash -xe ./mlaunch_ssl_setup.sh --mongodb_version {{ mongodb_version }} >> mongodb/setup_mongodb_ssl_{{ mongodb_version }}.log + + - name: Install pmm2-client on the mongodb_ssl_container + shell: "{{ item }}" + with_items: + - docker network connect pmm-qa {{ mongodb_ssl_container }} + - docker exec {{ mongodb_ssl_container }} bash -x ./pmm3-client-setup.sh --pmm_server_ip {{ pmm_server_ip }} --client_version {{ client_version }} --admin_password {{ admin_password }} --use_metrics_mode no + + - name: Add pmm-admin binary to path when tar ball installation + shell: docker exec {{ mongodb_ssl_container }} echo "export PATH=$PATH:/pmm2-client/bin" > setup_path.sh + when: '"http" in client_version' + + - name: Remove mongodb service if already added previously + shell: "{{ item }}" + with_items: + - docker exec {{ mongodb_ssl_container }} bash -c 'source ~/.bash_profile || true; pmm-admin remove mongodb {{ mongodb_ssl_container }}_service' + ignore_errors: true + + - name: Add mongodb_ssl for monitoring + shell: "{{ item }}" + with_items: + - docker exec {{ mongodb_ssl_container }} bash -c 'source ~/.bash_profile || true; pmm-admin list' + - docker exec {{ mongodb_ssl_container }} bash -c 'source ~/.bash_profile || true; pmm-admin add mongodb --tls --tls-skip-verify --authentication-mechanism=MONGODB-X509 --authentication-database=$external --tls-certificate-key-file=/certificates/client.pem --tls-certificate-key-file-password=/certificates/client.key --tls-ca-file=/certificates/ca.crt {{ mongodb_ssl_container }}_ssl_service' + + - name: Get client cert Files on host + shell: "{{ item }}" + with_items: + - mkdir -p mongodb/{{ mongodb_version }} || true + - docker exec {{ mongodb_ssl_container }} cat /certificates/ca.crt > mongodb/{{ mongodb_version }}/ca.crt + - docker exec {{ mongodb_ssl_container }} cat /certificates/client.key > mongodb/{{ mongodb_version }}/client.key + - docker exec {{ mongodb_ssl_container }} cat /certificates/client.pem > mongodb/{{ mongodb_version }}/client.pem diff --git a/pmm_qa/tls-ssl-setup/mongodb/mlaunch_ssl_setup.sh b/pmm_qa/tls-ssl-setup/mongodb/mlaunch_ssl_setup.sh new file mode 100644 index 0000000..92d4481 --- /dev/null +++ b/pmm_qa/tls-ssl-setup/mongodb/mlaunch_ssl_setup.sh @@ -0,0 +1,70 @@ +#!/bin/sh + +while [ $# -gt 0 ]; do + if [[ $1 == *"--"* ]]; then + param="${1/--/}" + declare $param="$2" + fi + shift +done + +if [ -z "$mongodb_version" ]; then + export mongodb_version=4.4 +fi + +wget https://raw.githubusercontent.com/percona/pmm-qa/main/pmm-tests/mongodb_user_setup.js +### Detect latest tarball link for specified mongodb_version: 7.0 | 6.0 | 5.0 | 4.4 | 4.2 at the moment +#psmdb_latest=$(wget -q --post-data "version=percona-server-mongodb-${mongodb_version}" https://www.percona.com/products-api.php -O - | grep -oP "(?<=value\=\")[^\"]*" | sort -V | tail -1) +psmdb_tarball=$(wget -q --post-data "version_files=percona-server-mongodb-${mongodb_version}&software_files=binary" https://www.percona.com/products-api.php -O - | jq -r '.[] | select(.link | contains("sha") | not) | .link' | grep glibc2\.17-minimal) + +echo "Downloading ${mongodb_version} ..." +wget -O percona_server_mongodb.tar.gz ${psmdb_tarball} +tar -xvf percona_server_mongodb.tar.gz +mv percona-server-mongodb-${mongodb_version}.* psmdb_${mongodb_version} +rm percona_server_mongodb.tar.gz* + +# TODO: refactor if to match range of versions 6.0+ +if [[ "$mongodb_version" == "6.0" || "$mongodb_version" == "7.0" ]]; then + ### PSMDB 6+ requires "percona-mongodb-mongosh" additionally + echo "Downloading mongosh ..." + mongosh_link=$(wget -q --post-data "version_files=percona-server-mongodb-${mongodb_version}&software_files=binary" https://www.percona.com/products-api.php -O - | jq -r '.[] | select(.link | contains("sha") | not) | .link' | grep mongosh) + wget -O mongosh.tar.gz ${mongosh_link} + tar -xvf mongosh.tar.gz + mv percona-mongodb-mongosh* mongosh + cp mongosh/bin/mongosh ./psmdb_${mongodb_version}/bin/mongo + rm mongosh.tar.gz +fi + +# For mongodb dependency in Debian +wget http://http.us.debian.org/debian/pool/main/o/openldap/libldap-2.4-2_2.4.47+dfsg-3+deb10u7_amd64.deb +apt install -y ./libldap-2.4-2_2.4.47+dfsg-3+deb10u7_amd64.deb + +mlaunch init --bind_ip 0.0.0.0 --binarypath "./psmdb_${mongodb_version}/bin" --replicaset --name rs1 --nodes 3 --sslMode requireSSL --sslPEMKeyFile /certificates/server.pem --sslCAFile /certificates/ca.crt --sslClientCertificate /certificates/client.pem +#bash ./mongo_startup.sh -m --ssl -x -e wiredTiger --mongodExtra="--profile 2 --slowms 1 --bind_ip_all" --b=/psmdb_${mongodb_version}/bin +sleep 20 +./psmdb_${mongodb_version}/bin/mongo --tls --host localhost --port 27017 --tlsCAFile /certificates/ca.crt --tlsCertificateKeyFile /certificates/client.pem mongodb_user_setup.js +cat > add_new_ssl_user.js <