| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Management console for managing Amazon Elastic Compute Cloud (EC2)
|
| 6 |
* images and instances.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_perm()
|
| 11 |
* @return array An array of valid permissions for the aws module
|
| 12 |
*/
|
| 13 |
function ec2_perm() {
|
| 14 |
return array('administer aws');
|
| 15 |
}
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Implementation hook_menu()
|
| 19 |
* @return array A menu api array for the module
|
| 20 |
*/
|
| 21 |
function ec2_menu($may_cache) {
|
| 22 |
$items = array();
|
| 23 |
if ($may_cache) {
|
| 24 |
// Intention is to use first page for a summary of running instances and costs
|
| 25 |
// but we'll just redirect to the instance list for now
|
| 26 |
$items[] = array(
|
| 27 |
'path' => 'aws/ec2',
|
| 28 |
'title' => t('EC2 console'),
|
| 29 |
'description' => t('Amazon Elastic Compute Cloud (EC2) management console.'),
|
| 30 |
'callback' => 'drupal_goto',
|
| 31 |
'callback arguments' => array('aws/ec2/instance'),
|
| 32 |
'access' => user_access('administer aws')
|
| 33 |
);
|
| 34 |
|
| 35 |
// Instances
|
| 36 |
$items[] = array(
|
| 37 |
'path' => 'aws/ec2/instance',
|
| 38 |
'title' => t('Instances'),
|
| 39 |
'description' => t('EC2 instances.'),
|
| 40 |
'callback' => 'drupal_get_form',
|
| 41 |
'callback arguments' => array('ec2_form_instance'),
|
| 42 |
'access' => user_access('administer aws'),
|
| 43 |
'type' => MENU_LOCAL_TASK,
|
| 44 |
'weight' => -8
|
| 45 |
);
|
| 46 |
$items[] = array(
|
| 47 |
'path' => 'aws/ec2/instance/list',
|
| 48 |
'title' => t('List'),
|
| 49 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 50 |
'weight' => -10
|
| 51 |
);
|
| 52 |
$items[] = array(
|
| 53 |
'path' => 'aws/ec2/instance/add',
|
| 54 |
'title' => t('Add'),
|
| 55 |
'callback' => 'drupal_get_form',
|
| 56 |
'callback arguments' => array('ec2_form_instance_add'),
|
| 57 |
'type' => MENU_LOCAL_TASK,
|
| 58 |
);
|
| 59 |
$items[] = array(
|
| 60 |
'path' => 'aws/ec2/instance/terminate',
|
| 61 |
'callback' => 'drupal_get_form',
|
| 62 |
'callback arguments' => array('ec2_instance_terminate_confirm'),
|
| 63 |
);
|
| 64 |
|
| 65 |
// Images
|
| 66 |
$items[] = array(
|
| 67 |
'path' => 'aws/ec2/image',
|
| 68 |
'title' => t('Images'),
|
| 69 |
'description' => t('Available EC2 images.'),
|
| 70 |
'callback' => 'drupal_get_form',
|
| 71 |
'callback arguments' => array('ec2_form_image'),
|
| 72 |
'type' => MENU_LOCAL_TASK,
|
| 73 |
'weight' => -6
|
| 74 |
);
|
| 75 |
$items[] = array(
|
| 76 |
'path' => 'aws/ec2/image/list',
|
| 77 |
'title' => t('List'),
|
| 78 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 79 |
'weight' => -10
|
| 80 |
);
|
| 81 |
$items[] = array(
|
| 82 |
'path' => 'aws/ec2/image/add',
|
| 83 |
'title' => t('Add'),
|
| 84 |
'callback' => 'drupal_get_form',
|
| 85 |
'callback arguments' => array('ec2_form_image_add'),
|
| 86 |
'type' => MENU_LOCAL_TASK,
|
| 87 |
);
|
| 88 |
$items[] = array(
|
| 89 |
'path' => 'aws/ec2/image/delete',
|
| 90 |
'callback' => 'drupal_get_form',
|
| 91 |
'callback arguments' => array('ec2_image_delete_confirm'),
|
| 92 |
);
|
| 93 |
|
| 94 |
// Keys
|
| 95 |
$items[] = array(
|
| 96 |
'path' => 'aws/ec2/key',
|
| 97 |
'title' => t('Keys'),
|
| 98 |
'description' => t('SSH keys.'),
|
| 99 |
'callback' => 'drupal_get_form',
|
| 100 |
'callback arguments' => array('ec2_form_key'),
|
| 101 |
'type' => MENU_LOCAL_TASK,
|
| 102 |
);
|
| 103 |
$items[] = array(
|
| 104 |
'path' => 'aws/ec2/key/list',
|
| 105 |
'title' => t('List'),
|
| 106 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 107 |
'weight' => -10
|
| 108 |
);
|
| 109 |
$items[] = array(
|
| 110 |
'path' => 'aws/ec2/key/add',
|
| 111 |
'title' => t('Add'),
|
| 112 |
'callback' => 'drupal_get_form',
|
| 113 |
'callback arguments' => array('ec2_form_key_add'),
|
| 114 |
'type' => MENU_LOCAL_TASK,
|
| 115 |
);
|
| 116 |
$items[] = array(
|
| 117 |
'path' => 'aws/ec2/key/delete',
|
| 118 |
'callback' => 'drupal_get_form',
|
| 119 |
'callback arguments' => array('ec2_key_delete_confirm'),
|
| 120 |
);
|
| 121 |
$items[] = array(
|
| 122 |
'path' => 'aws/ec2/key/download',
|
| 123 |
'callback' => 'ec2_key_download',
|
| 124 |
'type' => MENU_CALLBACK,
|
| 125 |
);
|
| 126 |
}
|
| 127 |
return $items;
|
| 128 |
}
|
| 129 |
|
| 130 |
/**
|
| 131 |
* Generate a form to list instances.
|
| 132 |
*/
|
| 133 |
function ec2_form_instance() {
|
| 134 |
|
| 135 |
// Load instance details from Amazon
|
| 136 |
$instance_xml = aws_ec2_DescribeInstances();
|
| 137 |
$instance_details = aws_xml_toarray($instance_xml->data);
|
| 138 |
$instance_details = $instance_details['DescribeInstancesResponse']['reservationSet']['instancesSet'];
|
| 139 |
if (is_array($instance_details) && array_key_exists("_num", $instance_details)) {
|
| 140 |
foreach ($instance_details as $key => $value) {
|
| 141 |
$instances[] = $value['item'];
|
| 142 |
}
|
| 143 |
}
|
| 144 |
else {
|
| 145 |
$instances = $instance_details;
|
| 146 |
}
|
| 147 |
|
| 148 |
// Exit if we don't have any instances.
|
| 149 |
if (! is_array($instances)) {
|
| 150 |
return $form;
|
| 151 |
}
|
| 152 |
|
| 153 |
foreach ($instances as $key => $instance) {
|
| 154 |
if ($instance['instanceId']) {
|
| 155 |
$title = db_result(db_query("SELECT title FROM {ec2_instance} WHERE instance_id = '%s'", $instance['instanceId']));
|
| 156 |
$form['instanceId'][$instance['instanceId']] = array('#type' => 'hidden', '#value' => check_plain($instance['instanceId']));
|
| 157 |
$form['title'][$instance['instanceId']] = array('#value' => check_plain($title));
|
| 158 |
$form['instanceId'][$instance['instanceId']] = array('#value' => check_plain($instance['instanceId']));
|
| 159 |
$form['imageId'][$instance['instanceId']] = array('#value' => check_plain($instance['imageId']));
|
| 160 |
$form['dnsName'][$instance['instanceId']] = array('#value' => check_plain($instance['dnsName']));
|
| 161 |
$form['instanceState'][$instance['instanceId']] = array('#value' => check_plain($instance['instanceState']['name']));
|
| 162 |
if ($instance['instanceState']['name'] != 'terminated' && $instance['instanceState']['name'] != 'shutting-down' ) {
|
| 163 |
$form['operations'][$instance['instanceId']] = array('#value' => l(t('terminate'), 'aws/ec2/instance/terminate/'. check_plain($instance['instanceId'])));
|
| 164 |
}
|
| 165 |
else {
|
| 166 |
$form['operations'][$instance['instanceId']] = array();
|
| 167 |
}
|
| 168 |
}
|
| 169 |
}
|
| 170 |
return $form;
|
| 171 |
}
|
| 172 |
|
| 173 |
/**
|
| 174 |
* Theme instance list.
|
| 175 |
*/
|
| 176 |
function theme_ec2_form_instance($form) {
|
| 177 |
// Overview table:
|
| 178 |
$header = array(t('Name'), t('Instance ID'), t('AMI'), t('Hostname'), t('Status'), ('Operations'));
|
| 179 |
|
| 180 |
$output .= drupal_render($form['options']);
|
| 181 |
if (isset($form['instanceId']) && is_array($form['instanceId'])) {
|
| 182 |
foreach (element_children($form['instanceId']) as $key) {
|
| 183 |
$row = array();
|
| 184 |
$row[] = drupal_render($form['title'][$key]);
|
| 185 |
$row[] = drupal_render($form['instanceId'][$key]);
|
| 186 |
$row[] = drupal_render($form['imageId'][$key]);
|
| 187 |
$row[] = drupal_render($form['dnsName'][$key]);
|
| 188 |
$row[] = drupal_render($form['instanceState'][$key]);
|
| 189 |
$row[] = drupal_render($form['operations'][$key]);
|
| 190 |
$rows[] = $row;
|
| 191 |
}
|
| 192 |
|
| 193 |
}
|
| 194 |
else {
|
| 195 |
$rows[] = array(array('data' => t('No instances available.'), 'colspan' => '8'));
|
| 196 |
}
|
| 197 |
|
| 198 |
$output .= theme('table', $header, $rows);
|
| 199 |
$output .= drupal_render($form);
|
| 200 |
|
| 201 |
return $output;
|
| 202 |
}
|
| 203 |
|
| 204 |
/**
|
| 205 |
* Generate a form to add/delete instances.
|
| 206 |
*/
|
| 207 |
function ec2_form_instance_add () {
|
| 208 |
$form['title'] = array(
|
| 209 |
'#type' => 'textfield',
|
| 210 |
'#title' => 'Instance name',
|
| 211 |
'#description' => t('A unique, user-defined name to refer to your instance.'),
|
| 212 |
'#required' => TRUE,
|
| 213 |
);
|
| 214 |
$form['image_id'] = array(
|
| 215 |
'#type' => 'select',
|
| 216 |
'#title' => 'AMI ID',
|
| 217 |
'#options' => ec2_image_list(),
|
| 218 |
'#required' => TRUE,
|
| 219 |
);
|
| 220 |
$form['min'] = array(
|
| 221 |
'#type' => 'hidden',
|
| 222 |
'#value' => 1,
|
| 223 |
);
|
| 224 |
$form['max'] = array(
|
| 225 |
'#type' => 'hidden',
|
| 226 |
'#value' => 1,
|
| 227 |
);
|
| 228 |
$form['key_name'] = array(
|
| 229 |
'#type' => 'select',
|
| 230 |
'#title' => 'Key name',
|
| 231 |
'#options' => ec2_key_list(),
|
| 232 |
'#required' => TRUE,
|
| 233 |
);
|
| 234 |
$form['type'] = array(
|
| 235 |
'#type' => 'select',
|
| 236 |
'#title' => 'Instance size',
|
| 237 |
'#options' => array('m1.small' => t('Small'), t('Large'), t('Extra Large')),
|
| 238 |
'#required' => TRUE,
|
| 239 |
);
|
| 240 |
$form['data'] = array(
|
| 241 |
'#type' => 'textarea',
|
| 242 |
'#title' => 'Data',
|
| 243 |
'#description' => t('Data to pass to the instance.'),
|
| 244 |
);
|
| 245 |
$form['submit'] = array(
|
| 246 |
'#type' => 'submit',
|
| 247 |
'#value' => t('Submit'),
|
| 248 |
);
|
| 249 |
return $form;
|
| 250 |
}
|
| 251 |
|
| 252 |
/**
|
| 253 |
* Validate instance form submissions.
|
| 254 |
*/
|
| 255 |
function ec2_form_instance_add_validate($form_id, $form_values) {
|
| 256 |
if (db_result(db_query("SELECT instance_id FROM {ec2_instance} WHERE title = '%s'", $form_values['title']))) {
|
| 257 |
form_set_error('title', t('This instance name already exists. Please enter a unique name.'));
|
| 258 |
}
|
| 259 |
}
|
| 260 |
|
| 261 |
/**
|
| 262 |
* Process instance form submissions.
|
| 263 |
*/
|
| 264 |
function ec2_form_instance_add_submit($form_id, $form_values) {
|
| 265 |
$response = aws_ec2_RunInstances(check_plain($form_values['image_id']), $form_values['min'], $form_values['max'], $form_values['key_name'], $form_values['groups'], check_plain($form_values['data']), $form_values['type']);
|
| 266 |
if ($response->code != "200") {
|
| 267 |
drupal_set_message(t('Amazon rejected new instance creation.'), 'error');
|
| 268 |
}
|
| 269 |
else {
|
| 270 |
$result_details = aws_xml_toarray($response->data);
|
| 271 |
$result = db_query("INSERT INTO {ec2_instance} (title, instance_id) VALUES ('%s', '%s')", $form_values['title'], $result_details['RunInstancesResponse']['instancesSet']['item']['instanceId']);
|
| 272 |
drupal_set_message(t('The instance %title has been created.', array('%title' => $form_values['title'])));
|
| 273 |
}
|
| 274 |
return 'aws/ec2/instance';
|
| 275 |
}
|
| 276 |
|
| 277 |
/**
|
| 278 |
* Instance termination confirmation.
|
| 279 |
*/
|
| 280 |
function ec2_instance_terminate_confirm($instance_id) {
|
| 281 |
$instance_id = check_plain($instance_id);
|
| 282 |
$title = db_result(db_query("SELECT title FROM {ec2_instance} WHERE instance_id = '%s'", $instance_id));
|
| 283 |
$form['instance_id'] = array('#type' => 'hidden', '#value' => $instance_id);
|
| 284 |
$form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
|
| 285 |
|
| 286 |
return confirm_form($form, t('Are you sure you want to terminate instance %name?', array('%name' => $title)),
|
| 287 |
'admin/build/block', '', t('Terminate'), t('Cancel'));
|
| 288 |
}
|
| 289 |
|
| 290 |
/**
|
| 291 |
* Instance termination processing.
|
| 292 |
*/
|
| 293 |
function ec2_instance_terminate_confirm_submit($form_id, $form_values) {
|
| 294 |
if ($form_values['confirm']) {
|
| 295 |
ec2_instance_terminate($form_values['instance_id']);
|
| 296 |
}
|
| 297 |
return 'aws/ec2/instance';
|
| 298 |
}
|
| 299 |
|
| 300 |
/**
|
| 301 |
* Process instance form terminations.
|
| 302 |
*/
|
| 303 |
function ec2_instance_terminate($instance_id) {
|
| 304 |
$response = aws_ec2_TerminateInstances(array($instance_id));
|
| 305 |
if ($response->code != "200") {
|
| 306 |
drupal_set_message(t('Amazon rejected instance termination request.'), 'error');
|
| 307 |
}
|
| 308 |
else {
|
| 309 |
$result_details = aws_xml_toarray($response->data);
|
| 310 |
$result = $result_details['TerminateInstancesResponse']['instancesSet']['item'];
|
| 311 |
$title = db_result(db_query("SELECT title FROM {ec2_instance} WHERE instance_id = '%s'", $result['instanceId']));
|
| 312 |
if ($result['shutdownState']['name'] == 'shutting-down') {
|
| 313 |
$result = db_query("DELETE FROM {ec2_instance} WHERE instance_id = '%s'", $result['instanceId']);
|
| 314 |
drupal_set_message(t('Instance %title is being terminated.', array('%title' => $title)));
|
| 315 |
}
|
| 316 |
else {
|
| 317 |
drupal_set_message(t('Instance %title was not terminated successfully.', array('%title' => $title)));
|
| 318 |
}
|
| 319 |
}
|
| 320 |
}
|
| 321 |
|
| 322 |
|
| 323 |
/**
|
| 324 |
* Generate a form to list images.
|
| 325 |
*/
|
| 326 |
function ec2_form_image() {
|
| 327 |
|
| 328 |
// Load favourite images
|
| 329 |
$image_list = ec2_image_list();
|
| 330 |
|
| 331 |
// Load image details from Amazon
|
| 332 |
$images = array();
|
| 333 |
if (count($image_list) > 0) {
|
| 334 |
$image_xml = aws_ec2_DescribeImages($image_list);
|
| 335 |
$image_details = aws_xml_toarray($image_xml->data);
|
| 336 |
if (is_array($image_details) && array_key_exists("_num", $image_details['DescribeImagesResponse']['imagesSet']['item'])) {
|
| 337 |
$images = $image_details['DescribeImagesResponse']['imagesSet']['item'];
|
| 338 |
}
|
| 339 |
else {
|
| 340 |
$images = $image_details['DescribeImagesResponse']['imagesSet'];
|
| 341 |
}
|
| 342 |
}
|
| 343 |
|
| 344 |
foreach ($images as $key => $image) {
|
| 345 |
// Non-data entries are prefixed by _
|
| 346 |
if (substr($key, 0, 1) != '_') {
|
| 347 |
$form['imageId'][$image['imageId']] = array('#value' => check_plain($image['imageId']));
|
| 348 |
$form['imageLocation'][$image['imageId']] = array('#value' => check_plain($image['imageLocation']));
|
| 349 |
$form['imageState'][$image['imageId']] = array('#value' => check_plain($image['imageState']));
|
| 350 |
$form['operations'][$image['imageId']] = array('#value' => l(t('remove'), 'aws/ec2/image/delete/'. check_plain($image['imageId']), array()));
|
| 351 |
}
|
| 352 |
}
|
| 353 |
return $form;
|
| 354 |
}
|
| 355 |
|
| 356 |
/**
|
| 357 |
* Theme image list.
|
| 358 |
*/
|
| 359 |
function theme_ec2_form_image($form) {
|
| 360 |
// Overview table:
|
| 361 |
$header = array(t('AMI ID'), t('Manifest'), t('State'), ('Operations'));
|
| 362 |
|
| 363 |
$output = drupal_render($form['options']);
|
| 364 |
if (isset($form['imageId']) && is_array($form['imageId'])) {
|
| 365 |
foreach (element_children($form['imageId']) as $key) {
|
| 366 |
$row = array();
|
| 367 |
$row[] = drupal_render($form['imageId'][$key]);
|
| 368 |
$row[] = drupal_render($form['imageLocation'][$key]);
|
| 369 |
$row[] = drupal_render($form['imageState'][$key]);
|
| 370 |
$row[] = drupal_render($form['operations'][$key]);
|
| 371 |
$rows[] = $row;
|
| 372 |
}
|
| 373 |
|
| 374 |
}
|
| 375 |
else {
|
| 376 |
$rows[] = array(array('data' => t('No images available.'), 'colspan' => '8'));
|
| 377 |
}
|
| 378 |
|
| 379 |
$output .= theme('table', $header, $rows);
|
| 380 |
$output .= drupal_render($form);
|
| 381 |
|
| 382 |
return $output;
|
| 383 |
}
|
| 384 |
|
| 385 |
/**
|
| 386 |
* Generate a form to add/delete images.
|
| 387 |
*/
|
| 388 |
function ec2_form_image_add () {
|
| 389 |
$form['image_id'] = array(
|
| 390 |
'#type' => 'textfield',
|
| 391 |
'#title' => 'AMI ID',
|
| 392 |
'#description' => t('The list of available images is available at ') . l('Amazon', 'http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=101&resultOffset=0&sortField=9&sortOrder=0&filterEntryTypeID=-1') .'. '. t('Remeber to use the AMI ID and not the name of the image.'),
|
| 393 |
'#required' => TRUE,
|
| 394 |
);
|
| 395 |
$form['submit'] = array(
|
| 396 |
'#type' => 'submit',
|
| 397 |
'#value' => t('Submit'),
|
| 398 |
);
|
| 399 |
return $form;
|
| 400 |
}
|
| 401 |
|
| 402 |
/**
|
| 403 |
* Validate image form submissions.
|
| 404 |
*/
|
| 405 |
function ec2_form_image_add_validate($form_id, $form_values) {
|
| 406 |
if (substr($form_values['image_id'], 0, 4) != "ami-") {
|
| 407 |
form_set_error('image_id', t('Invalid AMI ID. The ID must begin with "ami-"'));
|
| 408 |
}
|
| 409 |
else if (strlen($form_values['image_id']) != 12) {
|
| 410 |
form_set_error('image_id', t('Invalid AMI ID. The length is incorrect.'));
|
| 411 |
}
|
| 412 |
else if (db_fetch_object(db_query("SELECT image_id FROM {ec2_image} WHERE image_id = '%s'", $form_values['image_id']))) {
|
| 413 |
form_set_error('image_id', t('This image already exists. Please enter a unique image ID.'));
|
| 414 |
}
|
| 415 |
else {
|
| 416 |
$response = aws_ec2_DescribeImages(array(check_plain($form_values['image_id'])));
|
| 417 |
if ($response->code != "200") {
|
| 418 |
form_set_error('image_id', t('Amazon did not recognise this image. Is it valid?'));
|
| 419 |
}
|
| 420 |
}
|
| 421 |
}
|
| 422 |
|
| 423 |
/**
|
| 424 |
* Process image form submissions.
|
| 425 |
*/
|
| 426 |
function ec2_form_image_add_submit($form_id, $form_values) {
|
| 427 |
$result = db_query("INSERT INTO {ec2_image} (image_id) VALUES ('%s')", $form_values['image_id']);
|
| 428 |
drupal_set_message(t('The image %image has been added.', array('%image' => $form_values['image_id'])));
|
| 429 |
return 'aws/ec2/image';
|
| 430 |
}
|
| 431 |
|
| 432 |
/**
|
| 433 |
* Confirm delete image form submit.
|
| 434 |
*/
|
| 435 |
function ec2_image_delete_confirm($image_id) {
|
| 436 |
$image_id = check_plain($image_id);
|
| 437 |
if (! db_result(db_query("SELECT image_id FROM {ec2_image} WHERE image_id = '%s'", $image_id))) {
|
| 438 |
drupal_set_message(t('Invalid image specified.'), 'error');
|
| 439 |
drupal_goto('aws/ec2/image');
|
| 440 |
}
|
| 441 |
$form['image_id'] = array('#type' => 'hidden', '#value' => $image_id);
|
| 442 |
$form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
|
| 443 |
|
| 444 |
return confirm_form($form, t('Are you sure you want to remove image %name from the list?', array('%name' => $image_id)),
|
| 445 |
'aws/ec2/image', '', t('Delete'), t('Cancel'));
|
| 446 |
}
|
| 447 |
|
| 448 |
/**
|
| 449 |
* Process delete image form submit.
|
| 450 |
*
|
| 451 |
*/
|
| 452 |
function ec2_image_delete_confirm_submit($form_id, $form_values) {
|
| 453 |
if ($form_values['confirm']) {
|
| 454 |
if (db_query("DELETE FROM {ec2_image} WHERE image_id = '%s'", $form_values['image_id'])) {
|
| 455 |
drupal_set_message(t('Image %name has been deleted.', array('%name' => $form_values['image_id'])));
|
| 456 |
}
|
| 457 |
else {
|
| 458 |
drupal_set_message(t('Image %name was not deleted successfully.', array('%name' => $form_values['image_id'])));
|
| 459 |
}
|
| 460 |
}
|
| 461 |
return 'aws/ec2/image';
|
| 462 |
}
|
| 463 |
|
| 464 |
/**
|
| 465 |
* Form callback: list images.
|
| 466 |
*/
|
| 467 |
function ec2_image_list() {
|
| 468 |
$image_list = array();
|
| 469 |
$result = db_query('SELECT image_id FROM {ec2_image}');
|
| 470 |
while ($image = db_fetch_object($result)) {
|
| 471 |
$image_list[$image->image_id] = $image->image_id;
|
| 472 |
}
|
| 473 |
return $image_list;
|
| 474 |
}
|
| 475 |
|
| 476 |
|
| 477 |
/**
|
| 478 |
* Generate a form to list keys.
|
| 479 |
*/
|
| 480 |
function ec2_form_key() {
|
| 481 |
|
| 482 |
// Load key details from our database
|
| 483 |
$result = db_query('SELECT * FROM {ec2_key}');
|
| 484 |
while ($key = db_fetch_array($result)) {
|
| 485 |
$key_list[$key['name']] = $key;
|
| 486 |
}
|
| 487 |
|
| 488 |
// Load key details from Amazon
|
| 489 |
$keys = array();
|
| 490 |
$key_xml = aws_ec2_DescribeKeyPairs();
|
| 491 |
$key_details = aws_xml_toarray($key_xml->data);
|
| 492 |
|
| 493 |
if (array_key_exists("_num", $key_details['DescribeKeyPairsResponse']['keySet']['item'])) {
|
| 494 |
$keys = $key_details['DescribeKeyPairsResponse']['keySet']['item'];
|
| 495 |
}
|
| 496 |
else {
|
| 497 |
$keys = $key_details['DescribeKeyPairsResponse']['keySet'];
|
| 498 |
}
|
| 499 |
|
| 500 |
foreach ($keys as $key => $keypair) {
|
| 501 |
// Non-data entries are prefixed by _
|
| 502 |
if (substr($key, 0, 1) != '_') {
|
| 503 |
$form['keyName'][$keypair['keyName']] = array('#value' => check_plain($keypair['keyName']));
|
| 504 |
$form['keyFingerprint'][$keypair['keyName']] = array('#value' => check_plain($keypair['keyFingerprint']));
|
| 505 |
|
| 506 |
// Present a link to download the private key, if we know it.
|
| 507 |
if ($key_list[$keypair['keyName']]['private_key']) {
|
| 508 |
$form['download'][$keypair['keyName']][] = array('#value' => l(t('download'), 'aws/ec2/key/download/'. check_plain($keypair['keyName']), array()));
|
| 509 |
}
|
| 510 |
$form['delete'][$keypair['keyName']] = array('#value' => l(t('delete'), 'aws/ec2/key/delete/'. check_plain($keypair['keyName']), array()));
|
| 511 |
}
|
| 512 |
}
|
| 513 |
return $form;
|
| 514 |
}
|
| 515 |
|
| 516 |
/**
|
| 517 |
* Theme instance list.
|
| 518 |
*/
|
| 519 |
function theme_ec2_form_key($form) {
|
| 520 |
// Overview table:
|
| 521 |
$header = array(t('Keypair name'), t('Fingerprint'), array('data' => t('Operations'), 'colspan' => '2'));
|
| 522 |
|
| 523 |
$output = drupal_render($form['options']);
|
| 524 |
if (isset($form['keyName']) && is_array($form['keyName'])) {
|
| 525 |
foreach (element_children($form['keyName']) as $key) {
|
| 526 |
$row = array();
|
| 527 |
$row[] = drupal_render($form['keyName'][$key]);
|
| 528 |
$row[] = drupal_render($form['keyFingerprint'][$key]);
|
| 529 |
$row[] = drupal_render($form['download'][$key]);
|
| 530 |
$row[] = drupal_render($form['delete'][$key]);
|
| 531 |
$rows[] = $row;
|
| 532 |
}
|
| 533 |
}
|
| 534 |
else {
|
| 535 |
$rows[] = array(array('data' => t('No keys available.'), 'colspan' => '8'));
|
| 536 |
}
|
| 537 |
|
| 538 |
$output .= theme('table', $header, $rows);
|
| 539 |
$output .= drupal_render($form);
|
| 540 |
|
| 541 |
return $output;
|
| 542 |
}
|
| 543 |
|
| 544 |
/**
|
| 545 |
* Generate a form to add keys.
|
| 546 |
*/
|
| 547 |
function ec2_form_key_add () {
|
| 548 |
$form['name'] = array(
|
| 549 |
'#type' => 'textfield',
|
| 550 |
'#title' => 'Key name',
|
| 551 |
'#required' => TRUE,
|
| 552 |
);
|
| 553 |
$form['submit'] = array(
|
| 554 |
'#type' => 'submit',
|
| 555 |
'#value' => t('Submit'),
|
| 556 |
);
|
| 557 |
return $form;
|
| 558 |
}
|
| 559 |
|
| 560 |
/**
|
| 561 |
* Validate key form submissions.
|
| 562 |
*/
|
| 563 |
function ec2_form_key_add_validate($form_id, $form_values) {
|
| 564 |
if (db_result(db_query("SELECT name FROM {ec2_key} WHERE name = '%s'", $form_values['name']))) {
|
| 565 |
form_set_error('name', t('This key already exists. Please enter a unique key name.'));
|
| 566 |
}
|
| 567 |
}
|
| 568 |
|
| 569 |
/**
|
| 570 |
* Process key form submissions.
|
| 571 |
*/
|
| 572 |
function ec2_form_key_add_submit($form_id, $form_values) {
|
| 573 |
$response = aws_ec2_CreateKeyPair(check_plain($form_values['name']));
|
| 574 |
if ($response->code != "200") {
|
| 575 |
drupal_set_message(t('Amazon rejected new key.'), 'error');
|
| 576 |
}
|
| 577 |
else {
|
| 578 |
$key_details = aws_xml_toarray($response->data);
|
| 579 |
$fingerprint = $key_details['CreateKeyPairResponse']['keyFingerprint'];
|
| 580 |
$private_key = $key_details['CreateKeyPairResponse']['keyMaterial'];
|
| 581 |
$result = db_query("INSERT INTO {ec2_key} (name, fingerprint, private_key) VALUES ('%s', '%s', '%s')", $form_values['name'], $fingerprint, $private_key);
|
| 582 |
drupal_set_message(t('The key %key has been added.', array('%key' => $form_values['name'])));
|
| 583 |
}
|
| 584 |
return 'aws/ec2/key';
|
| 585 |
}
|
| 586 |
|
| 587 |
/**
|
| 588 |
* Form callback: list images.
|
| 589 |
*/
|
| 590 |
function ec2_key_list() {
|
| 591 |
$key_list = array();
|
| 592 |
$result = db_query('SELECT name FROM {ec2_key}');
|
| 593 |
while ($key = db_fetch_object($result)) {
|
| 594 |
$key_list[$key->name] = $key->name;
|
| 595 |
}
|
| 596 |
return $key_list;
|
| 597 |
}
|
| 598 |
|
| 599 |
/**
|
| 600 |
* Confirm delete key form submit.
|
| 601 |
*/
|
| 602 |
function ec2_key_delete_confirm($key) {
|
| 603 |
$key = check_plain($key);
|
| 604 |
if (! db_result(db_query("SELECT name FROM {ec2_key} WHERE name = '%s'", $key))) {
|
| 605 |
drupal_set_message(t('Invalid key specified.'), 'error');
|
| 606 |
drupal_goto('aws/ec2/key');
|
| 607 |
}
|
| 608 |
$form['key'] = array('#type' => 'hidden', '#value' => $key);
|
| 609 |
$form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
|
| 610 |
|
| 611 |
return confirm_form($form, t('Are you sure you want to delete key %name?', array('%name' => $key)),
|
| 612 |
'aws/ec2/key', '', t('Delete'), t('Cancel'));
|
| 613 |
}
|
| 614 |
|
| 615 |
/**
|
| 616 |
* Process delete key form submit.
|
| 617 |
*/
|
| 618 |
function ec2_key_delete_confirm_submit($form_id, $form_values) {
|
| 619 |
if ($form_values['confirm']) {
|
| 620 |
ec2_key_delete($form_values['key']);
|
| 621 |
}
|
| 622 |
return 'aws/ec2/key';
|
| 623 |
}
|
| 624 |
|
| 625 |
/**
|
| 626 |
* Delete key.
|
| 627 |
*/
|
| 628 |
function ec2_key_delete($key) {
|
| 629 |
$key = check_plain($key);
|
| 630 |
$response = aws_ec2_DeleteKeyPair($key);
|
| 631 |
if ($response->code != "200") {
|
| 632 |
drupal_set_message(t('Amazon rejected key deletion request.'), 'error');
|
| 633 |
}
|
| 634 |
else {
|
| 635 |
$result_details = aws_xml_toarray($response->data);
|
| 636 |
$result = $result_details['DeleteKeyPairResponse']['return'];
|
| 637 |
if ($result == 'true') {
|
| 638 |
$result = db_query("DELETE FROM {ec2_key} WHERE name = '%s'", $key);
|
| 639 |
drupal_set_message(t('Key %name has been deleted.', array('%name' => $key)));
|
| 640 |
}
|
| 641 |
else {
|
| 642 |
drupal_set_message(t('Key %name was not deleted successfully.', array('%name' => $key)));
|
| 643 |
}
|
| 644 |
}
|
| 645 |
}
|
| 646 |
|
| 647 |
/**
|
| 648 |
* Process delete key form submit.
|
| 649 |
*/
|
| 650 |
function ec2_key_download($key) {
|
| 651 |
if ($private_key = db_result(db_query("SELECT private_key FROM {ec2_key} WHERE name = '%s'", check_plain($key)))) {
|
| 652 |
drupal_set_header('Content-Type: text/plain; charset=utf-8');
|
| 653 |
print $private_key;
|
| 654 |
}
|
| 655 |
else {
|
| 656 |
drupal_set_message(t('The private key for key %name was not found.', array('%name' => check_plain($key))));
|
| 657 |
drupal_goto('aws/ec2/key');
|
| 658 |
}
|
| 659 |
}
|
| 660 |
|
| 661 |
|