hostmaster.install 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * Implement hook_install().
 *
 * Perform actions to set up the site for this profile.
 */
function hostmaster_install() {

ergonlogic's avatar
ergonlogic committed
10 11 12
  variable_set('install_profile', 'hostmaster');

// add support for nginx
13
  if (d()->platform->server->http_service_type === 'nginx') {
14
    module_enable(array('hosting_nginx'));
15 16 17 18 19
  }

  // Bootstrap and create all the initial nodes
  hostmaster_bootstrap();

20
  // Finalize and setup themes, optional modules etc
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  hostmaster_task_finalize();

}

function hostmaster_bootstrap() {
  /* Default node types and default node */
  $types =  node_types_rebuild();

  // Initialize the hosting defines
  hosting_init();

  /* Default client */
  $node = new stdClass();
  $node->uid = 1;
  $node->type = 'client';
  $node->title = drush_get_option('client_name', 'admin');
  $node->status = 1;
  node_save($node);
  variable_set('hosting_default_client', $node->nid);
  variable_set('hosting_admin_client', $node->nid);

  $client_id = $node->nid;

  /* Default server */
  $node = new stdClass();
  $node->uid = 1;
  $node->type = 'server';
  $node->title = php_uname('n');
  $node->status = 1;
  $node->hosting_name = 'server_master';
  $node->services = array();

  /* Make it compatible with more than apache and nginx */
  $master_server = d()->platform->server;
  hosting_services_add($node, 'http', $master_server->http_service_type, array(
   'restart_cmd' => $master_server->http_restart_cmd,
57
   'port' => $master_server->http_port,
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
   'available' => 1,
  ));

  /* examine the db server associated with the hostmaster site */
  $db_server = d()->db_server;
  $master_db = parse_url($db_server->master_db);
  /* if it's not the same server as the master server, create a new node
   * for it */
  if ($db_server->remote_host == $master_server->remote_host) {
    $db_node = $node;
  } else {
    $db_node = new stdClass();
    $db_node->uid = 1;
    $db_node->type = 'server';
    $db_node->title = $master_db['host'];
    $db_node->status = 1;
    $db_node->hosting_name = 'server_' . $db_server->remote_host;
    $db_node->services = array();
  }
  hosting_services_add($db_node, 'db', $db_server->db_service_type, array(
    'db_type' => $master_db['scheme'],
    'db_user' => urldecode($master_db['user']),
ergonlogic's avatar
ergonlogic committed
80
    'db_passwd' => isset($master_db['pass']) ? urldecode($master_db['pass']) : '',
81
    'port' => $db_server->db_port,
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    'available' => 1,
  ));

  drupal_set_message(st('Creating master server node'));
  node_save($node);
  if ($db_server->remote_host != $master_server->remote_host) {
    drupal_set_message(st('Creating db server node'));
    node_save($db_node);
  }
  variable_set('hosting_default_web_server', $node->nid);
  variable_set('hosting_own_web_server', $node->nid);

  variable_set('hosting_default_db_server', $db_node->nid);
  variable_set('hosting_own_db_server', $db_node->nid);

ergonlogic's avatar
ergonlogic committed
97
  // Create the hostmaster platform & packages
98 99 100 101 102 103
  $node = new stdClass();
  $node->uid = 1;
  $node->title = 'Drupal';
  $node->type = 'package';
  $node->package_type = 'platform';
  $node->short_name = 'drupal';
ergonlogic's avatar
ergonlogic committed
104 105
  $node->old_short_name = 'drupal';
  $node->description = 'Drupal code-base.';
106 107 108 109 110 111 112 113 114
  $node->status = 1;
  node_save($node);
  $package_id = $node->nid;

  $node = new stdClass();
  $node->uid = 1;
  $node->type = 'platform';
  $node->title = 'hostmaster';
  $node->publish_path = d()->root;
115
  $node->makefile = '';
116
  $node->verified = 1;
117
  $node->web_server = variable_get('hosting_default_web_server', 2);
118
  $node->platform_status = 1;
119
  $node->status = 1;
120
  $node->make_working_copy = 0;
121 122 123 124 125 126 127
  node_save($node);
  $platform_id = $node->nid;
  variable_set('hosting_own_platform', $node->nid);

  $instance = new stdClass();
  $instance->rid = $node->nid;
  $instance->version = VERSION;
128 129
  $instance->filename = '';
  $instance->version_code = 1;
130 131
  //$instance->schema_version = drupal_get_installed_schema_version('system');
  $instance->schema_version = 0;
132 133
  $instance->package_id = $package_id;
  $instance->status = 0;
134
  $instance->platform = $platform_id;
135 136
  hosting_package_instance_save($instance);

137
  // Create the hostmaster profile package node
138 139 140 141
  $node = new stdClass();
  $node->uid = 1;
  $node->title = 'hostmaster';
  $node->type = 'package';
142 143
  $node->old_short_name = 'hostmaster';
  $node->description = 'The Hostmaster profile.';
144 145 146 147 148 149
  $node->package_type = 'profile';
  $node->short_name = 'hostmaster';
  $node->status = 1;
  node_save($node);
  $profile_id = $node->nid;

150 151 152 153 154 155 156 157 158 159 160
  $instance = new stdClass();
  $instance->rid = $node->nid;
  $instance->version = VERSION;
  $instance->filename = '';
  $instance->version_code = 1;
  //$instance->schema_version = drupal_get_installed_schema_version('system');
  $instance->schema_version = 0;
  $instance->package_id = $profile_id;
  $instance->status = 0;
  $instance->platform = $platform_id;
  hosting_package_instance_save($instance);
161

162 163 164 165 166 167 168
  // Create the main Aegir site node
  $node = new stdClass();
  $node->uid = 1;
  $node->type = 'site';
  $node->title = d()->uri;
  $node->platform = $platform_id;
  $node->client = $client_id;
169
  $node->db_name = '';
170 171 172 173
  $node->db_server = $db_node->nid;
  $node->profile = $profile_id;
  $node->import = true;
  $node->hosting_name = 'hostmaster';
174 175
  $node->site_status = 1;
  $node->verified = 1;
176 177 178
  $node->status = 1;
  node_save($node);

179 180 181
  // Save the hostmaster site nid.
  variable_set('aegir_hostmaster_site_nid', $node->nid);

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  // Enable the hosting features of modules that we enable by default.
  // The module will already be enabled,
  // this makes sure we also set the default permissions.
  $default_hosting_features = array(
    'hosting_web_server' => 'web_server',
    'hosting_db_server' => 'db_server',
    'hosting_platform' => 'platform',
    'hosting_client' => 'client',
    'hosting_task' => 'task',
    'hosting_server' => 'server',
    'hosting_package' => 'package',
    'hosting_site' => 'site',
    'hosting' => 'hosting',
  );
  hosting_features_enable($default_hosting_features, $rebuild = TRUE, $enable = FALSE);

198 199 200 201 202 203 204 205 206 207 208 209
  variable_set('site_frontpage', 'hosting/sites');

  // do not allow user registration: the signup form will do that
  variable_set('user_register', 0);

  // This is saved because the config generation script is running via drush, and does not have access to this value
  variable_set('install_url' , $GLOBALS['base_url']);
}

function hostmaster_task_finalize() {
  variable_set('install_profile', 'hostmaster');

210
  // Enable Eldir, and set is as default theme
211 212
  theme_enable(array('eldir'));
  variable_set('theme_default', 'eldir');
213 214 215 216

  // Disable the default Bartik theme
  theme_disable(array('bartik'));

217
  drupal_set_message(st('Configuring default blocks'));
218 219
  hostmaster_place_blocks('eldir');

220 221 222 223 224 225
  // Save "menu_options" for our content types, so they don't offer to be put in menus.
  variable_set('menu_options_client', array());
  variable_set('menu_options_platform', array());
  variable_set('menu_options_server', array());
  variable_set('menu_options_site', array());

226 227
  // Rebuild node access permissions.
  node_access_rebuild();
228 229 230 231 232 233
}

/**
 * Helper function to place block.
 */
function hostmaster_place_blocks($theme) {
234 235 236 237
  $blocks = array(
    array(
      'module' => 'hosting',
      'delta' => 'hosting_queues',
238
      'theme' => $theme,
239
      'status' => 1,
ergonlogic's avatar
ergonlogic committed
240
      'weight' => -2,
ergonlogic's avatar
ergonlogic committed
241
      'region' => 'sidebar_first',
helmo's avatar
helmo committed
242
      'visibility' => 0,
243 244 245 246
      'pages' => '',
      'cache' => -1,
    ),
    array(
ergonlogic's avatar
ergonlogic committed
247 248
      'module' => 'system',
      'delta' => 'navigation',
249 250
      'theme' => $theme,
      'status' => 1,
ergonlogic's avatar
ergonlogic committed
251
      'weight' => 0,
252
      'region' => 'sidebar_first',
helmo's avatar
helmo committed
253
      'visibility' => 0,
254 255 256 257 258 259 260 261
      'pages' => '',
      'cache' => -1,
    ),
    array(
      'module' => 'views',
      'delta' => 'hosting_task_list-block',
      'theme' => $theme,
      'status' => 1,
ergonlogic's avatar
ergonlogic committed
262
      'weight' => -2,
263
      'region' => 'sidebar_first',
264
      'visibility' => 0,
265 266 267 268 269
      'pages' => '',
      'cache' => -1,
    ),
    array(
      'module' => 'views',
helmo's avatar
helmo committed
270
      'delta' => 'hosting_site_list-block_sites',
271 272 273 274
      'theme' => $theme,
      'status' => 1,
      'weight' => 0,
      'region' => 'content_bottom',
helmo's avatar
helmo committed
275
      'visibility' => 1,
276 277 278
      'pages' => 'hosting/c/platform_*',
      'cache' => -1,
    ),
279 280
    array(
      'module' => 'views',
281
      'delta' => 'hosting_platform_list-block_1',
282 283 284 285
      'theme' => $theme,
      'status' => 1,
      'weight' => 0,
      'region' => 'content_bottom',
helmo's avatar
helmo committed
286
      'visibility' => 1,
287 288 289
      'pages' => 'hosting/c/server_*',
      'cache' => -1,
    ),
290 291 292 293 294 295 296
    array(
      'module' => 'views',
      'delta' => 'hosting_site_list-block_profile',
      'theme' => $theme,
      'status' => 1,
      'weight' => 0,
      'region' => 'content_bottom',
helmo's avatar
helmo committed
297
      'visibility' => 0,
298 299 300 301 302 303 304 305 306 307 308
      'pages' => '',
      //"<?php\n\$node = menu_get_object();\nif (!empty(\$node)) {\n  return \$node->package_type == 'profile';\n}\n
      'cache' => -1,
    ),
    array(
      'module' => 'views',
      'delta' => 'hosting_site_list-block_client',
      'theme' => $theme,
      'status' => 1,
      'weight' => 0,
      'region' => 'content_bottom',
helmo's avatar
helmo committed
309
      'visibility' => 0,
310 311 312 313 314 315 316 317 318 319 320
      'pages' => '',
      //"<?php\n\$node = menu_get_object();\n\$menu_item = menu_get_item();\nif (!empty(\$node) && \$menu_item['number_parts'] == 2) {\n  return \$node->type == 'client';\n}\n
      'cache' => -1,
    ),
    array(
      'module' => 'views',
      'delta' => 'hosting_site_list-block_client2',
      'theme' => $theme,
      'status' => 1,
      'weight' => 0,
      'region' => 'content_bottom',
helmo's avatar
helmo committed
321
      'visibility' => 0,
322 323 324 325 326 327 328 329 330
      'pages' => '',
      //"<?php\nglobal \$user;\$node = menu_get_object();\n\$menu_item = menu_get_item();\nif (!empty(\$node)) && \$menu_item['number_parts'] == 2) {\n  return \$node->type == 'client' && $user->uid != 1;\n}\n
      'cache' => -1,
    ),
    array(
      'module' => 'views',
      'delta' => 'hosting_package_list-block_1',
      'theme' => $theme,
      'status' => 1,
ergonlogic's avatar
ergonlogic committed
331
      'weight' => 0,
332
      'region' => 'content_bottom',
helmo's avatar
helmo committed
333
      'visibility' => 0,
334 335 336 337
      'pages' => '',
      //"<?php\n\$node = menu_get_object();\nif (!empty(\$node)) {\n  return \$node->type == 'package' && \$node->package_type != 'profile';\n}\n
      'cache' => -1,
    ),
338
  );
339

helmo's avatar
helmo committed
340 341
  $query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'cache'));

342 343 344 345 346
  foreach ($blocks as $block) {
    $query->values($block);
  }
  $query->execute();

347
}