本文整理汇总了Python中boto.utils.get_instance_metadata方法的典型用法代码示例。如果您正苦于以下问题:Python utils.get_instance_metadata方法的具体用法?Python utils.get_instance_metadata怎么用?Python utils.get_instance_metadata使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boto.utils
的用法示例。
在下文中一共展示了utils.get_instance_metadata方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_metadata
# 需要导入模块: from boto import utils [as 别名]
# 或者: from boto.utils import get_instance_metadata [as 别名]
def write_metadata(self):
fp = open(os.path.expanduser(BotoConfigPath), 'w')
fp.write('[Instance]\n')
inst_data = get_instance_metadata()
for key in inst_data:
fp.write('%s = %s\n' % (key, inst_data[key]))
user_data = get_instance_userdata()
fp.write('\n%s\n' % user_data)
fp.write('[Pyami]\n')
fp.write('working_dir = %s\n' % self.working_dir)
fp.close()
# This file has the AWS credentials, should we lock it down?
# os.chmod(BotoConfigPath, stat.S_IREAD | stat.S_IWRITE)
# now that we have written the file, read it into a pyami Config object
boto.config = Config()
boto.init_logging()
示例2: _populate_keys_from_metadata_server
# 需要导入模块: from boto import utils [as 别名]
# 或者: from boto.utils import get_instance_metadata [as 别名]
def _populate_keys_from_metadata_server(self):
# get_instance_metadata is imported here because of a circular
# dependency.
boto.log.debug("Retrieving credentials from metadata server.")
from boto.utils import get_instance_metadata
timeout = config.getfloat('Boto', 'metadata_service_timeout', 1.0)
attempts = config.getint('Boto', 'metadata_service_num_attempts', 1)
# The num_retries arg is actually the total number of attempts made,
# so the config options is named *_num_attempts to make this more
# clear to users.
metadata = get_instance_metadata(
timeout=timeout, num_retries=attempts,
data='meta-data/iam/security-credentials/')
if metadata:
creds = self._get_credentials_from_metadata(metadata)
self._access_key = creds[0]
self._secret_key = creds[1]
self._security_token = creds[2]
expires_at = creds[3]
# I'm assuming there's only one role on the instance profile.
self._credential_expiry_time = datetime.strptime(
expires_at, "%Y-%m-%dT%H:%M:%SZ")
boto.log.debug("Retrieved credentials will expire in %s at: %s",
self._credential_expiry_time - datetime.now(),
expires_at)
示例3: _getCurrentAWSZone
# 需要导入模块: from boto import utils [as 别名]
# 或者: from boto.utils import get_instance_metadata [as 别名]
def _getCurrentAWSZone(spotBid=None, nodeType=None, ctx=None):
zone = None
try:
import boto
from boto.utils import get_instance_metadata
except ImportError:
pass
else:
zone = os.environ.get('TOIL_AWS_ZONE', None)
if not zone and runningOnEC2():
try:
zone = get_instance_metadata()['placement']['availability-zone']
except KeyError:
pass
if not zone and spotBid:
# if spot bid is present, all the other parameters must be as well
assert bool(spotBid) == bool(nodeType) == bool(ctx)
# if the zone is unset and we are using the spot market, optimize our
# choice based on the spot history
return optimize_spot_bid(ctx=ctx, instance_type=nodeType, spot_bid=float(spotBid))
if not zone:
zone = boto.config.get('Boto', 'ec2_region_name')
if zone is not None:
zone += 'a' # derive an availability zone in the region
return zone
示例4: _readClusterSettings
# 需要导入模块: from boto import utils [as 别名]
# 或者: from boto.utils import get_instance_metadata [as 别名]
def _readClusterSettings(self):
"""
Reads the cluster settings from the instance metadata, which assumes the instance
is the leader.
"""
instanceMetaData = get_instance_metadata()
region = zoneToRegion(self._zone)
conn = boto.ec2.connect_to_region(region)
instance = conn.get_all_instances(instance_ids=[instanceMetaData["instance-id"]])[0].instances[0]
self.clusterName = str(instance.tags["Name"])
self._buildContext()
self._subnetID = instance.subnet_id
self._leaderPrivateIP = instanceMetaData['local-ipv4'] # this is PRIVATE IP
self._keyName = list(instanceMetaData['public-keys'].keys())[0]
self._tags = self.getLeader().tags
self._masterPublicKey = self._setSSH()
self._leaderProfileArn = instanceMetaData['iam']['info']['InstanceProfileArn']
# The existing metadata API returns a single string if there is one security group, but
# a list when there are multiple: change the format to always be a list.
rawSecurityGroups = instanceMetaData['security-groups']
self._leaderSecurityGroupNames = [rawSecurityGroups] if not isinstance(rawSecurityGroups, list) else rawSecurityGroups
示例5: compute_instance_id
# 需要导入模块: from boto import utils [as 别名]
# 或者: from boto.utils import get_instance_metadata [as 别名]
def compute_instance_id(self):
"""
Look up the EC2 instance ID for this node.
"""
instance_id = get_instance_metadata().get('instance-id', None)
if instance_id is None:
raise UnknownInstanceID(self)
return instance_id.decode("ascii")
示例6: connect_to_ec2
# 需要导入模块: from boto import utils [as 别名]
# 或者: from boto.utils import get_instance_metadata [as 别名]
def connect_to_ec2(region='us-east-1', access_key=None, secret_key=None):
""" Connect to AWS ec2
:type region: str
:param region: AWS region to connect to
:type access_key: str
:param access_key: AWS access key id
:type secret_key: str
:param secret_key: AWS secret access key
:returns: boto.ec2.connection.EC2Connection -- EC2 connection
"""
if access_key:
# Connect using supplied credentials
logger.info('Connecting to AWS EC2 in {}'.format(region))
connection = ec2.connect_to_region(
region,
aws_access_key_id=access_key,
aws_secret_access_key=secret_key)
else:
# Fetch instance metadata
metadata = get_instance_metadata(timeout=1, num_retries=1)
if metadata:
try:
region = metadata['placement']['availability-zone'][:-1]
except KeyError:
pass
# Connect using env vars or boto credentials
logger.info('Connecting to AWS EC2 in {}'.format(region))
connection = ec2.connect_to_region(region)
if not connection:
logger.error('An error occurred when connecting to EC2')
sys.exit(1)
return connection
示例7: _get_credentials_from_metadata
# 需要导入模块: from boto import utils [as 别名]
# 或者: from boto.utils import get_instance_metadata [as 别名]
def _get_credentials_from_metadata(self, metadata):
# Given metadata, return a tuple of (access, secret, token, expiration)
# On errors, an InvalidInstanceMetadataError will be raised.
# The "metadata" is a lazy loaded dictionary means that it's possible
# to still encounter errors as we traverse through the metadata dict.
# We try to be careful and raise helpful error messages when this
# happens.
creds = list(metadata.values())[0]
if not isinstance(creds, dict):
# We want to special case a specific error condition which is
# where get_instance_metadata() returns an empty string on
# error conditions.
if creds == '':
msg = 'an empty string'
else:
msg = 'type: %s' % creds
raise InvalidInstanceMetadataError("Expected a dict type of "
"credentials instead received "
"%s" % (msg))
try:
access_key = creds['AccessKeyId']
secret_key = self._convert_key_to_str(creds['SecretAccessKey'])
security_token = creds['Token']
expires_at = creds['Expiration']
except KeyError as e:
raise InvalidInstanceMetadataError(
"Credentials from instance metadata missing "
"required key: %s" % e)
return access_key, secret_key, security_token, expires_at
示例8: account
# 需要导入模块: from boto import utils [as 别名]
# 或者: from boto.utils import get_instance_metadata [as 别名]
def account(self):
try:
arn = self.iam.get_user().arn
except BaseException:
# Agent boxes run with IAM role credentials instead of user credentials.
arn = get_instance_metadata()['iam']['info']['InstanceProfileArn']
_, partition, service, region, account, resource = arn.split(':', 6)
return account
示例9: get_instance_metadata
# 需要导入模块: from boto import utils [as 别名]
# 或者: from boto.utils import get_instance_metadata [as 别名]
def get_instance_metadata(timeout=None):
if not isinstance(timeout, (int, float)):
timeout = Duration(timeout).seconds
output = wrap({k.replace("-", "_"): v for k, v in boto_utils.get_instance_metadata(timeout=coalesce(timeout, 5), num_retries=2).items()})
return output
示例10: _get_metadata_from_from_aws
# 需要导入模块: from boto import utils [as 别名]
# 或者: from boto.utils import get_instance_metadata [as 别名]
def _get_metadata_from_from_aws(please_stop):
with suppress_exception:
ec2 = get_instance_metadata()
if ec2:
machine_metadata.aws_instance_type = ec2.instance_type
machine_metadata.name = ec2.instance_id