diff --git a/codes/deploy/README.md b/codes/deploy/README.md new file mode 100644 index 0000000..14ff36c --- /dev/null +++ b/codes/deploy/README.md @@ -0,0 +1,4 @@ +# deploy + +> 本目录下的代码用于部署 linux 环境。 +> diff --git a/codes/deploy/deployment.sh b/codes/deploy/deployment.sh new file mode 100644 index 0000000..742e246 --- /dev/null +++ b/codes/deploy/deployment.sh @@ -0,0 +1,118 @@ +#!/usr/bin/env bash + +# 次脚本适用于 Centos/RedHat + + +init() { + echo "${IP} 机器环境部署开始" |tee ${DEPLOY_LOG_PATH} + touch ${DEPLOY_LOG_PATH} + chmod 777 ${DEPLOY_LOG_PATH} + git clone git@github.com:dunwu/linux-notes.git ${SOURCE_PATH}/linux-notes +} + +# 获取当前机器 IP +IP="127.0.0.1" +getDeviceIp() { + IP=`ifconfig eth0 | grep "inet addr" | awk '{ print $2}' | awk -F: '{print $2}'` + if [ "$IP" == "" ]; then + IP=`ifconfig ens32 | grep "inet"|grep "broadcast" | awk '{ print $2}' | awk -F: '{print $1}'` + fi +} + +installGit() { + echo "安装 git" |tee ${DEPLOY_LOG_PATH} + yum install -y git +} + +initEnviromentConfig() { + echo "修改环境配置文件 profile 和 hosts" | tee ${DEPLOY_LOG_PATH} + if [ ! -f /etc/profile.bak ] + then + cp /etc/profile /etc/profile.bak + fi + cat ${SOURCE_PATH}/linux-notes/codes/deploy/profile >> /etc/profile + source /etc/profile +} + +installJava() { + echo "安装 jdk" | tee ${DEPLOY_LOG_PATH} + yum -y install java-1.8.0-openjdk-devel-debug.x86_64 | tee ${DEPLOY_LOG_PATH} +} + +installMaven() { + echo "安装 maven" | tee ${DEPLOY_LOG_PATH} + mkdir ${SOFTWARE_PATH}/maven + cd ${SOFTWARE_PATH}/maven + echo "解压 apache-maven-3.5.2-bin.tar.gz" | tee ${DEPLOY_LOG_PATH} + wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://mirrors.shuosc.org/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz + tar -zxvf apache-maven-3.5.2-bin.tar.gz +} + +installGcc() { + echo "安装 gcc" | tee ${DEPLOY_LOG_PATH} + yum -y install make gcc gcc-c++ kernel-devel +} + +installZlib() { + echo "安装 zlib" | tee ${DEPLOY_LOG_PATH} + yum -y install make zlib zlib-devel libtool openssl openssl-devel +} + +installOpenssl() { + echo "安装 openssl" | tee ${DEPLOY_LOG_PATH} + yum -y install make openssl openssl-devel +} + +installPcre() { + echo "安装 Pcre" | tee ${DEPLOY_LOG_PATH} + yum -y install pcre-devel.x86_64 +} + +installNginx() { + echo "安装 Nginx" | tee ${DEPLOY_LOG_PATH} + yum -y install make nginx.x86_64 + cp /etc/nginx/mime.types /usr/local/nginx/conf/ +} + +installNodejsAndNvm() { + echo "安装 Nodejs" | tee ${DEPLOY_LOG_PATH} + yum install -y nodejs npm --enablerepo=epel + + git clone https://github.com/creationix/nvm.git ~/.nvm && cd ~/.nvm + source ~/.nvm/nvm.sh +} + +shutdownFirewall() { + echo "************************关闭防火墙************************" |tee -a /home/depoly.log + /etc/init.d/iptables stop + chkconfig --level 35 iptables off +} + +setPrivilegeForUserIns() { + userdel zp + groupdel coder + groupadd coder + useradd -g coder zp + chown -R coder.zp /home/zp + chown -R coder.zp /opt/ + chown -R coder.zp /tmp/ +} +##############################__MAIN__######################################## +DEPLOY_LOG_PATH=/home/zp/deploy.log +SOURCE_PATH=/home/zp/source +SOFTWARE_PATH=/usr/lib + +init +getDeviceIp +installGit +initEnviromentConfig +installJava +installGcc +installZlib +installOpenssl +installPcre +installNginx +installMaven +installNodejsAndNvm +shutdownFirewall +setPrivilegeForUserIns diff --git a/codes/deploy/profile b/codes/deploy/profile new file mode 100644 index 0000000..c65f96e --- /dev/null +++ b/codes/deploy/profile @@ -0,0 +1,18 @@ + +# 环境变量 +export LANG=en_US.UTF-8 +export JAVA_HOME=/opt/software/java/jdk1.6.0_38 +export MAVEN_HOME=/opt/software/maven/apache-maven-3.0.5 +export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib +export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$MAVEN_HOME/bin + +# http 代理 +# 如果虚拟机连入的公司网络需要设置代理,可在此处配置 +# export http_proxy=**** +# export https_proxy=**** +# export no_proxy="****" + +# nvm +export NVM_DIR="$HOME/.nvm" +[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm +[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion diff --git a/codes/deploy/update-code.sh b/codes/deploy/update-code.sh new file mode 100644 index 0000000..2369b55 --- /dev/null +++ b/codes/deploy/update-code.sh @@ -0,0 +1,100 @@ +#!/bin/bash + +# zp 源码根目录 +SOURCE_PATH=/home/zp/source + +# +# 检查脚本参数,如必要参数未传入,退出脚本。 +# +checkInput() { + if [ "${repository}" == "" ] || [ "${branch}" == "" ]; + then + echo "请输入脚本参数:repository branch target" + echo " repository: git 仓储(必填)。如 apollo" + echo " branch: git 分支(必填)。如 develop, master" + echo " target: 代码存放目录。默认为脚本所在目录。" + exit 0 + fi +} + +# +# 判断 git 版本库是否存在。根据实际结果修改 ${gitok} 值。 +# +gitok=false +isGitExist() { + cd ${SOURCE_PATH} + if [ -d "./${repository}/${target}" ]; then + cd ./${repository}/${target} + #(1)删除git状态零时文件 + if [ -f "gitstatus.tmp" ]; then + rm -rf gitstatus.tmp + fi + + #(2) 判断是否存在.git目录 + if [ -d "./.git" ]; then + #(3) 判断git是否可用 + git status &> gitstatus.tmp + grep -iwq 'not a git repository' gitstatus.tmp && gitok=false || gitok=true + fi + + #返回到主目录 + cd ${SOURCE_PATH} + fi +} + +# +# 如果 git 版本库存在(根据 ${gitok} 值),执行 fetch 操作;反之,执行 clone 操作。 +# +doFetchOrClone() { + if ${gitok}; then + cd ${target} + git reset --hard + git clean -ffdx + git fetch + echo "git fetch ${repository} remote repository 到本地成功" + else + #删除所有内容,便于重新进行git clone + rm -rf ${repository} + git clone --no-checkout ${repository} ${target} + echo "git clone ${repository} remote repository 到本地成功" + cd ${target} + fi +} + +# +# 切换到 ${branch} 分支 +# +doCheckout() { + echo "检出 ${repository} ${branch} 分支代码" + isRemoteBranch=false + gitRemoteBranch=`git branch -r` + echo -e "$gitRemoteBranch" | grep -iwq ${branch} && isRemoteBranch=true || isRemoteBranch=false + if ${isRemoteBranch}; then + echo "找到 ${branch} 对应的远端分支" + git checkout -f 'origin/'${branch} + else + git checkout -f ${branch} + fi + echo "更新子模块代码" + git submodule update --init --recursive --force +} + +##############################__MAIN__######################################## +export LANG="zh_CN.UTF-8" +repository=`echo $1` +branch=`echo $2` +target=`echo $3` + +# 0. 检查传入的参数 +checkInput + +# 1. 判断本地是否已存在 Git 仓库 +isGitExist + +# 2. 如果本地已有代码,执行 fetch;反之,从远程 clone +doFetchOrClone + +# 3. 切换到指定分支 +doCheckout + +echo "代码检出完成!"