Beispiel #1
0
def read_image_as_raster(img_url):
    """ Reads image data from URL in raster format."""
    img = url_lib.urlopen(img_url)
    image_file = io.BytesIO(img.read())
    img = Image.open(image_file)
    w, h = img.size
    pixels = img.load()
    return [pixels[x, y] for x in range(w) for y in xrange(h)]
Beispiel #2
0
def read_image_as_raster(img_url):
    """ Reads image data from URL in raster format."""
    img = url_lib.urlopen(img_url)
    image_file = io.BytesIO(img.read())
    img = Image.open(image_file)
    w, h = img.size
    pixels = img.load()
    return [pixels[x, y] for x in range(w) for y in xrange(h)]
Beispiel #3
0
    def get_tags(agentConfig):
        if not agentConfig['collect_instance_metadata']:
            log.info(
                "Instance metadata collection is disabled. Not collecting it.")
            return []

        socket_to = None
        try:
            socket_to = socket.getdefaulttimeout()
            socket.setdefaulttimeout(EC2.TIMEOUT)
        except Exception:
            pass

        try:
            iam_role = url_lib.urlopen(
                EC2.URL + "/iam/security-credentials").read().strip()
            iam_params = json.loads(
                url_lib.urlopen(EC2.URL + "/iam/security-credentials" + "/" +
                                str(iam_role)).read().strip())
            from boto.ec2.connection import EC2Connection
            connection = EC2Connection(
                aws_access_key_id=iam_params['AccessKeyId'],
                aws_secret_access_key=iam_params['SecretAccessKey'],
                security_token=iam_params['Token'])
            instance_object = connection.get_only_instances(
                [EC2.metadata['instance-id']])[0]

            EC2_tags = [
                u"%s:%s" % (tag_key, tag_value)
                for tag_key, tag_value in iteritems(instance_object.tags)
            ]

        except Exception:
            log.exception("Problem retrieving custom EC2 tags")
            EC2_tags = []

        try:
            if socket_to is None:
                socket_to = 3
            socket.setdefaulttimeout(socket_to)
        except Exception:
            pass

        return EC2_tags
Beispiel #4
0
    def get_metadata(agentConfig):
        """Use the ec2 http service to introspect the instance. This adds latency \
        if not running on EC2
        """
        # >>> import urllib2
        # >>> urllib2.urlopen('http://169.254.169.254/latest/', timeout=1).read()
        # 'meta-data\nuser-data'
        # >>> urllib2.urlopen('http://169.254.169.254/latest/meta-data', timeout=1).read()
        # 'ami-id\nami-launch-index\nami-manifest-path\nhostname\ninstance-id\nlocal-ipv4\
        # npublic-keys/\nreservation-id\nsecurity-groups'
        # >>> urllib2.urlopen('http://169.254.169.254/latest/meta-data/instance-id',
        # timeout=1).read()
        # 'i-deadbeef'

        # Every call may add TIMEOUT seconds in latency so don't abuse this call
        # python 2.4 does not support an explicit timeout argument so force it here
        # Rather than monkey-patching urllib2, just lower the timeout globally for these calls

        if not agentConfig["collect_instance_metadata"]:
            log.info("Instance metadata collection is disabled. Not collecting it.")
            return {}

        socket_to = None
        try:
            socket_to = socket.getdefaulttimeout()
            socket.setdefaulttimeout(EC2.TIMEOUT)
        except Exception:
            pass

        for k in (
            "instance-id",
            "hostname",
            "local-hostname",
            "public-hostname",
            "ami-id",
            "local-ipv4",
            "public-keys",
            "public-ipv4",
            "reservation-id",
            "security-groups",
        ):
            try:
                v = url_lib.urlopen(EC2.URL + "/" + str(k)).read().strip()
                assert type(v) in (types.StringType, types.UnicodeType) and len(v) > 0, "%s is not a string" % v
                EC2.metadata[k] = v
            except Exception:
                pass

        try:
            if socket_to is None:
                socket_to = 3
            socket.setdefaulttimeout(socket_to)
        except Exception:
            pass

        return EC2.metadata
Beispiel #5
0
    def get_tags(agentConfig):
        if not agentConfig['collect_instance_metadata']:
            log.info("Instance metadata collection is disabled. Not collecting it.")
            return []

        socket_to = None
        try:
            socket_to = socket.getdefaulttimeout()
            socket.setdefaulttimeout(EC2.TIMEOUT)
        except Exception:
            pass

        try:
            iam_role = url_lib.urlopen(EC2.URL + "/iam/security-credentials").read().strip()
            iam_params = json.loads(url_lib.urlopen(EC2.URL + "/iam/security-credentials" + "/" +
                                    str(iam_role)).read().strip())
            from boto.ec2.connection import EC2Connection
            connection = EC2Connection(aws_access_key_id=iam_params['AccessKeyId'],
                                       aws_secret_access_key=iam_params['SecretAccessKey'],
                                       security_token=iam_params['Token'])
            instance_object = connection.get_only_instances([EC2.metadata['instance-id']])[0]

            EC2_tags = [u"%s:%s" % (tag_key, tag_value) for tag_key, tag_value
                        in iteritems(instance_object.tags)]

        except Exception:
            log.exception("Problem retrieving custom EC2 tags")
            EC2_tags = []

        try:
            if socket_to is None:
                socket_to = 3
            socket.setdefaulttimeout(socket_to)
        except Exception:
            pass

        return EC2_tags
Beispiel #6
0
def get_ec2_instance_id():
    try:
        # Remember the previous default timeout
        old_timeout = socket.getdefaulttimeout()

        # Try to query the EC2 internal metadata service, but fail fast
        socket.setdefaulttimeout(0.25)

        try:
            return url_lib.urlopen(url_lib.Request("http://169.254.169.254/latest/" "meta-data/instance-id")).read()
        finally:
            # Reset the previous default timeout
            socket.setdefaulttimeout(old_timeout)
    except Exception:
        return socket.gethostname()
Beispiel #7
0
def get_ec2_instance_id():
    try:
        # Remember the previous default timeout
        old_timeout = socket.getdefaulttimeout()

        # Try to query the EC2 internal metadata service, but fail fast
        socket.setdefaulttimeout(0.25)

        try:
            return url_lib.urlopen(url_lib.Request("http://169.254.169.254/latest/"
                                                   "meta-data/instance-id")).read()
        finally:
            # Reset the previous default timeout
            socket.setdefaulttimeout(old_timeout)
    except:
        return socket.gethostname()
Beispiel #8
0
    def get_metadata(agentConfig):
        """Use the ec2 http service to introspect the instance. This adds latency \
        if not running on EC2
        """
        # >>> import urllib2
        # >>> urllib2.urlopen('http://169.254.169.254/latest/', timeout=1).read()
        # 'meta-data\nuser-data'
        # >>> urllib2.urlopen('http://169.254.169.254/latest/meta-data', timeout=1).read()
        # 'ami-id\nami-launch-index\nami-manifest-path\nhostname\ninstance-id\nlocal-ipv4\
        # npublic-keys/\nreservation-id\nsecurity-groups'
        # >>> urllib2.urlopen('http://169.254.169.254/latest/meta-data/instance-id',
        # timeout=1).read()
        # 'i-deadbeef'

        # Every call may add TIMEOUT seconds in latency so don't abuse this call
        # python 2.4 does not support an explicit timeout argument so force it here
        # Rather than monkey-patching urllib2, just lower the timeout globally for these calls

        if not agentConfig['collect_instance_metadata']:
            log.info("Instance metadata collection is disabled. Not collecting it.")
            return {}

        socket_to = None
        try:
            socket_to = socket.getdefaulttimeout()
            socket.setdefaulttimeout(EC2.TIMEOUT)
        except Exception:
            pass

        for k in ('instance-id', 'hostname', 'local-hostname', 'public-hostname', 'ami-id',
                  'local-ipv4', 'public-keys', 'public-ipv4', 'reservation-id', 'security-groups'):
            try:
                v = url_lib.urlopen(EC2.URL + "/" + str(k)).read().strip()
                assert type(v) in (types.StringType, types.UnicodeType) and len(v) > 0, \
                    "%s is not a string" % v
                EC2.metadata[k] = v
            except Exception:
                pass

        try:
            if socket_to is None:
                socket_to = 3
            socket.setdefaulttimeout(socket_to)
        except Exception:
            pass

        return EC2.metadata