Skip to content
Snippets Groups Projects
Commit b6d7d891 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3038194 by Xiaohua Guan, yas, Masami, baldwinlouie: Update the...

Issue #3038194 by Xiaohua Guan, yas, Masami, baldwinlouie: Update the resources automatically after a cloud config was saved
parent e72a8a3b
No related branches found
No related tags found
No related merge requests found
......@@ -371,39 +371,13 @@ function aws_cloud_entity_view(array &$build, EntityInterface $entity, EntityVie
* Implements hook_cron().
*/
function aws_cloud_cron() {
/* @var \Drupal\aws_cloud\Service\AwsEc2ServiceInterface $aws_ec2_service */
$aws_ec2_service = \Drupal::service('aws_cloud.ec2');
$entities = \Drupal::service('plugin.manager.cloud_config_plugin')->loadConfigEntities('aws_ec2');
foreach ($entities as $entity) {
/* @var \Drupal\cloud\Entity\CloudConfig $entity */
$aws_ec2_service->setCloudContext($entity->getCloudContext());
// Terminate instances past the timestamp.
$instances = aws_cloud_get_expired_instances($entity->getCloudContext());
if ($instances) {
\Drupal::logger('aws_cloud')->notice(t('Terminating the following instances %instance', ['%instance' => implode(', ', $instances['InstanceIds'])]));
$aws_ec2_service->terminateInstance($instances);
}
$aws_ec2_service->updateInstances();
$aws_ec2_service->updateSecurityGroups();
$aws_ec2_service->updateKeyPairs();
$aws_ec2_service->updateElasticIp();
$aws_ec2_service->updateNetworkInterfaces();
$aws_ec2_service->updateSnapshots();
$aws_ec2_service->updateVolumes();
aws_cloud_update_ec2_resources($entity, TRUE);
// Update any pending images in the DB. Pending images are usually
// created from an active instance.
$images = aws_cloud_get_pending_images($entity->getCloudContext());
if (count($images)) {
$aws_ec2_service->updateImages([
'ImageIds' => $images,
]);
}
// @todo: re-enabled instance bundling
// Update any instance types if needed.
aws_cloud_refresh_instance_types($entity);
aws_cloud_update_instance_types($entity);
}
// Notify owners if their instances have been running for to long.
......@@ -418,7 +392,7 @@ function aws_cloud_rebuild() {
$entities = \Drupal::service('plugin.manager.cloud_config_plugin')->loadConfigEntities('aws_ec2');
foreach ($entities as $entity) {
// Update any instance types if needed.
aws_cloud_refresh_instance_types($entity);
aws_cloud_update_instance_types($entity);
}
}
......@@ -450,14 +424,73 @@ EOF;
* Implements hook_cloud_config_insert().
*/
function aws_cloud_cloud_config_insert(CloudConfig $cloud_config) {
aws_cloud_refresh_instance_types($cloud_config, TRUE);
if ($cloud_config->bundle() == 'aws_ec2') {
aws_cloud_update_instance_types($cloud_config, TRUE);
}
}
/**
* Implements hook_cloud_config_update().
*/
function aws_cloud_cloud_config_update(CloudConfig $cloud_config) {
aws_cloud_refresh_instance_types($cloud_config, TRUE);
if ($cloud_config->bundle() == 'aws_ec2') {
aws_cloud_update_instance_types($cloud_config, TRUE);
// Update resources.
aws_cloud_update_ec2_resources($cloud_config);
}
}
/**
* Update AWS EC2 resources.
*
* @param \Drupal\cloud\Entity\CloudConfig $cloud_config
* The cloud config object.
* @param bool $for_cron
* Update resources for cron job or not.
*/
function aws_cloud_update_ec2_resources(
CloudConfig $cloud_config,
$for_cron = FALSE
) {
$aws_ec2_service = \Drupal::service('aws_cloud.ec2');
$aws_ec2_service->setCloudContext($cloud_config->getCloudContext());
if ($for_cron) {
// Terminate instances past the timestamp.
$instances = aws_cloud_get_expired_instances($cloud_config->getCloudContext());
if ($instances) {
\Drupal::logger('aws_cloud')->notice(
t('Terminating the following instances %instance',
['%instance' => implode(', ', $instances['InstanceIds'])]
)
);
$aws_ec2_service->terminateInstance($instances);
}
}
$aws_ec2_service->updateInstances();
$aws_ec2_service->updateSecurityGroups();
$aws_ec2_service->updateKeyPairs();
$aws_ec2_service->updateElasticIp();
$aws_ec2_service->updateNetworkInterfaces();
$aws_ec2_service->updateSnapshots();
$aws_ec2_service->updateVolumes();
if ($for_cron) {
// Update any pending images in the DB. Pending images are usually
// created from an active instance.
$images = aws_cloud_get_pending_images($cloud_config->getCloudContext());
if (count($images)) {
$aws_ec2_service->updateImages([
'ImageIds' => $images,
]);
}
}
else {
$account_id = $cloud_config->get('field_account_id')->value;
$aws_ec2_service->updateImages(['Owners' => [$account_id]], TRUE);
}
}
/**
......@@ -1107,7 +1140,10 @@ function aws_cloud_form_cloud_config_aws_ec2_add_form_submit_batch(
$entity->set('field_image_upload_url', [$api_endpoint_uri]);
$entity->save();
aws_cloud_update_ec2_resources($entity);
}
}
/**
......@@ -1733,7 +1769,7 @@ function aws_cloud_get_instance_types($cloud_context) {
}
/**
* Refresh instance types every 30 days.
* Update instance types every 30 days.
*
* @param \Drupal\cloud\Entity\CloudConfig $cloud_config
* Cloud config entity.
......@@ -1741,7 +1777,7 @@ function aws_cloud_get_instance_types($cloud_context) {
* True to always refresh. False to look at next_import time or if no cache
* value.
*/
function aws_cloud_refresh_instance_types(CloudConfig $cloud_config, $force = FALSE) {
function aws_cloud_update_instance_types(CloudConfig $cloud_config, $force = FALSE) {
// Get the state key.
$state_key = _aws_cloud_get_instance_type_state_key($cloud_config);
// Get the timestamp for next import.
......
......@@ -1926,6 +1926,7 @@ class AwsEc2Service implements AwsEc2ServiceInterface {
private function getEntityId($entity_type, $id_field, $id_value) {
$entities = $this->entityTypeManager->getStorage($entity_type)->getQuery()
->condition($id_field, $id_value)
->condition('cloud_context', $this->cloudContext)
->execute();
return array_shift($entities);
}
......
......@@ -91,7 +91,6 @@ class AwsCloudConfigTest extends BrowserTestBase {
$add = $this->createConfigTestData();
for ($i = 0; $i < self::AWS_CLOUD_CONFIG_REPEAT_COUNT; $i++) {
unset($add[$i]['cloud_context']);
unset($add[$i]['field_region']);
unset($add[$i]['field_api_endpoint_uri']);
unset($add[$i]['field_image_upload_url']);
......@@ -139,7 +138,6 @@ class AwsCloudConfigTest extends BrowserTestBase {
$num = $i + 1;
$label[$i] = $edit[$i]['name[0][value]'];
unset($edit[$i]['cloud_context']);
unset($edit[$i]['field_cloud_type']);
$this->drupalPostForm('/admin/structure/cloud_config/' . $num . '/edit',
......@@ -241,16 +239,15 @@ class AwsCloudConfigTest extends BrowserTestBase {
$num = $i + 1;
$data[] = [
'cloud_context' => "amazon_us_west_$num",
'field_cloud_type' => 'amazon_ec2',
'name[0][value]' => "$num - " . $this->random->name(8, TRUE),
'field_description[0][value]' => "#$num: " . date('Y/m/d H:i:s - D M j G:i:s T Y') . $this->random->string(64, TRUE),
'field_api_version[0][value]' => date('Y-m-d'),
'field_api_version[0][value]' => 'latest',
'field_region' => "us-west-$num",
'field_api_endpoint_uri' => "https://ec2.us-west-$num.amazonaws.com",
'field_secret_key[0][value]' => $this->random->name(20, TRUE),
'field_access_key[0][value]' => $this->random->name(40, TRUE),
'field_account_id[0][value]' => $this->random->name(16, TRUE),
'field_account_id[0][value]' => $this->random->name(16, TRUE),
'field_image_upload_url' => "https://ec2.us-west-$num.amazonaws.com",
'field_x_509_certificate[0][value]' => $this->random->string(255, TRUE),
];
......
......@@ -91,7 +91,7 @@ class CloudConfigPluginManager extends DefaultPluginManager implements CloudConf
// Load the plugin variant since we know the cloud_context.
$this->plugin = $this->loadPluginVariant();
if ($this->plugin == FALSE) {
throw new CloudConfigPluginException(sprintf('Cannot load cloud config plugin for %s', ['%s' => $this->cloudContext]));
throw new CloudConfigPluginException(sprintf('Cannot load cloud config plugin for %s', $this->cloudContext));
}
}
......@@ -115,7 +115,7 @@ class CloudConfigPluginManager extends DefaultPluginManager implements CloudConf
public function loadConfigEntity() {
$config_entity = $this->plugin->loadConfigEntity($this->cloudContext);
if ($config_entity == FALSE) {
throw new CloudConfigPlugException(sprintf('Cannot load configuration entity for %s', ['%s' => $this->cloudContext]));
throw new CloudConfigPluginException(sprintf('Cannot load configuration entity for %s', $this->cloudContext));
}
return $config_entity;
}
......@@ -127,7 +127,7 @@ class CloudConfigPluginManager extends DefaultPluginManager implements CloudConf
/* @var \Drupal\cloud\Plugin\CloudConfigPluginInterface $plugin */
$plugin = $this->loadBasePluginDefinition($entity_bundle);
if ($plugin == FALSE) {
throw new CloudConfigPluginException(sprintf('Cannot load Cloud Config Entity for %s', ['%s' => $entity_bundle]));
throw new CloudConfigPluginException(sprintf('Cannot load Cloud Config Entity for %s', $entity_bundle));
}
return $plugin->loadConfigEntities();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment