Example #1
0
 def __init__(self, az_or_region):
     if re.match(r".*[a-z]$", az_or_region):
         self.az = az_or_region
         self.region_name = self.az[:-1]
     else:
         self.az = None
         self.region_name = az_or_region
     self.ami_id = Ec2Region.GetLatestAmiId(self.region_name)
Example #2
0
    def _ReqSpotInst(self):
        # This is run as root
        #
        # http://unix.stackexchange.com/questions/4342/how-do-i-get-sudo-u-user-to-use-the-users-env
        user_data = \
      """#!/bin/bash
cd /home/ubuntu/work
rm -rf /home/ubuntu/work/acorn-tools
sudo -i -u ubuntu bash -c 'git clone https://github.com/hobinyoon/acorn-tools.git /home/ubuntu/work/acorn-tools'
sudo -i -u ubuntu /home/ubuntu/work/acorn-tools/ec2/ec2-init.py {0} {1} {2} {3}
"""
        user_data = user_data.format(self.tags["init_script"], self.jr_sqs_url,
                                     self.jr_sqs_msg_receipt_handle,
                                     self.num_regions)

        ls = {
            'ImageId':
            Ec2Region.GetLatestAmiId(region=self.region, name=self.ami_name)
            #, 'KeyName': 'string'
            ,
            'SecurityGroups': ["cass-server"],
            'UserData':
            base64.b64encode(user_data)
            #, 'AddressingType': 'string'
            ,
            'InstanceType':
            self.inst_type,
            'EbsOptimized':
            True,
            'Placement': {
                'AvailabilityZone': self.az
            }
        }

        while True:
            try:
                r = BotoClient.Get(self.region).request_spot_instances(
                    SpotPrice=str(self.max_price),
                    #ClientToken='string',
                    InstanceCount=1,
                    Type='one-time',
                    #ValidFrom=datetime(2015, 1, 1),
                    #ValidUntil=datetime(2015, 1, 1),
                    #LaunchGroup='string',
                    #AvailabilityZoneGroup='string',

                    # https://aws.amazon.com/blogs/aws/new-ec2-spot-blocks-for-defined-duration-workloads/
                    #BlockDurationMinutes=123,
                    LaunchSpecification=ls,
                )
                self.log.P("SpotInstReqResp: %s" % pprint.pformat(r))
                if len(r["SpotInstanceRequests"]) != 1:
                    raise RuntimeError("len(r[\"SpotInstanceRequests\"])=%d" %
                                       len(r["SpotInstanceRequests"]))
                self.spot_req_id = r["SpotInstanceRequests"][0][
                    "SpotInstanceRequestId"]
                self.log.P("region=%s spot_req_id=%s" %
                           (self.region, self.spot_req_id))
                break
            except botocore.exceptions.ClientError as e:
                if e.response["Error"]["Code"] == "RequestLimitExceeded":
                    self.log.P("region=%s error=%s" % (self.region, e))
                    time.sleep(5)
                else:
                    raise e