Making Our CI Server into A Vagrant
Now let’s make our CI server into a Vagrant that is provisioned by Puppet. First we need a base box. Since my current theme is using AWS, I will use a CentOS distribution of Linux.
I found a CentOS Jenkins basebox from nrel (which at first glance I took to be RHEL… but not, it’s NREL which stands for National Renewable Energy Laboratory!)
This basebox can be located at this link:
http://nrel.github.io/vagrant-boxes
or Vagrant Cloud: nrel/CentOS-6.5-x86_64
$ vagrant init nrel/CentOS-6.5-x86_64
I modified the Vagrantfile for a custom IP address, just because that’s how I roll. I like to use an IP address and hack my local host file to point to it.
config.vm.network "private_network", ip: "192.168.33.88"
Our CI Server Vagrant box will feature:
Jenkins
PHPUnit
WP-Unit Testing
But first, we have to take care of the home front: the server itself. Let’s whip it into shape.
Clear IP Tables
CentOS has iptables in place to keep the server locked down when you first run it. Down the road, I will refine this to be more nuanced, but for now I’m just going to open all the ports.
exec{'clear_iptables': command => "/sbin/iptables -F", }
Install Apache
package { 'httpd': name => "httpd", ensure => present, } service { "httpd": ensure => "running", require => Package["httpd"], }
Install the Latest WordPress
exec{'download_latest_wordpress': command => "/usr/bin/wget -q http://wordpress.org/latest.tar.gz -O /var/www/html/latest.tar.gz", creates => "/var/www/html/latest.tar.gz", require => Package["httpd"], before => Exec["unpack_latest.tar.gz"], } exec {'unpack_latest.tar.gz': command => "tar xf /var/www/html/latest.tar.gz", cwd => "/var/www/html", path => "/bin", }
Install Java
Let’s get started with installing Java. Puppet considers the default package manager for CentOS to be yum, so since we know the repo exists in yum, we can do it just this easily.
package { "java-1.6.0-openjdk": ensure => "installed" }
Install Jenkins
exec{'download_latest_jenkins_repo': command => "/usr/bin/wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo", creates => "/etc/yum.repos.d/jenkins.repo", require => Package["java-1.6.0-openjdk"], before => Exec["import_jenkins_key"], } exec {'import_jenkins_key': command => "/bin/rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key", cwd => "/home/vagrant", before => Package["jenkins"] } package { "jenkins": ensure => "installed" } service { "jenkins": ensure => "running", require => Package["jenkins"], }
http://pkg.jenkins-ci.org/redhat/
Install PHPUnit
exec{'download_PHP_unit': command => "/usr/bin/wget -O /usr/bin/phpunit.phar https://phar.phpunit.de/phpunit.phar", creates => "/usr/bin/phpunit.phar", before => Exec["run_phpunit_phar_commands"], } exec{'run_phpunit_phar_commands': command => "/bin/chmod +x /usr/bin/phpunit.phar && /bin/mv /usr/bin/phpunit.phar /usr/local/bin/phpunit", cwd => "/usr/bin", }
Install mysql
package { "mysql-server": ensure => "installed", require => Package["httpd"], } service { "mysqld": enable => true, ensure => running, require => Package["mysql-server"], before => Exec["wait-for-mysqld"] } exec {"wait-for-mysqld": require => Service["mysqld"], command => "/usr/bin/mysql -u root --password= -e \"create database wp_test_database;grant all on wp_test_database.* to 'wptestuser'@'localhost' identified by 'vagrant';\"", } package { "php-mysql": ensure => "installed", require => Package["mysql-server"], }
Install PHP Cli
package { "php-cli": ensure => "installed", require => Package["php-mysql"], }
Install SVN
package { "mod_dav_svn": ensure => "installed", require => Package["php-cli"], }
Install WP Unit Testing
file { "/home/vagrant/svn": ensure => "directory", } file { "/home/vagrant/svn/wordpress-dev": ensure => "directory", } exec {"checkout-svn-wordpress-test": command => "/usr/bin/svn co http://develop.svn.wordpress.org/trunk/", cwd => "/home/vagrant/svn/wordpress-dev/", before => Exec["copy-wp-tests-config-sample"] } exec {"copy-wp-tests-config-sample": command => "/bin/cp wp-tests-config-sample.php wp-tests-config.php", cwd => "/home/vagrant/svn/wordpress-dev/trunk", before => Exec["configure-wp-tests-config-sample"] } exec {"configure-wp-tests-config-sample": command => "/bin/sed -i 's%youremptytestdbnamehere%wp_test_database%g' wp-tests-config.php && sudo sed -i 's%yourusernamehere%wptestuser%g' wp-tests-config.php && sudo sed -i 's%yourpasswordhere%vagrant%g' wp-tests-config.php", cwd => "/home/vagrant/svn/wordpress-dev/trunk", before => Package["php-xml"] }
So now we’re done, right?
[vagrant@localhost trunk]$ phpunit PHP Fatal error: Class 'DOMDocument' not found in phar:///usr/local/bin/phpunit/phpunit/Util/XML.php on line 146
Not so fast! Unfortunately, our local CentOS install doesn’t quite match our AWS Linux AMI. When I referenced the above error online, I quickly learned that it is the result of not having the php-xml module installed. So we have to add this to our manifest:
package { "php-xml": ensure => "installed", require => Exec["configure-wp-tests-config-sample"] }