1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2009, Eucalyptus Systems, Inc.
# All rights reserved.
#
# Redistribution and use of this software in source and binary forms, with or
# without modification, are permitted provided that the following conditions
# are met:
#
# Redistributions of source code must retain the above
# copyright notice, this list of conditions and the
# following disclaimer.
#
# Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Author: Neil Soman neil@eucalyptus.com
import getopt, sys, os
from euca2ools import Euca2ool, FileValidationError, DirValidationError, CopyError, MetadataReadError, Util
from subprocess import *
import platform
usage_string = """
Bundle the local filesystem of a running instance as a bundled image.
euca-bundle-vol -u, --user user -s, --size size_in_MB
[-c, --cert cert_path] [-k, --privatekey private_key_path]
[-a, --all] [-e, --exclude dir1, dir2,...dirN] [-p, --prefix prefix] [--[no-]inherit] [-v, --volume volume_path] [--fstab fstab_path] [--generate-fstab] [--kernel kernel_id] [--ramdisk ramdisk_id] [-B, --block-device-mapping mapping]
[-d, --destination destination_path] [--ec2cert ec2cert_path] [-r, --arch target_architecture] [--batch] [--version]
REQUIRED PARAMETERS
-u, --user User ID (12-digit) of the user who is bundling the image.
-s, --size Size for the image in MB (default: 10GB or 10240MB).
OPTIONAL PARAMETERS
-c, --cert Path to the user's PEM encoded certificate.
-k, --privatekey Path to the user's PEM encoded private key.
-a, --all Bundle all directories (including mounted filesystems).
-p, --prefix The prefix for the bundle image files. (default: image name).
--[no-]inherit Add (or do not add) instance metadata to the bundled image. Inherit is set by default.
-e, --exclude comma-separated list of directories to exclude.
--kernel The kernel to be associated with the bundled image.
--ramdisk The ramdisk to be associated with the bundled image.
-B, --block-device-mapping Default block device mapping for the image (comma-separated list of key=value pairs).
-d, --destination Directory to store the bundled image in (default: "/tmp"). Recommended.
--ec2cert The path to the Cloud's X509 public key certificate.
-r, --arch Target architecture for the image ('x86_64' or 'i386' default: 'x86_64').
-v, --volume Path to mounted volume to create the bundle from (default: "/").
--fstab Path to the fstab to be bundled into the image.
--generate-fstab Generate fstab to bundle into the image.
--batch Run in batch mode (compatibility only. has no effect).
"""
MAX_IMAGE_SIZE = 1024 * 10
version_string = """ euca-bundle-vol version: 1.0 (BSD)"""
def usage():
print usage_string
sys.exit()
def version():
print version_string
sys.exit()
def check_root():
if os.geteuid() == 0:
return
else:
print "Must be superuser to execute this command."
sys.exit()
def check_image_size(size):
if size > MAX_IMAGE_SIZE:
print "Image Size is too large (Max = %d MB)" % (MAX_IMAGE_SIZE)
sys.exit()
def parse_excludes(excludes_string):
excludes = []
if excludes_string:
excludes_string = excludes_string.replace(' ', '')
excludes = excludes_string.split(',')
return excludes
def get_instance_metadata(euca, ramdisk, kernel, mapping):
product_codes = None
ramdisk_id = ramdisk
kernel_id = kernel
block_dev_mapping = mapping
ancestor_ami_ids = None
try:
euca.can_read_instance_metadata()
if not ramdisk_id:
try:
ramdisk_id = euca.get_instance_ramdisk()
except MetadataReadError:
print "Unable to read ramdisk id"
if not kernel_id:
try:
kernel_id = euca.get_instance_kernel()
except MetadataReadError:
print "Unable to read kernel id"
if not block_dev_mapping:
try:
block_dev_mapping = euca.get_instance_block_device_mappings()
except MetadataReadError:
print "Unable to read block device mapping"
try:
product_codes = euca.get_instance_product_codes().split('\n')
except MetadataReadError:
print "Unable to read product codes"
try:
ancestor_ami_ids = euca.get_ancestor_ami_ids().split('\n')
except MetadataReadError:
print "Unable to read product codes"
except IOError:
print "Unable to read instance metadata. Pass the --no-inherit option if you wish to exclude instance metadata."
sys.exit()
return ramdisk_id, kernel_id, block_dev_mapping, product_codes, ancestor_ami_ids
def add_product_codes(product_code_string, product_codes):
if not product_codes:
product_codes = []
product_code_values = product_code_string.split(',')
for p in product_code_values:
product_codes.append(p)
return product_codes
def cleanup(path):
if os.path.exists(path):
os.remove(path)
def main():
euca = None
try:
euca = Euca2ool('a:c:k:u:B:d:br:p:s:v:e:',
['cert=', 'privatekey=', 'user=', 'prefix=', 'volume=', 'all',
'kernel=', 'ramdisk=', 'block-device-mapping=', 'destination=', 'ec2cert=', 'arch=', 'size=', 'exclude=', 'inherit=', 'no-inherit', 'batch', 'fstab=', 'generate-fstab', 'productcodes='])
except Exception, e:
print e
usage()
kernel=None
user=None
ramdisk=None
cert_path=euca.get_environ('EC2_CERT')
private_key_path=euca.get_environ('EC2_PRIVATE_KEY')
prefix='image'
size_in_MB = 10*1024
destination_path='/disk1'
ec2cert_path=euca.get_environ('EUCALYPTUS_CERT')
target_arch='x86_64'
mapping = None
excludes_string = None
excludes = None
all=False
volume_path = "/"
inherit = True
product_codes = None
ancestor_ami_ids = None
fstab_path = None
generate_fstab = False
product_code_string = None
user_string = euca.get_environ("EC2_USER_ID")
if user_string:
try:
user = int(user_string)
except ValueError:
print 'Invalid user', user_string
sys.exit()
user = user_string
for name, value in euca.opts:
if name in ('-h', '--help'):
usage()
elif name in ('-c', '--cert'):
cert_path = value
elif name in ('-k', '--privatekey'):
private_key_path = value
elif name in ('-u', '--user'):
try:
value = value.replace('-', '')
user = int(value)
except ValueError:
print 'Invalid user', value
sys.exit()
user = value
elif name == '--kernel':
kernel = value
elif name == '--ramdisk':
ramdisk = value
elif name in ('-p', '--prefix'):
prefix = value
elif name in ('-s', '--size'):
size_in_MB = int(value)
elif name in ('-v', '--volume'):
volume_path = value
elif name in ('-e', '--exclude'):
excludes_string = value
elif name in ('-d', '--destination'):
destination_path = value
elif name == '--ec2cert':
ec2cert_path = value
elif name in ('-a', '--all'):
all = True
elif name in ('-r', '--arch'):
target_arch = value
if target_arch != 'i386' and target_arch != 'x86_64':
print 'target architecture must be i386 or x86_64'
usage()
elif name in ('-B', '--block-device-mapping'):
mapping = value
elif name == '--no-inherit':
inherit = False
elif name == '--generate-fstab':
generate_fstab = True
elif name == '--fstab':
fstab_path = value
elif name == '--productcodes':
product_code_string = value
elif name == '--version':
version()
if size_in_MB and cert_path and private_key_path and user and ec2cert_path:
try:
euca.validate_file(cert_path)
except FileValidationError:
print 'Invalid cert'
sys.exit(1)
try:
euca.validate_file(private_key_path)
except FileValidationError:
print 'Invalid private key'
sys.exit(1)
try:
euca.validate_file(ec2cert_path)
except FileValidationError:
print 'Invalid ec2cert'
sys.exit(1)
try:
euca.validate_dir(volume_path)
except DirValidationError:
print 'Invalid directory', volume_path
sys.exit(1)
if generate_fstab and fstab_path:
print "--generate-fstab and --fstab path cannot both be set."
sys.exit(1)
if fstab_path:
try:
euca.validate_file(fstab_path)
except FileValidationError:
print 'Invalid fstab path'
sys.exit(1)
if not fstab_path:
if platform.machine() == 'i386':
fstab_path = "old"
else:
fstab_path = "new"
check_root()
check_image_size(size_in_MB)
volume_path = os.path.normpath(volume_path)
if not all:
excludes = parse_excludes(excludes_string)
euca.add_excludes(volume_path, excludes)
if inherit:
ramdisk, kernel, mapping, product_codes, ancestor_ami_ids = get_instance_metadata(euca, ramdisk, kernel, mapping)
if product_code_string:
product_codes = add_product_codes(product_code_string, product_codes)
image_path = euca.make_image(size_in_MB, excludes, prefix, destination_path)
image_path = os.path.normpath(image_path)
if (image_path.find(volume_path) == 0):
exclude_image = image_path.replace(volume_path, "", 1)
image_path_parts = exclude_image.split("/")
if(len(image_path_parts) > 1):
exclude_image = exclude_image.replace(image_path_parts[0] + "/", "", 1)
excludes.append(exclude_image)
try:
euca.copy_volume(image_path, volume_path, excludes, generate_fstab, fstab_path)
except CopyError:
print "Unable to copy files"
cleanup(image_path)
sys.exit(1)
image_size, sha_image_digest = euca.check_image(image_path, destination_path)
if not prefix:
prefix = euca.get_relative_filename(image_path)
tgz_file = euca.tarzip_image(prefix, image_path, destination_path)
encrypted_file, key, iv, bundled_size = euca.encrypt_image(tgz_file)
os.remove(tgz_file)
parts, parts_digest = euca.split_image(encrypted_file)
euca.generate_manifest(destination_path, prefix, parts, parts_digest, image_path, key, iv, cert_path, ec2cert_path, private_key_path, target_arch, image_size, bundled_size, sha_image_digest, user, kernel, ramdisk, mapping, product_codes, ancestor_ami_ids)
os.remove(encrypted_file)
# cleanup(image_path)
else:
if not size_in_MB:
print 'size must be specified.'
if not cert_path:
print 'cert must be specified.'
if not private_key_path:
print 'privatekey must be specified.'
if not user:
print 'user must be specified.'
if not ec2cert_path:
print 'ec2cert must be specified.'
usage()
if __name__ == "__main__":
main()
|