当前位置: 首页>>代码示例>>Python>>正文


Python Volume.add_tag方法代码示例

本文整理汇总了Python中boto.ec2.volume.Volume.add_tag方法的典型用法代码示例。如果您正苦于以下问题:Python Volume.add_tag方法的具体用法?Python Volume.add_tag怎么用?Python Volume.add_tag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在boto.ec2.volume.Volume的用法示例。


在下文中一共展示了Volume.add_tag方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: EucaCloudPlugin

# 需要导入模块: from boto.ec2.volume import Volume [as 别名]
# 或者: from boto.ec2.volume.Volume import add_tag [as 别名]
class EucaCloudPlugin(EC2CloudPlugin):
    _name = 'euca'

    def add_plugin_args(self, *args, **kwargs):
        context = self._config.context
        EC2CloudPlugin.add_plugin_args(self)
        cloud = self._parser.add_argument_group(title='EC2 Options', description='EC2 Connection Information')
        cloud.add_argument('--ec2-endpoint', dest='ec2_endpoint', help='EC2 endpoint  to connect to',
                           action=conf_action(config=context.cloud))

    def configure(self, config, parser):
        super(EucaCloudPlugin, self).configure(config, parser)
        host = config.context.web_log.get('host', False)
        if not host:
            md = get_instance_metadata()
            pub, ipv4 = 'public-hostname', 'local-ipv4'
            config.context.web_log['host'] = md[pub] if pub in md else md[ipv4]

    def connect(self, **kwargs):
        if self._connection:
            log.warn('Already connected to Euca')
        else:
            log.info('Connecting to Euca')
            self._connect(**kwargs)

    def _connect(self, **kwargs):
        cloud_config = self._config.plugins[self.full_name]
        context = self._config.context
        self._instance_metadata = get_instance_metadata()
        euca_path = "/services/Eucalyptus"
        euca_port = 8773
        ec2_region = RegionInfo()
        ec2_region.name = 'eucalyptus'
        ec2_region.endpoint = context.cloud.ec2_endpoint
        connection_args = { 'is_secure': False,
                            'debug': 0,
                            'port' : 8773,
                            'path' : euca_path,
                            'host' : context.cloud.ec2_endpoint,
                            'api_version': '2012-07-20',
                            'region': ec2_region }

        if float(boto.__version__[0:3]) >= 2.6:
            connection_args['validate_certs'] = False

        self._connection = boto.connect_ec2(**connection_args)

        log.info('Aminating in region {0}: http://{1}:{2}{3}'.format(ec2_region.name,
                                                                      context.cloud.ec2_endpoint,
                                                                      euca_port,
                                                                      euca_path))

    def allocate_base_volume(self, tag=True):
        cloud_config = self._config.plugins[self.full_name]
        context = self._config.context

        self._volume = Volume(connection=self._connection)

        rootdev = context.base_ami.block_device_mapping[context.base_ami.root_device_name]
        self._volume.id = self._connection.create_volume(size=rootdev.size, zone=self._instance.placement,
                                                         snapshot=rootdev.snapshot_id).id
        if not self._volume_available():
            log.critical('{0}: unavailable.')
            return False

        if tag:
            tags = {
                'purpose': cloud_config.get('tag_ami_purpose', 'amination'),
                'status': 'busy',
                'ami': context.base_ami.id,
                'ami-name': context.base_ami.name,
                'arch': context.base_ami.architecture,
            }
            self._connection.create_tags([self._volume.id], tags)
        self._volume.update()
        log.debug('Volume {0} created'.format(self._volume.id))

    @retry(VolumeException, tries=2, delay=1, backoff=1, logger=log)
    def attach_volume(self, blockdevice, tag=True):
        self.allocate_base_volume(tag=tag)
        # must do this as amazon still wants /dev/sd*
        ec2_device_name = blockdevice.replace('sd', 'vd')
        log.debug('Attaching volume {0} to {1}:{2}({3})'.format(self._volume.id, self._instance.id, ec2_device_name,
                                                                blockdevice))
        self._volume.attach(self._instance.id, ec2_device_name)
        attached_device = self.is_volume_attached(ec2_device_name)
        if attached_device != ec2_device_name:
            log.debug('{0} attachment to {1}:{2}({3}) timed out'.format(self._volume.id, self._instance.id,
                                                                        ec2_device_name, blockdevice))
            self._volume.add_tag('status', 'used')
            # trigger a retry
            raise VolumeException('Timed out waiting for {0} to attach to {1}:{2}'.format(self._volume.id,
                                                                                              self._instance.id,
                                                                                          blockdevice))
        log.debug('Volume {0} attached to {1}:{2}'.format(self._volume.id, self._instance.id, blockdevice))
        return blockdevice

    def is_volume_attached(self, ec2_device_name):
        try:
            attached_device = self._volume_attached(ec2_device_name)
#.........这里部分代码省略.........
开发者ID:aminator-plugins,项目名称:eucalyptus-cloud,代码行数:103,代码来源:euca.py

示例2: EC2CloudPlugin

# 需要导入模块: from boto.ec2.volume import Volume [as 别名]
# 或者: from boto.ec2.volume.Volume import add_tag [as 别名]
class EC2CloudPlugin(BaseCloudPlugin):
    _name = 'ec2'

    def add_metrics(self, metric_base_name, cls, func_name):
        newfunc = succeeds("{0}.count".format(metric_base_name), self)(raises("{0}.error".format(metric_base_name), self)(timer("{0}.duration".format(metric_base_name), self)(getattr(cls, func_name))))
        setattr(cls, func_name, newfunc)

    def __init__(self):
        super(EC2CloudPlugin, self).__init__()
        # wrap each of the functions so we can get timer and error metrics
        for ec2func in ["create_volume", "create_tags", "register_image", "get_all_images"]:
            self.add_metrics("aminator.cloud.ec2.connection.{0}".format(ec2func), EC2Connection, ec2func)
        for volfunc in ["add_tag", "attach", "create_snapshot", "delete", "detach", "update"]:
            self.add_metrics("aminator.cloud.ec2.volume.{0}".format(volfunc), Volume, volfunc)
        for imgfunc in ["update"]:
            self.add_metrics("aminator.cloud.ec2.image.{0}".format(imgfunc), Image, imgfunc)
        for insfunc in ["update"]:
            self.add_metrics("aminator.cloud.ec2.instance.{0}".format(insfunc), Instance, insfunc)

    def add_plugin_args(self, *args, **kwargs):
        context = self._config.context
        base_ami = self._parser.add_argument_group(
            title='Base AMI', description='EITHER AMI id OR name, not both!')
        base_ami_mutex = base_ami.add_mutually_exclusive_group(required=True)
        base_ami_mutex.add_argument(
            '-b', '--base-ami-name', dest='base_ami_name',
            action=conf_action(config=context.ami),
            help='The name of the base AMI used in provisioning')
        base_ami_mutex.add_argument(
            '-B', '--base-ami-id', dest='base_ami_id',
            action=conf_action(config=context.ami),
            help='The id of the base AMI used in provisioning')
        cloud = self._parser.add_argument_group(
            title='EC2 Options', description='EC2 Connection Information')
        cloud.add_argument(
            '-r', '--region', dest='region',
            help='EC2 region (default: us-east-1)',
            action=conf_action(config=context.cloud))
        cloud.add_argument(
            '--boto-secure', dest='is_secure', help='Connect via https',
            action=conf_action(config=context.cloud, action='store_true'))
        cloud.add_argument(
            '--boto-debug', dest='boto_debug', help='Boto debug output',
            action=conf_action(config=context.cloud, action='store_true'))
        volume_mutex = cloud.add_mutually_exclusive_group()
        volume_mutex.add_argument(
            '-V', '--volume-id', dest='volume_id',
            action=conf_action(config=context.ami),
            help='The Base AMI volume id already attached to the system')
        volume_mutex.add_argument(
            '--provisioner-ebs-type', dest='provisioner_ebs_type',
            action=conf_action(config=context.cloud),
            help='The type of EBS volume to create from the Base AMI snapshot')
        cloud.add_argument(
            '--register-ebs-type', dest='register_ebs_type',
            action=conf_action(config=context.cloud),
            help='The root volume EBS type for AMI registration')
        cloud.add_argument(
            '--root-volume-size', dest='root_volume_size',
            action=conf_action(config=context.ami),
            help='Root volume size (in GB). The default is to inherit from the base AMI.')

    def configure(self, config, parser):
        super(EC2CloudPlugin, self).configure(config, parser)
        host = config.context.web_log.get('host', False)
        if not host:
            md = get_instance_metadata()
            pub, ipv4 = 'public-hostname', 'local-ipv4'
            config.context.web_log['host'] = md[pub] if pub in md else md[ipv4]

    def connect(self, **kwargs):
        if self._connection:
            log.warn('Already connected to EC2')
        else:
            log.info('Connecting to EC2')
            self._connect(**kwargs)

    def _connect(self, **kwargs):
        cloud_config = self._config.plugins[self.full_name]
        context = self._config.context
        self._instance_metadata = get_instance_metadata()
        instance_region = self._instance_metadata['placement']['availability-zone'][:-1]
        region = kwargs.pop('region', context.get('region', cloud_config.get('region', instance_region)))
        log.debug('Establishing connection to region: {0}'.format(region))

        context.cloud.setdefault('boto_debug', False)
        if context.cloud.boto_debug:
            from aminator.config import configure_datetime_logfile
            configure_datetime_logfile(self._config, 'boto')
            kwargs['debug'] = 1
            log.debug('Boto debug logging enabled')
        else:
            logging.getLogger('boto').setLevel(logging.INFO)
        if 'is_secure' not in kwargs:
            kwargs['is_secure'] = context.get('is_secure', cloud_config.get('is_secure', True))
        self._connection = connect_to_region(region, **kwargs)
        log.info('Aminating in region {0}'.format(region))

    def allocate_base_volume(self, tag=True):
        cloud_config = self._config.plugins[self.full_name]
#.........这里部分代码省略.........
开发者ID:Netflix,项目名称:aminator,代码行数:103,代码来源:ec2.py


注:本文中的boto.ec2.volume.Volume.add_tag方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。