How To Manage Your Servers With Rex - Best Practice - Page 2
On this page
Creating The Database Tasks
Change to the database repository.
wks01 Service# cd ../database
wks01 database# touch Rexfile
We will now add our 2 common repositories as an external repository.
wks01 database# svn propedit svn:externals .
Add the following lines into the editor, save and close it.
lib/Common http://svn01/svn/common lib/Service http://svn01/svn/service
After you've set the externals, run an update and commit the new settings.
wks01 database# svn up
wks01 database# svn ci -m "added external repositories"
The svn up command now checks both external repositories for updates. So if anyone adds new services to one of this repositories you will get the new services afert an svn up. Now you can start creating your Rexfile to set up a database server.
set user => "root"; set password => "f00b4r"; include qw/ Common::NTP Service::MySQL /; set group => srvdb => "db01"; task "prepare", group => "srvdb", sub { # run the common ntp task "prepare" Common::NTP::prepare(); # install mysql Service::MySQL::prepare(); # configure mysql Service::MySQL::configure({ key_buffer => "32M", max_allowed_packet => "32M", }); # restart mysql service mysql => "restart"; };
First (in line 1 and 2) we set the authentication to use against the target host (db01). Here you can also use key authentication (see http://rexify.org/howtos/start.html or more information). Then we include the Common::NTP and Service::MySQL module so that we can use the tasks from these modules. Every task gets registered as a perl function, too. So we can just call them like any other perl functions. After we've included the wanted modules, we define a new server group called "srvdb" and add the host "db01" to it. It is possible to add multiple hosts to a group. For example:
set group => srvdb => "db01", "db02", "db03"; set group => srvdb => "db[01..03]";
In line 12 we define our first task named "prepare". This task gets executed on all servers in the group "srvdb". This task calls the "prepare" task of our Common::NTP and Service::MySQL module. After the setup is done, we configure the mysql server in line 20 and restart the mysql service in line 26. That's all. This will install, configure and start the MySQL Server. You can now commit everything back to the repository.
wks01 database# svn add Rexfile
wks01 database# svn ci -m "inital commit of Rexfile"
To set up your database server you can type the following command:
wks01 database# rex prepare
If you want to list the tasks inside a Rexfile you can use the following command.
wks01 database# rex -T
After our database is running we can now create the tasks for the website project.
Creating The Website Tasks
First, change to the website project directory.
wks01 database# cd ../website
wks01 website# touch Rexfile
First we need to define the externals here as well.
wks01 website# svn propedit svn:externals .
Add the following lines into the editor, save and close it.
lib/Common http://svn01/svn/common lib/Service http://svn01/svn/service
After you've set the externals, run an update and commit the new settings.
wks01 database# svn up
wks01 database# svn ci -m "added external repositories"
The svn up command now checks both external repositories for updates. So if anyone adds new services to one of this repositories you will get the new services afert an svn up. Now you can start creating your Rexfile to set up a web server.
# Rexfile set user => "root"; set password => "test"; include qw/ Common::NTP Service::Apache /; set group => srvweb => "web01"; task "prepare", group => "srvweb", sub { # run the common ntp task "prepare" Common::NTP::prepare(); # install apache Service::Apache::prepare(); # configure apache Service::Apache::configure({ timeout => 60, }); # restart apache service apache2 => "restart"; };
Save the file and at it to the repository.
wks01 website# svn add Rexfile
wks01 website# svn ci -m "initial Rexfile"
Now you're ready to set up your webserver with the following command.
wks01 website# rex prepare
After you've set up your database and your webserver you are ready to deploy an application. For example, if you're getting a ZIP or TAR archive from the development team, you can do this by adding a second task to your Rexfile.
task "deploy", group => "srvweb", sub { upload "your-web-application.tar.gz", "/tmp"; extract "/tmp/your-web-application.tar.gz", owner => "www-data", group => "www-data", to => "/var/www"; rm "/tmp/your-web-application.tar.gz"; };