From 06f51aaec2392c9c8084ef021af9bc93f46043e3 Mon Sep 17 00:00:00 2001 From: Zhang Peng Date: Tue, 28 Apr 2020 12:07:00 +0800 Subject: [PATCH] update codes --- codes/linux/dunwu-soft.sh | 130 +++++++++++++++++++++-- codes/linux/soft/lib/git.sh | 188 +++++++++++++++++++++++++--------- docs/linux/soft/gitlab-ops.md | 127 ++++++++++------------- 3 files changed, 316 insertions(+), 129 deletions(-) diff --git a/codes/linux/dunwu-soft.sh b/codes/linux/dunwu-soft.sh index 023c518..c542198 100644 --- a/codes/linux/dunwu-soft.sh +++ b/codes/linux/dunwu-soft.sh @@ -5,16 +5,130 @@ # @author Zhang Peng # ------------------------------------------------------------------------------ -# ------------------------------------------------------------------------------ libs -# 装载其它库 -LINUX_SCRIPTS_DIR=$(cd `dirname $0`; pwd) +# ------------------------------------------------------------------------------ env -if [[ ! -x ${LINUX_SCRIPTS_DIR}/lib/utils.sh ]]; then - logError "必要脚本库 ${LINUX_SCRIPTS_DIR}/lib/utils.sh 不存在!" - exit 1 -fi +# Regular Color +export ENV_COLOR_BLACK="\033[0;30m" +export ENV_COLOR_RED="\033[0;31m" +export ENV_COLOR_GREEN="\033[0;32m" +export ENV_COLOR_YELLOW="\033[0;33m" +export ENV_COLOR_BLUE="\033[0;34m" +export ENV_COLOR_MAGENTA="\033[0;35m" +export ENV_COLOR_CYAN="\033[0;36m" +export ENV_COLOR_WHITE="\033[0;37m" +# Bold Color +export ENV_COLOR_B_BLACK="\033[1;30m" +export ENV_COLOR_B_RED="\033[1;31m" +export ENV_COLOR_B_GREEN="\033[1;32m" +export ENV_COLOR_B_YELLOW="\033[1;33m" +export ENV_COLOR_B_BLUE="\033[1;34m" +export ENV_COLOR_B_MAGENTA="\033[1;35m" +export ENV_COLOR_B_CYAN="\033[1;36m" +export ENV_COLOR_B_WHITE="\033[1;37m" +# Underline Color +export ENV_COLOR_U_BLACK="\033[4;30m" +export ENV_COLOR_U_RED="\033[4;31m" +export ENV_COLOR_U_GREEN="\033[4;32m" +export ENV_COLOR_U_YELLOW="\033[4;33m" +export ENV_COLOR_U_BLUE="\033[4;34m" +export ENV_COLOR_U_MAGENTA="\033[4;35m" +export ENV_COLOR_U_CYAN="\033[4;36m" +export ENV_COLOR_U_WHITE="\033[4;37m" +# Background Color +export ENV_COLOR_BG_BLACK="\033[40m" +export ENV_COLOR_BG_RED="\033[41m" +export ENV_COLOR_BG_GREEN="\033[42m" +export ENV_COLOR_BG_YELLOW="\033[43m" +export ENV_COLOR_BG_BLUE="\033[44m" +export ENV_COLOR_BG_MAGENTA="\033[45m" +export ENV_COLOR_BG_CYAN="\033[46m" +export ENV_COLOR_BG_WHITE="\033[47m" +# Reset Color +export ENV_COLOR_RESET="$(tput sgr0)" -source ${LINUX_SCRIPTS_DIR}/lib/utils.sh +# status +export ENV_YES=0 +export ENV_NO=1 +export ENV_SUCCEED=0 +export ENV_FAILED=1 + + +# ------------------------------------------------------------------------------ functions + +# 显示打印日志的时间 +SHELL_LOG_TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") +# 那个用户在操作 +USER=$(whoami) +# 日志路径 +LOG_PATH=${ENV_LOG_PATH:-/var/log/shell.log} +# 日志目录 +LOG_DIR=${LOG_PATH%/*} + +createLogFileIfNotExists() { + if [[ ! -x "${LOG_PATH}" ]]; then + mkdir -p "${LOG_DIR}" + touch "${LOG_PATH}" + fi +} + +redOutput() { + echo -e "${ENV_COLOR_RED} $@${ENV_COLOR_RESET}" +} +greenOutput() { + echo -e "${ENV_COLOR_B_GREEN} $@${ENV_COLOR_RESET}" +} +yellowOutput() { + echo -e "${ENV_COLOR_YELLOW} $@${ENV_COLOR_RESET}" +} +blueOutput() { + echo -e "${ENV_COLOR_BLUE} $@${ENV_COLOR_RESET}" +} +magentaOutput() { + echo -e "${ENV_COLOR_MAGENTA} $@${ENV_COLOR_RESET}" +} +cyanOutput() { + echo -e "${ENV_COLOR_CYAN} $@${ENV_COLOR_RESET}" +} +whiteOutput() { + echo -e "${ENV_COLOR_WHITE} $@${ENV_COLOR_RESET}" +} + +logInfo() { + echo -e "${ENV_COLOR_B_GREEN}[INFO] $@${ENV_COLOR_RESET}" + createLogFileIfNotExists + echo "[${SHELL_LOG_TIMESTAMP}] [${USER}] [INFO] [$0] $@" >> "${LOG_PATH}" +} +logWarn() { + echo -e "${ENV_COLOR_B_YELLOW}[WARN] $@${ENV_COLOR_RESET}" + createLogFileIfNotExists + echo "[${SHELL_LOG_TIMESTAMP}] [${USER}] [WARN] [$0] $@" >> "${LOG_PATH}" +} +logError() { + echo -e "${ENV_COLOR_B_RED}[ERROR] $@${ENV_COLOR_RESET}" + createLogFileIfNotExists + echo "[${SHELL_LOG_TIMESTAMP}] [${USER}] [ERROR] [$0] $@" >> "${LOG_PATH}" +} + +printInfo() { + echo -e "${ENV_COLOR_B_GREEN}[INFO] $@${ENV_COLOR_RESET}" +} +printWarn() { + echo -e "${ENV_COLOR_B_YELLOW}[WARN] $@${ENV_COLOR_RESET}" +} +printError() { + echo -e "${ENV_COLOR_B_RED}[ERROR] $@${ENV_COLOR_RESET}" +} + +callAndLog () { + $* + if [[ $? -eq ${ENV_SUCCEED} ]]; then + logInfo "$@" + return ${ENV_SUCCEED} + else + logError "$@ EXECUTE FAILED" + return ${ENV_FAILED} + fi +} # ------------------------------------------------------------------------------ functions # 打印头部信息 diff --git a/codes/linux/soft/lib/git.sh b/codes/linux/soft/lib/git.sh index 1846174..c729cc7 100644 --- a/codes/linux/soft/lib/git.sh +++ b/codes/linux/soft/lib/git.sh @@ -1,42 +1,154 @@ #!/usr/bin/env bash # ----------------------------------------------------------------------------------------------------- -# git operation utils +# Shell Utils # @author Zhang Peng # ----------------------------------------------------------------------------------------------------- -# ------------------------------------------------------------------------------ load libs +# ------------------------------------------------------------------------------ env -LINUX_SCRIPTS_LIB_DIR=`dirname ${BASH_SOURCE[0]}` +# Regular Color +export ENV_COLOR_BLACK="\033[0;30m" +export ENV_COLOR_RED="\033[0;31m" +export ENV_COLOR_GREEN="\033[0;32m" +export ENV_COLOR_YELLOW="\033[0;33m" +export ENV_COLOR_BLUE="\033[0;34m" +export ENV_COLOR_MAGENTA="\033[0;35m" +export ENV_COLOR_CYAN="\033[0;36m" +export ENV_COLOR_WHITE="\033[0;37m" +# Bold Color +export ENV_COLOR_B_BLACK="\033[1;30m" +export ENV_COLOR_B_RED="\033[1;31m" +export ENV_COLOR_B_GREEN="\033[1;32m" +export ENV_COLOR_B_YELLOW="\033[1;33m" +export ENV_COLOR_B_BLUE="\033[1;34m" +export ENV_COLOR_B_MAGENTA="\033[1;35m" +export ENV_COLOR_B_CYAN="\033[1;36m" +export ENV_COLOR_B_WHITE="\033[1;37m" +# Underline Color +export ENV_COLOR_U_BLACK="\033[4;30m" +export ENV_COLOR_U_RED="\033[4;31m" +export ENV_COLOR_U_GREEN="\033[4;32m" +export ENV_COLOR_U_YELLOW="\033[4;33m" +export ENV_COLOR_U_BLUE="\033[4;34m" +export ENV_COLOR_U_MAGENTA="\033[4;35m" +export ENV_COLOR_U_CYAN="\033[4;36m" +export ENV_COLOR_U_WHITE="\033[4;37m" +# Background Color +export ENV_COLOR_BG_BLACK="\033[40m" +export ENV_COLOR_BG_RED="\033[41m" +export ENV_COLOR_BG_GREEN="\033[42m" +export ENV_COLOR_BG_YELLOW="\033[43m" +export ENV_COLOR_BG_BLUE="\033[44m" +export ENV_COLOR_BG_MAGENTA="\033[45m" +export ENV_COLOR_BG_CYAN="\033[46m" +export ENV_COLOR_BG_WHITE="\033[47m" +# Reset Color +export ENV_COLOR_RESET="$(tput sgr0)" -if [[ ! -x ${LINUX_SCRIPTS_LIB_DIR}/utils.sh ]]; then - logError "${LINUX_SCRIPTS_LIB_DIR}/utils.sh not exists!" - exit 1 -fi - -source ${LINUX_SCRIPTS_LIB_DIR}/utils.sh +# status +export ENV_YES=0 +export ENV_NO=1 +export ENV_SUCCEED=0 +export ENV_FAILED=1 -# ------------------------------------------------------------------------------ functions +# ------------------------------------------------------------------------------ util functions -GIT_LOCAL_BRANCH= -getGitLocalBranch() { - GIT_LOCAL_BRANCH=$(git symbolic-ref -q --short HEAD) +# 显示打印日志的时间 +SHELL_LOG_TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") +# 那个用户在操作 +USER=$(whoami) +# 日志路径 +LOG_PATH=${ENV_LOG_PATH:-/var/log/shell.log} +# 日志目录 +LOG_DIR=${LOG_PATH%/*} + +createLogFileIfNotExists() { + if [[ ! -x "${LOG_PATH}" ]]; then + mkdir -p "${LOG_DIR}" + touch "${LOG_PATH}" + fi +} + +redOutput() { + echo -e "${ENV_COLOR_RED} $@${ENV_COLOR_RESET}" +} +greenOutput() { + echo -e "${ENV_COLOR_B_GREEN} $@${ENV_COLOR_RESET}" +} +yellowOutput() { + echo -e "${ENV_COLOR_YELLOW} $@${ENV_COLOR_RESET}" +} +blueOutput() { + echo -e "${ENV_COLOR_BLUE} $@${ENV_COLOR_RESET}" +} +magentaOutput() { + echo -e "${ENV_COLOR_MAGENTA} $@${ENV_COLOR_RESET}" +} +cyanOutput() { + echo -e "${ENV_COLOR_CYAN} $@${ENV_COLOR_RESET}" +} +whiteOutput() { + echo -e "${ENV_COLOR_WHITE} $@${ENV_COLOR_RESET}" +} + +logInfo() { + echo -e "${ENV_COLOR_B_GREEN}[INFO] $@${ENV_COLOR_RESET}" + createLogFileIfNotExists + echo "[${SHELL_LOG_TIMESTAMP}] [${USER}] [INFO] [$0] $@" >> "${LOG_PATH}" +} +logWarn() { + echo -e "${ENV_COLOR_B_YELLOW}[WARN] $@${ENV_COLOR_RESET}" + createLogFileIfNotExists + echo "[${SHELL_LOG_TIMESTAMP}] [${USER}] [WARN] [$0] $@" >> "${LOG_PATH}" +} +logError() { + echo -e "${ENV_COLOR_B_RED}[ERROR] $@${ENV_COLOR_RESET}" + createLogFileIfNotExists + echo "[${SHELL_LOG_TIMESTAMP}] [${USER}] [ERROR] [$0] $@" >> "${LOG_PATH}" +} + +printInfo() { + echo -e "${ENV_COLOR_B_GREEN}[INFO] $@${ENV_COLOR_RESET}" +} +printWarn() { + echo -e "${ENV_COLOR_B_YELLOW}[WARN] $@${ENV_COLOR_RESET}" +} +printError() { + echo -e "${ENV_COLOR_B_RED}[ERROR] $@${ENV_COLOR_RESET}" +} + +callAndLog () { + $* + if [[ $? -eq ${ENV_SUCCEED} ]]; then + logInfo "$@" + return ${ENV_SUCCEED} + else + logError "$@ EXECUTE FAILED" + return ${ENV_FAILED} + fi +} + +# ------------------------------------------------------------------------------ git functions + +getGitLocalBranch() { + export GIT_LOCAL_BRANCH=$(git symbolic-ref -q --short HEAD) } -GIT_ORIGIN_BRANCH= getGitOriginBranch() { - GIT_ORIGIN_BRANCH=$(git rev-parse --abbrev-ref --symbolic-full-name "@{u}") + export GIT_ORIGIN_BRANCH=$(git rev-parse --abbrev-ref --symbolic-full-name "@{u}") } # check specified path is git project or not +IS_GIT=false checkGit() { local source=$1 if [[ -d "${source}" ]]; then - cd ${source} || return ${ENV_NO} + cd ${source} # (1) delete gitstatus.tmp if [[ -f "gitstatus.tmp" ]]; then - rm -rf gitstatus.tmp + rm -rf gitstatus.tmp fi # (2) check git status @@ -45,16 +157,13 @@ checkGit() { grep -iwq 'not a git repository' gitstatus.tmp && gitStatus=false || gitStatus=true rm -rf gitstatus.tmp if [[ ${gitStatus} == true ]]; then - return ${ENV_YES} - else - return ${ENV_NO} + export IS_GIT=true + return fi - - return ${ENV_NO} fi logWarn "${source} is not exists." - return ${ENV_NO} + export IS_GIT=false } # execute git clone or fetch @@ -67,7 +176,7 @@ cloneOrPullGit() { local branch=$4 local root=$5 - if [[ ! ${repository} ]] || [[ ! ${group} ]] || [[ ! ${project} ]] || [[ ! ${branch} ]] || [[ ! ${root} ]]; then + if [[ ! ${repository} || ! ${group} || ! ${project} || ! ${branch} || ! ${root} ]]; then logError "Please input root, group, project, branch." return ${ENV_FAILED} fi @@ -80,52 +189,31 @@ cloneOrPullGit() { local source=${root}/${group}/${project} logInfo "project directory is ${source}." logInfo "git url is ${repository}:${group}/${project}.git." - mkdir -p ${root}/${group} checkGit ${source} - if [[ "${ENV_YES}" == "$?" ]]; then + if [[ "${IS_GIT}" == "true" ]]; then cd ${source} || return ${ENV_FAILED} - git fetch --all - git checkout -f ${branch} - if [[ "${ENV_SUCCEED}" != "$?" ]]; then - logError "<<<< git checkout ${branch} failed." - return ${ENV_FAILED} - fi + git checkout ${branch} logInfo "git checkout ${branch} succeed." - getGitOriginBranch + git fetch --all git reset --hard ${GIT_ORIGIN_BRANCH} - if [[ "${ENV_SUCCEED}" != "$?" ]]; then - logError "<<<< git reset --hard ${GIT_ORIGIN_BRANCH} failed." - return ${ENV_FAILED} - fi logInfo "git reset --hard ${GIT_ORIGIN_BRANCH} succeed." git pull - if [[ "${ENV_SUCCEED}" != "$?" ]]; then - logError "<<<< git pull failed." - return ${ENV_FAILED} - fi logInfo "git pull succeed." else git clone "${repository}:${group}/${project}.git" ${source} - if [[ "${ENV_SUCCEED}" != "$?" ]]; then - logError "<<<< git clone ${project} failed." - return ${ENV_FAILED} - fi logInfo "git clone ${project} succeed." cd ${source} || return ${ENV_FAILED} - git checkout -f ${branch} - if [[ "${ENV_SUCCEED}" != "$?" ]]; then - logError "<<<< git checkout ${branch} failed." - return ${ENV_FAILED} - fi + git checkout ${branch} logInfo "git checkout ${branch} succeed." fi logInfo "Clone or pull git project [$2/$3:$4] succeed." + cd ${SOURCE_DIR} return ${ENV_SUCCEED} } diff --git a/docs/linux/soft/gitlab-ops.md b/docs/linux/soft/gitlab-ops.md index 1aff5f7..b90dbd3 100644 --- a/docs/linux/soft/gitlab-ops.md +++ b/docs/linux/soft/gitlab-ops.md @@ -72,6 +72,60 @@ docker run -d \ ![img](http://dunwu.test.upcdn.net/snap/20190131150515.png!zp) +### 自签名证书 + +首先,创建认证目录 + +``` +sudo mkdir -p /etc/gitlab/ssl +sudo chmod 700 /etc/gitlab/ssl +``` + +(1)创建 Private Key + +``` +sudo openssl genrsa -des3 -out /etc/gitlab/ssl/gitlab.domain.com.key 2048 +``` + +会提示输入密码,请记住 + +(2)生成 Certificate Request + +``` +sudo openssl req -new -key /etc/gitlab/ssl/gitlab.domain.com.key -out /etc/gitlab/ssl/gitlab.domain.com.csr +``` + +根据提示,输入信息 + +``` +Country Name (2 letter code) [XX]:CN +State or Province Name (full name) []:JS +Locality Name (eg, city) [Default City]:NJ +Organization Name (eg, company) [Default Company Ltd]:xxxxx +Organizational Unit Name (eg, section) []: +Common Name (eg, your name or your server's hostname) []:gitlab.xxxx.io +Email Address []: + +Please enter the following 'extra' attributes +to be sent with your certificate request +A challenge password []: +An optional company name []: +``` + +(3)移除 Private Key 中的密码短语 + +``` +sudo cp -v /etc/gitlab/ssl/gitlab.domain.com.{key,original} +sudo openssl rsa -in /etc/gitlab/ssl/gitlab.domain.com.original -out /etc/gitlab/ssl/gitlab.domain.com.key +sudo rm -v /etc/gitlab/ssl/gitlab.domain.com.original +``` + +(4)设置文件权限 + +``` +sudo chmod 600 /etc/gitlab/ssl/gitlab.domain.com.* +``` + ## 二、gitlab-ci-multi-runner 安装 > 参考:https://docs.gitlab.com/runner/install/ @@ -176,74 +230,6 @@ docker run -d --name gitlab-runner --restart always \ gitlab/gitlab-runner:latest ``` -## 自签名证书 - -首先,创建认证目录 - -``` -sudo mkdir -p /etc/gitlab/ssl -sudo chmod 700 /etc/gitlab/ssl -``` - -### 创建证书 - -#### 创建 Private Key - -``` -sudo openssl genrsa -des3 -out /etc/gitlab/ssl/gitlab.domain.com.key 2048 -``` - -会提示输入密码,请记住 - -#### 生成 Certificate Request - -``` -sudo openssl req -new -key /etc/gitlab/ssl/gitlab.domain.com.key -out /etc/gitlab/ssl/gitlab.domain.com.csr -``` - -根据提示,输入信息 - -``` -Country Name (2 letter code) [XX]:CN -State or Province Name (full name) []:JS -Locality Name (eg, city) [Default City]:NJ -Organization Name (eg, company) [Default Company Ltd]:xxxxx -Organizational Unit Name (eg, section) []: -Common Name (eg, your name or your server's hostname) []:gitlab.xxxx.io -Email Address []: - -Please enter the following 'extra' attributes -to be sent with your certificate request -A challenge password []: -An optional company name []: -``` - -#### 移除 Private Key 中的密码短语 - -``` -sudo cp -v /etc/gitlab/ssl/gitlab.domain.com.{key,original} -sudo openssl rsa -in /etc/gitlab/ssl/gitlab.domain.com.original -out /etc/gitlab/ssl/gitlab.domain.com.key -sudo rm -v /etc/gitlab/ssl/gitlab.domain.com.original -``` - -#### 创建证书 - -``` -sudo openssl x509 -req -days 1460 -in /etc/gitlab/ssl/gitlab.domain.com.csr -signkey /etc/gitlab/ssl/gitlab.domain.com.key -out /etc/gitlab/ssl/gitlab.domain.com.crt -``` - -#### 移除证书请求文件 - -``` -sudo rm -v /etc/gitlab/ssl/gitlab.domain.com.csr -``` - -#### 设置文件权限 - -``` -sudo chmod 600 /etc/gitlab/ssl/gitlab.domain.com.* -``` - ## 三、gitlab 配置 ### 基本配置 @@ -375,7 +361,7 @@ sudo gitlab-ctl restart 将备份的压缩包拷贝到新机器的备份路径下(默认为 `/var/opt/gitlab/backups`)。 -(1)将备份文件权限修改为777,不然可能恢复的时候会出现权限不够,不能解压的问题 +(1)将备份文件权限修改为 777,不然可能恢复的时候会出现权限不够,不能解压的问题 ```shell chmod 777 1585910556_2020_04_03_11.3.0_gitlab_backup.tar @@ -423,11 +409,10 @@ https://packages.gitlab.com/gitlab/gitlab-ce 再次执行官方升级命令即可完成自动安装。 -## 资料 +## 参考资料 - 官网:https://about.gitlab.com/ - 中文网:https://www.gitlab.com.cn/ - 官网下载:https://about.gitlab.com/downloads/ - 官网安装说明:https://about.gitlab.com/installation/#centos-7 - - [操作系统、运维部署总结系列](https://github.com/dunwu/OS)