上次简单介绍了利用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
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/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/27181165/viewspace-776386/