管理 Drupal 站点

Drupal 是一个内容管理系统,它通过插拔组装一系列罐装的模块让你快速构建网站, 它使用户创建和编辑自己的内容变的相对容易。 Drupal 特别适合使用 Puppet 来管理,因为有一个强大的命令行工具 drush, 你可以使用这个工具安装、管理 Drupal 站点。

如果我们将自动化管理 Drupal 站点的 drush 工具与我们已经创建的用于管理 MySQL 数据库和 Apache 虚拟主机的 Puppet 处方相结合, 就可以使用单一资源创建一个安装 Drupal 站点所需一切的新处方。

准备工作

  1. 创建一个新的 drupal 模块如下:

    # mkdir /etc/puppet/modules/drupal
    # mkdir /etc/puppet/modules/drupal/manifests
    
  2. 使用如下内容创建 /etc/puppet/modules/drupal/manifests/init.pp 文件:

    class drupal {
        $drupalversion = "7.2"
    
        exec { "download-drush":
            cwd => "/root",
            command => "/usr/bin/wget http://ftp.drupal.org/files/
     projects/drush-7.x-4.4.tar.gz ",
            creates => "/root/drush-7.x-4.4.tar.gz",
            require => Package["php5-mysql"],
        }
    
        exec { "install-drush":
            cwd     => "/usr/local",
            command => "/bin/tar xvzf /root/drush-7.x-4.4.tar.gz",
            creates => "/usr/local/drush",
            require => Exec["download-drush"],
        }
    
        file { "/usr/local/bin/drush":
            ensure => link,
            target => "/usr/local/drush/drush",
            require => Exec["install-drush"],
        }
    
        exec { "install-drupal":
            cwd     => "/var/www",
            command => "/usr/local/drush/drush dl drupal-
      ${drupalversion}",
            creates => "/var/www/drupal-${drupalversion}",
            require => Exec["install-drush"],
        }
    
        file { "/var/www/drupal":
            ensure => link,
            target => "/var/www/drupal-${drupalversion}",
            require => Exec["install-drupal"],
        }
    
        package { [ "libapache2-mod-php5",
                    "php5-mysql" ]: ensure => installed }
    
        exec { "enable-mod-php5":
            command => "/usr/bin/a2enmod php5",
            creates => "/etc/apache2/mods-enabled/php5.conf",
            require => Package["libapache2-mod-php5"],
        }
    }
    

操作步骤

  1. 在 init.pp 文件的 drupal 类中添加如下内容:

    define site( $password, $sitedomain = "" ) {
        include drupal
    
        if $sitedomain == "" {
            $drupal_domain = $name
        } else {
            $drupal_domain = $sitedomain
        }
    
        $dbname = regsubst( $drupal_domain, "\.", "" )
        mysql::server::db { $dbname:
            user => $dbname,
            password => $password,
        }
    
        exec { "site-install-${name}":
            cwd => "/var/www/drupal",
            command => "/usr/local/bin/drush site-install -y
     --site-name=${name} --sites-subdir=${drupal_domain}
     --db-url=mysql://${dbname}:${password}@localhost/${dbname}",
            creates => "/var/www/drupal/sites/${drupal_domain}",
            require => [ File["/var/www/drupal"], Exec["install-drupal"],
             Mysql::Server::Db[$dbname] ],
            logoutput => on_failure,
        }
    
        apache::site { $drupal_domain:
            documentroot => "/var/www/drupal",
        }
    }
    
  2. 添加如下内容到一个节点:

    drupal::site { "crispinfo.com":
        password => "crunch",
    }
    
  3. 运行 Puppet:

    # puppet agent --test
    info: Retrieving plugin
    info: Caching catalog for cookbook.bitfieldconsulting.com
    info: Applying configuration version '1309783783'
    
    notice: /Stage[main]//Node[cookbook]/Drupal::Site[crispinfo.com]/
    Mysql::Server::Db[crispinfocom]/Exec[create-crispinfocom-db]/
    returns: executed successfully
    
    notice: /Stage[main]//Node[cookbook]/Drupal::Site[crispinfo.com]/
    Apache::Site[crispinfo.com]/File[/etc/apache2/sites-available/
    crispinfo.com.conf]/ensure: defined content as '{md5}15c5bbffa6128
    fce0b8a3996914af549'
    
    info: /Stage[main]//Node[cookbook]/Drupal::Site[crispinfo.com]/
    Apache::Site[crispinfo.com]/File[/etc/apache2/sites-available/
    crispinfo.com.conf]: Scheduling refresh of Exec[enable-crispinfo.
    com-vhost]
    
    notice: /Stage[main]//Node[cookbook]/Drupal::Site[crispinfo.com]/
    Apache::Site[crispinfo.com]/Exec[enable-crispinfo.com-vhost]:
    Triggered 'refresh' from 1 events
    
    info: /Stage[main]//Node[cookbook]/Drupal::Site[crispinfo.com]/
    Apache::Site[crispinfo.com]/Exec[enable-crispinfo.com-vhost]:
    Scheduling refresh of Service[apache2]
    
    notice: /Stage[main]/Apache/Service[apache2]: Triggered 'refresh'
    from 1 events
    
    notice: /Stage[main]//Node[cookbook]/Drupal::Site[crispinfo.com]/
    Exec[site-install-crispinfo.com]/returns: executed successfully
    notice: Finished catalog run in 22.51 seconds
    
  4. 在 /etc/hosts 文件中创建一个条目将 crispinfo.com 指向你正在使用的节点 IP (如果还没设置 DNS):

    10.0.2.15 crispinfo.com
    
  5. 在浏览器中检查站点,以确保一切都已经正确创建。你应该看到 Drupal 的登录提示,在下图所示:

    img/ch07sec05.png

    使用由 drush site-install 创建的默认的管理员登录,用户名为 admin 其口令为 admin。 显然你应该为实际生产线上的站点设置强壮的口令 (查看 drush 文档获得如何使用命令行工具设置的信息)。

工作原理

真是神奇呀!尤其是 drupal 类首先安装 drush,然后使用它安装 Drupal 的核心代码 (你可以通过修改 $drupalversion 的值改变版本)。

drupal::sitedefine 为你想要创建的每个站点运行 drush site-install 。 在我们的例子中,创建了一个名为 crispinfo.com 的站点并为其传递了站点数据库使用的口令, 其余的工作都由 drush 去完成。

drupal::site 也为我们的站点创建了所需的 Apache 虚拟主机 (使用本章 创建 Apache 虚拟主机 一节中的处方) 和 MySQL 数据库 (使用本章 创建 MySQL 数据库及用户 一节中的处方)。

更多用法

要管理 Drupal 站点,drush 可以帮你做很多事,包括更新 Drupal 的核心代码、 安装模块和主题模板、管理用户以及备份站点数据库等。 你可以在 http://drush.ws/ 找到更多关于 drush 的信息。