| 1 |
<?php
|
| 2 |
|
| 3 |
function provisionator_page() {
|
| 4 |
$output = t('');
|
| 5 |
|
| 6 |
$output .= t('<p>This module provides utilities to easily create, deploy, and manage sites in a shared Drupal hosting environment.</p>');
|
| 7 |
|
| 8 |
// DEPLOY NEW SITE
|
| 9 |
$form['deploy'] = array(
|
| 10 |
'#type' => 'fieldset',
|
| 11 |
'#title' => t('Deploy new site'),
|
| 12 |
'#tree' => TRUE,
|
| 13 |
'#collapsible' => TRUE,
|
| 14 |
'#collapsed' => TRUE,
|
| 15 |
);
|
| 16 |
|
| 17 |
$form['deploy']['sitename'] = array(
|
| 18 |
'#type' => 'textfield',
|
| 19 |
'#title' => 'Site Name',
|
| 20 |
'#size' => 30,
|
| 21 |
'#maxlength' => 255,
|
| 22 |
'#required' => TRUE,
|
| 23 |
'#default_value' => variable_get('sitename',''),
|
| 24 |
'#description' => t('The title for the site, to be used in the site, and in lists of Drupal sites.'),
|
| 25 |
);
|
| 26 |
|
| 27 |
$form['deploy']['shortname'] = array(
|
| 28 |
'#type' => 'textfield',
|
| 29 |
'#title' => 'Short Name',
|
| 30 |
'#size' => 30,
|
| 31 |
'#maxlength' => 64,
|
| 32 |
'#required' => TRUE,
|
| 33 |
'#default_value' => variable_get('shortname',''),
|
| 34 |
'#description' => t('The shortname for the site, to be used in the URL and database.'),
|
| 35 |
);
|
| 36 |
|
| 37 |
$profiles = drupal_map_assoc(getProvisionatorProfiles(), NULL);
|
| 38 |
$form['deploy']['profile'] = array(
|
| 39 |
'#type' => 'select',
|
| 40 |
'#title' => t('Profile'),
|
| 41 |
'#required' => TRUE,
|
| 42 |
'#default_value' => variable_get('profile', ''),
|
| 43 |
'#options' => $profiles,
|
| 44 |
'#description' => t('Site profile for new site.'),
|
| 45 |
);
|
| 46 |
|
| 47 |
$form['deploy']['notes'] = array(
|
| 48 |
'#type' => 'textarea',
|
| 49 |
'#title' => 'Notes',
|
| 50 |
'#required' => FALSE,
|
| 51 |
'#default_value' => variable_get('notes',''),
|
| 52 |
'#description' => t('Some notes. Who is the site for? Anything to keep in mind?.'),
|
| 53 |
);
|
| 54 |
|
| 55 |
$form['deploy']['submit'] = array('#type' => 'submit', '#value' => t('Deploy'));
|
| 56 |
|
| 57 |
$output .= drupal_get_form('provisionator_page', $form);
|
| 58 |
|
| 59 |
|
| 60 |
// LIST EXISTING SITES, PROVIDE FUNCTIONS FOR EACH
|
| 61 |
// display code borrowed/modified from watchdog.module
|
| 62 |
$header = array(
|
| 63 |
array('data' => t('Site Name'), 'field' => 'p.sitename'),
|
| 64 |
array('data' => t('Date Deployed'), 'field' => 'p.timestamp', 'sort' => 'desc'),
|
| 65 |
array('data' => t('Profile'), 'field' => 'p.profile'),
|
| 66 |
array('data' => t('User'), 'field' => 'u.name'),
|
| 67 |
array('data' => t('Notes'), 'field' => 'p.notes'),
|
| 68 |
array('data' => t('Operations'), NULL)
|
| 69 |
);
|
| 70 |
|
| 71 |
$sql = "SELECT *, u.name, u.uid FROM {provisionator_sites} p INNER JOIN {users} u ON p.uid = u.uid";
|
| 72 |
$tablesort = tablesort_sql($header);
|
| 73 |
$result = pager_query($sql . $tablesort, 100);
|
| 74 |
|
| 75 |
while ($deployedsite = db_fetch_object($result)) {
|
| 76 |
$rows[] = array('data' =>
|
| 77 |
array(
|
| 78 |
// Cells
|
| 79 |
'<a href="/'.$deployedsite->shortname.'/">'.$deployedsite->sitename.'</a>',
|
| 80 |
format_date($deployedsite->timestamp, 'small'),
|
| 81 |
t($deployedsite->profile),
|
| 82 |
theme('username', $deployedsite),
|
| 83 |
t(truncate_utf8($deployedsite->notes, 56, TRUE, TRUE)),
|
| 84 |
t('n/a'),
|
| 85 |
),
|
| 86 |
// Attributes for tr
|
| 87 |
'class' => "provisionator-". preg_replace('/[^a-z]/i', '-', $deployedsite->type)
|
| 88 |
);
|
| 89 |
}
|
| 90 |
|
| 91 |
if (!$rows) {
|
| 92 |
$rows[] = array(array('data' => t('No sites deployed using Provisionator.'), 'colspan' => 6));
|
| 93 |
}
|
| 94 |
|
| 95 |
$manifest_table = theme('table', $header, $rows);
|
| 96 |
$manifest_table .= theme('pager', NULL, 100, 0);
|
| 97 |
|
| 98 |
$output .= $manifest_table;
|
| 99 |
|
| 100 |
return $output;
|
| 101 |
}
|
| 102 |
|
| 103 |
function provisionator_page_validate( $form_id, $form_values ) {
|
| 104 |
|
| 105 |
$shortname = $form_values['deploy']['shortname'];
|
| 106 |
$dbprefix = variable_get('provisionator_dbprefix','');
|
| 107 |
$drupal_site_dir_prefix = variable_get('provisionator_drupal_site_dir_prefix', '');
|
| 108 |
$apache_target_dir = variable_get('provisionator_apache_target_dir','');
|
| 109 |
$drupal_installed_path = variable_get('provisionator_drupal_installed_path', '');
|
| 110 |
$mysqlbin = variable_get('provisionator_mysqlbin','');
|
| 111 |
$mysqlhost = variable_get('provisionator_mysqlhost','');
|
| 112 |
$mysqluser = variable_get('provisionator_mysqluser','');
|
| 113 |
$mysqlpass = variable_get('provisionator_mysqlpass','');
|
| 114 |
$dbname = $dbprefix.$shortname;
|
| 115 |
|
| 116 |
|
| 117 |
// verify that the drupal site directory doesn't already exist
|
| 118 |
$drupalsitedir = $drupal_installed_path.'/sites/'.$drupal_site_dir_prefix.'.'.$shortname;
|
| 119 |
if (file_exists($drupalsitedir)) {
|
| 120 |
$output .= 'Drupal already has a site configured at '.$drupalsitedir;
|
| 121 |
}
|
| 122 |
|
| 123 |
// verify that no database is using the shortname
|
| 124 |
|
| 125 |
|
| 126 |
// verify that nothing lives at the symlink target
|
| 127 |
$apachesymlink = $apache_target_dir.'/'.$shortname;
|
| 128 |
if (file_exists($apachesymlink)) {
|
| 129 |
$output .= '<br />Symlink already exists at '.$apachesymlink;
|
| 130 |
}
|
| 131 |
|
| 132 |
if ($output <> NULL) {
|
| 133 |
form_set_error('', t($output));
|
| 134 |
}
|
| 135 |
}
|
| 136 |
|
| 137 |
function provisionator_page_submit( $form_id, $form_values ) {
|
| 138 |
// settings. All of these values should be customizable via admin/settings/provisionator
|
| 139 |
$profilesdir = variable_get('provisionator_profilesdir','');
|
| 140 |
$mysqlbin = variable_get('provisionator_mysqlbin','');
|
| 141 |
$mysqlhost = variable_get('provisionator_mysqlhost','');
|
| 142 |
$mysqluser = variable_get('provisionator_mysqluser','');
|
| 143 |
$mysqlpass = variable_get('provisionator_mysqlpass','');
|
| 144 |
$dbprefix = variable_get('provisionator_dbprefix','');
|
| 145 |
$drupal_site_template_dir = variable_get('provisionator_drupal_site_template_dir', '');
|
| 146 |
$drupal_site_dir_prefix = variable_get('provisionator_drupal_site_dir_prefix', '');
|
| 147 |
$apache_target_dir = variable_get('provisionator_apache_target_dir','');
|
| 148 |
$drupal_installed_path = variable_get('provisionator_drupal_installed_path', '');
|
| 149 |
$manifest_filename = variable_get('provisionator_manifest_index_filename', '');
|
| 150 |
|
| 151 |
$output = 'Drupal site <a href="/' . $form_values['deploy']['shortname'] . '/">'. $form_values['deploy']['shortname'].'</a>';
|
| 152 |
|
| 153 |
$shortname = $form_values['deploy']['shortname'];
|
| 154 |
$dbname = $dbprefix.$shortname;
|
| 155 |
//$output .= '<br />Creating database: ' .$dbname;
|
| 156 |
|
| 157 |
$profile = $form_values['deploy']['profile'];
|
| 158 |
|
| 159 |
$sqlfile = $profilesdir.'/'.$profile;
|
| 160 |
|
| 161 |
// create the database
|
| 162 |
$result = db_query( 'create database '.$dbname );
|
| 163 |
|
| 164 |
// populate the database with the sql template file
|
| 165 |
//$output .= ($mysqlbin.' -h '.$mysqlhost.' -u '.$mysqluser.' -p'.$mysqlpass.' '.$dbname.' < '.$sqlfile);
|
| 166 |
exec($mysqlbin.' -h '.$mysqlhost.' -u '.$mysqluser.' -p'.$mysqlpass.' '.$dbname.' < '.$sqlfile);
|
| 167 |
|
| 168 |
// copy the drupal site template directory
|
| 169 |
$filescopied = copydir( $drupal_site_template_dir, $drupal_installed_path.'/sites/'.$drupal_site_dir_prefix.'.'.$shortname);
|
| 170 |
|
| 171 |
// replace values in the new drupal site directory for this site
|
| 172 |
$settingsfilename = $drupal_installed_path.'/sites/'.$drupal_site_dir_prefix.'.'.$shortname.'/settings.php';
|
| 173 |
|
| 174 |
$settingsfilecontents = file_get_contents($settingsfilename);
|
| 175 |
$settingsfilecontents = str_replace('database_name_goes_here', $dbname, $settingsfilecontents);
|
| 176 |
$settingsfilecontents = str_replace('file_directory_path_goes_here', $drupal_site_dir_prefix.'.'.$shortname, $settingsfilecontents);
|
| 177 |
|
| 178 |
$result = compat_file_put_contents($settingsfilename, $settingsfilecontents);
|
| 179 |
|
| 180 |
// link the sitename into the web document root
|
| 181 |
symlink( $drupal_installed_path, $apache_target_dir.'/'.$shortname);
|
| 182 |
|
| 183 |
$output .= '<br />'.$form_values['deploy']['sitename'] . ' deployed. yay.';
|
| 184 |
|
| 185 |
// now, do something to add a manifest record
|
| 186 |
$notes = $form_values['deploy']['notes'];
|
| 187 |
$sitename = $form_values['deploy']['sitename'];
|
| 188 |
|
| 189 |
$status = 1; // only have one value for now. 1 == "active"
|
| 190 |
|
| 191 |
global $user;
|
| 192 |
|
| 193 |
db_query("INSERT INTO {provisionator_sites} (uid, status, timestamp, shortname, sitename, profile, notes) VALUES (%d, %d, %d, '%s', '%s', '%s', '%s')", $user->uid, $status, time(), $shortname, $sitename, $profile, $notes);
|
| 194 |
|
| 195 |
watchdog('Provisionator', 'Site deployed: '.$shortname);
|
| 196 |
|
| 197 |
|
| 198 |
// LIST EXISTING SITES, PROVIDE FUNCTIONS FOR EACH
|
| 199 |
// display code borrowed/modified from watchdog.module
|
| 200 |
$header = array(
|
| 201 |
array('data' => t('Site Name'), null),
|
| 202 |
array('data' => t('Date Deployed'), 'field' => 'p.timestamp', 'sort' => 'desc'),
|
| 203 |
array('data' => t('Profile'), null),
|
| 204 |
array('data' => t('User'), null),
|
| 205 |
array('data' => t('Notes'), null),
|
| 206 |
);
|
| 207 |
|
| 208 |
$sql = "SELECT *, u.name, u.uid FROM {provisionator_sites} p INNER JOIN {users} u ON p.uid = u.uid";
|
| 209 |
$tablesort = tablesort_sql($header);
|
| 210 |
$result = pager_query($sql . $tablesort, 1000);
|
| 211 |
|
| 212 |
while ($deployedsite = db_fetch_object($result)) {
|
| 213 |
$rows[] = array('data' =>
|
| 214 |
array(
|
| 215 |
// Cells
|
| 216 |
'<a href="/'.$deployedsite->shortname.'/">'.$deployedsite->sitename.'</a>',
|
| 217 |
format_date($deployedsite->timestamp, 'small'),
|
| 218 |
t($deployedsite->profile),
|
| 219 |
theme('username', $deployedsite),
|
| 220 |
t(truncate_utf8($deployedsite->notes, 56, TRUE, TRUE)),
|
| 221 |
),
|
| 222 |
// Attributes for tr
|
| 223 |
'class' => "provisionator-". preg_replace('/[^a-z]/i', '-', $deployedsite->type)
|
| 224 |
);
|
| 225 |
}
|
| 226 |
|
| 227 |
if (!$rows) {
|
| 228 |
$rows[] = array(array('data' => t('No sites deployed using Provisionator.'), 'colspan' => 6));
|
| 229 |
}
|
| 230 |
|
| 231 |
$manifest_table = theme('table', $header, $rows);
|
| 232 |
$manifest_table .= theme('pager', NULL, 100, 0);
|
| 233 |
|
| 234 |
// write $manifest_table to the static index file
|
| 235 |
$manifest_file_contents = '<html><head><title>Provisioned Sites</title></head><body>'.
|
| 236 |
$manifest_file_contents .= $manifest_table;
|
| 237 |
$manifest_file_contents .= '</body></html>';
|
| 238 |
$result = compat_file_put_contents($manifest_filename, $manifest_file_contents);
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
drupal_set_message( t( $output) );
|
| 243 |
}
|
| 244 |
|
| 245 |
function provisionator_deploy() {
|
| 246 |
return t('Provisionator deploy page!');
|
| 247 |
}
|
| 248 |
|
| 249 |
|
| 250 |
// provide a page to edit a manifest record and/or perform operations on a site
|
| 251 |
function provisionator_managesite( ) {
|
| 252 |
$output = 'manage a site and that site\'s manifest record...';
|
| 253 |
drupal_set_message( t($output) );
|
| 254 |
}
|
| 255 |
|
| 256 |
|
| 257 |
/**
|
| 258 |
* Implementation of hook_db_rewrite_sql().
|
| 259 |
*/
|
| 260 |
function provisionator_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
|
| 261 |
|
| 262 |
}
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
?>
|