Package VMBuilder :: Package plugins :: Package ec2
[frames] | no frames]

Source Code for Package VMBuilder.plugins.ec2

  1  # 
  2  #    Uncomplicated VM Builder 
  3  #    Copyright (C) 2007-2009 Canonical Ltd. 
  4  #     
  5  #    See AUTHORS for list of contributors 
  6  # 
  7  #    This program is free software: you can redistribute it and/or modify 
  8  #    it under the terms of the GNU General Public License version 3, as 
  9  #    published by the Free Software Foundation. 
 10  # 
 11  #    This program is distributed in the hope that it will be useful, 
 12  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  #    GNU General Public License for more details. 
 15  # 
 16  #    You should have received a copy of the GNU General Public License 
 17  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 18  # 
 19  import VMBuilder 
 20  from   VMBuilder import register_distro_plugin, register_hypervisor_plugin, Plugin, VMBuilderUserError, VMBuilderException 
 21  from   VMBuilder.util import run_cmd 
 22  import logging 
 23  import os 
 24   
25 -class EC2(Plugin):
26 name = 'EC2 integration' 27
28 - def register_options(self):
29 # Don't pretend like we can do EC2 30 if not isinstance(self.context.hypervisor, VMBuilder.plugins.xen.Xen): 31 return 32 group = self.context.setting_group('EC2 integation') 33 group.add_option('--ec2', action='store_true', help='Build for EC2') 34 group.add_option('--ec2-name','--ec2-prefix', metavar='EC2_NAME', help='Name for the EC2 image.') 35 group.add_option('--ec2-cert', metavar='CERTFILE', help='PEM encoded public certificate for EC2.') 36 group.add_option('--ec2-key', metavar='KEYFILE', help='PEM encoded private key for EC2.') 37 group.add_option('--ec2-user', metavar='AWS_ACCOUNT', help='EC2 user ID (a.k.a. AWS account number, not AWS access key ID).') 38 group.add_option('--ec2-bucket', metavar='BUCKET', help='S3 bucket to hold the AMI.') 39 group.add_option('--ec2-access-key', metavar='ACCESS_ID', help='AWS access key ID.') 40 group.add_option('--ec2-secret-key', metavar='SECRET_ID', help='AWS secret access key.') 41 group.add_option('--ec2-kernel','--ec2-aki', metavar='AKI', help='EC2 AKI (kernel) to use.') 42 group.add_option('--ec2-ramdisk','--ec2-ari', metavar='ARI', help='EC2 ARI (ramdisk) to use.') 43 group.add_option('--ec2-version', metavar='EC2_VER', help='Specify the EC2 image version.') 44 group.add_option('--ec2-landscape', action='store_true', help='Install landscape client support') 45 group.add_option('--ec2-bundle', action='store_true', help='Bundle the instance') 46 group.add_option('--ec2-upload', action='store_true', help='Upload the instance') 47 group.add_option('--ec2-register', action='store_true', help='Register the instance') 48 self.context.register_setting_group(group)
49
50 - def preflight_check(self):
51 if not getattr(self.vm, 'ec2', False): 52 return True 53 54 if not self.context.hypervisor.name == 'Xen': 55 raise VMBuilderUserError('When building for EC2 you must use the xen hypervisor.') 56 57 if self.context.ec2_bundle: 58 try: 59 run_cmd('ec2-ami-tools-version') 60 except VMBuilderException, e: 61 raise VMBuilderUserError('You need to have the Amazon EC2 AMI tools installed') 62 63 if not self.context.ec2_name: 64 raise VMBuilderUserError('When building for EC2 you must supply the name for the image.') 65 66 if not self.context.ec2_cert: 67 if "EC2_CERT" in os.environ: 68 self.context.ec2_cert = os.environ["EC2_CERT"] 69 else: 70 raise VMBuilderUserError('When building for EC2 you must provide your PEM encoded public key certificate') 71 72 if not self.context.ec2_key: 73 if "EC2_PRIVATE_KEY" in os.environ: 74 self.context.ec2_key = os.environ["EC2_PRIVATE_KEY"] 75 else: 76 raise VMBuilderUserError('When building for EC2 you must provide your PEM encoded private key file') 77 78 if not self.context.ec2_user: 79 raise VMBuilderUserError('When building for EC2 you must provide your EC2 user ID (your AWS account number, not your AWS access key ID)') 80 81 if not self.context.ec2_kernel: 82 self.context.ec2_kernel = self.vm.distro.get_ec2_kernel() 83 logging.debug('%s - to be used for AKI.' %(self.context.ec2_kernel)) 84 85 if not self.context.ec2_ramdisk: 86 self.context.ec2_ramdisk = self.vm.distro.ec2_ramdisk_id() 87 logging.debug('%s - to be use for the ARI.' %(self.context.ec2_ramdisk)) 88 89 if self.context.ec2_upload: 90 if not self.context.ec2_bucket: 91 raise VMBuilderUserError('When building for EC2 you must provide an S3 bucket to hold the AMI') 92 93 if not self.context.ec2_access_key: 94 raise VMBuilderUserError('When building for EC2 you must provide your AWS access key ID.') 95 96 if not self.context.ec2_secret_key: 97 raise VMBuilderUserError('When building for EC2 you must provide your AWS secret access key.') 98 99 if not self.context.ec2_version: 100 raise VMBuilderUserError('When building for EC2 you must provide version info.') 101 102 if not self.context.addpkg: 103 self.context.addpkg = [] 104 105 if self.context.ec2_landscape: 106 logging.info('Installing landscape support') 107 self.context.addpkg += ['landscape-client']
108
109 - def post_install(self):
110 if not getattr(self.vm, 'ec2', False): 111 return 112 113 logging.info("Running ec2 postinstall") 114 self.install_from_template('/etc/ec2_version', 'ec2_version', { 'version' : self.context.ec2_version } ) 115 self.install_from_template('/etc/ssh/sshd_config', 'sshd_config') 116 self.install_from_template('/etc/sudoers', 'sudoers') 117 118 if self.context.ec2_landscape: 119 self.install_from_template('/etc/default/landscape-client', 'landscape_client') 120 121 self.context.distro.disable_hwclock_access()
122
123 - def deploy(self):
124 if not getattr(self.vm, 'ec2', False): 125 return False 126 127 if self.context.ec2_bundle: 128 logging.info("Building EC2 bundle") 129 bundle_cmdline = ['ec2-bundle-image', '--image', self.context.filesystems[0].filename, '--cert', self.vm.ec2_cert, '--privatekey', self.vm.ec2_key, '--user', self.vm.ec2_user, '--prefix', self.vm.ec2_name, '-r', ['i386', 'x86_64'][self.vm.arch == 'amd64'], '-d', self.vm.workdir, '--kernel', self.vm.ec2_kernel, '--ramdisk', self.vm.ec2_ramdisk] 130 run_cmd(*bundle_cmdline) 131 132 manifest = '%s/%s.manifest.xml' % (self.context.workdir, self.vm.ec2_name) 133 if self.context.ec2_upload: 134 logging.info("Uploading EC2 bundle") 135 upload_cmdline = ['ec2-upload-bundle', '--retry', '--manifest', manifest, '--bucket', self.context.ec2_bucket, '--access-key', self.vm.ec2_access_key, '--secret-key', self.vm.ec2_secret_key] 136 run_cmd(*upload_cmdline) 137 138 if self.context.ec2_register: 139 from boto.ec2.connection import EC2Connection 140 conn = EC2Connection(self.context.ec2_access_key, self.vm.ec2_secret_key) 141 amiid = conn.register_image('%s/%s.manifest.xml' % (self.context.ec2_bucket, self.vm.ec2_name)) 142 print 'Image registered as %s' % amiid 143 else: 144 self.context.result_files.append(manifest) 145 else: 146 self.context.result_files.append(self.vm.filesystems[0].filename) 147 148 return True
149 150 #register_plugin(EC2) 151