Skip to content
Snippets Groups Projects
Commit 27edd7f4 authored by baldwinlouie's avatar baldwinlouie
Browse files

Issue #1911252 by baldwinlouie: Added configurable option for termination

parent 23e53f30
No related branches found
No related tags found
No related merge requests found
......@@ -61,8 +61,8 @@ function cloud_metering_instances_list() {
$instance['instance_id'],
$public_dns,
ucwords($instance['state']),
number_format($budget_data['budget'], 2, '.', ','),
$budget_data['threshold'],
number_format($budget_data['initial_budget'], 2, '.', ','),
isset($budget_data['cost_threshold']) ? $budget_data['cost_threshold'] : 0,
$link,
);
}
......@@ -435,16 +435,23 @@ function cloud_metering_set_threshold($form, $form_state, $cloud_context, $insta
'#type' => 'textfield',
'#title' => t('Initial Budget ($) for @instance_id', array('@instance_id' => $instance_id)),
'#description' => t('Enter Zero(0) for unlimited budget.'),
'#default_value' => $budget_data['budget'],
'#default_value' => $budget_data['initial_budget'],
'#size' => 20,
);
$form['wrapper']['cost_threshold'] = array(
'#title' => t('Threshold (%) for @instance_id', array('@instance_id' => $instance_id)),
'#description' => t('The cost threshold percentage. Thershold value will be percent of the initial budget.'),
'#description' => t('The cost threshold percentage. Threshold value will be percent of the initial budget.'),
'#type' => 'textfield',
'#default_value' => $budget_data['threshold'],
'#default_value' => $budget_data['cost_threshold'],
'#size' => 20,
);
$form['wrapper']['terminate'] = array(
'#title' => t('Terminate instance if budget is surpassed'),
'#description' => t('If checked, the instance will be automatically terminated if the budget is surpassed'),
'#type' => 'checkbox',
'#default_value' => $budget_data['terminate'],
'#disabled' => $locked,
);
$form['wrapper']['cloud_context'] = array(
'#type' => 'value',
'#value' => $cloud_context,
......@@ -500,12 +507,12 @@ function cloud_metering_mail_settings($form, $form_state) {
$form['wrapper'] = array(
'#type' => 'fieldset',
'#title' => t('Mail Settings'),
'#description' => t('The metering module sends out notification mails whenever a cloud instance\'s cost has reached the budget threshold or when an instance\'s cost has gone past the budget allocated and needs to be terminated. The format of the mails can be configured using the following forms.'),
'#description' => t('The metering module sends out notification mails when a cloud instance\'s cost has reached the budget threshold or when an instance\'s cost has gone past the budget allocated and needs to be terminated. The format of the mails can be configured using the following forms.'),
'#collapsible' => FALSE,
);
$form['wrapper']['threshold_mails'] = array(
'#type' => 'fieldset',
'#title' => t('Instance cost reached or crossed threshold'),
'#title' => t('Instance cost has crossed the threshold'),
'#description' => $description . '<br>' . $instance_token_help,
'#collapsible' => TRUE,
);
......@@ -519,10 +526,28 @@ function cloud_metering_mail_settings($form, $form_state) {
'#title' => t('Body'),
'#default_value' => CLOUD_METERING_DEFAULT_THRESHOLD_MAIL_BODY,
);
$form['wrapper']['terminate_warning_mails'] = array(
'#type' => 'fieldset',
'#title' => t('Instance cost has crossed the intended budget.'),
'#description' => '<strong>' . t('This is a warning email telling the user to terminate the instance.') . '</strong><br>' . $description . '<br>' . $instance_token_help,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['wrapper']['terminate_warning_mails']['cloud_metering_terminate_warning_mail_subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#default_value' => CLOUD_METERING_DEFAULT_TERMINATE_WARNING_MAIL_SUBJECT,
);
$form['wrapper']['terminate_warning_mails']['cloud_metering_terminate_warning_mail_template'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#default_value' => CLOUD_METERING_DEFAULT_TERMINATE_WARNING_MAIL_BODY,
);
$form['wrapper']['terminate_mails'] = array(
'#type' => 'fieldset',
'#title' => t('Instance cost reached or crossed budget'),
'#description' => $description . '<br>' . $instance_token_help,
'#title' => t('Instance cost has crossed budget.'),
'#description' => '<strong> ' . t('This is a termination email informing users the instance will be terminated') . '</strong><br>' . $description . '<br>' . $instance_token_help,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
......@@ -538,7 +563,7 @@ function cloud_metering_mail_settings($form, $form_state) {
);
$form['wrapper']['user_mails'] = array(
'#type' => 'fieldset',
'#title' => t('Total cost of all instances of user reached or crossed budget'),
'#title' => t('Total cost of all instances of a user has crossed the budget'),
'#description' => $description . '<br>' . $user_token_help,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
......@@ -555,7 +580,7 @@ function cloud_metering_mail_settings($form, $form_state) {
);
$form['wrapper']['group_mails'] = array(
'#type' => 'fieldset',
'#title' => t('Total cost of all instances of group reached or crossed budget'),
'#title' => t('Total cost of all instances of a group has crossed the budget'),
'#description' => $description . '<br>' . $group_token_help,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
......@@ -584,7 +609,8 @@ function cloud_metering_notify_instance_users($instances) {
$instance_name = $instance['name'];
// Check if a notification has already been sent for this instance
if (!cloud_metering_notification_sent('threshold', $cloud_context, $instance_id)) {
// send out threshold mail every hour
if (!cloud_metering_notification_sent('threshold', $cloud_context, $instance_id, 0, 0, CLOUD_METERING_THRESHOLD_MAIL_INTERVAL)) {
// Send a notification
$user = _cloud_metering_get_instance_user($instance);
$mail_result = cloud_metering_send_mail($user->uid, 'threshold_mail', array('user' => $user, 'cloud_context' => $cloud_context, 'instance_id' => $instance_id, 'instance_name' => $instance_name));
......@@ -613,31 +639,48 @@ function cloud_metering_terminate_budget_crossed_instances($maxed_instances) {
$instance_volumes = cloud_get_instance_volumes($cloud_context, $instance_id);
$volumes = array();
// if only one volume and if it is the root volume, it can't be detached
if (is_array($instance_volumes) && sizeof($instance_volumes) > 0) {
foreach ($instance_volumes as $volume_id => $volume) {
$volumes[$volume_id] = array();
// look at the configurable flag.
$budget = _cloud_metering_get_instance_budget($cloud_context, $instance_id);
if ($budget['terminate'] == TRUE) {
// if only one volume and if it is the root volume, it can't be detached
if (is_array($instance_volumes) && sizeof($instance_volumes) > 0) {
foreach ($instance_volumes as $volume_id => $volume) {
$volumes[$volume_id] = array();
}
// Initiate a backup task
_cloud_metering_add_task($cloud_context, $instance_id, CLOUD_METERING_BACKUP, array('volumes' => $volumes));
}
else {
// Initiate terminate
_cloud_metering_add_task($cloud_context, $instance_id, CLOUD_METERING_TERMINATE, array());
}
// Send notification if it hasn't been sent
if (!cloud_metering_notification_sent('terminate', $cloud_context, $instance_id)) {
// The Drupal associated with the instance
$user = _cloud_metering_get_instance_user($instance);
$mail_result = cloud_metering_send_mail($user->uid, 'terminate_mail', array('user' => $user, 'cloud_context' => $cloud_context, 'instance_id' => $instance_id, 'instance_name' => $instance['name']));
if ($mail_result['result']) {
cloud_metering_save_notification_info('terminate', $cloud_context, $instance_id);
}
else {
_cloud_metering_activity_audit(t('Failed to send notification mail for instance %instance_id.', array('%instance_id' => $instance_id)), 'user_activity', '', WATCHDOG_ERROR);
}
}
$data = array('volumes' => $volumes);
// Initiate a backup task
_cloud_metering_add_task($cloud_context, $instance_id, CLOUD_METERING_BACKUP, $data);
}
else {
// Initiate terminate
_cloud_metering_add_task($cloud_context, $instance_id, CLOUD_METERING_TERMINATE, array());
}
// Send notification if it hasn't been sent
if (!cloud_metering_notification_sent('terminate', $cloud_context, $instance_id)) {
// The Drupal associated with the instance
$user = _cloud_metering_get_instance_user($instance);
$mail_result = cloud_metering_send_mail($user->uid, 'terminate_mail', array('user' => $user, 'cloud_context' => $cloud_context, 'instance_id' => $instance_id, 'instance_name' => $instance['name']));
if ($mail_result['result']) {
cloud_metering_save_notification_info('terminate', $cloud_context, $instance_id);
}
else {
_cloud_metering_activity_audit(t('Failed to send notification mail for instance %instance_id.', array('%instance_id' => $instance_id)), 'user_activity', '', WATCHDOG_ERROR);
// send out a terminate warning every 10 minutes
if (!cloud_metering_notification_sent('terminate_warning', $cloud_context, $instance_id, 0, 0, CLOUD_METERING_TERMINATE_WARNING_INTERVAL)) {
$user = _cloud_metering_get_instance_user($instance);
$mail_result = cloud_metering_send_mail($user->uid, 'terminate_warning_mail', array('user' => $user, 'cloud_context' => $cloud_context, 'instance_id' => $instance_id, 'instance_name' => $instance['name']));
if ($mail_result['result']) {
cloud_metering_save_notification_info('terminate_warning', $cloud_context, $instance_id);
}
else {
_cloud_metering_activity_audit(t('Failed to send notification mail for instance %instance_id.', array('%instance_id' => $instance_id)), 'user_activity', '', WATCHDOG_ERROR);
}
}
}
}
......@@ -684,8 +727,8 @@ function cloud_metering_get_grouped_instances($cloud_context = NULL) {
$runtime = $instance['runtime'];
$start_date = date('c', strtotime($instance['runtime']));
$budget_data = _cloud_metering_get_instance_budget($cloud, $instance_id);
$initial_budget = $budget_data['budget'];
$threshold = $budget_data['threshold'];
$initial_budget = $budget_data['initial_budget'];
$threshold = $budget_data['cost_threshold'];
$instance['budget'] = $initial_budget;
$instance['threshold'] = $threshold;
......@@ -848,9 +891,10 @@ function _cloud_metering_update_instance_budget($form, &$form_state) {
$cloud_context = $form_values['cloud_context'];
$initial_budget = $form_values['initial_budget'];
$threshold = round($form_values['cost_threshold'], 2);
$terminate = $form_values['terminate'];
// Update records
_cloud_metering_instance_budget_save($cloud_context, $instance_id, array('budget' => $initial_budget, 'threshold' => $threshold));
_cloud_metering_instance_budget_save($cloud_context, $instance_id, array('budget' => $initial_budget, 'threshold' => $threshold, 'terminate' => $terminate));
}
/**
......@@ -970,7 +1014,7 @@ function _cloud_metering_get_user_instances_budget($user_name) {
$budget = _cloud_metering_get_instance_budget($cloud_context, $instace_id);
if (!is_null($budget['instance_id'])) {
$total_budget += $budget['budget'];
$total_budget += $budget['initial_budget'];
}
}
return $total_budget;
......
......@@ -150,6 +150,12 @@ function cloud_metering_schema() {
'not null' => TRUE,
'default' => 0,
),
'terminate' => array(
'type' => 'int',
'size' => 'normal',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('cloud_context', 'instance_id'),
);
......@@ -234,3 +240,18 @@ function cloud_metering_update_7101() {
db_change_field(CLOUD_METERING_QUEUE_TABLE, 'created', 'created', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
db_change_field(CLOUD_METERING_QUEUE_TABLE, 'updated', 'updated', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE));
}
/**
* Add terminate flag to the instance table. To be used to determine if
* an instance should be terminated when the budget is passed.
*/
function cloud_metering_update_7102() {
$field = array(
'type' => 'int',
'size' => 'normal',
'not null' => TRUE,
'default' => 0,
);
db_add_field(CLOUD_METERING_INSTANCES_BUDGET_TABLE, 'terminate', $field);
}
......@@ -151,7 +151,7 @@ function cloud_metering_form_aws_cloud_display_instance_info_alter(&$form, &$for
$lock_status = cloud_perform_action('', 'get_instance_lock_status', $cloud_context, array('instance_id' => $instance_id));
$locked = ( $lock_status == 'Lock' ? TRUE : FALSE );
$budget_data = _cloud_metering_get_instance_budget($cloud_context, $instance_id);
$form['initial_budget_info'] = array(
'#type' => 'fieldset',
'#title' => t('Threshold and Initial Budget for @instance_id', array('@instance_id' => $instance_id)),
......@@ -162,20 +162,25 @@ function cloud_metering_form_aws_cloud_display_instance_info_alter(&$form, &$for
'#type' => 'textfield',
'#title' => t('Initial Budget ($)'),
'#description' => t('Enter Zero(0) for unlimited budget.'),
'#default_value' => $budget_data['budget'],
'#default_value' => $budget_data['initial_budget'],
'#size' => 20,
'#disabled' => $locked,
);
$form['initial_budget_info']['cost_threshold'] = array(
'#title' => t('Threshold (%)'),
'#description' => t('The cost threshold percentage. Thershold value will be percent of the Initial Budget.'),
'#description' => t('The cost threshold percentage. Threshold value will be percent of the Initial Budget.'),
'#type' => 'textfield',
'#default_value' => $budget_data['threshold'],
'#default_value' => $budget_data['cost_threshold'],
'#size' => 20,
'#disabled' => $locked,
);
$form['initial_budget_info']['terminate'] = array(
'#title' => t('Terminate instance if budget is surpassed'),
'#description' => t('If checked, the instance will be automatically terminated if the budget is surpassed'),
'#type' => 'checkbox',
'#default_value' => $budget_data['terminate'],
'#disabled' => $locked,
);
$form['initial_budget_info']['update_budget'] = array(
'#type' => 'submit',
'#value' => t('Update budget'),
......@@ -262,6 +267,10 @@ function cloud_metering_mail($key, &$message, $params) {
$message['subject'] = token_replace(CLOUD_METERING_DEFAULT_TERMINATE_MAIL_SUBJECT, $params, array('language' => $language, 'sanitize' => FALSE, 'clear' => TRUE));
$message['body'][] = token_replace(CLOUD_METERING_DEFAULT_TERMINATE_MAIL_BODY, $params, array('language' => $language, 'sanitize' => FALSE, 'clear' => TRUE));
break;
case 'terminate_warning_mail':
$message['subject'] = token_replace(CLOUD_METERING_DEFAULT_TERMINATE_WARNING_MAIL_SUBJECT, $params, array('language' => $language, 'sanitize' => FALSE, 'clear' => TRUE));
$message['body'][] = token_replace(CLOUD_METERING_DEFAULT_TERMINATE_WARNING_MAIL_BODY, $params, array('language' => $language, 'sanitize' => FALSE, 'clear' => TRUE));
break;
case 'user_mail':
$message['subject'] = token_replace(CLOUD_METERING_DEFAULT_USER_MAIL_SUBJECT, $params, array('language' => $language, 'sanitize' => FALSE, 'clear' => TRUE));
$message['body'][] = token_replace(CLOUD_METERING_DEFAULT_USER_MAIL_BODY, $params, array('language' => $language, 'sanitize' => FALSE, 'clear' => TRUE));
......@@ -364,9 +373,6 @@ function cloud_metering_add_checker_tasks() {
if ($task == CLOUD_METERING_BACKUP) {
$task = CLOUD_METERING_BACKUP_CHECK;
}
elseif ($task == CLOUD_METERING_DETACH) {
$task = CLOUD_METERING_DETACH_CHECK;
}
elseif ($task == CLOUD_METERING_TERMINATE) {
$task = CLOUD_METERING_TERMINATE_CHECK;
}
......
......@@ -29,7 +29,10 @@ define('CLOUD_METERING_DEFAULT_THRESHOLD_MAIL_BODY' , variable_get('cloud_me
// terminate mail
define('CLOUD_METERING_DEFAULT_TERMINATE_MAIL_SUBJECT' , variable_get('cloud_metering_terminate_mail_subject', 'The instance [cloud_metering_instance:instance-name] has crossed the allocated budget!'));
define('CLOUD_METERING_DEFAULT_TERMINATE_MAIL_BODY' , variable_get('cloud_metering_terminate_mail_template', "Dear [cloud_metering_instance:username],\n\nYour cloud instance named [cloud_metering_instance:instance-name] has crossed the budget allocated to it. The instance will be automatically terminated after the volumes are backed up.\n\n-- [site:name]"));
define('CLOUD_METERING_DEFAULT_TERMINATE_MAIL_BODY' , variable_get('cloud_metering_terminate_mail_template', "Dear [cloud_metering_instance:username],\n\nThe cloud instance named [cloud_metering_instance:instance-name] has crossed the budget allocated to it. The instance will automatically terminate after the volumes are backed up.\n\n-- [site:name]"));
define('CLOUD_METERING_DEFAULT_TERMINATE_WARNING_MAIL_SUBJECT' , variable_get('cloud_metering_terminate_warning_mail_subject', 'Warning! [cloud_metering_instance:instance-name] has crossed the allocated budget.'));
define('CLOUD_METERING_DEFAULT_TERMINATE_WARNING_MAIL_BODY' , variable_get('cloud_metering_terminate_warning_mail_template', "Dear [cloud_metering_instance:username],\n\nThe cloud instance [cloud_metering_instance:instance-name] has crossed the budget allocated to it. Consider terminating it to keep with your budget.\n\n-- [site:name]"));
// user default mail templates
define('CLOUD_METERING_DEFAULT_USER_MAIL_SUBJECT' , variable_get('cloud_metering_user_mail_subject', 'The total cost of your cloud instances has gone past your budget!'));
......@@ -39,7 +42,9 @@ define('CLOUD_METERING_DEFAULT_USER_MAIL_BODY' , variable_get('cloud_me
define('CLOUD_METERING_DEFAULT_GROUP_MAIL_SUBJECT' , variable_get('cloud_metering_group_mail_subject', "Cloud instances has gone past the group budget!"));
define('CLOUD_METERING_DEFAULT_GROUP_MAIL_BODY' , variable_get('cloud_metering_group_mail_template', "Dear [cloud_metering_group:username],\n\nThe total cost of your group's cloud instances has gone past the allocated budget of [cloud_metering_group:group_budget]. It is currently [cloud_metering_group:group_cost]. Please consider terminating some instances or increasing the group's budget.\n\n-- [site:name]"));
define('CLOUD_METERING_MAIL_INTERVAL' , 7); // 7 days
define('CLOUD_METERING_MAIL_INTERVAL' , 604800); // 7 days in seconds
define('CLOUD_METERING_THRESHOLD_MAIL_INTERVAL' , 3600); //1 hour for threshold emails
define('CLOUD_METERING_TERMINATE_WARNING_INTERVAL' , 600);
// Queue related constants
define('CLOUD_METERING_QUEUE' , 'cloud_metering_queue');
......
......@@ -57,34 +57,29 @@ function _cloud_metering_instance_cost_data($start, $end, $cloud_context = '', $
/**
* Check if a notification has been sent against an instance
*/
function cloud_metering_notification_sent($type, $cloud_context = '', $instance_id = '', $uid = 0, $gid = 0) {
$where = '';
function cloud_metering_notification_sent($type, $cloud_context = '', $instance_id = '', $uid = 0, $gid = 0, $interval = CLOUD_METERING_MAIL_INTERVAL) {
$query = db_select(CLOUD_METERING_MAILS_TABLE);
if (!empty($cloud_context) && !empty($instance_id)) {
$where = "cloud_name = '$cloud_context' AND instance_id = '$instance_id' ";
$query
->condition('cloud_name', $cloud_context)
->condition('instance_id', $instance_id);
}
elseif ($uid > 0) {
$where = "uid = $uid";
elseif ($uid) {
$query->condition('uid', $uid);
}
elseif ($gid > 0) {
$where = "gid = $gid";
elseif ($gid) {
$query->condition('gid', $gid);
}
$where .= empty($where) ? "" : " AND";
$where .= " type = '$type' AND timestamp > " . strtotime('-' . CLOUD_METERING_MAIL_INTERVAL . ' Days');
$query = "SELECT COUNT(mid) FROM {" . CLOUD_METERING_MAILS_TABLE . "} WHERE " . $where;
$result = db_query( $query)->fetchField();
if ($result === FALSE) {
$result = 0;
}
return $result;
// add in time limit
$count = $query
->condition('type', $type)
->condition('timestamp', time() - $interval, '>')
->countQuery()->execute()->fetchField();
return $count;
}
/**
* Save notification mail sending info
*/
......@@ -137,54 +132,42 @@ function _cloud_metering_instance_budget_delete($cloud_context, $instance_id) {
* array containing the budget and the threshold
*/
function _cloud_metering_instance_budget_save($cloud_context, $instance_id, $data) {
// Extra permission check
if (!user_access('configure instance budget')) {
drupal_set_message(t('You do not have permissions to set budget settings.'));
return;
}
$budget = (isset($data['budget'])) ? $data['budget'] : NULL;
$threshold = (isset($data['threshold'])) ? $data['threshold'] : NULL;
$terminate = (isset($data['terminate'])) ? $data['terminate'] : 0; // defaults to false
if (is_null($budget) && is_null($threshold)) {
return;
}
$existing_data = _cloud_metering_get_instance_budget($cloud_context, $instance_id);
$query = '';
$params = array();
if (!is_null($existing_data['instance_id'])) {
$query_update = db_update(CLOUD_METERING_INSTANCES_BUDGET_TABLE);
if ($existing_data != FALSE) {
$fields = array();
if (!is_null($budget)) {
$fields['initial_budget'] = $budget;
}
if (!is_null($threshold)) {
$fields['cost_threshold'] = $threshold;
}
$query_update->fields($fields);
$result = $query_update
->condition('cloud_context', $cloud_context, '=')
->condition('instance_id', $instance_id, '=')
->execute();
$fields['terminate'] = $terminate;
db_update(CLOUD_METERING_INSTANCES_BUDGET_TABLE)
->fields($fields)
->condition('cloud_context', $cloud_context, '=')
->condition('instance_id', $instance_id, '=')
->execute();
}
else {
$budget = is_null($budget) ? 0 : $budget;
$threshold = is_null($threshold) ? 0 : $threshold;
$result = db_insert(CLOUD_METERING_INSTANCES_BUDGET_TABLE)
db_insert(CLOUD_METERING_INSTANCES_BUDGET_TABLE)
->fields(array(
'cloud_context' => $cloud_context,
'instance_id' => $instance_id,
'initial_budget' => $budget,
'cost_threshold' => $threshold,
))
'cloud_context' => $cloud_context,
'instance_id' => $instance_id,
'initial_budget' => $budget,
'cost_threshold' => $threshold,
))
->execute();
}
drupal_set_message(t('The instance budget settings have been saved.'));
......@@ -195,27 +178,14 @@ function _cloud_metering_instance_budget_save($cloud_context, $instance_id, $dat
* @param string cloud_context
* @param string instance_id
* @return array data
* array containing instance_id, budget and threshold
* array containing instance_id, budget and threshold
*/
function _cloud_metering_get_instance_budget($cloud_context, $instance_id) {
$return = array(
'instance_id' => NULL,
'budget' => 0,
'threshold' => 0,
);
$query = "SELECT instance_id, initial_budget, cost_threshold FROM {" . CLOUD_METERING_INSTANCES_BUDGET_TABLE . "} WHERE cloud_context = '$cloud_context' AND instance_id = '$instance_id'";
$result = db_query($query);
$row = $result->fetchAssoc();
if ($row) {
$return['instance_id'] = $row['instance_id'];
$return['budget'] = $row['initial_budget'];
$return['threshold'] = $row['cost_threshold'];
}
return $return;
return db_select(CLOUD_METERING_INSTANCES_BUDGET_TABLE, 'c')
->fields('c', array('instance_id', 'initial_budget', 'cost_threshold', 'terminate'))
->condition('cloud_context', $cloud_context)
->condition('instance_id', $instance_id)
->execute()->fetchAssoc();
}
/**
......
......@@ -63,30 +63,23 @@ function _cloud_metering_check_task($task) {
$data = $task['data'];
$cloud_context = $data['cloud_context'];
$instance_id = $data['instance_id'];
$module = cloud_get_module($cloud_context);
switch ($task['task']) {
case CLOUD_METERING_BACKUP:
$volumes = $data['volumes'];
$snapshots_complete = array();
foreach ($volumes as $volume_id => $volume) {
if (!isset($volume['snapshot_id'])) {
break;
if (isset($volume['snapshot_id'])) {
$snapshot_id = $volume['snapshot_id'];
// check if volumes are backedup
$snapshot_complete = cloud_perform_action('', 'check_snapshot_completion', $cloud_context, array('cloud_context' => $cloud_context, 'snapshot_id' => $snapshot_id));
if ($snapshot_complete) {
$snapshots_complete[] = $snapshot_id;
}
}
$snapshot_id = $volume['snapshot_id'];
$params = array(
'cloud_context' => $cloud_context,
'snapshot_id' => $snapshot_id,
);
$snapshot_complete = module_invoke($module, 'cloud_action', 'check_snapshot_completion', $params);
if (!$snapshot_complete) {
break;
}
$snapshots_complete[] = $snapshot_id;
}
if ((sizeof($volumes) == sizeof($snapshots_complete))) {
// Backup is complete. We mark the backup task as complete and start a detach job for the instance
if ((count($volumes) == count($snapshots_complete))) {
// Backup is complete. We mark the backup task as complete and initiate terminate
_cloud_metering_update_task_status($qid, CLOUD_METERING_QUEUE_STATUS_COMPLETED, array());
// Start detach task, send in the volume information already in $data
_cloud_metering_add_task($cloud_context, $instance_id, CLOUD_METERING_DETACH, $data);
......@@ -94,11 +87,7 @@ function _cloud_metering_check_task($task) {
break;
case CLOUD_METERING_TERMINATE:
// Check if the instance is terminated
$params = array(
'cloud_context' => $cloud_context,
'instance_id' => $instance_id,
);
$terminated = module_invoke($module, 'cloud_action', 'check_instance_terminated', $params);
$terminated = cloud_perform_action('', 'check_instance_terminated', $cloud_context, array('cloud_context' => $cloud_context, 'instance_id' => $instance_id));
if ($terminated) {
// Termination complete
_cloud_metering_update_task_status($qid, CLOUD_METERING_QUEUE_JOB_COMPLETED, array());
......@@ -152,19 +141,12 @@ function _cloud_metering_terminate($task) {
$cloud_context = $data['cloud_context'];
$instance_id = $data['instance_id'];
// DEBUG -- Terminate is not being continued
drupal_set_message(t('Terminate request for instance %instance-id', array('%instance-id' => $instance_id)));
_cloud_metering_update_task_status($qid, CLOUD_METERING_QUEUE_STATUS_COMPLETED, array());
return;
$params = array(
0 => $instance_id,
'instance_id' => $instance_id,
);
// Start termination of instance
cloud_perform_action('', 'terminate', $cloud_context, $params);
cloud_perform_action('', 'terminate', $cloud_context, array($instance_id));
_cloud_metering_activity_audit(t('Terminated instance %instance-id because budget was exceeded', array('%instance-id' => $instance_id)));
// Update the task status. Terminate task will be waiting for the termination to complete.
_cloud_metering_update_task_status($qid, CLOUD_METERING_QUEUE_STATUS_WAITING);
_cloud_metering_update_task_status($qid, CLOUD_METERING_QUEUE_STATUS_IN_PROGRESS);
}
/**
......@@ -172,48 +154,37 @@ function _cloud_metering_terminate($task) {
* If backups completed, go straight for terminate. Let EC2 do the volume detaching
*/
function _cloud_metering_check_backup($task) {
$qid = $task['qid'];
$data = $task['data'];
$b_qid = $data['qid'];
$cloud_context = $data['cloud_context'];
$instance_id = $data['instance_id'];
$volumes = $data['volumes'];
$snapshots_complete = array();
foreach ($volumes as $volume_id => $volume) {
$snapshot_id = $volume['snapshot_id'];
$params = array('snapshot_id' => $snapshot_id);
$snapshot_complete = cloud_perform_action('', 'check_snapshot_completion', $cloud_context, $params);
if (!$snapshot_complete) {
break;
$snapshot_complete = cloud_perform_action('', 'check_snapshot_completion', $cloud_context, array('snapshot_id' => $snapshot_id));
if ($snapshot_complete) {
$snapshots_complete[] = $snapshot_id;
}
$snapshots_complete[] = $snapshot_id;
}
if (sizeof($volumes) == sizeof($snapshots_complete)) {
// Backup of all volumes completed
_cloud_metering_update_task_status($b_qid, CLOUD_METERING_QUEUE_STATUS_COMPLETED, array());
// go straight to terminate. Let EC2 detach the volumes
_cloud_metering_add_task($cloud_context, $instance_id, CLOUD_METERING_TERMINATE, $data);
}
else {
$backup_task = _cloud_metering_get_task($b_qid);
if ($backup_task['time_running'] > 10) {
_cloud_metering_update_task_status($b_qid, CLOUD_METERING_QUEUE_STATUS_FAILED, array());
_cloud_metering_activity_audit(t('Task with %qid failed due to expiry of allocated time.', array('%qid' => $b_qid)), '', WATCHDOG_ERROR);
}
}
// The current check task is completed
_cloud_metering_update_task_status($qid, CLOUD_METERING_QUEUE_STATUS_COMPLETED, array());
}
/**
......@@ -227,8 +198,7 @@ function _cloud_metering_check_terminate($task) {
$instance_id = $data['instance_id'];
// Check if instance has got terminated
$params = array('instance_id' => $instance_id);
$terminated = cloud_perform_action('', 'check_instance_terminated', $cloud_context, $params);
$terminated = cloud_perform_action('', 'check_instance_terminated', $cloud_context, array('instance_id' => $instance_id));
if ($terminated) {
// Termination task completed
......@@ -241,7 +211,6 @@ function _cloud_metering_check_terminate($task) {
_cloud_metering_activity_audit(t('Task with Id %qid failed due to expiry of allocated time.', array('%qid' => $t_qid)), 'user_activity', '', WATCHDOG_ERROR);
}
}
// This task is complete
_cloud_metering_update_task_status($qid, CLOUD_METERING_QUEUE_STATUS_COMPLETED, array());
}
......@@ -267,10 +236,10 @@ function _cloud_metering_get_checker_tasks() {
*/
function _cloud_metering_get_task_type($task) {
$base_tasks = _cloud_metering_get_base_tasks();
$checker_tasks = _cloud_metering_get_checker_tasks();
if (in_array($task, $base_tasks)) {
return 'base';
}
$checker_tasks = _cloud_metering_get_checker_tasks();
if (in_array($task, $checker_tasks)) {
return 'checker';
}
......
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