diff --git a/clonecode.sh b/clonecode.sh index cd7b12a91abd17a89ecd025258d6af2eb2fafb47..530963f6fa95aff170f81d1da5dfffae843e2766 100644 --- a/clonecode.sh +++ b/clonecode.sh @@ -1,6 +1,9 @@ version=5.0.1 server_repo=https://gitee.com/opengauss/openGauss-server.git +plugin_repo=https://gitee.com/opengauss/Plugin.git git clone $server_repo -b v5.0.1 openGauss-server-$version +git clone $plugin_repo -b v5.0.1 Plugin-$version +cp -rf Plugin-$version/contrib/* openGauss-server-$version/contrib/ cd openGauss-server-$version gitcommit=$(git log 2>/dev/null | grep commit | head -1 | awk '{print $2}' | cut -b 1-8) echo $gitcommit > ../COMMIT @@ -11,7 +14,6 @@ rm -rf src/test/regress/sql rm -rf src/test/regress/data rm -rf src/test/regress/jdbc_test rm -rf src/test/regress/input -rm -rf src/test/regress/jdbc_client_lib rm -rf .git rm -rf ./docker cd .. diff --git a/cmake_compile.patch b/cmake_compile.patch index 125b1b6f33bf061a1c9c4f513c8e37d7dc26f3d5..7db4e2b1b0be40b55e1f2043541ec43ffa77f630 100644 --- a/cmake_compile.patch +++ b/cmake_compile.patch @@ -275,3 +275,61 @@ diff -crN '--exclude=.git' '--exclude=.gitee' '--exclude=.vscode' openGauss-serv } } } +diff -crN '--exclude=.git' '--exclude=.gitee' '--exclude=.vscode' Plugin-5.0.1/contrib/dolphin/cmake.sh Plugin-5.0.1-edit/contrib/dolphin/cmake.sh +*** Plugin-5.0.1/contrib/dolphin/cmake.sh 2024-06-12 20:17:51.731405913 +0800 +--- Plugin-5.0.1-edit/contrib/dolphin/cmake.sh 2024-06-12 20:43:24.223308216 +0800 +*************** +*** 1,5 **** + #!/bin/bash +! CMAKE_OPT="-DENABLE_MULTIPLE_NODES=OFF -DENABLE_PRIVATEGAUSS=OFF -DENABLE_THREAD_SAFETY=ON -DENABLE_LITE_MODE=ON" + cpus_num=$(grep -w processor /proc/cpuinfo|wc -l) + rm -f dolphin--1.0.sql + touch dolphin--1.0.sql +--- 1,5 ---- + #!/bin/bash +! CMAKE_OPT="-DENABLE_MULTIPLE_NODES=OFF -DENABLE_PRIVATEGAUSS=OFF -DENABLE_THREAD_SAFETY=ON -DENABLE_LITE_MODE=ON -DENABLE_OPENEULER_MAJOR=ON -DWITH_OPENEULER_OS=ON" + cpus_num=$(grep -w processor /proc/cpuinfo|wc -l) + rm -f dolphin--1.0.sql + touch dolphin--1.0.sql +*************** +*** 9,14 **** +--- 9,15 ---- + touch dolphin--1.2.sql + cat dolphin--1.0.sql >> dolphin--1.2.sql + cat upgrade_script/dolphin--1.0--1.2.sql >> dolphin--1.2.sql ++ BUILD_TUPLE=$(uname -p) + cp llvmir/openGauss_expr_dolphin_${BUILD_TUPLE}.ir openGauss_expr_dolphin.ir + DOLPHIN_CMAKE_BUILD_DIR=`pwd`/tmp_build + [ -d "${DOLPHIN_CMAKE_BUILD_DIR}" ] && rm -rf ${DOLPHIN_CMAKE_BUILD_DIR} +diff -crN '--exclude=.git' '--exclude=.gitee' '--exclude=.vscode' openGauss-server-5.0.1/src/bin/pg_ctl/backup.cpp openGauss-server-5.0.1-edit/src/bin/pg_ctl/backup.cpp +*** openGauss-server-5.0.1/src/bin/pg_ctl/backup.cpp 2024-05-09 14:48:32.000000000 +0800 +--- openGauss-server-5.0.1-edit/src/bin/pg_ctl/backup.cpp 2024-06-19 16:22:57.390413059 +0800 +*************** +*** 1939,1945 **** + } + while (1) { + de = readdir(dir); +! if (de <= 0) { + break; + } + if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) { +--- 1939,1945 ---- + } + while (1) { + de = readdir(dir); +! if (de == NULL) { + break; + } + if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) { +*************** +*** 2799,2802 **** + /* free sysidentifier after use */ + pg_free(sysidentifier); + sysidentifier = NULL; +! } +\ No newline at end of file +--- 2799,2802 ---- + /* free sysidentifier after use */ + pg_free(sysidentifier); + sysidentifier = NULL; +! } diff --git a/openGauss-server-5.0.1.tar.gz b/openGauss-server-5.0.1.tar.gz index f2e0742771c2038be1bdec40dfcd89eff393dc5b..f9d4da41ad685ee482e3d9209e01b075913a43c0 100644 Binary files a/openGauss-server-5.0.1.tar.gz and b/openGauss-server-5.0.1.tar.gz differ diff --git a/opengauss-bashprofile b/opengauss-bashprofile index 0d44355e2b2375f6d324d6189a73bb651895fcae..012b243208feb7e79b17eb802ed10e14c94a6647 100644 --- a/opengauss-bashprofile +++ b/opengauss-bashprofile @@ -1,4 +1,4 @@ -export GAUSSHOME=/usr/local/opengauss/ +export GAUSSHOME=/usr/local/opengauss export LD_LIBRARY_PATH=/usr/local/opengauss/lib:$LD_LIBRARY_PATH export PATH=/usr/local/opengauss/bin:$PATH export PGDATA=/var/lib/opengauss/data diff --git a/opengauss-server.spec b/opengauss-server.spec index 5f4f83ba7bccd866ecb9e6f8374bde8323c9ee34..262014fc155a65490ac6f4eb38dbafaac7e7e6d3 100755 --- a/opengauss-server.spec +++ b/opengauss-server.spec @@ -7,10 +7,11 @@ %define port 7654 %define datapath /var/lib/opengauss %define apppath %{_prefix}/local/opengauss +%define tmppath /var/lib/opengauss/pkg_5.0.1 Name: opengauss Version: 5.0.1 -Release: 12 +Release: 15 Summary: openGauss is an open source relational database management system License: MulanPSL-2.0 and MIT and BSD and zlib and TCL and Apache-2.0 and BSL-1.0 URL: https://gitee.com/opengauss/openGauss-server @@ -21,11 +22,17 @@ Source4: %{xgboost_name}-%{xgboost_version}.tar.gz Source5: opengauss-bashprofile Source6: opengauss.service Source7: autostart.sh +Source8: version.cfg +Source9: opengauss_upgrade_start.sh +Source10: opengauss_upgrade_common.sh +Source11: opengauss_upgrade_config.sh +Source12: opengauss_upgrade_errorcode.sh Patch0: og-edit.patch Patch1: cmake_compile.patch Patch2: compile_2309.patch Patch3: openssl3-adptor.patch +Patch4: upgrade.patch Patch20: zlib.patch Patch21: zlib-CVE-2022-37434.patch @@ -33,7 +40,7 @@ BuildRequires: cmake gcc gcc-c++ openssl-devel python BuildRequires: cjson lz4-devel zstd-devel boost-devel cjson-devel BuildRequires: libcgroup-devel libcurl-devel unixODBC-devel jemalloc-devel krb5-devel BuildRequires: java-1.8.0-openjdk-devel libedit-devel libaio-devel -BuildRequires: bison flex DCF +BuildRequires: bison flex, DCF >= 5 BuildRequires: numactl-devel %ifarch sw_64 BuildRequires: libatomic @@ -46,7 +53,7 @@ BuildRequires: libatomic Requires: lz4-devel zstd-devel boost-devel cjson-devel Requires: libcgroup-devel libcurl-devel unixODBC-devel jemalloc-devel Requires: java-1.8.0-openjdk-devel libedit-devel libaio-devel -Requires: DCF lsof +Requires: DCF >= 5, lsof Requires: numactl-devel %description @@ -60,11 +67,14 @@ openGauss kernel : openGauss is an open source relational database management sy %setup -q -D -T -a 3 %setup -q -D -T -a 4 -pushd openGauss-server-%{version} +pushd openGauss-server-%{version} + + %patch0 -p1 %patch1 -p1 %patch2 -p1 %patch3 -p1 +%patch4 -p1 popd pushd %{zlib_name}-%{zlib_version} @@ -135,15 +145,19 @@ export LD_LIBRARY_PATH=$THIRD_BIN_PATH/kernel/dependency/zlib1.2.11/comm/lib:$LD export LD_LIBRARY_PATH=$THIRD_BIN_PATH/kernel/dependency/xgboost/comm/lib:$LD_LIBRARY_PATH export PREFIX_HOME=${opengauss_source_dir}/mppdb_temp_install -mkdir -p %{buildroot}/var/lib/opengauss/data -mkdir -p %{buildroot}/%{apppath}/script - mkdir -p tmp_build cd tmp_build -cmake .. -DENABLE_MULTIPLE_NODES=OFF -DENABLE_PRIVATEGAUSS=OFF -DENABLE_THREAD_SAFETY=ON -DENABLE_LITE_MODE=ON -DENABLE_OPENEULER_MAJOR=ON -DWITH_OPENEULER_OS=ON && +cmake .. -DENABLE_MULTIPLE_NODES=OFF -DENABLE_PRIVATEGAUSS=OFF -DENABLE_THREAD_SAFETY=ON -DENABLE_LITE_MODE=ON -DENABLE_OPENEULER_MAJOR=ON -DWITH_OPENEULER_OS=ON make %{?_smp_mflags} make install +# clean build root +rm -rf %{buildroot}/* + +mkdir -p %{buildroot}/var/lib/opengauss/data +mkdir -p %{buildroot}%{apppath}/script +mkdir -p %{buildroot}%{tmppath}/script + # separate_debug_symbol.sh dir cd ${opengauss_source_dir}/build/script chmod +x ./separate_debug_information.sh @@ -151,10 +165,45 @@ chmod +x ./separate_debug_information.sh rm -rf ${opengauss_source_dir}/mppdb_temp_install/packages rm -rf ${opengauss_source_dir}/mppdb_temp_install/symbols -# copy binarylibs packages to /usr/local/opengauss/ -cp -r ${opengauss_source_dir}/mppdb_temp_install/* %{buildroot}/usr/local/opengauss +function get_os_kernel() { + kernel="" + if [[ -f "/etc/euleros-release" ]]; then + kernel=$(cat /etc/euleros-release | awk -F ' ' '{print $1}' | tr a-z A-Z) + if [[ "${kernel}" = "Euleros" ]]; then + kernel="Euler" + fi + elif [[ -f "/etc/openEuler-release" ]]; then + kernel=$(cat /etc/openEuler-release | awk -F ' ' '{print $1}') + elif [[ -f "/etc/centos-release" ]]; then + kernel=$(cat /etc/centos-release | awk -F ' ' '{print $1}') + else + kernel=$(lsb_release -d | awk -F ' ' '{print $2}') + fi +} +get_os_kernel +platform_arch=$(uname -p) +cd ${opengauss_source_dir}/mppdb_temp_install +tar -zcf openGauss-Lite-%{version}-${kernel}-${platform_arch}.bin * +sha256sum openGauss-Lite-%{version}-${kernel}-${platform_arch}.bin | awk '{print $1}' > openGauss-Lite-%{version}-${kernel}-${platform_arch}.sha256 + +# copy binarylibs packages to %{tmppath} +cp -r ${opengauss_source_dir}/mppdb_temp_install/* %{buildroot}%{tmppath} sed -i "/wal_insert_status_entries/d" ${opengauss_source_dir}/build/script/opengauss_config_file_mini -cp ${opengauss_source_dir}/build/script/opengauss_config_file_mini %{buildroot}/%{apppath}/share/postgresql/ +cp ${opengauss_source_dir}/build/script/opengauss_config_file_mini %{buildroot}%{tmppath}/share/postgresql/ + +# make package upgrade sql +cd ${opengauss_source_dir}/tmp_build +cp -r ${opengauss_source_dir}/src/include/catalog/upgrade_sql ./upgrade_sql +tar -zcf upgrade_sql.tar.gz ./upgrade_sql +rm -rf ./upgrade_sql > /dev/null 2>&1 +sha256sum upgrade_sql.tar.gz | awk '{print $1}' > upgrade_sql.sha256 +if [ $? -ne 0 ]; then + echo "Generate upgrade_sql.sha256 failed." + exit 1 +fi +cp -r upgrade_sql.tar.gz %{buildroot}%{tmppath} +cp -r upgrade_sql.sha256 %{buildroot}%{tmppath} + popd # opengauss datanode dir. @@ -163,15 +212,25 @@ install -d -m 700 $RPM_BUILD_ROOT%{?_localstatedir}/lib/opengauss/data # opengauss .bash_profile install -m 644 %{SOURCE5} $RPM_BUILD_ROOT%{?_localstatedir}/lib/opengauss/.bash_profile # auto start files -install -m 644 %{SOURCE6} %{buildroot}/%{apppath}/script/opengauss.service -install -m 700 %{SOURCE7} %{buildroot}/%{apppath}/script/autostart.sh +install -m 644 %{SOURCE6} %{buildroot}%{tmppath}/script/opengauss.service +install -m 700 %{SOURCE7} %{buildroot}%{tmppath}/script/autostart.sh + +# upgrade script +install -m 644 %{SOURCE8} %{buildroot}%{tmppath}/version.cfg +install -m 644 %{SOURCE9} %{buildroot}%{tmppath}/opengauss_upgrade_start.sh +install -m 644 %{SOURCE10} %{buildroot}%{tmppath}/opengauss_upgrade_common.sh +install -m 644 %{SOURCE11} %{buildroot}%{tmppath}/opengauss_upgrade_config.sh +install -m 644 %{SOURCE12} %{buildroot}%{tmppath}/opengauss_upgrade_errorcode.sh %pre /usr/sbin/groupadd -r opengauss >/dev/null 2>&1 || : /usr/sbin/useradd -M -N -g opengauss -r -d %{datapath} -s /bin/bash -c "openGauss Server" opengauss >/dev/null 2>&1 || : -# for install step -if [ $1 = 1 ]; then +# for install step +# 1:install 2:upgrade +if [ $1 -gt 1 ]; then + echo "upgrade pre" +else portinfo=$(lsof -i:%{port}) if [ "${portinfo}" != "" ]; then echo "The port[%{port}] is occupied. Please use command 'lsof -i:%{port} to check it.'" @@ -179,12 +238,6 @@ if [ $1 = 1 ]; then fi fi -# upgrade installation -if [ $1 = 2 ]; then - pushd openGauss-server-%{version}/liteom - sh upgrade_GAUSSv5.sh upgrade_pre - popd -fi %post # set opengauss mini config parameters @@ -212,7 +265,7 @@ set_mini_configparam(){ } start_opengauss(){ - process_id=$(ps -ef | grep /usr/local/opengauss/bin/gaussdb | grep /var/lib/opengauss/data | grep -v grep | awk '{print $2}') + process_id=$(ps -ef | grep /var/lib/opengauss/data | grep -v grep | awk '{print $2}') if [ "$process_id" != "" ]; then echo "A process of opengauss already exists. Use command (ps -ef | grep /var/lib/opengauss/data) to confirm." echo "Please kill the process and reinstall opengauss." @@ -239,41 +292,77 @@ add_service(){ systemctl daemon-reload } -copy_bashrc(){ - copyResult=$(su - opengauss -c "echo 'export GAUSSHOME=/usr/local/opengauss/' >> ~/.bashrc && - echo 'export LD_LIBRARY_PATH=/usr/local/opengauss/lib:$LD_LIBRARY_PATH' >> ~/.bashrc && - echo 'export PATH=/usr/local/opengauss/bin:$PATH' >> ~/.bashrc && - echo 'export PGDATA=/var/lib/opengauss/data' >> ~/.bashrc && - echo 'export PORT=7654' >> ~/.bashrc && source ~/.bashrc") - if [ $? -ne 0 ]; then - exit $result - fi -} - # for install step -if [ $1 = 1 ]; then +# 1:install 2:upgrade +if [ $1 -eq 1 ]; then + echo "install" > /var/lib/opengauss/recode_install_flag + if [ -d /usr/local/opengauss ]; then + rm -rf /usr/local/opengauss + fi + mkdir -p /usr/local/opengauss + cp -r /var/lib/opengauss/pkg_%{version}/* /usr/local/opengauss chmod -R 755 /usr/local/opengauss chown -R opengauss:opengauss /usr/local/opengauss set_mini_configparam start_opengauss add_service - copy_bashrc -fi +elif [ $1 -eq 2 ]; then + echo "upgrade" > /var/lib/opengauss/recode_install_flag + mkdir -p /var/lib/opengauss/opengauss_upgrade + mkdir -p /var/lib/opengauss/opengauss_upgrade/bak + mkdir -p /var/lib/opengauss/opengauss_upgrade/tmp + cp -r /var/lib/opengauss/pkg_%{version} /var/lib/opengauss/opengauss_upgrade + chmod -R 755 /var/lib/opengauss/opengauss_upgrade + chown -R opengauss:opengauss /var/lib/opengauss/opengauss_upgrade + chmod -R 755 /usr/local/opengauss + chown -R opengauss:opengauss /usr/local/opengauss + chmod -R 755 /var/lib/opengauss/pkg_%{version} + chown -R opengauss:opengauss /var/lib/opengauss/pkg_%{version} + + echo "start upgrade..." + echo "upgrade pre" + su - opengauss -c "source ~/.bash_profile; cd /var/lib/opengauss/opengauss_upgrade/pkg_%{version}/; sh opengauss_upgrade_start.sh -t upgrade_pre" + if [ $? -ne 0 ]; then + echo "Upgrade pre-check failed." + echo "Please cd /var/lib/opengauss_upgrade/pkg_%{version}/; sh opengauss_upgrade_start.sh -t upgrade_pre" + exit 1 + fi + echo "upgrade bin" + su - opengauss -c "source ~/.bash_profile; cd /var/lib/opengauss/opengauss_upgrade/pkg_%{version}/; sh opengauss_upgrade_start.sh -t upgrade_bin" + if [ $? -ne 0 ]; then + echo "Upgrade binary failed." + exit 1 + fi + echo "upgrade post" + su - opengauss -c "source ~/.bash_profile; cd /var/lib/opengauss/opengauss_upgrade/pkg_%{version}/; sh opengauss_upgrade_start.sh -t upgrade_post" + if [ $? -ne 0 ]; then + echo "Upgrade post-check failed." + echo "Please cd /var/lib/opengauss/opengauss_upgrade/pkg_%{version}/; sh opengauss_upgrade_start.sh -t upgrade_post" + exit 1 + fi + su - opengauss -c "source ~/.bash_profile; cd /var/lib/opengauss/opengauss_upgrade/pkg_%{version}/; sh opengauss_upgrade_start.sh -t upgrade_commit" + if [ $? -ne 0 ]; then + echo "Upgrade commit failed." + exit 1 + fi -# upgrade installation -if [ $1 = 2 ]; then - pushd openGauss-server-%{version}/liteom - sh upgrade_GAUSSv5.sh upgrade_bin servicename=/usr/lib/systemd/system/opengauss.service - if [ -f $servicename ]; then - systemctl restart opengauss.service - fi - sh upgrade_GAUSSv5.sh upgrade_post - sh upgrade_GAUSSv5.sh upgrade_commit - popd + if [ -f $servicename ]; then + systemctl disable opengauss.service + rm $servicename + fi + cp %{tmppath}/script/opengauss.service /usr/lib/systemd/system/ + systemctl daemon-reload + + # upgrade backup + pid=$(ps -ef | grep /var/lib/opengauss/data | grep -v grep | awk '{print $2}') + if [ "$pid" != "" ]; then + kill -9 $pid + fi + cp -rf /usr/local/opengauss /usr/local/opengauss_%{version} fi -%postun +%preun remove_service(){ servicename=/usr/lib/systemd/system/opengauss.service if [ -f $servicename ]; then @@ -281,35 +370,77 @@ remove_service(){ rm $servicename fi } + +# 0: uninstall 1:upgrade +if [ $1 -eq 0 ]; then + echo "remove opengauss service" + remove_service +fi + +%postun clear_database(){ pid=$(ps -ef | grep /var/lib/opengauss/data | grep -v grep | awk '{print $2}') if [ "$pid" != "" ]; then kill -9 $pid fi - if [ -d /var/lib/opengauss/data ]; then - rm -rf /var/lib/opengauss/data - fi if [ -d /usr/local/opengauss ]; then rm -rf /usr/local/opengauss fi + if [ -d /usr/local/opengauss_%{version} ]; then + rm -rf /usr/local/opengauss_%{version} + fi + if [ -f /var/lib/opengauss/recode_install_flag ]; then + rm -rf /var/lib/opengauss/recode_install_flag + fi + if [ -d /var/lib/opengauss/pkg_%{version} ]; then + rm -rf /var/lib/opengauss/pkg_%{version} + fi + if [ -d /var/lib/opengauss/opengauss_upgrade ]; then + rm -rf /var/lib/opengauss/opengauss_upgrade + fi } -if [ "$1" = "0" ]; then - remove_service + +# 0: uninstall +if [ $1 -eq 0 ]; then + echo "clean database" clear_database fi +%posttrans +flag=$(cat /var/lib/opengauss/recode_install_flag) +if [ $flag = "install" ]; then + echo "opengauss install successfully!" + echo "Please run: systemctl start opengauss.service" +else + echo "upgrade posttrans" + cp -rf /usr/local/opengauss_%{version}/* /usr/local/opengauss + chown -R opengauss:opengauss /usr/local/opengauss + chmod -R 755 /usr/local/opengauss + systemctl restart opengauss.service + echo "opengauss upgrade successfully" +fi + + %files %defattr (-,root,root) %{apppath} +%{tmppath} %doc -%{?_localstatedir}/lib/opengauss %attr(700,opengauss,opengauss) %dir %{?_localstatedir}/lib/opengauss %attr(700,opengauss,opengauss) %dir %{?_localstatedir}/lib/opengauss/data +%attr(755,opengauss,opengauss) %dir %{apppath} +%attr(755,opengauss,opengauss) %dir %{tmppath} %attr(644,opengauss,opengauss) %config(noreplace) %{?_localstatedir}/lib/opengauss/.bash_profile %changelog -* Fri May 24 2024 zhangzhihui - 5.0.1-12 +* Thu Jun 20 2024 liuheng - 5.0.1-15 +- Fix bugs: Initialize Remove Password + +* Wed Jun 12 2024 liuheng - 5.0.1-14 +- Fix bugs: #IA4KLQ #IA4PTU + +* Fri May 24 2024 zhangzhihui - 5.0.1-13 - upgrade function * Mon May 6 2024 liuheng - 5.0.1-12 diff --git a/opengauss.service b/opengauss.service index 2a47b47e7b294c87ff68bcaee4f638d202ab5aca..6da78c89075ac45ece835d34315d0e9e494e8312 100755 --- a/opengauss.service +++ b/opengauss.service @@ -6,9 +6,9 @@ After=local-fs.target Type=forking User=opengauss WorkingDirectory=/var/lib/opengauss -ExecStart=/bin/bash -c 'source ~/.bashrc; gs_ctl start -D /var/lib/opengauss/data' -ExecStop=/bin/bash -c 'source ~/.bashrc; gs_ctl stop -D /var/lib/opengauss/data' -ExecReload=/bin/bash -c 'source ~/.bashrc; gs_ctl reload -D /var/lib/opengauss/data' +ExecStart=/bin/bash -c 'source ~/.bash_profile; gs_ctl start -D /var/lib/opengauss/data' +ExecStop=/bin/bash -c 'source ~/.bash_profile; gs_ctl stop -D /var/lib/opengauss/data' +ExecReload=/bin/bash -c 'source ~/.bash_profile; gs_ctl reload -D /var/lib/opengauss/data' Delegate=yes RemainAfterExit=yes diff --git a/opengauss_upgrade_common.sh b/opengauss_upgrade_common.sh new file mode 100644 index 0000000000000000000000000000000000000000..ee0ab2be0a1c732fae15698d880da7e1ec1d1290 --- /dev/null +++ b/opengauss_upgrade_common.sh @@ -0,0 +1,1238 @@ +#!/bin/bash +# Copyright (c) Huawei Technologies Co., Ltd. 2010-2022. All rights reserved. +# date: 2021-12-22 +# version: 1.0 + +dn_role="" +min_disk="" +old_version="" +new_version="" +old_cfg="" +big_cfg="" +binfile="" + +upgrade_path=$( + cd $(dirname $0) + pwd +) +source ${upgrade_path}/opengauss_upgrade_errorcode.sh + +function check_and_init() { + check_config + check_cmd_conflict +} + +function check_config() { + #check and init GAUSS_LOG_FILE + if [[ X"$GAUSS_LOG_PATH" = X ]]; then + GAUSS_LOG_FILE="$upgrade_path"/opengauss_upgrade.log + else + check_config_path "$GAUSS_LOG_PATH" + GAUSS_LOG_FILE="$GAUSS_LOG_PATH"/opengauss_upgrade.log + fi + log_dir=$(dirname "$GAUSS_LOG_FILE") + if [[ ! -d "$log_dir" ]]; then + if ! mkdir -p -m 700 "$log_dir"; then + echo "mkdir -p -m 700 ${log_dir} failed" + exit ${err_check_init} + fi + fi + if touch "$GAUSS_LOG_FILE" && chmod 600 "$GAUSS_LOG_FILE"; then + echo "check log file is ok" >>"$GAUSS_LOG_FILE" + else + echo "touch $GAUSS_LOG_FILE && chmod 600 $GAUSS_LOG_FILE failed" + exit ${err_check_init} + fi + check_env + #check GAUSS_LISTEN_PORT + if [[ X"$GAUSS_LISTEN_PORT" = X ]]; then + GAUSS_LISTEN_PORT=$(grep -E "^[ ]{0,}port" "$PGDATA"/postgresql.conf | sed 's/\s*//g' | awk -F# '{print $1}' | awk -F= '{print $2}') + fi + if [[ X"$GAUSS_LISTEN_PORT" = X ]]; then + GAUSS_LISTEN_PORT=5432 + debug "The value of port cannot be obtained from configuration, use default value 5432" + fi + if ! echo "$GAUSS_LISTEN_PORT" | grep -Eqw "[0-9]{4,5}"; then + die "GAUSS_LISTEN_PORT may be not right" ${err_check_init} + fi + + #check GAUSS_ADMIN_USER + if [[ X"$GAUSS_ADMIN_USER" = X ]]; then + GAUSS_ADMIN_USER=$(whoami) + else + check_config_user "$GAUSS_ADMIN_USER" + fi + + #check GAUSS_UPGRADE_BASE_PATH and cd in + if [[ X"$GAUSS_UPGRADE_BASE_PATH" = X ]]; then + GAUSS_UPGRADE_BASE_PATH=${upgrade_path} + fi + check_config_path "$GAUSS_UPGRADE_BASE_PATH" + if [[ ! -d "$GAUSS_UPGRADE_BASE_PATH" ]]; then + die "$GAUSS_UPGRADE_BASE_PATH must exist" ${err_check_init} + fi + if ! cd "$GAUSS_UPGRADE_BASE_PATH"; then + die "Cannot access $GAUSS_UPGRADE_BASE_PATH" ${err_check_init} + fi + + #check GAUSS_SQL_TAR_PATH + if [[ X"$GAUSS_SQL_TAR_PATH" = X ]]; then + GAUSS_SQL_TAR_PATH=${GAUSS_UPGRADE_BASE_PATH} + fi + check_config_path "$GAUSS_SQL_TAR_PATH" + + #check GAUSS_BACKUP_BASE_PATH + if [[ X"$GAUSS_BACKUP_BASE_PATH" = X ]]; then + GAUSS_BACKUP_BASE_PATH=${GAUSS_UPGRADE_BASE_PATH}/backup + fi + check_config_path "$GAUSS_BACKUP_BASE_PATH" + if [[ ! -d "$GAUSS_BACKUP_BASE_PATH" ]]; then + if ! mkdir -p -m 700 "$GAUSS_BACKUP_BASE_PATH"; then + die "mkdir -p -m 700 $GAUSS_BACKUP_BASE_PATH failed" ${err_check_init} + fi + fi + + #check GAUSS_TMP_PATH + if [[ X"$GAUSS_TMP_PATH" = X ]]; then + GAUSS_TMP_PATH=${GAUSS_UPGRADE_BASE_PATH}/tmp + fi + check_config_path "$GAUSS_TMP_PATH" + if [[ ! -d "$GAUSS_TMP_PATH" ]]; then + if ! mkdir -p -m 700 "$GAUSS_TMP_PATH"; then + die "mkdir -p -m 700 $GAUSS_TMP_PATH failed" ${err_check_init} + fi + fi + + #check GAUSS_UPGRADE_BIN_PATH + if [[ X"$GAUSS_UPGRADE_BIN_PATH" != X ]]; then + check_config_path "$GAUSS_UPGRADE_BIN_PATH" + fi + + #check GAUSS_UPGRADE_SYNC_CONFIG_LIST + check_config_sync_path +} + +function usage() { + echo " +Usage: $0 [OPTION] +Arguments: + -h|--help show this help, then exit + -t upgrade_pre,upgrade_bin,upgrade_post,rollback_pre + rollback_bin,rollback_post,upgrade_commit,switch_over + --min_disk reserved upgrade disk space in MB, default 2048 + -m|--mode normal、primary、standby and cascade_standby + " +} + +function debug() { + local current_time=$(date +"%Y-%m-%d %H:%M:%S") + echo "[$current_time]" "$1" >>"${GAUSS_LOG_FILE}" +} + +function log() { + local current_time=$(date +"%Y-%m-%d %H:%M:%S") + echo "[$current_time]" "$1" >>"${GAUSS_LOG_FILE}" + echo "$1" +} + +function die() { + local current_time=$(date +"%Y-%m-%d %H:%M:%S") + if [[ -f "${GAUSS_LOG_FILE}" ]];then + echo "[$current_time]" "$1" >>"${GAUSS_LOG_FILE}" + fi + echo -e "\033[31mError: $1\033[0m" + exit $2 +} + +function parse_cmd_line() { + if [[ $# -gt 6 ]];then + log "please input right parameter, the following command may help you" + log "sh opengauss_upgrade.sh --help or sh opengauss_upgrade.sh -h" + exit ${err_parameter} + fi + while [[ $# -gt 0 ]]; do + case "$1" in + -h | --help) + usage + exit 0 + ;; + -m | --mode) + if [[ "$2" = "" ]]; then + die "the parameter -m|--mode cannot be empty." ${err_parameter} + fi + dn_role=$2 + dn_role_list="normal primary standby cascade_standby" + if ! echo "$dn_role_list"|grep -wq "$dn_role"; then + die "only these modes are supported: normal,primary,standby and cascade_standby" ${err_parameter} + fi + shift 2 + ;; + -t) + if [[ "$2" = "" ]]; then + die "the parameter -t cannot be empty." ${err_parameter} + fi + action=$2 + action_list="upgrade_pre upgrade_bin upgrade_post rollback_pre rollback_bin rollback_post upgrade_commit switch_over" + if ! echo "$action_list"|grep -wq "$action"; then + die "only these actions are supported: upgrade_pre, upgrade_bin, upgrade_post, rollback_pre, \ +rollback_bin, rollback_post, upgrade_commit and switch_over" ${err_parameter} + fi + shift 2 + ;; + + --min_disk) + if [[ "$2" = "" ]]; then + die "the parameter --min_disk cannot be empty." ${err_parameter} + fi + min_disk=$2 + if echo ${min_disk} | grep -q "[^0-9]";then + die "min_disk value must be int " ${err_parameter} + fi + if echo ${min_disk} | grep -q "^0";then + die "min_disk value cannot start with 0 " ${err_parameter} + fi + if [[ ${min_disk} -lt 2048 || ${min_disk} -gt 204800 ]]; then + die "min_disk value must be >= 2048 and <= 204800" ${err_parameter} + fi + shift 2 + ;; + *) + log "please input right parameter, the following command may help you" + log "sh opengauss_upgrade.sh --help or sh opengauss_upgrade.sh -h" + exit ${err_parameter} + ;; + esac + done + log "Parse cmd line successfully." + +} + +function check_env() { + if [[ "$GAUSSHOME" == "" ]]; then + die "GAUSSHOME cannot be null!" ${err_check_init} + fi + if [[ "$GAUSSDATA" == "" ]] && [[ "$PGDATA" == "" ]]; then + die "GAUSSDATA or PGDATA cannot be all null!" ${err_check_init} + fi + if [[ "$PGDATA" == "" ]]; then + PGDATA=${GAUSSDATA} + fi + if [[ "$GAUSSDATA" == "" ]]; then + GAUSSDATA=${PGDATA} + fi + check_config_path "$GAUSSHOME" + check_config_path "$GAUSSDATA" + check_config_path "$PGDATA" + # ensure GAUSSDATA not contain GAUSSHOME + if echo "$GAUSSDATA"|grep -wq "^$GAUSSHOME";then + die "GAUSSDATA cannot be in GAUSSHOME!" ${err_check_init} + fi + log "Current env value: GAUSSHOME is $GAUSSHOME, PGDATA is $PGDATA." +} + +function check_config_path() { + local temp_value="$1" + if [[ "$temp_value" == *[\(\)\{\}\[\]\<\>\"\'\`\\\ \*\!\|\;\&\$\~\?]* ]];then + die "$temp_value may contain illegal characters" ${err_check_init} + fi + if echo "$temp_value"|grep -Eq "^/{1,}$"; then + die "path cannot be / " ${err_check_init} + fi +} + +function check_config_sync_path() { + if [[ "abc$GAUSS_UPGRADE_SYNC_CONFIG_LIST" == "abc" ]]; then + debug "GAUSS_UPGRADE_SYNC_CONFIG_LIST is null" + fi + if [[ "$GAUSS_UPGRADE_SYNC_CONFIG_LIST" == *[\(\)\{\}\[\]\<\>\"\'\`\\\ \*\!\|\;\&\$\~\?]* ]];then + die "$GAUSS_UPGRADE_SYNC_CONFIG_LIST may contain illegal characters" ${err_check_init} + fi + local array=(${GAUSS_UPGRADE_SYNC_CONFIG_LIST//,/ }) + for var in ${array[@]} + do + if echo "$var"|grep -Eq "^/"; then + die "path in ${GAUSS_UPGRADE_SYNC_CONFIG_LIST} must not be start with /" ${err_check_init} + fi + done + +} + +function check_config_user() { + local temp_value="$1" + if [[ "$temp_value" == *[\(\)\{\}\[\]\<\>\"\'\`\\\ \*\!\|\;\&\$\~\?/]* ]];then + die "$temp_value may contain illegal characters" ${err_check_init} + fi +} + +function check_version() { + version=$(gaussdb -V) + if [[ $version =~ "V500R002C00" || $version =~ "2.1.0" ]]; then + echo "2.1.0" > ${GAUSSHOME}/version.cfg + echo "92.421" >> ${GAUSSHOME}/version.cfg + echo "1f6832d" >> ${GAUSSHOME}/version.cfg + old_version=1f6832d + old_cfg=$(sed -n 2p "$GAUSSHOME/version.cfg" | sed 's/\.//g') + else + if [[ ! -f "${GAUSSHOME}/version.cfg" ]]; then + die "Cannot find current version.cfg!" ${err_upgrade_pre} + else + old_version=$(tail -n 1 "$GAUSSHOME"/version.cfg) + old_cfg=$(sed -n 2p "$GAUSSHOME/version.cfg" | sed 's/\.//g') + fi + fi + if [[ -f "$GAUSS_UPGRADE_BIN_PATH"/version.cfg ]]; then + new_version_cfg_path="${GAUSS_UPGRADE_BIN_PATH}/version.cfg" + elif [[ -f "$GAUSS_UPGRADE_BASE_PATH"/version.cfg ]]; then + new_version_cfg_path="${GAUSS_UPGRADE_BASE_PATH}/version.cfg" + else + die "Cannot find new version.cfg!" ${err_upgrade_pre} + fi + + new_version=$(tail -n 1 "$new_version_cfg_path") + new_cfg=$(sed -n 2p "$new_version_cfg_path" | sed 's/\.//g') + + if [[ X"$old_version" == X || X"$old_cfg" == X || X"$new_version" == X || X"$new_cfg" == X ]]; then + die "Maybe version.cfg is not normal" ${err_upgrade_pre} + fi + if ! echo "$old_cfg"|grep -Ewq "[0-9]{3,6}";then + die "Maybe version.cfg is not normal" ${err_upgrade_pre} + fi + if ! echo "$new_cfg"|grep -Ewq "[0-9]{3,6}";then + die "Maybe version.cfg is not normal" ${err_upgrade_pre} + fi + + if [[ "$old_version" == "$new_version" ]]; then + die "New version is same as old, the commitId is $old_version!" ${err_version_same} + fi + if [[ ${new_cfg} -lt ${old_cfg} ]]; then + die "Current version is newer!" ${err_upgrade_pre} + fi + big_cfg="False" + if [[ ${new_cfg} -gt ${old_cfg} ]]; then + log "Big upgrade is needed!" + big_cfg="True" + fi + + local flag_file="$GAUSS_TMP_PATH"/version_flag + if echo "old_version=$old_version" > "$flag_file" && chmod 600 "$flag_file"; then + debug "Begin to generate $flag_file" + else + die "Write $flag_file failed" ${err_upgrade_pre} + fi + if ! echo "new_version=$new_version" >> "$flag_file"; then + die "Write $flag_file failed" ${err_upgrade_pre} + fi + if ! echo "big_cfg=$big_cfg" >> "$flag_file"; then + die "Write $flag_file failed" ${err_upgrade_pre} + fi + if ! echo "old_cfg=$old_cfg" >> "$flag_file"; then + die "Write $flag_file failed" ${err_upgrade_pre} + fi + + log "Old version commitId is $old_version, version info is $old_cfg" + log "New version commitId is $new_version, version info is $new_cfg" + + ##need version.cfg to check big upgrade,note user exec sql on primary dn +} + +function check_disk() { + avail_disk=$(df -BM "$GAUSS_UPGRADE_BASE_PATH" | tail -n 1 | awk '{print $4}') + avail_disk=${avail_disk:0:-1} + if [[ X"$min_disk" == "X" ]]; then + min_disk=2048 + fi + if [[ ${avail_disk} -lt ${min_disk} ]]; then + die "avail disk must be >= ${min_disk}MB, check with cmd: df -BM $GAUSS_UPGRADE_BASE_PATH!" ${err_check_init} + fi + log "Check available disk space successfully." +} + +function check_db_process() { + legal_path=$(echo "$GAUSSHOME/bin/gaussdb" | sed 's#//*#/#g') + ps wwx | grep "${legal_path}" | grep -v grep > /dev/null +} + +function check_cmd_conflict() { + LOCKFILE="${GAUSS_TMP_PATH}/.lock_opengauss_upgrade" + if [[ -f "$LOCKFILE" ]] + then + pid=$(cat ${LOCKFILE}) + if [[ -n "$pid" ]];then + if ps -p ${pid} | grep -w ${pid} >/dev/null;then + die "Maybe opengauss_upgrade.sh is running!" ${err_check_init} + fi + fi + fi + if ! echo $$ > "$LOCKFILE"; then + die "Write $LOCKFILE failed" ${err_check_init} + fi +} + +function check_pkg() { + if [[ -d "${GAUSS_UPGRADE_BIN_PATH}" ]]; then + local file_list=(bin etc include lib share version.cfg) + for temp_file in ${file_list[@]} + do + if [[ ! -e "${GAUSS_UPGRADE_BIN_PATH}/${temp_file}" ]];then + die "$GAUSS_UPGRADE_BIN_PATH may be not right, ${temp_file} not exits" ${err_upgrade_pre} + fi + done + if [[ $(ls "${GAUSS_UPGRADE_BIN_PATH}" |wc -l) -eq 6 ]]; then + log "The upgrade will use existing files in $GAUSS_UPGRADE_BIN_PATH" + return 0 + else + die "$GAUSS_UPGRADE_BIN_PATH may be not right,exits other files" ${err_upgrade_pre} + fi + + fi + #get OS distributed version. + kernel="" + if [[ -f "/etc/euleros-release" ]]; then + kernel=$(cat /etc/euleros-release | awk -F ' ' '{print $1}' | tr a-z A-Z) + if [[ "${kernel}" = "Euleros" ]]; then + kernel="Euler" + fi + elif [[ -f "/etc/openEuler-release" ]]; then + kernel=$(cat /etc/openEuler-release | awk -F ' ' '{print $1}') + elif [[ -f "/etc/centos-release" ]]; then + kernel=$(cat /etc/centos-release | awk -F ' ' '{print $1}') + else + kernel=$(lsb_release -d | awk -F ' ' '{print $2}') + fi + log "kernel: ${kernel}" + + #detect platform information. + platform_arch=$(uname -p) + bin_name="openGauss-Lite.*-${kernel}-${platform_arch}" + binfile=$(ls -a | grep -E "${binname}.bin") + shafile=${binfile%.*}.sha256 + if [[ ! -f "${binfile}" ]] || [[ ! -f "${shafile}" ]]; then + die "bin or sha256 file not exit for the platform ${kernel}-${platform_arch}!" ${err_upgrade_pre} + fi + sha_expect=$(cat ${shafile}) + sha_current=$(sha256sum ${binfile} | awk '{print $1}') + if [[ "$sha_expect" != "$sha_current" ]]; then + die "The sha256 value of $binfile does not match $shafile!" ${err_upgrade_pre} + fi + if ! echo "binfile=$binfile" >>"$GAUSS_TMP_PATH"/version_flag; then + die "Write $GAUSS_TMP_PATH/version_flag failed" ${err_upgrade_pre} + fi + if [[ ! -f "$GAUSS_UPGRADE_BASE_PATH"/version.cfg ]]; then + die "version.cfg must be exit in $GAUSS_UPGRADE_BASE_PATH" ${err_upgrade_pre} + fi +} + +function bak_gauss() { + ##bak app & postgresql.conf & pg_hba.conf + if [[ -d "$GAUSS_BACKUP_BASE_PATH"/bak_bin_"$old_version" ]];then + chmod -R 700 "$GAUSS_BACKUP_BASE_PATH"/bak_bin_"$old_version" && rm -rf "$GAUSS_BACKUP_BASE_PATH"/bak_bin_"$old_version" + fi + if cp -rf "${GAUSSHOME}" "$GAUSS_BACKUP_BASE_PATH"/bak_bin_"$old_version"; then + log "Bak gausshome successfully." + else + die "Bak gausshome failed!" ${err_upgrade_pre} + fi + + if ! cp -rf "$PGDATA"/postgresql.conf "$GAUSS_BACKUP_BASE_PATH"/postgresql.conf_"$old_version"; then + die "Bak postgresql.conf failed!" ${err_upgrade_pre} + else + log "Bak postgresql.conf successfully." + fi + + if ! cp -rf "$PGDATA"/pg_hba.conf "$GAUSS_BACKUP_BASE_PATH"/pg_hba.conf_"$old_version"; then + die "Bak pg_hba.conf failed!" ${err_upgrade_pre} + else + log "Bak pg_hba.conf successfully." + fi + +} + +function decompress_pkg() { + if [[ -d "${GAUSS_UPGRADE_BIN_PATH}" ]]; then + return 0 + fi + if [[ -d "$GAUSS_TMP_PATH"/install_bin_"$new_version" ]];then + chmod -R 700 "$GAUSS_TMP_PATH"/install_bin_"$new_version" && rm -rf "$GAUSS_TMP_PATH"/install_bin_"$new_version" + fi + if mkdir -p -m 700 "$GAUSS_TMP_PATH"/install_bin_"$new_version"; then + log "begin decompress pkg in $GAUSS_TMP_PATH/install_bin_$new_version" + fi + if [[ X"$binfile" == X ]]; then + binfile=$(grep binfile "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + if [[ X"$binfile" == X ]]; then + die "binfile name cannot be null" ${err_upgrade_pre} + fi + fi + + if cp "$binfile" "$GAUSS_TMP_PATH"/install_bin_"$new_version" && cd "$GAUSS_TMP_PATH"/install_bin_"$new_version" && tar -zxf "$binfile" \ + && rm -f "$binfile"; then + + log "Decompress $binfile successfully." + else + die "Decompress $binfile failed" ${err_upgrade_pre} + fi + + if cd "$GAUSS_UPGRADE_BASE_PATH" && cp "$GAUSS_UPGRADE_BASE_PATH"/version.cfg "$GAUSS_TMP_PATH"/install_bin_"$new_version"; then + log "cp version.cfg successfully" + else + die "cp version.cfg failed" ${err_upgrade_pre} + fi +} + +function cp_pkg() { + if [[ X"$new_version" == X ]]; then + new_version=$(grep new_version "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + fi + if [[ -d "$GAUSS_UPGRADE_BIN_PATH" ]]; then + new_bin_path="${GAUSS_UPGRADE_BIN_PATH}" + else + new_bin_path="$GAUSS_TMP_PATH"/install_bin_"$new_version" + fi + if find "${new_bin_path}"/* -type f|xargs chmod 500 \ + && find "${new_bin_path}"/* -type f -regextype posix-extended -regex ".*\.(conf|sql|cfg|txt)"|xargs chmod 400 \ + && find "${new_bin_path}"/* -type d|xargs chmod 500 && find "${new_bin_path}"/include -type f|xargs chmod 400 \ + && find "${new_bin_path}"/share -type d|xargs chmod 700 && find "${new_bin_path}"/share -type f|xargs chmod 600 ; then + debug "chmod files and dirs successfully" + else + die "chmod files or dirs failed" ${err_upgrade_bin} + fi + #check pkg's version.cfg is equal to version_flag + temppkg_version=$(tail -n 1 "$new_bin_path"/version.cfg) + if [[ "$new_version" != "$temppkg_version" ]]; then + die "pkg's version.cfg is not correct!" ${err_upgrade_bin} + fi + old_version=$(grep old_version "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + if [[ -d "$GAUSSHOME" ]];then + if rm -rf $GAUSSHOME/*; then + debug "remove $GAUSSHOME successfully" + else + die "remove $GAUSSHOME failed" ${err_upgrade_bin} + fi + fi + + if cp -rfp "$new_bin_path"/* "$GAUSSHOME"; then + log "Binfile upgrade to new version successfully." + else + die "Binfile upgrade to new version failed" ${err_upgrade_bin} + fi + +} + +function prepare_sql() { + #$1: upgrade,upgrade-post,rollback,rollback-post + #$2: maindb,otherdb + temp_old=${old_cfg} + temp_new=${new_cfg} + local action="$1" + local dbname="$2" + local tempfile="$GAUSS_TMP_PATH"/temp_sql/"temp_"${action}_${dbname}.sql + temp_file_num=0 + if echo "START TRANSACTION;set IsInplaceUpgrade = on;" > "$tempfile" && chmod 600 "$tempfile"; then + debug "Begin to generate $tempfile" + else + die "Write $tempfile failed" ${err_upgrade_pre} + fi + if ! echo "SET search_path = 'pg_catalog';SET local client_min_messages = NOTICE;SET local log_min_messages = NOTICE;" >> "$tempfile"; then + die "Write $tempfile failed" ${err_upgrade_pre} + fi + if ! echo "SET statement_timeout = 3600000;" >> "$tempfile"; then + die "Write $tempfile failed" ${err_upgrade_pre} + fi + if [[ "$action" == "upgrade" || "$action" == "upgrade-post" ]]; then + while [[ ${temp_old} -lt ${temp_new} ]]; do + ((temp_old=$temp_old+1)) + local upgrade_sql_file="upgrade_sql/upgrade_catalog_${dbname}/${action}_catalog_${dbname}_${temp_old:0:2}_${temp_old:2}.sql" + if [[ -f "$upgrade_sql_file" ]]; then + if ! cat "$upgrade_sql_file" >> "$tempfile"; then + die "Write $tempfile failed" ${err_upgrade_pre} + fi + debug "$upgrade_sql_file >> $tempfile" + ((temp_file_num=temp_file_num+1)) + fi + done + fi + if [[ "$1" == "rollback" || "$1" == "rollback-post" ]]; then + while [[ ${temp_new} -gt ${temp_old} ]]; do + local upgrade_sql_file="upgrade_sql/rollback_catalog_${dbname}/${action}_catalog_${dbname}_${temp_new:0:2}_${temp_new:2}.sql" + if [[ -f "$upgrade_sql_file" ]]; then + if ! cat "$upgrade_sql_file" >> "$tempfile"; then + die "Write $tempfile failed" ${err_upgrade_pre} + fi + debug "$upgrade_sql_file >>$tempfile" + ((temp_file_num=temp_file_num+1)) + fi + ((temp_new=$temp_new-1)) + done + fi + if ! echo "COMMIT;" >> "$tempfile";then + die "Write $tempfile failed" ${err_upgrade_pre} + fi + #file not meet requirements + if [[ ${temp_file_num} -eq 0 ]]; then + debug "No sql file for ${action} ${dbname}!" + rm -f "$tempfile" + else + debug "get ${temp_file_num} files for ${action} ${dbname}!" + fi +} + +function prepare_sql_all() { + local dir_temp_sql="$GAUSS_TMP_PATH"/temp_sql + local sql_tar_file="$GAUSS_SQL_TAR_PATH"/upgrade_sql.tar.gz + local sql_tar_sha="$GAUSS_SQL_TAR_PATH"/upgrade_sql.sha256 + + if [[ ! -f "${sql_tar_file}" ]] || [[ ! -f "${sql_tar_sha}" ]]; then + die "${sql_tar_file} or ${sql_tar_sha} not exit!" ${err_upgrade_pre} + else + local sha_expect=$(cat ${sql_tar_sha}) + local sha_current=$(sha256sum ${sql_tar_file} | awk '{print $1}') + if [[ "$sha_expect" != "$sha_current" ]]; then + die "The sha256 value of $sql_tar_file does not match $sql_tar_sha!" ${err_upgrade_pre} + fi + if [[ -d "$dir_temp_sql" ]];then + rm -rf "$dir_temp_sql" + fi + if mkdir -p -m 700 "$dir_temp_sql" && tar -zxf "$sql_tar_file" -C "$dir_temp_sql"; then + log "decompress upgrade_sql.tar.gz successfully." + else + die "decompress upgrade_sql.tar.gz failed" ${err_upgrade_pre} + fi + fi + #total 8 + cd "$dir_temp_sql" + for action in upgrade upgrade-post rollback rollback-post; do + for db_base in maindb otherdb; do + prepare_sql ${action} ${db_base} + done + done + cd ${GAUSS_UPGRADE_BASE_PATH} + +} + +function exec_sql() { + #$1: sqlfilename + #$2: maindb,otherdb + query_dn_role + if [[ X"$dn_role" == X"standby" || X"$dn_role" == X"cascade_standby" ]]; then + return 0 + fi + if [[ ! -f "$1" ]]; then + return 0 + fi + + # if [[ X"$GAUSS_ADMIN_PASSWORD" == X ]]; then + # die "GAUSS_ADMIN_PASSWORD is null" ${err_upgrade_pre} + # fi + if ! check_upgrade_mode_by_sql;then + return 1 + fi + + tempresult="$GAUSS_TMP_PATH"/"temp_sql_tempresult_$(date +%Y%m%d_%H%M%S)" + if echo "" > "$tempresult" && chmod 600 "$tempresult"; then + debug "begin exec sql ,file name is $1" + else + log "Generate $tempresult failed." + fi + sqlbegin="gsql -p $GAUSS_LISTEN_PORT -X -t -A " + if [[ "$2" == "maindb" ]]; then + if ${sqlbegin} -d postgres --echo-queries --set ON_ERROR_STOP=on -f $1 >> "$tempresult" 2>&1;then + debug "Exec $1 on database: postgres successfully" + else + log "Exec sql on postgres failed." + debug "$(cat ${tempresult})" + rm -f ${tempresult} + return 1 + fi + else + if databases=$(${sqlbegin} -d postgres -c "SELECT datname FROM pg_catalog.pg_database where datname != 'postgres';");then + temp_num=$(echo ${databases}|awk '{print NF}') + debug "Num of other databases: $temp_num" + else + log "Exec sql to get databases failed." + return 1 + fi + for database in ${databases}; do + debug "Begin exec $1 on database: $database " + ${sqlbegin} -d ${database} --echo-queries --set ON_ERROR_STOP=on -f $1 >> "$tempresult" 2>&1 + done + fi + if grep -wE "ERROR:|FATAL:|could not connect to server" ${tempresult}; then + log "Exec sql failed." + debug "$(cat ${tempresult})" + rm -f ${tempresult} + return 1 + else + debug "Exec all sql successfully." + rm -f ${tempresult} + return 0 + fi + +} + +function guc_delete() { + bak_ifs=$IFS + IFS=$'\n' + if [[ ! -f "$GAUSS_TMP_PATH"/temp_sql/upgrade_sql/set_guc/delete_guc ]]; then + log "No need to delete guc" + fi + for para in $(cat "$GAUSS_TMP_PATH"/temp_sql/upgrade_sql/set_guc/delete_guc); do + if echo ${para}|grep -w datanode > /dev/null;then + para=$(echo ${para}|awk '{print $1}') + if sed -i "/^${para}[ =]/d" ${PGDATA}/postgresql.conf; then + debug "$para was deleted successfully." + else + die "$para was deleted failed" ${err_upgrade_bin} + fi + fi + done + IFS=${bak_ifs} + log "Delete guc successfully" +} + +function stop_dbnode() { + if ! check_db_process; then + return 0 + fi + gs_ctl stop -D ${PGDATA} >>"${GAUSS_LOG_FILE}" 2>&1 +} + +function start_dbnode() { + start_cmd="gs_ctl start -D ${PGDATA} " + if [[ X"$dn_role" = X ]]; then + return 1 + fi + if [[ X"$dn_role" != X"normal" ]]; then + start_cmd="$start_cmd""-M $dn_role " + fi + log "start gaussdb by cmd: $start_cmd" + ${start_cmd} >>"${GAUSS_LOG_FILE}" 2>&1 + +} + +function query_dn_role() { + timeout 1 gs_ctl query -D ${PGDATA} >"${GAUSS_TMP_PATH}/temp_dn_role" + dn_role_temp=$(grep local_role "${GAUSS_TMP_PATH}/temp_dn_role" | head -1 | awk '{print $3}') + rm -f "${GAUSS_TMP_PATH}/temp_dn_role" + + if [[ "$dn_role_temp" = "Normal" ]]; then + dn_role_temp="normal" + elif [[ "$dn_role_temp" = "Primary" ]]; then + dn_role_temp="primary" + elif [[ "$dn_role_temp" = "Standby" ]]; then + dn_role_temp="standby" + elif [[ "$dn_role_temp" = "Cascade" ]]; then + dn_role_temp="cascade_standby" + else + dn_role_temp="" + fi + + if [[ X"$dn_role" = X ]] && [[ X"$dn_role_temp" = X ]]; then + die "dn_role cannot be null" ${err_dn_role_null} + fi + if [[ X"$dn_role" = X ]]; then + dn_role="$dn_role_temp" + fi + if [[ X"$dn_role_temp" = X ]]; then + dn_role_temp="$dn_role" + fi + if [[ "$dn_role" != "$dn_role_temp" ]]; then + die "dn_role maybe not right" ${err_dn_role_null} + fi +} + +function reload_upgrade_config() { + if check_upgrade_config "$1" "$2"; then + return 0 + fi + # only primary need to reload upgrade_mode, standby wait sync from primary + query_dn_role + if [[ X"$dn_role" == X"standby" || X"$dn_role" == X"cascade_standby" ]]; then + return 0 + fi + # ensure value of sync_config_strategy is all_node or default + if ! check_upgrade_config "sync_config_strategy" "all_node"; then + return 1 + fi + + local current_time=$(date +"%Y-%m-%d %H:%M:%S") + echo -n \[${current_time}\] " " >>"${GAUSS_LOG_FILE}" + for i in $(seq 1 3);do + if gs_guc reload -D ${PGDATA} -c "$1=$2" >>"${GAUSS_LOG_FILE}" 2>&1; then + return 0 + fi + sleep 2 + done + return 1 +} + +function set_upgrade_config() { + if check_upgrade_config "$1" "$2"; then + return 0 + fi + local current_time=$(date +"%Y-%m-%d %H:%M:%S") + echo -n \[${current_time}\] " " >>"${GAUSS_LOG_FILE}" + for i in $(seq 1 3);do + if gs_guc set -D ${PGDATA} -c "$1=$2" >>"${GAUSS_LOG_FILE}" 2>&1; then + debug "guc set $1=$2 successfully" + return 0 + fi + sleep 2 + done + return 1 +} + +function check_upgrade_config() { + local tempfile="$GAUSS_TMP_PATH"/".temp_check_guc_value" + # guc output from opengauss or gauss is diff + if gs_guc check -D ${PGDATA} -c "$1" > "$tempfile" 2>&1 ;then + tempvalue=$(cat "$tempfile"|tail -2|head -1|sed 's/\[[^][]*\]//g'|sed 's/[[:space:]]//g'|awk -F= '{print $2}') + if ! rm -f ${tempfile}; then + log "rm -f $tempfile failed" + return 1 + fi + if [[ "$tempvalue" == "$2" ]]; then + debug "guc check $1=$2 successfully" + return 0 + elif [[ "$1" == "sync_config_strategy" && "$tempvalue" == "NULL" ]];then + debug "guc check $1=$2 successfully" + return 0 + else + return 1 + fi + else + if ! rm -f ${tempfile}; then + log "rm -f $tempfile failed" + return 1 + fi + return 1 + fi +} + +function check_upgrade_mode_by_sql(){ + # check upgrade_mode = 2 by sql + check_upgrade_mode_result="$GAUSS_TMP_PATH"/".temp_upgrade_mode" + if echo "" > ${check_upgrade_mode_result} && chmod 600 ${check_upgrade_mode_result}; then + debug "Begin to generate check_upgrade_mode_result." + else + log "generate $check_upgrade_mode_result failed." + return 1 + fi + gsql -p ${GAUSS_LISTEN_PORT} -d postgres --pipeline -X -t -A \ + -c "show upgrade_mode;" > ${check_upgrade_mode_result} 2>&1 & + sleep 0.1 + + for i in $(seq 1 60); + do + check_mode_sql=$(cat ${check_upgrade_mode_result}) + if [[ "$check_mode_sql" == "2" ]];then + rm -f ${check_upgrade_mode_result} + return 0 + elif [[ "$check_mode_sql" == "0" ]];then + rm -f ${check_upgrade_mode_result} + gsql -p ${GAUSS_LISTEN_PORT} -d postgres --pipeline -X -t -A \ + -c "show upgrade_mode;" > ${check_upgrade_mode_result} 2>&1 & + elif [[ "$check_mode_sql" == "" ]];then + debug "Wait for check_upgrade_mode_result..." + else + log "$(cat ${check_upgrade_mode_result})" + return 1 + fi + sleep 0.5 + done + if [[ -f "${check_upgrade_mode_result}" ]]; then + debug "check_upgrade_mode_result is $(cat ${check_upgrade_mode_result})" + rm -f ${check_upgrade_mode_result} + else + debug "get upgrade_mode by gsql failed" + fi + return 1 +} + +function rollback_pre() { + parses_step + if [[ "$current_step" -lt 1 ]]; then + log "no need do rollback_pre step" + elif [[ "$current_step" -gt 2 ]]; then + die "You should rollback_bin first" ${err_rollback_pre} + else + if [[ X"$big_cfg" == X ]]; then + big_cfg=$(grep big_cfg "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + fi + if [[ "$big_cfg" == "True" ]]; then + if ! check_db_process; then + die "Gaussdb is not running" ${err_rollback_pre} + fi + if ! reload_upgrade_config upgrade_mode 2; then + die "set upgrade_mode to 2 failed" ${err_rollback_pre} + fi + record_step 1 + if exec_sql "$GAUSS_TMP_PATH"/temp_sql/temp_rollback_maindb.sql maindb && exec_sql "$GAUSS_TMP_PATH"/temp_sql/temp_rollback_otherdb.sql otherdb; then + debug "rollback pre sql successfully" + else + die "rollback pre sql failed" ${err_rollback_pre} + fi + if ! reload_upgrade_config upgrade_mode 0; then + die "set upgrade_mode to 0 failed" ${err_upgrade_pre} + fi + fi + record_step 0 + # need rm back dir and tempdir + delete_tmp_files + log "The rollback_pre step is executed successfully. " + fi +} + +function rollback_bin() { + parses_step + if [[ "$current_step" -lt 3 ]]; then + log "no need do rollback_bin step" + elif [[ "$current_step" -gt 4 ]]; then + die "You should rollback_post first" ${err_rollback_bin} + else + if [[ X"$old_version" == X ]]; then + old_version=$(grep old_version "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + fi + if ! cd "$GAUSS_BACKUP_BASE_PATH"; then + die "$GAUSS_BACKUP_BASE_PATH cannot access" ${err_rollback_bin} + fi + # Once the rollback starts, you must change the status code first. + # Otherwise, the upgrade may continue after the rollback is interrupted. + record_step 3 + query_dn_role + if ! stop_dbnode; then + die "Stop gaussdb failed" ${err_rollback_bin} + fi + cp_gauss_home_config_to_temp ${err_rollback_bin} + if [[ -d "$GAUSSHOME" ]];then + tmp_upgrade_dir="$GAUSSHOME"_tmp_upgrade + if mv "$GAUSSHOME" "$tmp_upgrade_dir" && chmod -R 700 "$tmp_upgrade_dir" && rm -rf "$tmp_upgrade_dir";then + debug "remove $GAUSSHOME successfully" + else + die "remove $GAUSSHOME failed" ${err_rollback_bin} + fi + fi + if mkdir -m 700 "$GAUSSHOME" && cp -rpf bak_bin_"$old_version"/* "$GAUSSHOME";then + log "Restore gausshome successfully!" + else + die "Restore gausshome failed!" ${err_rollback_bin} + fi + cp_temp_config_to_gauss_home ${err_rollback_bin} + + if ! cp -rf ./postgresql.conf_"$old_version" "$PGDATA"/postgresql.conf; then + die "Restore postgresql.conf failed!" ${err_rollback_bin} + fi + if ! cp -rf ./pg_hba.conf_"$old_version" "$PGDATA"/pg_hba.conf; then + die "Restore pg_hba.conf failed!" ${err_rollback_bin} + fi + log "Restore GAUSSHOME,postgresql.conf,pg_hba.conf successfully." + # when rollback postgresql.conf, upgrade_mode should be set to 2 + if [[ X"$big_cfg" == X ]]; then + big_cfg=$(grep big_cfg "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + fi + if [[ "$big_cfg" == "True" ]]; then + if ! set_upgrade_config upgrade_mode 2; then + die "set upgrade_mode to 2 failed" ${err_rollback_bin} + fi + fi + if ! cd "$GAUSS_UPGRADE_BASE_PATH"; then + die "$GAUSS_UPGRADE_BASE_PATH cannot access" ${err_rollback_bin} + fi + + if ! start_dbnode; then + die "Start gaussdb failed" ${err_rollback_bin} + fi + record_step 2 + log "The rollback_bin step is executed successfully. " + fi + +} + +function rollback_post() { + parses_step + if [[ "$current_step" -lt 5 ]]; then + log "Cannot do rollback_post step" + else + if [[ X"$big_cfg" == X ]]; then + big_cfg=$(grep big_cfg "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + fi + if [[ "$big_cfg" == "True" ]]; then + if ! check_db_process; then + die "Gaussdb is not running" ${err_rollback_post} + fi + if ! reload_upgrade_config upgrade_mode 2; then + die "set upgrade_mode to 2 failed" ${err_upgrade_post} + fi + record_step 5 + if exec_sql "$GAUSS_TMP_PATH"/temp_sql/temp_rollback-post_maindb.sql maindb && exec_sql "$GAUSS_TMP_PATH"/temp_sql/temp_rollback-post_otherdb.sql otherdb; then + debug "rollback post sql successfully" + else + die "rollback post sql failed" ${err_rollback_post} + fi + fi + record_step 4 + log "The rollback_post step is executed successfully. " + fi + +} + +function upgrade_pre() { + parses_step + if [[ "$current_step" -lt 0 ]]; then + die "Step file may be changed invalid" ${err_upgrade_pre} + elif [[ "$current_step" -lt 1 ]]; then + upgrade_pre_step1 + upgrade_pre_step2 + elif [[ "$current_step" -eq 1 ]]; then + rollback_pre + upgrade_pre_step1 + upgrade_pre_step2 + else + log "no need do upgrade_pre step" + fi +} +function upgrade_pre_step1() { + check_disk + check_version + prepare_sql_all + check_pkg + bak_gauss + decompress_pkg + record_step 1 +} + +function upgrade_pre_step2() { + if [[ X"$big_cfg" == X ]]; then + big_cfg=$(grep big_cfg "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + fi + if [[ "$big_cfg" == "True" ]]; then + if ! check_db_process; then + die "Gaussdb isnot running" ${err_upgrade_pre} + fi + if ! reload_upgrade_config upgrade_mode 2; then + die "set upgrade_mode to 2 failed" ${err_upgrade_pre} + fi + + if exec_sql "$GAUSS_TMP_PATH"/temp_sql/temp_upgrade_maindb.sql maindb && exec_sql "$GAUSS_TMP_PATH"/temp_sql/temp_upgrade_otherdb.sql otherdb; then + debug "exec pre sql successfully" + else + die "exec pre sql failed" ${err_upgrade_pre} + fi + + fi + record_step 2 + log "The upgrade_pre step is executed successfully. " +} + +function upgrade_bin() { + parses_step + if [[ "$current_step" -lt 0 ]]; then + die "Step file may be changed invalid" ${err_upgrade_bin} + elif [[ "$current_step" -lt 2 ]]; then + die "exec upgrade pre first" ${err_upgrade_bin} + elif [[ "$current_step" -gt 3 ]]; then + log "no need do upgrade_bin step" + else + upgrade_bin_step4 + fi +} + +function upgrade_bin_step4() { + record_step 3 + query_dn_role + if ! stop_dbnode; then + die "Stop gaussdb failed" ${err_upgrade_bin} + fi + cp_gauss_home_config_to_temp ${err_upgrade_bin} + cp_pkg + cp_temp_config_to_gauss_home ${err_upgrade_bin} + guc_delete + if [[ X"$big_cfg" == X ]]; then + big_cfg=$(grep big_cfg "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + fi + if [[ X"$old_cfg" == X ]]; then + old_cfg=$(grep old_cfg "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + fi + if [[ "$big_cfg" == "True" ]]; then + old_version=$(grep old_version "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + if cp ${GAUSS_BACKUP_BASE_PATH}/bak_bin_"$old_version"/version.cfg "$GAUSSHOME"/old_upgrade_version \ + && chmod 400 "$GAUSSHOME"/old_upgrade_version;then + debug "Create $GAUSSHOME/old_upgrade_version successfully" + else + die "Create $GAUSSHOME/old_upgrade_version failed" ${err_upgrade_bin} + fi + fi + if ! start_dbnode; then + die "Start gaussdb failed" ${err_upgrade_bin} + fi + record_step 4 + log "The upgrade_bin step is executed successfully. " +} + +function cp_gauss_home_config_to_temp() { + local temp_err_code="$1" + if ! cd "$GAUSSHOME";then + die "cd ${GAUSSHOME} failed" ${temp_err_code} + fi + local array=(${GAUSS_UPGRADE_SYNC_CONFIG_LIST//,/ }) + if [[ -e "temp_gauss_upgrade_conf.tar" ]];then + rm -f temp_gauss_upgrade_conf.tar + fi + for var in ${array[@]} + do + if [[ -e "$var" ]];then + debug "begin to tar ${var}" + if ! tar -rf temp_gauss_upgrade_conf.tar ${var};then + die "tar -rf temp_gauss_upgrade_conf.tar $var failed" ${temp_err_code} + fi + fi + done + if [[ -f temp_gauss_upgrade_conf.tar ]];then + if chmod 600 temp_gauss_upgrade_conf.tar && mv temp_gauss_upgrade_conf.tar "${GAUSS_TMP_PATH}";then + log "cp the configs from GAUSSHOME to temp successfully." + else + die "mv temp_gauss_upgrade_conf.tar "${GAUSS_TMP_PATH}" failed" ${temp_err_code} + fi + + fi + if ! cd - > /dev/null; then + die "cd - failed" ${temp_err_code} + fi +} + +function cp_temp_config_to_gauss_home() { + local temp_err_code="$1" + if [[ -f "${GAUSS_TMP_PATH}/temp_gauss_upgrade_conf.tar" ]];then + if ! tar -xf "${GAUSS_TMP_PATH}/temp_gauss_upgrade_conf.tar" -C "${GAUSSHOME}";then + die "tar -xf temp_gauss_upgrade_conf.tar -C ${GAUSSHOME} failed" ${temp_err_code} + fi + if ! rm -f "${GAUSS_TMP_PATH}/temp_gauss_upgrade_conf.tar";then + die "rm -f temp_gauss_upgrade_conf.tar" ${temp_err_code} + fi + log "Move the configs from temp to GAUSSHOME successfully." + fi +} + +function upgrade_post() { + parses_step + if [[ "$current_step" -lt 0 ]]; then + die "Step file may be changed invalid" ${err_upgrade_post} + elif [[ "$current_step" -lt 4 ]]; then + die "You should exec upgrade_bin first" ${err_upgrade_post} + elif [[ "$current_step" -eq 4 ]]; then + upgrade_post_step56 + elif [[ "$current_step" -eq 5 ]]; then + rollback_post + upgrade_post_step56 + else + log "no need do upgrade_post step" + fi +} +function upgrade_post_step56() { + if [[ X"$big_cfg" == X ]]; then + big_cfg=$(grep big_cfg "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + fi + if [[ "$big_cfg" == "True" ]]; then + if ! check_db_process; then + die "Guassdb is not running" ${err_upgrade_post} + fi + record_step 5 + if exec_sql "$GAUSS_TMP_PATH"/temp_sql/temp_upgrade-post_maindb.sql maindb && exec_sql "$GAUSS_TMP_PATH"/temp_sql/temp_upgrade-post_otherdb.sql otherdb; then + debug "upgrade post sql successfully" + else + die "upgrade post sql failed" ${err_upgrade_post} + fi + + fi + record_step 6 + log "The upgrade_post step is executed successfully. " +} +function upgrade_commit() { + parses_step + if [[ "$current_step" -eq 0 ]]; then + die "No need commit,upgrade directly" ${err_no_need_commit} + fi + if [[ "$current_step" -ne 6 ]]; then + die "Now you can't commit because the steps are wrong" ${err_upgrade_commit} + fi + if ! rm -f "$GAUSSHOME"/old_upgrade_version;then + die "rm $GAUSSHOME/old_upgrade_version file failed" ${err_upgrade_commit} + fi + if ! reload_upgrade_config upgrade_mode 0; then + die "set upgrade_mode to 0 failed" ${err_upgrade_commit} + fi + if [[ X"$old_cfg" == X ]]; then + old_cfg=$(grep old_cfg "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + fi + if ! sed -i "s/\"-u\"\ \"$old_cfg\"\ //g" "$GAUSSDATA"/postmaster.opts; then + die "modify postmaster.opts failed, please check." ${err_upgrade_commit} + fi + # after commit, need to reset step to 0 + record_step 0 + # need rm back dir and tempdir + delete_tmp_files + log "The upgrade_commit step is executed successfully. " +} +function record_step() { + local record_file="$GAUSS_TMP_PATH"/record_step.txt + if echo "$1" > "$record_file" && chmod 600 "$record_file"; then + debug "record step $1 successfully." + else + die "Write step file failed" ${err_inner_sys} + fi +} + +function parses_step() { + if [[ ! -f "$GAUSS_TMP_PATH"/record_step.txt ]]; then + current_step=0 + else + if grep -q [^0-6] "$GAUSS_TMP_PATH"/record_step.txt; then + die "$GAUSS_TMP_PATH/record_step.txt may be changed manually" ${err_inner_sys} + else + current_step=$(cat "$GAUSS_TMP_PATH"/record_step.txt) + fi + fi + debug "current_step is $current_step" +} + +function switch_over() { + local temp_file="${GAUSS_TMP_PATH}/temp_switch_over" + if gs_ctl query -D "$PGDATA" > "$temp_file" && chmod 400 "$temp_file"; then + local_role_temp=$(grep local_role "$temp_file" | head -1 | awk '{print $3}') + db_state_temp=$(grep db_state "$temp_file" | head -1 | awk '{print $3}') + peer_role_temp=$(grep peer_role "$temp_file" | head -1 | awk '{print $3}') + rm -f "$temp_file" + else + die "gs_ctl query -D $PGDATA failed" 1 + fi + + if [[ "$local_role_temp" = "Standby" ]] && [[ "$db_state_temp" = "Normal" ]] && [[ "$peer_role_temp" = "Primary" ]]; then + log "Current node can do switchover" + if gs_ctl switchover -D "$GAUSSDATA" -f ;then + log "Switchover success" + else + die "Switchover failed" + fi + else + log "no need do switchover" + fi +} + +function delete_tmp_files() { + # need rm back dir and tempdir + new_version=$(grep new_version "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + old_version=$(grep old_version "$GAUSS_TMP_PATH"/version_flag | awk -F= '{print $2}') + if [[ -d "$GAUSS_BACKUP_BASE_PATH"/bak_bin_"$old_version" ]];then + chmod -R 700 "$GAUSS_BACKUP_BASE_PATH"/bak_bin_"$old_version" && rm -rf "$GAUSS_BACKUP_BASE_PATH"/bak_bin_"$old_version" + fi + rm -f "$GAUSS_BACKUP_BASE_PATH"/postgresql.conf_"$old_version" + rm -f "$GAUSS_BACKUP_BASE_PATH"/pg_hba.conf_"$old_version" + + if [[ -d "$GAUSS_TMP_PATH"/temp_sql ]];then + rm -rf "$GAUSS_TMP_PATH"/temp_sql + fi + if [[ -d "$GAUSS_TMP_PATH"/install_bin_"$new_version" ]];then + chmod -R 700 "$GAUSS_TMP_PATH"/install_bin_"$new_version" && rm -rf "$GAUSS_TMP_PATH"/install_bin_"$new_version" + fi + rm -f "$GAUSS_TMP_PATH"/version_flag + rm -f "$GAUSS_TMP_PATH"/record_step.txt +} \ No newline at end of file diff --git a/opengauss_upgrade_config.sh b/opengauss_upgrade_config.sh new file mode 100644 index 0000000000000000000000000000000000000000..d442d9027c8cf10c18126156c72ababcfd911a1d --- /dev/null +++ b/opengauss_upgrade_config.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# Copyright (c) Huawei Technologies Co., Ltd. 2010-2022. All rights reserved. +# date: 2021-12-22 +# version: 1.0 + +# 数据库监听端口 +GAUSS_LISTEN_PORT="7654" + +# 数据库管理员用户名 +GAUSS_ADMIN_USER="opengauss" + +#数据库升级回退日志路径 +GAUSS_LOG_PATH="/var/lib/opengauss/opengauss_upgrade" + +#数据库升级根位置 +GAUSS_UPGRADE_BASE_PATH="/var/lib/opengauss/opengauss_upgrade/pkg_5.0.1" + +#数据库SQL包位置 +GAUSS_SQL_TAR_PATH="/var/lib/opengauss/opengauss_upgrade/pkg_5.0.1" + +#数据库低版本备份位置 +GAUSS_BACKUP_BASE_PATH="/var/lib/opengauss/opengauss_upgrade/bak" + +#数据库临时目录 +GAUSS_TMP_PATH="/var/lib/opengauss/opengauss_upgrade/tmp" + +#是否使用存在的bin解压包 +GAUSS_UPGRADE_BIN_PATH="" + +#需要同步的cluster config 列表 +GAUSS_UPGRADE_SYNC_CONFIG_LIST="" \ No newline at end of file diff --git a/opengauss_upgrade_errorcode.sh b/opengauss_upgrade_errorcode.sh new file mode 100644 index 0000000000000000000000000000000000000000..9c42f1a018e4ec7de274fd6135502df9c0efd07b --- /dev/null +++ b/opengauss_upgrade_errorcode.sh @@ -0,0 +1,18 @@ +#!/bin/bash +# Copyright (c) Huawei Technologies Co., Ltd. 2010-2022. All rights reserved. +# date: 2021-12-22 +# version: 1.0 + +err_upgrade_pre=201 +err_upgrade_bin=202 +err_upgrade_post=203 +err_rollback_pre=204 +err_rollback_bin=205 +err_rollback_post=206 +err_check_init=207 +err_parameter=208 +err_upgrade_commit=209 +err_version_same=210 +err_no_need_commit=211 +err_inner_sys=212 +err_dn_role_null=213 \ No newline at end of file diff --git a/opengauss_upgrade_start.sh b/opengauss_upgrade_start.sh new file mode 100644 index 0000000000000000000000000000000000000000..d78c77547eda322c5e236727c13825fba57e96a1 --- /dev/null +++ b/opengauss_upgrade_start.sh @@ -0,0 +1,56 @@ +#!/bin/bash +# Copyright (c) Huawei Technologies Co., Ltd. 2010-2022. All rights reserved. +# date: 2021-12-22 +# version: 1.0 +CURRENT_DIR=$( + cd $(dirname $0) + pwd +) + +source ${CURRENT_DIR}/opengauss_upgrade_config.sh +source ${CURRENT_DIR}/opengauss_upgrade_common.sh + +function main() { + check_and_init + parse_cmd_line $@ + case "${action}" in + upgrade_pre) + upgrade_pre + exit 0 + ;; + upgrade_bin) + upgrade_bin + exit 0 + ;; + upgrade_post) + upgrade_post + exit 0 + ;; + rollback_pre) + rollback_pre + exit 0 + ;; + rollback_bin) + rollback_bin + exit 0 + ;; + rollback_post) + rollback_post + exit 0 + ;; + upgrade_commit) + upgrade_commit + exit 0 + ;; + switch_over) + switch_over + exit 0 + ;; + *) + log "please input right parameter, the following command may help you" + log "sh upgrade_GAUSSV5.sh --help or sh upgrade_GAUSSV5.sh -h" + die "Must input parameter -t action" ${err_parameter} + ;; + esac +} +main $@ diff --git a/upgrade.patch b/upgrade.patch new file mode 100644 index 0000000000000000000000000000000000000000..fdca56d712882abb289f3fa168170962da23d4e5 --- /dev/null +++ b/upgrade.patch @@ -0,0 +1,159 @@ +diff -crN '--exclude=.git' '--exclude=.gitee' '--exclude=.vscode' openGauss-server-5.0.1/liteom/upgrade_common.sh openGauss-server-5.0.1-edit/liteom/upgrade_common.sh +*** openGauss-server-5.0.1/liteom/upgrade_common.sh 2024-05-09 14:48:32.000000000 +0800 +--- openGauss-server-5.0.1-edit/liteom/upgrade_common.sh 2024-06-07 17:31:52.228407016 +0800 +*************** +*** 273,284 **** + } + + function check_version() { +- if [[ ! -f "${GAUSSHOME}/version.cfg" ]]; then +- die "Cannot find current version.cfg!" ${err_upgrade_pre} +- else +- old_version=$(tail -n 1 "$GAUSSHOME"/version.cfg) +- old_cfg=$(sed -n 2p "$GAUSSHOME"/version.cfg | sed 's/\.//g') +- fi + if [[ -f "$GAUSS_UPGRADE_BIN_PATH"/version.cfg ]]; then + new_version_cfg_path="${GAUSS_UPGRADE_BIN_PATH}/version.cfg" + elif [[ -f "$GAUSS_UPGRADE_BASE_PATH"/version.cfg ]]; then +--- 273,278 ---- +*************** +*** 290,333 **** + new_version=$(tail -n 1 "$new_version_cfg_path") + new_cfg=$(sed -n 2p "$new_version_cfg_path" | sed 's/\.//g') + +- if [[ X"$old_version" == X || X"$old_cfg" == X || X"$new_version" == X || X"$new_cfg" == X ]]; then +- die "Maybe version.cfg is not normal" ${err_upgrade_pre} +- fi +- if ! echo "$old_cfg"|grep -Ewq "[0-9]{3,6}";then +- die "Maybe version.cfg is not normal" ${err_upgrade_pre} +- fi + if ! echo "$new_cfg"|grep -Ewq "[0-9]{3,6}";then + die "Maybe version.cfg is not normal" ${err_upgrade_pre} + fi + +! if [[ "$old_version" == "$new_version" ]]; then +! die "New version is same as old, the commitId is $old_version!" ${err_version_same} +! fi +! if [[ ${new_cfg} -lt ${old_cfg} ]]; then +! die "Current version is newer!" ${err_upgrade_pre} +! fi +! big_cfg="False" +! if [[ ${new_cfg} -gt ${old_cfg} ]]; then +! log "Big upgrade is needed!" +! big_cfg="True" +! fi + + local flag_file="$GAUSS_TMP_PATH"/version_flag +- if echo "old_version=$old_version" > "$flag_file" && chmod 600 "$flag_file"; then +- debug "Begin to generate $flag_file" +- else +- die "Write $flag_file failed" ${err_upgrade_pre} +- fi + if ! echo "new_version=$new_version" >> "$flag_file"; then + die "Write $flag_file failed" ${err_upgrade_pre} + fi + if ! echo "big_cfg=$big_cfg" >> "$flag_file"; then + die "Write $flag_file failed" ${err_upgrade_pre} + fi +- if ! echo "old_cfg=$old_cfg" >> "$flag_file"; then +- die "Write $flag_file failed" ${err_upgrade_pre} +- fi +- log "Old version commitId is $old_version, version info is $old_cfg" + log "New version commitId is $new_version, version info is $new_cfg" + + ##need version.cfg to check big upgrade,note user exec sql on primary dn +--- 284,302 ---- + new_version=$(tail -n 1 "$new_version_cfg_path") + new_cfg=$(sed -n 2p "$new_version_cfg_path" | sed 's/\.//g') + + if ! echo "$new_cfg"|grep -Ewq "[0-9]{3,6}";then + die "Maybe version.cfg is not normal" ${err_upgrade_pre} + fi + +! big_cfg="True" + + local flag_file="$GAUSS_TMP_PATH"/version_flag + if ! echo "new_version=$new_version" >> "$flag_file"; then + die "Write $flag_file failed" ${err_upgrade_pre} + fi + if ! echo "big_cfg=$big_cfg" >> "$flag_file"; then + die "Write $flag_file failed" ${err_upgrade_pre} + fi + log "New version commitId is $new_version, version info is $new_cfg" + + ##need version.cfg to check big upgrade,note user exec sql on primary dn +*************** +*** 1239,1242 **** + fi + rm -f "$GAUSS_TMP_PATH"/version_flag + rm -f "$GAUSS_TMP_PATH"/record_step.txt +! } +\ No newline at end of file +--- 1208,1211 ---- + fi + rm -f "$GAUSS_TMP_PATH"/version_flag + rm -f "$GAUSS_TMP_PATH"/record_step.txt +! } +diff -crN '--exclude=.git' '--exclude=.gitee' '--exclude=.vscode' openGauss-server-5.0.1/liteom/upgrade_config.sh openGauss-server-5.0.1-edit/liteom/upgrade_config.sh +*** openGauss-server-5.0.1/liteom/upgrade_config.sh 2024-05-09 14:48:32.000000000 +0800 +--- openGauss-server-5.0.1-edit/liteom/upgrade_config.sh 2024-06-06 16:56:26.681705002 +0800 +*************** +*** 4,31 **** + # version: 1.0 + + # 数据库监听端口 +! GAUSS_LISTEN_PORT="" + + # 数据库管理员用户名 +! GAUSS_ADMIN_USER="" + + #数据库升级回退日志路径 +! GAUSS_LOG_PATH="" + + #数据库升级根位置 +! GAUSS_UPGRADE_BASE_PATH="" + + #数据库SQL包位置 +! GAUSS_SQL_TAR_PATH="" + + #数据库低版本备份位置 +! GAUSS_BACKUP_BASE_PATH="" + + #数据库临时目录 +! GAUSS_TMP_PATH="" + + #是否使用存在的bin解压包 + GAUSS_UPGRADE_BIN_PATH="" + + #需要同步的cluster config 列表 +! GAUSS_UPGRADE_SYNC_CONFIG_LIST="" +\ No newline at end of file +--- 4,31 ---- + # version: 1.0 + + # 数据库监听端口 +! GAUSS_LISTEN_PORT="7654" + + # 数据库管理员用户名 +! GAUSS_ADMIN_USER="opengauss" + + #数据库升级回退日志路径 +! GAUSS_LOG_PATH="/usr/local/opengauss_upgrade" + + #数据库升级根位置 +! GAUSS_UPGRADE_BASE_PATH="/usr/local/opengauss_upgrade/pkg_5.0.1" + + #数据库SQL包位置 +! GAUSS_SQL_TAR_PATH="/usr/local/opengauss_upgrade/pkg_5.0.1" + + #数据库低版本备份位置 +! GAUSS_BACKUP_BASE_PATH="/usr/local/opengauss_upgrade/bak" + + #数据库临时目录 +! GAUSS_TMP_PATH="/usr/local/opengauss_upgrade/tmp" + + #是否使用存在的bin解压包 + GAUSS_UPGRADE_BIN_PATH="" + + #需要同步的cluster config 列表 +! GAUSS_UPGRADE_SYNC_CONFIG_LIST="" diff --git a/version.cfg b/version.cfg new file mode 100644 index 0000000000000000000000000000000000000000..77656eee1bbf0ae4f8a025b67e1c1ff41642cda5 --- /dev/null +++ b/version.cfg @@ -0,0 +1,3 @@ +openGauss-Lite-5.0.1 +92.854 +33b035fd \ No newline at end of file