cloud-8.x-2.0-beta1/cloud.install
cloud.install
<?php
/**
* @file
* Install and updates for aws_cloud.
*/
use Drupal\Core\Config\FileStorage;
use Drupal\Component\Serialization\Yaml;
/**
* Removed obsolete cloud_pricing interrupts update from beta1 to beta2.
*
* See also: https://www.drupal.org/node/2487215.
*/
function cloud_update_8101() {
\Drupal::database()->delete('key_value')
->condition('collection', 'system.schema')
->condition('name', 'cloud_pricing')
->execute();
}
/**
* Update cloud_list and server_template_listing view's yml files.
*/
function cloud_update_8102() {
cloud_update_yml_definitions([
'views.view.cloud_listing.yml',
'views.view.server_template_listing.yml',
]);
}
/**
* Change the field name of user_id to uid.
*/
function cloud_update_8103() {
$db = \Drupal::database();
$tables = [
'cloud_config_field_data',
'cloud_server_template_field_data',
];
$old_data = [];
foreach ($tables as $table) {
$old_data[$table] = [];
if ($db->schema()->fieldExists($table, 'user_id')) {
$result = $db->select($table, 't')
->fields('t', ['id', 'user_id'])
->execute()
->fetchAll();
foreach ($result as $row) {
$old_data[$table][] = $row;
}
$db->update($table)
->fields(['user_id' => NULL])
->execute();
}
}
aws_cloud_update_entity_definitions();
foreach ($tables as $table) {
foreach ($old_data[$table] as $row) {
$db->update($table)
->fields(['uid' => $row->user_id])
->condition('id', $row->id)
->execute();
}
}
}
/**
* Update cloud_listing view.
*/
function cloud_update_8104() {
cloud_update_yml_definitions([
'views.view.cloud_listing.yml',
]);
}
/**
* Migrate cloud server template entity to Cloud module.
*
* Backup database before performing this update!
*/
function cloud_update_8110() {
// Flush the cache first.
drupal_flush_all_caches();
// Clear the definition cache.
\Drupal::service('config.typed')->clearCachedDefinitions();
$update_manager = \Drupal::entityDefinitionUpdateManager();
// Update the cloud server template configuration entity.
$server_template_type = $update_manager->getEntityType('cloud_server_template_type');
$server_template_type->set('class', 'Drupal\cloud\Entity\CloudServerTemplateType');
$server_template_type->set('originalClass', 'Drupal\cloud\Entity\CloudServerTemplateType');
$handlers = [
'list_builder' => 'Drupal\cloud\Entity\CloudServerTemplateTypeListBuilder',
'form' => [
'add' => 'Drupal\cloud\Form\CloudServerTemplateTypeForm',
'edit' => 'Drupal\cloud\Form\CloudServerTemplateTypeForm',
'delete' => 'Drupal\cloud\Form\CloudServerTemplateTypeDeleteForm',
],
];
$server_template_type->set('handlers', $handlers);
$route_provider = [
'html' => 'Drupal\cloud\Routing\CloudServerTemplateTypeHtmlRouteProvider',
];
$server_template_type->set('route_provider', $route_provider);
$server_template_type->set('provider', 'cloud');
$update_manager->updateEntityType($server_template_type);
// Update the cloud server template definition.
$server_template = $update_manager->getEntityType('cloud_server_template');
$server_template->set('class', 'Drupal\cloud\Entity\CloudServerTemplate');
$server_template->set('originalClass', 'Drupal\cloud\Entity\CloudServerTemplate');
$handlers = [
'storage' => 'Drupal\cloud\Entity\CloudServerTemplateStorage',
'list_builder' => 'Drupal\cloud\Controller\CloudServerTemplateListBuilder',
'views_data' => 'Drupal\cloud\Entity\CloudServerTemplateViewsData',
'translation' => 'Drupal\cloud\Entity\CloudServerTemplateTranslationHandler',
'form' => [
'default' => 'Drupal\cloud\Form\CloudServerTemplateForm',
'add' => 'Drupal\cloud\Form\CloudServerTemplateForm',
'edit' => 'Drupal\cloud\Form\CloudServerTemplateForm',
'delete' => 'Drupal\cloud\Form\CloudServerTemplateDeleteForm',
'launch' => 'Drupal\cloud_server_template\Form\CloudServerTemplateLaunchConfirm',
],
'access' => 'Drupal\cloud\Controller\CloudServerTemplateAccessControlHandler',
'route_provider' => [
'html' => 'Drupal\cloud_server_template\Routing\CloudServerTemplateHtmlRouteProvider',
],
];
$server_template->set('handlers', $handlers);
$server_template->set('provider', 'cloud');
$update_manager->updateEntityType($server_template);
// Update the configurations.
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('cloud_server_template.cloud_server_template_type.aws_cloud');
$config->setName('cloud.cloud_server_template_type.aws_cloud');
$config->save();
\Drupal::service('config.typed')->clearCachedDefinitions();
drupal_flush_all_caches();
// Update the yml files.
cloud_update_yml_definitions(cloud_get_server_template_yml_to_update(), 'aws_cloud');
}
/**
* Remove the old cloud_server_template definition.
*/
function cloud_update_8111() {
// Delete the old cloud_server_template.cloud_server_template_type.aws_cloud.
\Drupal::configFactory()
->getEditable('cloud_server_template.cloud_server_template_type.aws_cloud')
->delete();
// Uninstall the cloud_server_template module.
\Drupal::service('module_installer')->uninstall(['cloud_server_template']);
}
/**
* Update the cloud server template and cloud listing views.
*/
function cloud_update_8112() {
cloud_update_yml_definitions([
'views.view.cloud_listing.yml',
'views.view.server_template_listing.yml',
]);
}
/**
* Yml files to update for cloud server template move.
*
* @return array
* An array of YAML files
*/
function cloud_get_server_template_yml_to_update() {
return [
'cloud.cloud_server_template_type.aws_cloud.yml',
'core.entity_form_display.cloud_server_template.aws_cloud.default.yml',
'core.entity_view_display.cloud_server_template.aws_cloud.default.yml',
'field.field.cloud_server_template.aws_cloud.field_availability_zone.yml',
'field.storage.cloud_server_template.field_availability_zone.yml',
'field.field.cloud_server_template.aws_cloud.field_description.yml',
'field.storage.cloud_server_template.field_description.yml',
'field.field.cloud_server_template.aws_cloud.field_iam_role.yml',
'field.storage.cloud_server_template.field_iam_role.yml',
'field.field.cloud_server_template.aws_cloud.field_image_id.yml',
'field.storage.cloud_server_template.field_image_id.yml',
'field.field.cloud_server_template.aws_cloud.field_instance_shutdown_behavior.yml',
'field.storage.cloud_server_template.field_instance_shutdown_behavior.yml',
'field.field.cloud_server_template.aws_cloud.field_instance_type.yml',
'field.storage.cloud_server_template.field_instance_type.yml',
'field.field.cloud_server_template.aws_cloud.field_kernel_id.yml',
'field.storage.cloud_server_template.field_kernel_id.yml',
'field.field.cloud_server_template.aws_cloud.field_max_count.yml',
'field.storage.cloud_server_template.field_max_count.yml',
'field.field.cloud_server_template.aws_cloud.field_min_count.yml',
'field.storage.cloud_server_template.field_min_count.yml',
'field.field.cloud_server_template.aws_cloud.field_monitoring.yml',
'field.storage.cloud_server_template.field_monitoring.yml',
'field.field.cloud_server_template.aws_cloud.field_network.yml',
'field.storage.cloud_server_template.field_network.yml',
'field.field.cloud_server_template.aws_cloud.field_ram.yml',
'field.storage.cloud_server_template.field_ram.yml',
'field.field.cloud_server_template.aws_cloud.field_schedule.yml',
'field.storage.cloud_server_template.field_schedule.yml',
'field.field.cloud_server_template.aws_cloud.field_security_group.yml',
'field.storage.cloud_server_template.field_security_group.yml',
'field.field.cloud_server_template.aws_cloud.field_ssh_key.yml',
'field.storage.cloud_server_template.field_ssh_key.yml',
'field.field.cloud_server_template.aws_cloud.field_subnet.yml',
'field.storage.cloud_server_template.field_subnet.yml',
'field.field.cloud_server_template.aws_cloud.field_tags.yml',
'field.storage.cloud_server_template.field_tags.yml',
'field.field.cloud_server_template.aws_cloud.field_termination_protection.yml',
'field.storage.cloud_server_template.field_termination_protection.yml',
'field.field.cloud_server_template.aws_cloud.field_test_only.yml',
'field.storage.cloud_server_template.field_test_only.yml',
'field.field.cloud_server_template.aws_cloud.field_user_data.yml',
'field.storage.cloud_server_template.field_user_data.yml',
'field.field.cloud_server_template.aws_cloud.field_vpc.yml',
'field.storage.cloud_server_template.field_vpc.yml',
];
}
/**
* Update cloud_list and server_template_listing view's yml files.
*/
function cloud_update_8113() {
cloud_update_yml_definitions([
'views.view.cloud_listing.yml',
'views.view.server_template_listing.yml',
]);
}
/**
* Change cloud_list and server_template_listing view's ids and yml file names.
*/
function cloud_update_8114() {
$view_ids = [
'cloud_listing',
'server_template_listing',
];
cloud_delete_views($view_ids);
cloud_update_yml_definitions([
'views.view.cloud_service_providers.yml',
'views.view.cloud_server_templates.yml',
]);
}
/**
* Change view's ids and yml file names.
*/
function cloud_update_8115() {
$view_ids = [
'cloud_service_providers',
'cloud_server_templates',
];
cloud_delete_views($view_ids);
cloud_update_yml_definitions([
'views.view.cloud_config.yml',
'views.view.cloud_server_template.yml',
]);
}
/**
* Add multiple cloud service provider (CloudConfig) delete action.
*
* Update view cloud_config, add cloud_config_delete_action.
*/
function cloud_update_8116() {
cloud_update_yml_definitions([
'views.view.cloud_config.yml',
'system.action.cloud_config_delete_action.yml',
]);
}
/**
* Update cloud service providers (CloudConfig) view.
*/
function cloud_update_8117() {
cloud_update_yml_definitions([
'views.view.cloud_config.yml',
]);
}
/**
* Update cloud service providers (CloudConfig) view permissions.
*/
function cloud_update_8118() {
cloud_update_yml_definitions([
'views.view.cloud_config.yml',
]);
}
/**
* Update server_template_listing view's yml files.
*/
function cloud_update_8119() {
cloud_update_yml_definitions([
'views.view.cloud_server_template.yml',
]);
}
/**
* Migrate cloud service provider and cloud server template permissions.
*
* Backup database before performing this update!
*/
function cloud_update_8120() {
$permission_map = [
'view all cloud config entities' => 'view all cloud service providers',
'view cloud config admin list' => 'view cloud service provider admin list',
'add cloud config entities' => 'add cloud service providers',
'administer cloud config entities' => 'administer cloud service providers',
'delete cloud config entities' => 'delete cloud service providers',
'delete own cloud config entities' => 'delete own cloud service providers',
'edit cloud config entities' => 'edit cloud service providers',
'edit own cloud config entities' => 'edit own cloud service providers',
'view published cloud config entities' => 'view published cloud service providers',
'view own published cloud config entities' => 'view own published cloud service providers',
'view unpublished cloud config entities' => 'view unpublished cloud service providers',
'view own unpublished cloud config entities' => 'view own unpublished cloud service providers',
'view all cloud config revisions' => 'view all cloud service provider revisions',
'revert all cloud config revisions' => 'revert all cloud service provider revisions',
'delete all cloud config revisions' => 'delete all cloud service provider revisions',
'add cloud server template entities' => 'add cloud server templates',
'administer cloud server template entities' => 'administer cloud server templates',
'delete any cloud server template entities' => 'delete any cloud server templates',
'delete own cloud server template entities' => 'delete own cloud server templates',
'edit any cloud server template entities' => 'edit any cloud server templates',
'edit own cloud server template entities' => 'edit own cloud server templates',
'view any published cloud server template entities' => 'view any published cloud server templates',
'view own published cloud server template entities' => 'view own published cloud server templates',
'view any unpublished cloud server template entities' => 'view any unpublished cloud server templates',
'view own unpublished cloud server template entities' => 'view own unpublished cloud server templates',
'launch server template' => 'launch cloud server template',
];
cloud_update_permissions($permission_map);
}
/**
* Add multiple cloud server template delete action.
*/
function cloud_update_8121() {
cloud_update_yml_definitions(['system.action.cloud_server_template_delete_action.yml'], 'cloud');
}
/**
* Add fields about Locaion to cloud_config.
*/
function cloud_update_8122() {
// Add new fields.
$fields = [
'field_location_country',
'field_location_city',
'field_location_latitude',
'field_location_longitude',
];
$config_path = drupal_get_path('module', 'cloud') . '/config/install';
$source = new FileStorage($config_path);
foreach ($fields as $field) {
// Obtain the storage manager for field termination protection bases
// Create a new field from the yaml configuration and save.
\Drupal::entityTypeManager()->getStorage('field_storage_config')
->create($source->read("field.storage.cloud_config.$field"))
->save();
}
$config_path = realpath(drupal_get_path('module', 'cloud')) . '/config/install';
$filename = $config_path . '/cloud.settings.yml';
$file = file_get_contents($filename);
if ($file) {
$values = Yaml::decode($file);
if (!empty($values) && is_array($values)) {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('cloud.settings');
foreach ($values as $key => $value) {
$config->set($key, $value);
}
$config->save();
}
}
}
