puppet之mysql批量安裝案例

wang_0720發表於2013-11-12
上次簡單介紹了利用puppet批量安裝java的案例,這次來說說puppet自動化安裝mysql。前提是配置好了puppet的服務端和客戶端,puppet安裝配置參考blog http://space.itpub.net/27181165/viewspace-776318/.
mysql模組
[root@master manifests]# tree /etc/puppet/modules/mysql/manifests/
/etc/puppet/modules/mysql/manifests/
|-- add_user.pp
|-- init.pp
`-- install.pp

0 directories, 3 files
在mysql模組的manifests中新建幾個檔案,add_user.pp建立mysql使用者,init.pp puppet初始化檔案,install.pp mysql安裝檔案,各個檔案具體配置如下
初始化檔案
[root@master manifests]# cat init.pp
class mysql {
    include mysql::add_user
    include mysql::install
}
init.pp是每個puppet模組的必需的初始化檔案。包含有模組中定義的各個類。
[root@master manifests]# cat add_user.pp
class mysql::add_user {
    group {"mysql":
        ensure => "present",
        gid => 501,
        name => "mysql";
        }
    user {"mysql":
        ensure => "present",
        uid =>501,
        gid =>501,
        home => "/home/mysql",
        managehome=> "true";
        }
    file {"/home/mysql":
        owner => 501,
        group => 501,
        mode => 700,
        ensure => directory;
        }
}
在add_user.pp中,我建立了一個mysql組,組名為mysql,gid為501。建立了一個mysql使用者,指定了uid,gid及mysql的家目錄,用file資源給mysql家目錄賦予一定的許可權。
[root@master manifests]# cat install.pp
class mysql::install {
    file { "/usr/local/src/mysql-5.1.62.tar.gz":
        owner =>root,
        group =>root,
        mode =>644,
        source =>"puppet://$puppetserver/files/mysql-5.1.62.tar.gz",
        }
    
    file { "/usr/local/src/myinstall.sh":
          owner =>root,
          group =>root,
          mode =>755,
          source =>"puppet://$puppetserver/files/myinstall.sh",
          require => File["/usr/local/src/mysql-5.1.62.tar.gz"],
                }

    exec {"build_mysql":
         cwd =>"/usr/local/src",
         path =>"/bin:/usr/bin:/sbin:/usr/bin",
         creates => "/usr/local/mysql",
         command =>"/bin/sh /usr/local/src/myinstall.sh",
         timeout => "0",
         require => File["/usr/local/src/myinstall.sh"],
        }
}
install.pp檔案是用來安裝mysql的,其中定義了2個file資源,一個是從puppet服務端取mysql安裝包的,另一個是取mysql安裝指令碼的。exec是執行mysql安裝指令碼安裝mysql。我把所有獲取到的檔案都放在/usr/local/src下。myinstall.sh是依賴於/usr/local/src/mysql-5.1.62.tar.gz的,因為,只有mysql-5.1.62.tar.gz檔案存在,myinstall.sh才有存在的意義。exec資源中,用creates判斷如果存在/usr/local/mysql說明mysql已經安裝過了,無需執行exec。安裝mysql的過程可能需要一些時間,因此在exec中設定了一個timeout超時時間,timeout=0表示時間不做限制。同樣的exec是依賴於myinstall.sh這個檔案的。
mysql安裝指令碼
mysql安裝指令碼放在puppet的檔案資源庫中,方便客戶端獲取
[root@master files]# cat /etc/puppet/files/myinstall.sh
#!/bin/bash
#Author: Andy
#Time: 20130905
#File:/etc/puppet/files/myinstall.sh
FILE_PATH=/usr/local/src
GROUP=mysql
USER=mysql
PORT=3306
VERSION=5.1.62
MYSQL_BASE=/usr/local/mysql
DATA_HOME=/var/lib/mysql
function add_user ()
{
cat /etc/group|grep "${GROUP}"
if [ $? -ne 0 ];then
    echo "mysql group is not exist,create" >/tmp/mysqlinstall
    /usr/sbin/groupadd -g 501 ${GROUP}
    if [ $? -eq 0 ];then
        echo "mysql group is created" >>/tmp/mysqlinstall
    fi
   else
    echo "mysql group is exist">> /tmp/mysqlinstall
fi
id mysql >/dev/null 2>&1
if [ $? -ne 0 ];then
    echo "the user mysql is not exist,create" >>/tmp/mysqlinstall
    /usr/sbin/useradd -u 501 -g ${GROUP} ${USER}
    if [ $? -eq 0 ];then
        echo "user mysql is created" >>/tmp/mysqlinstall
    fi
   else
    echo "mysql user is exist" >>/tmp/mysqlinstall
fi
}

function uncomp_tar ()
{
ls `pwd` |grep -w ^mysql-5.1.62$
if [ $? -ne 0 ];then
    echo "uncompress mysql target package" >/tmp/mysqlinstall
    tar -zxvf mysql-${VERSION}.tar.gz
    echo "uncompress target package successful">>/tmp/mysqlinstall
    sleep 5
fi
}

function configure()
{
echo "mysql configure" >>/tmp/mysqlinstall
cd ${FILE_PATH}/mysql-${VERSION}
sh configure --prefix=${MYSQL_BASE} \
    --sysconfdir=/etc \
    --localstatedir=${DATA_HOME} \
    --with-tcp-port=${PORT} \
    --with-unix-socket-path=/tmp/mysqld.sock \
    --with-mysqld-user=${USER} \
    --with-plugins=innobase,myisam #innobase要求記憶體大於2GB
echo "mysql configure completed">>/tmp/mysqlinstall
sleep 5
}

function makein()
{
echo "start make">>/tmp/mysqlinstall
cd ${FILE_PATH}/mysql-${VERSION}
make clean
make
echo "make completed">>/tmp/mysqlinstall
sleep 5
echo "start make install">>/tmp/mysqlinstall
make install
echo "make install completed">>/tmp/mysqlinstall
sleep 5
}

function setup()
{
echo "mysql setup" >>/tmp/mysqlinstall
sleep 5
cp ${MYSQL_BASE}/share/mysql/my-innodb-heavy-4G.cnf /etc/my.cnf
sed -i '/storage-engine/ s/MYISAM/INNODB/' /etc/my.cnf
sed -i '/rehash/ s/no-auto-rehash/auto-rehash/' /etc/my.cnf
cp ${MYSQL_BASE}/share/mysql/mysql.server /etc/rc.d/init.d/mysqld
chmod 700 /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
chown -R ${USER} ${MYSQL_BASE}
chgrp -R ${GROUP} ${MYSQL_BASE}
echo "mysql setup successful" >>/tmp/mysqlinstall
}

function initial()
{
echo "mysql initail" >>/tmp/mysqlinstall
sleep 5
${MYSQL_BASE}/bin/mysql_install_db --user=mysql
chown -R root ${MYSQL_BASE}
${MYSQL_BASE}/bin/mysqld_safe --user=mysql &
echo "mysql enviroment configure" >>/tmp/mysqlinstall
cat /etc/profile|grep mysql >/dev/null
if [ $? -ne 0 ];then
echo "export PATH=\$PATH:${MYSQL_BASE}/bin" >>/etc/profile
fi
source /etc/profile
echo "mysql install completed" >>/tmp/mysqlinstall
}
#===============================================
add_user
uncomp_tar
configure
makein
setup
initial


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27181165/viewspace-776386/,如需轉載,請註明出處,否則將追究法律責任。

相關文章