编写任务

到了工作站,现在你可以检出版本库。

wks01# svn co http://svn01/svn/common Common

wks01# svn co http://svn01/svn/service Service

wks01# svn co http://svn01/svn/database

wks01# svn co http://svn01/svn/website

首先,我们添加一个常见任务来建立NTP。之后你可以添加其他常见任务。所以,更换到Common目录,建立一个名为NTP.pm的文件。

wks01# cd Common

# Common/NTP.pm

package Common::NTP;

use Rex -base;

task prepare => sub {

install "ntp";

file "/etc/ntp.conf",

source => "files/ntp.conf",

on_change => sub {

service ntp => "restart";

};

};

1;

这建立了一个名为“prepare”的任务。该任务登记在“Namespace”NTP中。如果软件包“ntp”之前还没有安装,该任务将安装该软件包,并将配置文件上传至服务器。要是文件的内容发生变化,它会重新启动ntp服务。现在,你需要创建ntp.conf文件。

bash Common# mkdir files

把上面这行粘贴到文件files/ntp.conf中。这是一个简单的默认ntp.conf文件。当然,你可以更改该文件以满足自己的要求。

# /etc/ntp.conf,由rex来管理。

driftfile /var/lib/ntp/ntp.drift

statistics loopstats peerstats clockstats

filegen loopstats file loopstats type day enable

filegen peerstats file peerstats type day enable

filegen clockstats file clockstats type day enable

server 0.ubuntu.pool.ntp.org

server 1.ubuntu.pool.ntp.org

server 2.ubuntu.pool.ntp.org

server 3.ubuntu.pool.ntp.org

# 使用Ubuntu的ntp服务器作为应急后备机制。

server ntp.ubuntu.com

restrict -4 default kod notrap nomodify nopeer noquery

restrict -6 default kod notrap nomodify nopeer noquery

# 本地用户可能更密切地询问ntp服务器。

restrict 127.0.0.1

restrict ::1

就开始而言,这足够了。所以,现在你可以把新文件添加到版本库。

wks01 Common# svn add NTP.pm files

wks01 Common# svn ci -m "added NTP task"

现在,我们将为服务库添加内容。

wks01 Common# cd ../Service

wks01 Service# touch Apache.pm MySQL.pm

现在,把下列代码粘贴到Apache.pm模块。

package Service::Apache;

use Rex -base;

task prepare => sub {

install "apache2";

};

task configure => sub {

my $param = shift;

file "/etc/apache2/apache2.conf",

owner => "root",

mode => 644,

content => template("templates/apache2/apache2.conf.tpl", %{ $param });

file "/etc/apache2/conf.d/security",

owner => "root",

mode => 644,

content => template("templates/apache2/conf.d/security.tpl", %{ $param });

};

1;

并创建由该模块使用的模板。

wks01 Service# mkdir -p templates/apache2/conf.d

templates/apache2/apache2.conf.tpl的内容如下。我已去掉了所有注释。

LockFile /var/run/apache2/accept.lock

PidFile /var/run/apache2.pid

Timeout

KeepAlive

MaxKeepAliveRequests

KeepAliveTimeout

StartServers 5

MinSpareServers 5

MaxSpareServers 10

MaxClients 150

MaxRequestsPerChild 0

StartServers 2

MinSpareThreads 25

MaxSpareThreads 75

ThreadLimit 64

ThreadsPerChild 25

MaxClients 150

MaxRequestsPerChild 0

StartServers 2

MinSpareThreads 25

MaxSpareThreads 75

ThreadLimit 64

ThreadsPerChild 25

MaxClients 150

MaxRequestsPerChild 0

User

Group

AccessFileName .htaccess

Order allow,deny

Deny from all

Satisfy all

DefaultType None

HostnameLookups

ErrorLog

LogLevel

Include mods-enabled/*.load

Include mods-enabled/*.conf

Include httpd.conf

Include ports.conf

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined

LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

LogFormat "%h %l %u %t \"%r\" %>s %O" common

LogFormat "%{Referer}i -> %U" referer

LogFormat "%{User-agent}i" agent

Include conf.d/

Include sites-enabled/

templates/apache2/conf.d/security.tpl的内容如下。我已去掉了所有注释。

ServerTokens

ServerSignature

TraceEnable

现在我们继续使用MySQL模块。打开文件MySQL.pm,添加下列内容。

package Service::MySQL;

use Rex -base;

task prepare => sub {

install "mysql-server";

};

task configure => sub {

my $param = shift;

file "/etc/mysql/my.cnf",

owner => "root",

mode => 644,

content => template("templates/mysql/my.cnf.tpl", %{ $param });

};

1;

另外创建文件templates/mysql/my.cnf.tpl。

[mysqld]

user =

pid-file = /var/run/mysqld/mysqld.pid

socket = /var/run/mysqld/mysqld.sock

port =

basedir = /usr

datadir = /var/lib/mysql

tmpdir = /tmp

lc-messages-dir = /usr/share/mysql

skip-external-locking

bind-address =

key_buffer =

max_allowed_packet =

thread_stack =

thread_cache_size =

myisam-recover = BACKUP

query_cache_limit =

query_cache_size =

expire_logs_days =

max_binlog_size =

[mysqldump]

quick

quote-names

max_allowed_packet =

[mysql]

[isamchk]

key_buffer =

!includedir /etc/mysql/conf.d/

现在,添加所有文件到版本库。

wks01 Service# svn add *

wks01 Service# svn ci -m "inital commit of apache and mysql service"

好了,现在你已有了第一批常见模块。现在,可以为数据库项目创建任务了。


相关内容