Skip to content
Snippets Groups Projects
Commit 66d9a5c0 authored by baldwinlouie's avatar baldwinlouie
Browse files

Updated README.txt files for aws_cloud and cloud module.

parent 49cd3a23
No related branches found
No related tags found
No related merge requests found
...@@ -5,12 +5,50 @@ Cloud 7.x-1.x ...@@ -5,12 +5,50 @@ Cloud 7.x-1.x
BASIC INFO BASIC INFO
========== ==========
- Provides common functionalites for cloud management. Cloud (a.k.a Clanavi) is a set of modules that lets users administer public and
- Cloud module is a heart of cloud package. This requires at least one private clouds from within Drupal. Clanavi provide users the ability to manage public
"cloud support module" such as AWS module, XCP and so on. clouds such as AWS EC2 clouds as well as private clouds like OpenStack, Eucalyptus and XCP.
- Once you install the 'Cloud' module be sure to grant the 'access dashboard' permission.
Users with above permission can view all the running instances for the enabled sub-clouds. FEATURES
- Be sure to only grant 'administer cloud' permission to the cloud administrator. ========
- Clanavi provides functionalites for cloud management, such as:
* starting/stopping instances
* managing volumes and snapshot
* firewall management with security groups
* SSH Key management
- Clanavi features a pluggable architecture and can be extended to support other
public/private clouds.
- Clanavi provides hooks for external modules to interact with instances when they
are stopped and started. See cloud.api.php for more details.
INSTALLATION: FOR USAGE WITH AMAZON EC2
=======================================
1. Download Libraries Module.
2. Download the AWS SDK for PHP from Amazon: http://pear.amazonwebservices.com/get/sdk-latest.zip
3. Copy the AWS SDK for PHP into sites/all/libraries as awssdk. If the SDK library is not installed correctly
the module will not function properly
3. Turn on the Cloud module
4. Turn on the following modules for EC2 management
- Server Templates
- AWS Cloud (Found under Iaas package)
- Pricing
5. In order for the system to automatically update the database with EC2 data, cron must be running. If cron is not running,
data can still be imported using the "Refresh page" link at the bottom of listing pages
BASIC USAGE: AMAZON EC2
=======================
1. Download, install and enable all modules as described in the installation instructions
2. Go to admin/config/clouds to add an Amazon EC2 Region. Click Add Cloud
3. Enter properly EC2 credentials and save
4. Upon successfully saving the credentials, click the link in the success message to download all EC2 data
Additional documentation on Clanavi can be found at: http://drupal.org/node/1855518
PERMISSONS
==========
Clanavi provides a very granular permission set for managing instances. For example, certain user roles
can be configured to administer instances by giving them view, edit and delete permissions. Other user roles
can be configured for read only.
e.g. (For an administrator) e.g. (For an administrator)
- Turn on - Turn on
...@@ -23,20 +61,15 @@ e.g. (For an administrator) ...@@ -23,20 +61,15 @@ e.g. (For an administrator)
* Please see also cloud/modules/iaas/modules/aws_cloud/README.txt for AWS compatible cloud. * Please see also cloud/modules/iaas/modules/aws_cloud/README.txt for AWS compatible cloud.
* After ver.1.1, the database schema has changed. If you already install
Clanavi 1.01 or less (before 06/10/2011), please uninstall and install modules.
SYSTEM REQUIREMENTS SYSTEM REQUIREMENTS
=================== ===================
- PHP 5.2 or Higher - PHP 5.2 or Higher
- MySQL 5.1 or Higher - MySQL 5.1 or Higher
- Drupal 6.x - Drupal 7.x
- 512MB Memory: If you use Amazon EC2 module, the running host of this - 512MB Memory: If you use AWS Cloud module, the running host of this
system requires more than 512MB memory to download a list of images system requires more than 512MB memory to download a list of images
(because it's huge amount of data for listing). (because it's huge amount of data for listing).
DIRECTORY STRUCTURE DIRECTORY STRUCTURE
=================== ===================
...@@ -44,49 +77,18 @@ cloud ...@@ -44,49 +77,18 @@ cloud
+-modules (depends on Cloud module) (Cloud is a core module for Cloud package) +-modules (depends on Cloud module) (Cloud is a core module for Cloud package)
+-cloud_activity_audit +-cloud_activity_audit
+-cloud_alerts +-cloud_alerts
x-cloud_auto_scaling
x-cloud_billing
x-cloud_cluster
x-cloud_dashboard
x-cloud_failover
+-cloud_inputs +-cloud_inputs
x-cloud_metering
+-cloud_pricing +-cloud_pricing
x-cloud_resource_allocator
x-cloud_scaling_manager
+-cloud_scripting +-cloud_scripting
+-cloud_server_templates +-cloud_server_templates
+-modules
x... Not released yet. -iaas
- aws_cloud
hook_* FOR SUBSIDIARY MODULES hook_* FOR SUBSIDIARY MODULES
============================= =============================
See cloud.api.php for more details on api functions and hooks
* See also cloud/cloud.api.php
* Example for Sub Cloud Family
e.g. Amazon EC2, XCP and so on...
function hook_cloud_set_info() {
return array(
'cloud_name' => 'generic_cloud_context', // Used for cloud ID
'cloud_display_name' => 'Generic Cloud' , // Uuser for display name in menu, etc.
'instance_types' => array(
)
);
}
function hook_server_template($op, $params = array())
$form = array( // make some form);
return $form;
}
Copyright Copyright
========= =========
Copyright (c) 2011-2012 DOCOMO Innovations, Inc. Copyright (c) 2011-2012 DOCOMO Innovations, Inc.
\ No newline at end of file
End of README.txt
\ No newline at end of file
BASIC INFO BASIC INFO
========== ==========
- THIS MODULES DOES NOT DO ANYTHING AT THIS TIME.
- Provides a set of trigger and action for cloud management - Provides a set of trigger and action for cloud management
- Works with Cloud. Failover and auto-scaling module. - Works with Cloud. Failover and auto-scaling module.
......
...@@ -61,6 +61,6 @@ CHANGE HISTORY ...@@ -61,6 +61,6 @@ CHANGE HISTORY
Copyright Copyright
========= =========
Copyright (c) 2010-2011 DOCOMO Innovations, Inc. Copyright (c) 2010-2012 DOCOMO Innovations, Inc.
End of README.txt End of README.txt
\ No newline at end of file
...@@ -60,6 +60,6 @@ CHANGE HISTORY ...@@ -60,6 +60,6 @@ CHANGE HISTORY
Copyright Copyright
========= =========
Copyright (c) 2010-2011 DOCOMO Innovations, Inc. Copyright (c) 2010-2012 DOCOMO Innovations, Inc.
End of README.txt End of README.txt
\ No newline at end of file
BASIC INFO BASIC INFO
========== ==========
- THIS MODULE IS NOT STABLE FOR USAGE. USE AT YOUR OWN RISK.
- Provides failover feature. - Provides failover feature.
- Works with Cloud, Cluster and Scripting module. - Works with Cloud, Cluster and Scripting module.
...@@ -36,6 +36,6 @@ CHANGE HISTORY ...@@ -36,6 +36,6 @@ CHANGE HISTORY
Copyright Copyright
========= =========
Copyright (c) 2011 DOCOMO Innovations, Inc. Copyright (c) 2012 DOCOMO Innovations, Inc.
End of README.txt End of README.txt
\ No newline at end of file
...@@ -60,6 +60,4 @@ CHANGE HISTORY ...@@ -60,6 +60,4 @@ CHANGE HISTORY
Copyright Copyright
========= =========
Copyright (c) 2011 DOCOMO Innovations, Inc. Copyright (c) 2012 DOCOMO Innovations, Inc.
End of README.txt
\ No newline at end of file
...@@ -70,6 +70,6 @@ CHANGE HISTORY ...@@ -70,6 +70,6 @@ CHANGE HISTORY
Copyright Copyright
========= =========
Copyright (c) 2011 DOCOMO Innovations, Inc. Copyright (c) 2012 DOCOMO Innovations, Inc.
End of README.txt End of README.txt
...@@ -14,29 +14,82 @@ SYSTEM REQUIREMENTS ...@@ -14,29 +14,82 @@ SYSTEM REQUIREMENTS
(because it's huge amount of data for listing). (because it's huge amount of data for listing).
- For OpenStack, use Cactus. - For OpenStack, use Cactus.
HOW TO USE INSTALLATION: FOR USAGE WITH AMAZON EC2
========== =======================================
- Download the Libraries module, http://drupal.org/project/libraries. 1. Download Libraries Module.
- The AWSSDK for PHP is required. Download it from http://aws.amazon.com/sdkforphp/ and move it 2. Download the AWS SDK for PHP from Amazon: http://pear.amazonwebservices.com/get/sdk-latest.zip
into sites/all/libraries/awssdk 3. Copy the AWS SDK for PHP into sites/all/libraries as awssdk. If the SDK library is not installed correctly
- Enable AWS Cloud module (Requires Cloud module) the module will not function properly
- Add a new cloud using the AWS Cloud in Administer Menu, then goto 3. Turn on the Cloud module
4. Turn on the following modules for EC2 management
i) Clouds | [NEW CLOUD] | Instaces -> Click - Refresh Page - - Server Templates
ii) Clouds | [NEW CLOUD] | Images -> Click - Refresh Page - - AWS Cloud (Found under Iaas package)
- Pricing
- You also need to setup cron.php on Drupal for automatic update 5. In order for the system to automatically update the database with EC2 data, cron must be running. If cron is not running,
for the latest information on EC2. data can still be imported using the "Refresh page" link at the bottom of listing pages
6. Make sure the sites/default/files directory is writable.
- Make 'file' directory writable. e.g. If you have /var/www/html/sites/default/files,
e.g. If you have /var/www/html/sites/default/files, chmod 777 /var/www/html/sites/default/files
chmod 777 /var/www/html/sites/default/files
(because it creates files/cloud sub-directory) (because it creates files/cloud sub-directory)
BASIC USAGE
===========
The following sections describes how to use aws_cloud to manage instances and launch an instance.
1. CONFIGURE AWS CREDENTIALS
============================
1. Download, install and enable all modules as described in the installation instructions
2. Go to admin/config/clouds to add an Amazon EC2 Region. Click Add Cloud
3. Enter properly EC2 credentials and save
4. Upon successfully saving the credentials, click the link in the success message to refresh all EC2 data
5. Refresh the AWS images by going to Clouds > [YOUR CLOUD] > Images. Click the "Refresh Page".
2. SETUP SSH KEY
================
Setup a SSH Key for Clanavi to use to execute remote scripts and for SSH access via the SSH Console
1. Go to Clouds > [YOUR CLOUD] > SSH Keys
2. Click "Create" Button
3. Enter the "Key Pair Name"
4. Click "Create"
3. SETUP SERVER TEMPLATE
========================
Launching an EC2 instance requires a configured Server Template. A server template tells the system the following:
- AMI to launch (OS choices are packaged as AMIs. There are many to choose from such as Centos, Ubuntu...etc)
- Size of the instance (tiny, small, medium, large, #of processors). Detailed description of instances can be found
here: http://aws.amazon.com/ec2/instance-types/
- A Server Template can instruct the system to launch multiple instances at the same time
- The SSH key used with the ROOT user
- Security group the instance adheres to
- Zone to launch the server (us-west-1a, us-west-1b...etc)
1. Go to Clouds > [YOUR CLOUD] > Launch
2. Click "Create" Button
3. Fill out the "Name" field
4. Fill out the "EC2 Image" field with the AMI to use. This field will auto-complete to help you locate images
5. Fill out the "User Name". The "User Name" is used when trying to ssh into the machine via the SSH Console and when a remote script has to be invoked.
This is usually "root". For some ubuntu image, this will be "ubuntu"
6. Fill out the "SSH Key"
7. Select a security group. The default group is sufficient for basic purposes.
8. Fill out the "Count". Count tells the system how many instances to launch.
9. Select "Availablity Zone" if there is a preference.
10. Click "Add" to save the template
4. LAUNCH A SERVER TEMPLATE
===========================
Using the newly created server template, an instance can be launched.
1. Go to Clouds > [YOUR CLOUD] > Launch
2. Find the newly created template
3. Use the "Play" Icon to find the "Launch" command.
4. Click "Launch"
5. A message will inform you of the launch status. If successful, the cloud listing
page will show the instance state as "Pending".
6. If cron is running, the status will be updated automatically. If there is no cron running,
click the "Refresh Page"
How to Install OpenStack on Amazon EC2 How to Install OpenStack on Amazon EC2
====================================== ======================================
1) Launch an 'm1.large' Instance of 1) Launch an 'm1.large' Instance of
e.g. us-east-1 : ubuntu-natty-11.04-amd64-server-20110426 (ami-68ad5201) e.g. us-east-1 : ubuntu-natty-11.04-amd64-server-20110426 (ami-68ad5201)
...@@ -142,9 +195,7 @@ SSH CONSOLE SETUP ...@@ -142,9 +195,7 @@ SSH CONSOLE SETUP
MOUDLE DEPENDENCY MOUDLE DEPENDENCY
================= =================
This module works with the following modules: This module works with the following modules:
- Cloud - Cloud
- Server Templates - Server Templates
- Libraries - Libraries
...@@ -172,14 +223,9 @@ x... NOTE: NOT IMPLEMENTED. SHOWN ONLY AS A REFERENCE AND PROPOSED STRUCTURE SP ...@@ -172,14 +223,9 @@ x... NOTE: NOT IMPLEMENTED. SHOWN ONLY AS A REFERENCE AND PROPOSED STRUCTURE SP
LIMITATION LIMITATION
========== ==========
- Some of features are not implemented - Some of features are not implemented
Copyright Copyright
========= =========
Copyright (c) 2010-2011 DOCOMO Innovations, Inc. Copyright (c) 2010-2012 DOCOMO Innovations, Inc.
End of README.txt
\ No newline at end of file
...@@ -823,8 +823,8 @@ function aws_cloud_cron() { ...@@ -823,8 +823,8 @@ function aws_cloud_cron() {
foreach ($clouds as $cloud) { foreach ($clouds as $cloud) {
aws_cloud_fetch_data($cloud['cloud_name']); // This will fetch Cloud related information aws_cloud_fetch_data($cloud['cloud_name']); // This will fetch Cloud related information
// load the bundle code and run it // load the bundle code and run it
module_load_include('inc', 'aws_cloud', 'includes/aws_cloud_bundle_instance'); // module_load_include('inc', 'aws_cloud', 'includes/aws_cloud_bundle_instance');
aws_cloud_process_bundle_instance(); // aws_cloud_process_bundle_instance();
} }
} }
......
<?php
/**
* @file
* Functions for bundling instance
*/
/**
* Helper function to replace script values
*/
function aws_cloud_replace_script_values($contents, $instance) {
$cloud_context = $instance['cloud_context'];
$script = str_replace("\r\n", "\n", $contents);
// Update bucketname
$nickname = str_replace( array('-', '_', ':', ' '), '', $instance['nickname']);
// Replace necessary parameters
$script = str_replace("@CLANAVI_BUCKET_NAME", $instance['bucketname'], $script);
$script = str_replace("@CLANAVI_FOLDER_NAME", $instance['foldername'], $script);
$script = str_replace("@CLANAVI_IMAGE_NAME", $instance['nickname'], $script);
// check size
$script = str_replace("@CLANAVI_SIZE", $instance['volume_size'], $script);
$script = str_replace("@CLANAVI_AWS_USER_ID", cloud_get_info($cloud_context, 'user_id'), $script);
$script = str_replace("@CLANAVI_AWS_ACCESS_KEY_ID", cloud_get_info($cloud_context, 'aws_access_key'), $script);
$script = str_replace("@CLANAVI_AWS_SECRET_ACCESS_KEY", cloud_get_info($cloud_context, 'aws_secret_key'), $script);
$script = str_replace("@CLANAVI_IMAGE_UPLOAD_URL", cloud_get_info($cloud_context, 'image_upload_url'), $script);
$script = str_replace("@CLANAVI_IMAGE_REGISTER_URL", cloud_get_info($cloud_context, 'image_register_url'), $script);
return $script;
}
/**
* Helper function to initiate bundling images
*/
function aws_cloud_init_bundling() {
$instances = aws_cloud_db_bundle_instance_get('initiated');
foreach ($instances as $inst) {
$instance_id = $inst['instance_id'];
$cloud_context = $inst['cloud_context'];
$instance = cloud_get_instance($cloud_context, $instance_id);
if ($instance) {
$ssh_user_name = aws_cloud_get_ssh_user( $cloud_context, $instance_id );
$key_name = $instance['key_name'];
$instance_ip = $instance['dns_name'];
$perm_file = _cloud_scripting_get_perm_file($instance_id, $key_name, $cloud_context);
// check ssh enabled
if (_cloud_scripting_is_ssh_enabled($cloud_context, $instance_ip, $instance_id, $perm_file) == '1') {
// prepare bundle_instance directory
if (($path = _cloud_scripting_prepare_directory($instance_id . CLOUD_PATH_SEPARATOR . 'bundle_instance')) != FALSE) {
$script_file = drupal_realpath($path . CLOUD_PATH_SEPARATOR . 'bundle_instance.sh');
$cert_file = drupal_realpath($path . CLOUD_PATH_SEPARATOR . 'cert.pem');
$perm_file = _cloud_scripting_get_perm_file($instance_id, $key_name, $cloud_context);
// read in the script contents
$script_filename = drupal_get_path('module', 'aws_cloud') . CLOUD_PATH_SEPARATOR . 'scripts/aws_cloud_bundle_image_ami_tools.sh';
$script_handle = fopen( $script_filename, "r" );
$contents = fread($script_handle, filesize($script_filename) );
fclose($script_handle);
// return a script with the contents replaced with real values
$script_str = aws_cloud_replace_script_values($contents, array_merge($inst, $instance));
//write out the script_str and the perm file
if (file_unmanaged_save_data($script_str, $script_file, FILE_EXISTS_REPLACE) != FALSE &&
file_unmanaged_save_data(cloud_get_info($cloud_context, 'certificate'), $cert_file, FILE_EXISTS_REPLACE) != FALSE) {
$script_dest_path = '/tmp/clanavi/' . $instance_id . CLOUD_PATH_SEPARATOR . 'bundle_instance.sh';
$log_file_name = drupal_realpath($path) . '/' . REQUEST_TIME . '.log';
// Update status
aws_cloud_db_bundle_instance_update_status($instance_id, 'processed');
$tmp_args = ' ' . $instance_ip . ' ' . $perm_file . ' ' . $log_file_name . ' ' . "\"" . $script_file . "\"" .
' ' . $cert_file . ' ' . $perm_file .
' ' . $instance_id . ' bundle_instance ' . CLOUD_SSH_PATH . ' ' . CLOUD_SCP_PATH .
' ' . drupal_realpath('public://cloud') . ' ' . drupal_realpath('public://cloud') . ' ' . $ssh_user_name .
' ' . "\"" . $script_dest_path . "\"" ;
$command = CLOUD_PHP_PATH . ' -f ' . dirname(__FILE__) . CLOUD_PATH_SEPARATOR . 'aws_cloud_bundle_instance_exec.inc ' . $tmp_args . ' >> /tmp/cloud.log' ;
exec($command, $output, $retval);
}
else {
// set watchdog
$error = t('Unable to write script file/certificate file. Cannot bundle instance: @instance', array('@instance' => $instance_id));
watchdog('aws_cloud', $error);
}
}
}
else {
// set watchdog where ssh is not enabled
$error = t('Cannot ssh into instance: @instance. Unable to bundle instance.', array('@instance' => $instance_id));
watchdog('aws_cloud', $error);
}
}
}
}
/**
* Function to process bundle
*/
function aws_cloud_process_bundle() {
// Check the in progress bundle_instances
$instances = aws_cloud_db_bundle_instance_get('processed');
foreach ($instances as $inst) {
$cloud_context = $inst['cloud_context'];
$instance_id = $inst['instance_id'];
$instance = cloud_get_instance($cloud_context, $instance_id);
if ($instance) {
$key_name = $instance['key_name'];
$instance_ip = $instance['dns_name'];
$perm_file = _cloud_scripting_get_perm_file($instance_id, $key_name, $cloud_context);
$status = 'success';
$completed = _aws_cloud_bundle_instance_check_status($cloud_context, $instance_ip, $instance_id, $perm_file, $status);
if ($completed) {
aws_cloud_db_bundle_instance_update_status($instance_id, $status);
continue;
}
$status = 'failed';
$failed = _aws_cloud_bundle_instance_check_status($cloud_context, $instance_ip, $instance_id, $perm_file, $status);
if ($failed) {
aws_cloud_db_bundle_instance_update_status($instance_id, $status);
continue;
}
}
}
}
/**
* Register the bundled image
*/
function aws_cloud_register_bundle_image() {
$inst_to_bundle = aws_cloud_db_bundle_instance_get('success');
foreach ($inst_to_bundle as $inst) {
$cloud_context = $inst['cloud_context'] ;
$bucket = $inst['bucketname'] . '/' . $inst['foldername'] . '/' . $inst['nickname'] . '.manifest.xml' ;
$obj_response = aws_cloud_sdk_register_image($cloud_context, $bucket);
$instance_id = $inst['instance_id'];
if (isset($obj_response->imageId) && empty($obj_response->imageId) == FALSE) {
$img_id = $obj_response->imageId;
aws_cloud_db_bundle_instance_update_status($instance_id, 'complete:' . $img_id);
}
else {
aws_cloud_db_bundle_instance_update_status($instance_id, 'register-failed');
}
}
}
/**
* Bundle cloud instance
*/
function aws_cloud_process_bundle_instance() {
$script_filename = drupal_get_path('module', 'aws_cloud') . CLOUD_PATH_SEPARATOR . 'scripts/aws_cloud_bundle_image_ami_tools.sh';
$script_handle = fopen( $script_filename, "r" );
$contents = fread($script_handle, filesize($script_filename) );
fclose( $script_handle );
$output_dir = _aws_cloud_prepare_directory();
if ($output_dir != FALSE) {
// initialize any instances that needs to be bundled
aws_cloud_init_bundling();
// Registration Process
aws_cloud_process_bundle();
// Register image
aws_cloud_register_bundle_image();
}
}
/**
* Bundle cloud instance
*/
function aws_cloud_process_bundle_instance_1() {
$script_filename = drupal_get_path('module', 'aws_cloud') . CLOUD_PATH_SEPARATOR . 'scripts/aws_cloud_bundle_image_ami_tools.sh';
$script_handle = fopen( $script_filename, "r" );
$contents = fread($script_handle, filesize($script_filename) );
fclose( $script_handle );
$output_dir = _aws_cloud_prepare_directory();
if ($output_dir != FALSE) {
// lift into another function
$inst_to_bundle = aws_cloud_db_bundle_instance_get('initiated');
foreach ($inst_to_bundle as $inst) {
$cloud_context = $inst['cloud_context'] ;
$certificate = cloud_get_info($cloud_context, 'certificate' );
$image_upload_url = cloud_get_info($cloud_context, 'image_upload_url' );
$user_id = cloud_get_info($cloud_context, 'user_id' );
$access_key = cloud_get_info($cloud_context, 'aws_access_key' );
$secret_key = cloud_get_info($cloud_context, 'aws_secret_key' );
$image_register_url = cloud_get_info($cloud_context, 'image_register_url' );
$instance_id = $inst['instance_id'];
$vol_size = $inst['volume_size'];
$nickname = $inst['nickname' ];
$bucketname = $inst['bucketname'];
$foldername = $inst['foldername'];
$imagename = $inst['nickname' ];
$instance = cloud_get_instance($cloud_context, $instance_id);
if ($instance) {
$ssh_user_name = aws_cloud_get_ssh_user( $cloud_context, $instance_id );
$key_name = $instance['key_name'];
$instance_ip = $instance['dns_name'];
$dir = $output_dir . CLOUD_PATH_SEPARATOR . $instance_id . CLOUD_PATH_SEPARATOR . 'bundle_instance';
$command = 'mkdir -p ' . $dir;
exec($command, $output, $retval); //command for creating new folder
$script_file = $dir . CLOUD_PATH_SEPARATOR . 'bundle_instance.sh' ;
$cert_file = $dir . CLOUD_PATH_SEPARATOR . 'cert.pem' ;
//$perm_file = _cloud_scripting_create_temp_permission_file($cloud_context, $instance_id, $key_name);
$perm_file = _cloud_scripting_get_perm_file($instance_id, $key_name, $cloud_context);
$script_str = str_replace("\r\n", "\n", $contents );
// Update bucketname
$nickname = str_replace( array('-', '_', ':', ' '), '' , $nickname );
// Replace necessary parameters
$script_str = str_replace("@CLANAVI_BUCKET_NAME" , $bucketname , $script_str );
$script_str = str_replace("@CLANAVI_FOLDER_NAME" , $foldername , $script_str );
$script_str = str_replace("@CLANAVI_IMAGE_NAME" , $imagename , $script_str );
$script_str = str_replace("@CLANAVI_SIZE" , $vol_size , $script_str );
$script_str = str_replace("@CLANAVI_AWS_USER_ID" , $user_id , $script_str );
$script_str = str_replace("@CLANAVI_AWS_ACCESS_KEY_ID" , $access_key , $script_str );
$script_str = str_replace("@CLANAVI_AWS_SECRET_ACCESS_KEY", $secret_key , $script_str );
$script_str = str_replace("@CLANAVI_IMAGE_UPLOAD_URL" , $image_upload_url , $script_str );
$script_str = str_replace("@CLANAVI_IMAGE_REGISTER_URL" , $image_register_url , $script_str );
$fh = fopen($script_file, 'w');
if ( $fh == FALSE) {
drupal_set_message( t('Unable to create the script file.'), 'error' );
continue;
}
fwrite($fh, $script_str );
fclose($fh);
$fh = fopen($cert_file, 'w');
if ( $fh == FALSE) {
drupal_set_message( t('Unable to create the certificate file.'), 'error' );
continue;
}
fwrite($fh, $certificate );
fclose($fh);
$script_dest_path = '/tmp/clanavi/' . $instance_id . CLOUD_PATH_SEPARATOR . 'bundle_instance.sh';
if ( _cloud_scripting_is_ssh_enabled($cloud_context, $instance_ip, $instance_id, $perm_file) == '0' ) {
// Instance is not ready for ssh
// Do not execute script right now
continue;
}
// Execute the bundle instance
$log_file_name = CLOUD_SCRIPTING_TMP_LOG_FILE_PATH . $instance_id . '/' . REQUEST_TIME . '.log' ;
// Update status
aws_cloud_db_bundle_instance_update_status($instance_id, 'processed');
$script_id = 'bundle_instance';
//BALDWIN - THIS DOESN'T WORK YET
$tmp_args = ' ' . $instance_ip . ' ' . $perm_file . ' ' . $log_file_name . ' ' . "\"" . $script_file . "\"" .
' ' . $cert_file . ' ' . $perm_file .
' ' . $instance_id . ' ' . $script_id . ' ' . CLOUD_SSH_PATH . ' ' . CLOUD_SCP_PATH .
' ' . CLOUD_SCRIPTING_TEMP_DIRECTORY . ' ' . CLOUD_SCRIPTING_OUTPUT_LOG_FILE . ' ' . $ssh_user_name .
' ' . "\"" . $script_dest_path . "\"" ;
$command = CLOUD_PHP_PATH . ' -f ' . dirname(__FILE__) . CLOUD_PATH_SEPARATOR . 'aws_cloud_bundle_instance_exec.inc ' . $tmp_args . ' >> /tmp/cloud.log' ;
exec( $command, $output, $retval);
}
}
// Check the in progress bundle_instances
$inst_to_bundle = aws_cloud_db_bundle_instance_get('processed');
foreach ($inst_to_bundle as $inst) {
$cloud_context = $inst['cloud_context'] ;
$instance_id = $inst['instance_id'];
$instance = cloud_get_instance($cloud_context, $instance_id);
if ($instance) {
$key_name = $instance['key_name'];
$instance_ip = $instance['dns_name'];
$dir = $output_dir . CLOUD_PATH_SEPARATOR . $instance_id . CLOUD_PATH_SEPARATOR . 'bundle_instance';
$command = 'mkdir -p ' . $dir;
exec($command, $output, $retval); //command for creating new empty folder
//$perm_file = _cloud_scripting_create_temp_permission_file($cloud_context, $instance_id, $key_name);
$perm_file = _cloud_scripting_get_perm_file($instance_id, $key_name, $cloud_context);
$status = 'success';
$completed = _aws_cloud_bundle_instance_check_status($cloud_context, $instance_ip, $instance_id, $perm_file, $status );
if ($completed) {
aws_cloud_db_bundle_instance_update_status($instance_id, $status);
continue;
}
$status = 'failed';
$failed = _aws_cloud_bundle_instance_check_status($cloud_context, $instance_ip, $instance_id, $perm_file, $status );
if ($failed) {
aws_cloud_db_bundle_instance_update_status($instance_id, $status);
continue;
}
}
}
// Registration Process
$inst_to_bundle = aws_cloud_db_bundle_instance_get('success');
foreach ($inst_to_bundle as $inst) {
$cloud_context = $inst['cloud_context'] ;
$bucket = $inst['bucketname'] . '/' . $inst['foldername'] . '/' . $inst['nickname'] . '.manifest.xml' ;
//$myclient = aws_ec2_api_register_image($cloud_context, $bucket);
$obj_response = aws_cloud_sdk_register_image($cloud_context, $bucket);
//$obj_response = new SimpleXMLElement($myclient->data);
$instance_id = $inst['instance_id'];
if ( isset($obj_response->imageId) && empty($obj_response->imageId) == FALSE ) {
$img_id = $obj_response->imageId;
aws_cloud_db_bundle_instance_update_status($instance_id, 'complete:' . $img_id );
continue;
}
else {
aws_cloud_db_bundle_instance_update_status($instance_id, 'regisHter-failed');
continue;
}
}
}
}
/**
*
* @param <type> $instance_ip
* @param <type> $instance_id
* @param <type> $perm_file
* @param <type> $script_id
* @return <type>
*/
function _aws_cloud_bundle_instance_check_status($cloud_context, $instance_ip, $instance_id, $perm_file, $status ) {
$ssh_user_name = aws_cloud_get_ssh_user( $cloud_context, $instance_id );
$status_file = '/tmp/clanavi/bundle_instance.' . $status;
$remote_command = "[ -f $status_file ] && echo '1' || echo '0'";
$command = CLOUD_SSH_PATH . ' ' . $ssh_user_name . '@' . $instance_ip . ' -i "' . $perm_file . '" -o \'StrictHostKeyChecking no\' "' . $remote_command . '"';
exec($command, $output, $retval);
$result = isset($output[0]) ? $output[0] : 0;
return $result;
}
\ No newline at end of file
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