前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >公有云镜像模版的创建<二>

公有云镜像模版的创建<二>

作者头像
明哥的运维笔记
发布2019-01-30 09:55:12
2K0
发布2019-01-30 09:55:12
举报
文章被收录于专栏:运维笔记

代码语言:javascript
复制
base_init.sh  <!-- 自动修改主机名/更改编码/本来准备加内核优化,后来放弃,原因省略1000字-->
#/bin/bash 
#Changes in the system initialization
#Some args need to Change!!
Localdir=`pwd`
BACKUP=$Localdir/backup
CONF=$Localdir/sys.conf
shellname=$(basename $0)

#Determine whether you are in the right position
if [[ !  -f  $Localdir/$shellname  ]];then
echo "Please cd to the directory location of the script at the same level,Exit"
exit
fi

[[ -d $BACKUP  ]] || mkdir -p $BACKUP
. $CONF
function gennip ()
{
re=`echo $IP | awk -F. '{printf "%d",$1*256^3+$2*256^2+$3*256+$4}'`
echo "$re"
}

function config_hostname()
{
IPADDR=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
for IP in $IPADDR
do
ipn=`gennip $IP`
if [ $ipn -ge 167772160 -a $ipn -le 184549376 -o $ipn -ge 2130706432 -a $ipn -le 2147483648 -o $ipn -ge 2886729728 -a $ipn -le 2887778304 -o $ipn -ge 3232235520 -a $ipn -le 3232301056 ]; then
IPP=`echo $IP|awk -F'.' '{print $NF}'`
echo "-----Starting configure the Hostname!"
        local _HOST_NAME=$base_hostname$IPP
        
        cp -fpv /etc/sysconfig/network $BACKUP
        
        if grep "^HOSTNAME=" /etc/sysconfig/network > /dev/null 2>&1
        then
                sed -i '/^HOSTNAME=.*$/d' /etc/sysconfig/network
        fi
        
        hostname $_HOST_NAME
        echo "HOSTNAME=$_HOST_NAME" >> /etc/sysconfig/network
        . /etc/sysconfig/network
        if ! grep  "$_HOST_NAME"  /etc/hosts |grep "$IP" > /dev/null 2>&1
then
echo "$IP $_HOST_NAME"  >> /etc/hosts
fi
echo "-----Configure hostname Have done!"
fi
done 
}

config_lang()
{
if ! grep  "UTF-8"  /etc/sysconfig/i18n > /dev/null 2>&1
then
cp -fpv /etc/sysconfig/i18n $BACKUP
sed -i 's/^LANG=.*$/LANG="en_US.UTF-8"/' /etc/sysconfig/i18n
. /etc/sysconfig/i18n
fi
}

config_network_param()
{
   if ! grep "synack_retries" /etc/sysctl.conf > /dev/null 2>&1
   then
cp -fpv /etc/sysctl.conf /etc/rc.local $BACKUP
echo "" >> /etc/sysctl.conf
echo  "
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
fs.file-max = 6553500
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.eth0.secure_redirects = 1
net.ipv4.conf.lo.secure_redirects = 1
net.ipv4.conf.default.secure_redirects = 1
net.ipv4.conf.all.secure_redirects = 1
net.ipv4.conf.eth0.accept_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
net.ipv4.conf.lo.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1800
net.core.wmem_max = 8388608
net.core.rmem_max = 8388608
net.ipv4.tcp_rmem = 4096 873814 8738140
net.ipv4.tcp_wmem = 4096 873814 8738140
net.ipv4.tcp_max_syn_backlog = 4096
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1" >> /etc/sysctl.conf
sysctl -p
fi
}

config_hostname
config_lang
#config_network_param

if  grep base_init.sh /etc/rc.d/rc.local  > /dev/null 2>&1
then
sed -i '/base_init.sh/ {s/^/#/}'  /etc/rc.d/rc.local
fi 

sysinit.sh  <!--自定义初始化镜像脚本,自动安装jdk/nginx/iftop/ifstat/zabbix/puppet/salt/log.io等常用  -->
#!/bin/bash 
#Add by GM.H
#Create time 2014-04-03
#System Initialization templates

Base_dir=`pwd`
Tmp_dir=$Base_dir/tmp
Install_log=$Base_dir/install.log
Sys_base_shell=$Base_dir/base_init.sh
Conf=$Base_dir/sys.conf
shellname=$(basename $0)

#Determine whether you are in the right position
if [[ !  -f  $Base_dir/$shellname  ]];then
echo "Please cd to the directory location of the script at the same level,Exit"
exit
fi
###Define some base pakgs!!
defile_pakges="
$Base_dir/pakgs/bw_resin_install.tar.gz
$Base_dir/pakgs/bw_tomcat_install.tar.gz
$Base_dir/pakgs/bw_tomcat7.0_install.tar.gz
$Base_dir/pakgs/bw_nginx_install.zip
$Base_dir/pakgs/bw_nginx_install.tar.gz
$Base_dir/pakgs/jdk1.6.0_25.tar.gz
$Base_dir/pakgs/jdk-7u17-linux-x64.tar.gz
$Base_dir/pakgs/iftop.tar.gz
$Base_dir/pakgs/ifstat-1.1.tar.gz
$Base_dir/pakgs/bw_zabbixclient_install.tar.gz
$Base_dir/pakgs/bw_puppetclient_install.tar.gz
$Base_dir/pakgs/epel-release-5-4.noarch.rpm
$Base_dir/pakgs/epel-release-6-8.noarch.rpm
$Base_dir/pakgs/log.io.tar.gz
$Base_dir/pakgs/mysqlbk_init.zip
$Base_dir/pakgs/bw_backup_install.tar.gz
$Base_dir/pakgs/bw_mysql5.6_install.zip
$Base_dir/pakgs/bw_mysql_install.tar.gz
"

. $Conf
datef() { date "+%Y/%m/%d %H:%M" ; }
print_to_log() {
echo "$1" 
echo "[$(datef)] $1" >> $Install_log ;}

[[ -d $Tmp_dir ]] || mkdir  -p $Tmp_dir
> $Install_log
#functions for System Initialization templates 

#1.Define Hostname 
define_hostname()
{
if ! grep base_init.sh  /etc/rc.d/rc.local > /dev/null 2>&1 ;then
echo "/bin/bash $Sys_base_shell > /dev/null 2>&1"  >> /etc/rc.d/rc.local
/bin/bash $Sys_base_shell > /dev/null 2>&1
print_to_log "$FUNCNAME(): have finished!"
fi
}

#2.Define resin/tomcat/mysql/nginx-0.7.65 && Install nginx-1.0.6 and jdk
confirm_pakges()
{
#confirm pakges
for i in $defile_pakges
do
if [[ ! -f $i   ]];then
print_to_log "$FUNCNAME(): $i is not exist!!"
fi 
done
print_to_log "$FUNCNAME(): resin/tomcat/mysql/nginx-0.7.65 pakges check have finished!"
#nginx
if [[ ! -d /usr/local/nginx  ]];then
[[ ! -d $Tmp_dir/bw_nginx_install ]] ||  rm -rf  $Tmp_dir/bw_nginx_install
tar -zxf $Base_dir/pakgs/bw_nginx_install.tar.gz  -C $Tmp_dir
cd $Tmp_dir/bw_nginx_install
/bin/bash init.sh  > /dev/null 2>&1 
print_to_log  "$FUNCNAME(): Nginx-1.0.6 have install successfully!"
else
print_to_log  "$FUNCNAME(): Nginx have already Intalled!"
fi
#jdk
if [[ ! -d /usr/local/jdk  ]];then
if [[ ! -d  /usr/local/jdk1.7.0_17  ]];then
tar -zxf $Base_dir/pakgs/jdk-7u17-linux-x64.tar.gz  -C /usr/local/
fi
ln -s /usr/local/jdk1.7.0_17  /usr/local/jdk
print_to_log  "$FUNCNAME(): jdk1.7.0_17 have install successfully!"
else
print_to_log  "$FUNCNAME(): Jdk1.7.0_17 have already Intalled!"
fi
if !  grep "/usr/local/jdk/bin"  /etc/profile > /dev/null 2>&1 ;then
echo "export  PATH=/usr/local/jdk/bin:$PATH"  >> /etc/profile
fi
}

#3.Install some Common package
Install_com_pakges()
{
#iftop
iftop -h > /dev/null 2>&1
if [[ $? -ne 0 ]];then
[[  -d $Tmp_dir/iftop ]] || tar -zxf $Base_dir/pakgs/iftop.tar.gz  -C $Tmp_dir 
cd $Tmp_dir/iftop
/bin/bash iftop_init.sh > /dev/null 2>&1
print_to_log  "$FUNCNAME(): iftop have installed successfully!" 
else
print_to_log  "$FUNCNAME(): iftop have already installed!"
fi
#ifstat
ifstat -h > /dev/null 2>&1
if [[ $? -ne 0 ]];then
[[  -d $Tmp_dir/ifstat-1.1 ]] || tar -zxf $Base_dir/pakgs/ifstat-1.1.tar.gz  -C $Tmp_dir
cd $Tmp_dir/ifstat-1.1
/bin/bash ifstat_init.sh > /dev/null 2>&1
print_to_log  "$FUNCNAME(): ifstat have installed successfully!"
else
print_to_log  "$FUNCNAME(): ifstat have already installed!"
fi
#zabbix
if [[ ! -f /etc/zabbix/zabbix_agentd.conf ]];then
[[ ! -d $Tmp_dir/bw_zabbixclient_install ]] ||  rm -rf  $Tmp_dir/bw_zabbixclient_install
[[  -d $Tmp_dir/bw_zabbixclient_install ]] || tar -zxf $Base_dir/pakgs/bw_zabbixclient_install.tar.gz  -C $Tmp_dir
cd $Tmp_dir/bw_zabbixclient_install
/bin/bash install.sh  > /dev/null 2>&1
sed -i "s/192.168.100.241/$zabbix_server/" /etc/zabbix/zabbix_agentd.conf
sed -i "s/Zabbix server/`hostname`/" /etc/zabbix/zabbix_agentd.conf
chkconfig zabbix_agentd  off
print_to_log  "$FUNCNAME(): zabbixclient have installed successfully!"
else
print_to_log  "$FUNCNAME(): zabbixclient have already installed!"
fi
#puppet
if [[ ! -f /etc/puppet/puppet.conf  ]];then
[[ ! -d $Tmp_dir/bw_puppetclient_install ]] ||  rm -rf  $Tmp_dir/bw_puppetclient_install
[[  -d $Tmp_dir/bw_puppetclient_install ]] || tar -zxf $Base_dir/pakgs/bw_puppetclient_install.tar.gz  -C $Tmp_dir
cd $Tmp_dir/bw_puppetclient_install
/bin/bash install.sh > /dev/null 2>&1
sed -i "s/V**server/$puppet_server/" /etc/puppet/puppet.conf 
chkconfig puppet off
print_to_log  "$FUNCNAME(): puppetclient have installed successfully!"
else
print_to_log  "$FUNCNAME(): puppetclient have already installed!"
fi
#salt-minion   
if [[ ! -d  /etc/salt ]];then
if  grep "6." /etc/redhat-release > /dev/null 2>&1 ;then  
rpm -ivh $Base_dir/pakgs/epel-release-6-8.noarch.rpm  > /dev/null 2>&1
 yum install salt-minion  -y  > /dev/null 2>&1
else
rpm -ivh $Base_dir/pakgs/epel-release-5-4.noarch.rpm  > /dev/null 2>&1
yum install salt-minion  -y    > /dev/null 2>&1
fi
if ! grep "^master:"  /etc/salt/minion > /dev/null 2>&1 ;then
echo "master:  $salt_server" >> /etc/salt/minion
fi
if ! grep "^id"  /etc/salt/minion > /dev/null 2>&1  ;then
echo "id:  `hostname`" >> /etc/salt/minion
fi
chkconfig salt-minion off
print_to_log  "$FUNCNAME(): salt-minion  have installed successfully!"
else
print_to_log  "$FUNCNAME(): salt-minion  have already installed!"
fi
#log.io
if [[ ! -d ~/.log.io/  ]];then
[[  -d $Tmp_dir/log.io ]] || tar -zxf $Base_dir/pakgs/log.io.tar.gz  -C $Tmp_dir
cd  $Tmp_dir/log.io
/bin/bash logio_install.sh > /dev/null 2>&1 
chkconfig log.io-harvester  off
chkconfig log.io-server off
print_to_log  "$FUNCNAME(): log.io have installed successfully!"
else
print_to_log  "$FUNCNAME(): log.io have already installed!"
fi
}
#4. Install some usefull 
install_other_pakges()
{
#mysqlbk
if [[ ! -d  /data/bw_mon/bw_mysqlbk ]] ; then
[[ ! -d $Tmp_dir/mysqlbk_init ]] ||  rm -rf  $Tmp_dir/mysqlbk_init
[[  -d $Tmp_dir/mysqlbk_init  ]] || unzip  $Base_dir/pakgs/mysqlbk_init.zip  -d $Tmp_dir
cd $Tmp_dir/mysqlbk_init 
/bin/bash initbk.sh > /dev/null 2>&1
print_to_log  "$FUNCNAME(): mysqlbk  have installed successfully!"
else 
print_to_log  "$FUNCNAME(): mysqlbk   have already installed!"
fi
#backup
if [[ ! -d  /data/bw_mon/bw_backup ]] ; then
[[ ! -d $Tmp_dir/bw_backup_install ]] ||  rm -rf  $Tmp_dir/bw_backup_install
[[  -d $Tmp_dir/bw_backup_install  ]] || tar -zxf  $Base_dir/pakgs/bw_backup_install.tar.gz  -C $Tmp_dir
cd  $Tmp_dir/bw_backup_install
/bin/bash init.sh > /dev/null 2>&1
print_to_log  "$FUNCNAME(): backup  have installed successfully!"
else
print_to_log  "$FUNCNAME(): backup  have already installed!"
fi
}

define_hostname
confirm_pakges
Install_com_pakges
install_other_pakges

######nginx tomcat resin mysql等源码自动安装脚本后续更新!############################

文章作者

明哥

文章地址

创建时间

2014-04-08

关注订阅

微信订阅号

开源项目

工具市场

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-04-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档