Beispiel #1
0
 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
Beispiel #2
0
    def _discoverAMI(self):
        """
        :return: The AMI ID (a string like 'ami-0a9a5d2b65cce04eb') for CoreOS
                 or a compatible replacement like Flatcar.
        :rtype: str
        """

        # Take a user override
        ami = os.environ.get('TOIL_AWS_AMI')
        if ami is not None:
            return ami

        # CoreOS is dead, long live Flatcar

        # Flatcar images, however, only live for 9 months.
        # Rather than hardcode a list of AMIs by region that will die, we use
        # their JSON feed of the current ones.
        JSON_FEED_URL = 'https://stable.release.flatcar-linux.net/amd64-usr/current/flatcar_production_ami_all.json'

        # What region do we care about?
        region = zoneToRegion(self._zone)

        for attempt in old_retry(predicate=lambda e: True):
            # Until we get parseable JSON
            # TODO: What errors do we get for timeout, JSON parse failure, etc?
            with attempt:
                # Try to get the JSON and parse it.
                feed = json.loads(urllib.request.urlopen(JSON_FEED_URL).read())

        try:
            for ami_record in feed['amis']:
                # Scan the klist of regions
                if ami_record['name'] == region:
                    # When we find ours
                    # Save the AMI ID
                    ami = ami_record['hvm']
                    # And stop scanning
                    break
        except KeyError:
            # We didn't see a field we need
            raise RuntimeError(
                'Flatcar image feed at {} does not have expected format'.
                format(JSON_FEED_URL))

        if ami is None:
            # We didn't find it
            raise RuntimeError(
                'Flatcar image feed at {} does not have an image for region {}'
                .format(JSON_FEED_URL, region))

        return ami
Beispiel #3
0
    def __init__(self, clusterName, zone, nodeStorage, nodeStorageOverrides, sseKey):
        super(AWSProvisioner, self).__init__(clusterName, zone, nodeStorage, nodeStorageOverrides)
        self.cloud = 'aws'
        self._sseKey = sseKey
        self._zone = zone if zone else getCurrentAWSZone()

        # establish boto3 clients
        self.session = boto3.Session(region_name=zoneToRegion(zone))
        self.ec2 = self.session.resource('ec2')

        if clusterName:
            self._buildContext()  # create connection (self._ctx)
        else:
            self._readClusterSettings()