示例#1
0
 def get(self):
     """Returns list of images (BotsAMI) associated with bots aws account."""
     ec2 = ec2_manager.EC2Manager()
     bots_amis = ec2.GetImages()
     self.response.out.write(
         simplejson.dumps(
             [ec2_manager.BotoEC2DataObject.Encode(x) for x in bots_amis]))
示例#2
0
 def post(self):
     """Stops all running instances."""
     ec2 = ec2_manager.EC2Manager()
     bots_instances = ec2.StopAllInstances()
     self.response.out.write(
         simplejson.dumps([
             ec2_manager.BotoEC2DataObject.Encode(x) for x in bots_instances
         ]))
示例#3
0
 def post(self):
     """Terminates all instances or all running instances."""
     ec2 = ec2_manager.EC2Manager()
     only_running = self.GetOptionalParameter('only_running', 'true')
     only_running = simplejson.loads(only_running)
     bots_instances = ec2.TerminateAllInstances(only_running)
     self.response.out.write(
         simplejson.dumps([
             ec2_manager.BotoEC2DataObject.Encode(x) for x in bots_instances
         ]))
示例#4
0
 def post(self):
     """Gets information about instances given list of instance ids."""
     instance_ids = self.GetOptionalParameter('instance_ids', None)
     if instance_ids:
         instance_ids = simplejson.loads(instance_ids)
     ec2 = ec2_manager.EC2Manager()
     bots_instances = ec2.GetInstances(instance_ids)
     self.response.out.write(
         simplejson.dumps([
             ec2_manager.BotoEC2DataObject.Encode(x) for x in bots_instances
         ]))
示例#5
0
 def post(self):
     """Stops instances given list of instance ids."""
     instance_ids = simplejson.loads(
         self.GetRequiredParameter('instance_ids'))
     if not instance_ids:
         logging.error('Missing Instance ID.')
         self.response.out.write('Missing Instance ID.')
     ec2 = ec2_manager.EC2Manager()
     bots_instances = ec2.StopInstances(instance_ids)
     self.response.out.write(
         simplejson.dumps([
             ec2_manager.BotoEC2DataObject.Encode(x) for x in bots_instances
         ]))
示例#6
0
def RebootMachine(instance_id):
    """Reboot the machine associated with the given instance id.

  The status of the machine is set to RUNNING.

  Args:
    instance_id: A string that uniquely identifies a machine.
  """
    # Terminate the EC2 instance.
    ec2 = ec2_manager.EC2Manager()

    logging.info('Rebooting machine with instance id "%s".', instance_id)
    ec2.RebootInstances([instance_id])

    # Update the corresponding client machine model.
    client_machine.SetMachineStatus(instance_id, enum.MACHINE_STATUS.RUNNING)
    client_machine.IncrementRetryCount(instance_id)
示例#7
0
 def post(self):
     """Starts given amount of instances of AMI (image)."""
     ami_id = self.GetRequiredParameter('ami_id')
     count = self.GetOptionalParameter('count', 1)
     key_name = self.GetOptionalParameter('key_name',
                                          ec2_manager.BOTS_KEY_PAIR_NAME)
     instance_type = self.GetOptionalParameter('instance_type',
                                               ec2_manager.MICRO_INSTANCE)
     user_data = self.GetOptionalParameter('user_data', None)
     if not ami_id:
         logging.error('Missing AMI ID.')
         self.response.out.write('Missing AMI ID.')
     ec2 = ec2_manager.EC2Manager()
     bots_instances = ec2.StartAMI(ami_id, count, key_name, instance_type,
                                   user_data)
     self.response.out.write(
         simplejson.dumps([
             ec2_manager.BotoEC2DataObject.Encode(x) for x in bots_instances
         ]))
示例#8
0
def TerminateMachine(instance_id, status):
    """Terminate the machine associated with the given instance id.

  Args:
    instance_id: A string that uniquely identifies a machine.
    status: An integer representing an enum.MACHINE_STATUS value.
  """
    # Terminate the EC2 instance.
    ec2 = ec2_manager.EC2Manager()

    if TERMINATE_INSTANCES:
        logging.info('Terminating the machine with instance id "%s".',
                     instance_id)
        ec2.TerminateInstances([instance_id])
    else:
        logging.info('Stopping the machine with instance id "%s".',
                     instance_id)
        ec2.StopInstances([instance_id])

    # Update the corresponding client machine model.
    client_machine.SetMachineStatus(instance_id, status)
示例#9
0
def CreateMachines(num_instances,
                   token,
                   os,
                   browser,
                   browser_version,
                   download_info,
                   retries=0):
    """Create and launch EC2 machines for the given parameters.

  Args:
    num_instances: An integer representing the number of instances to spawn
      with the given configuration.
    token: A string representing the token for this run.
    os: An integer that corresponds to an enum.OS value.
    browser: An integer that corresponds to an enum.BROWSER value.
    browser_version: A string representing browser version to use.
      Specifically, this should be the channel for Chrome.
    download_info: A string representing the information necessary for
      calculating the version and browser download url for the given machine.
    retries: An optional paramater specifying the initial retry count for the
      machine.
  """
    logging.info(
        '\n'.join([
            'num_instances: %d', 'token: %s', 'os: %d', 'browser: %d',
            'browser_version: %s'
        ]), num_instances, token, os, browser, browser_version)

    ec2 = ec2_manager.EC2Manager()
    user_data = simplejson.dumps({
        'channel': browser_version,
        'os': OS_TO_USER_DATA[os],
        'token': token,
        'download_info': download_info
    })
    logging.info('Spawning EC2 machines.')
    # Note: All exceptions are caught here because the EC2 API could fail after
    # successfully starting a machine. Because this task is rescheduled on
    # failure, we need to make sure we don't keep spawning EC2 machines.
    try:
        bots_instances = ec2.StartAmiWithOs(
            os,
            count=num_instances,
            instance_type=DEFAULT_INSTANCE_SIZE,
            user_data=user_data)
    except Exception:
        logging.exception('Something failed when setting up the EC2 instance. '
                          'Stopping setup for this instance.')
        return

    logging.info('Creating the corresponding ClientMachine models.')
    new_instances = []
    for instance in bots_instances:
        new_instances.append(
            client_machine.ClientMachine(
                vm_service=enum.VM_SERVICE.EC2,
                os=os,
                browser=browser,
                browser_version=browser_version,
                client_id=instance.inst_id,
                status=enum.MACHINE_STATUS.PROVISIONED,
                retry_count=retries,
                token=token,
                download_info=download_info))

    db.put(new_instances)
    logging.info('Finished creating the ClientMachine models.')