forked from p15670423/monkey
Improved c'tor of AwsInstance
This commit is contained in:
parent
6b2eb126b5
commit
1f75b72a97
|
@ -1,6 +1,8 @@
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
import urllib2
|
import urllib2
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
__author__ = 'itay.mizeretz'
|
__author__ = 'itay.mizeretz'
|
||||||
|
|
||||||
|
@ -9,26 +11,33 @@ AWS_LATEST_METADATA_URI_PREFIX = 'http://{0}/latest/'.format(AWS_INSTANCE_METADA
|
||||||
ACCOUNT_ID_KEY = "accountId"
|
ACCOUNT_ID_KEY = "accountId"
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class AwsInstance(object):
|
class AwsInstance(object):
|
||||||
"""
|
"""
|
||||||
Class which gives useful information about the current instance you're on.
|
Class which gives useful information about the current instance you're on.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.instance_id = None
|
||||||
|
self.region = None
|
||||||
|
self.account_id = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.instance_id = urllib2.urlopen(
|
self.instance_id = urllib2.urlopen(
|
||||||
AWS_LATEST_METADATA_URI_PREFIX + 'meta-data/instance-id', timeout=2).read()
|
AWS_LATEST_METADATA_URI_PREFIX + 'meta-data/instance-id', timeout=2).read()
|
||||||
self.region = self._parse_region(
|
self.region = self._parse_region(
|
||||||
urllib2.urlopen(AWS_LATEST_METADATA_URI_PREFIX + 'meta-data/placement/availability-zone').read())
|
urllib2.urlopen(AWS_LATEST_METADATA_URI_PREFIX + 'meta-data/placement/availability-zone').read())
|
||||||
except urllib2.URLError:
|
except urllib2.URLError as e:
|
||||||
self.instance_id = None
|
logger.error("Failed init of AwsInstance while getting metadata: {}".format(e.message))
|
||||||
self.region = None
|
|
||||||
try:
|
try:
|
||||||
self.account_id = self._extract_account_id(
|
self.account_id = self._extract_account_id(
|
||||||
urllib2.urlopen(
|
urllib2.urlopen(
|
||||||
AWS_LATEST_METADATA_URI_PREFIX + 'dynamic/instance-identity/document', timeout=2).read())
|
AWS_LATEST_METADATA_URI_PREFIX + 'dynamic/instance-identity/document', timeout=2).read())
|
||||||
except urllib2.URLError:
|
except urllib2.URLError as e:
|
||||||
self.account_id = None
|
logger.error("Failed init of AwsInstance while getting dynamic instance data: {}".format(e.message))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse_region(region_url_response):
|
def _parse_region(region_url_response):
|
||||||
|
|
Loading…
Reference in New Issue