Пример #1
0
    def assert_required_parameters(self, parameters, operation):
        """ Checks the given parameters to make sure that they can be used to
    interact with Google Compute Engine.

    Args:
      parameters: A dict that maps the name of each credential to be used in GCE
        with the value we should use.
      operation: A BaseAgent.OPERATION that indicates if we wish to add or
        delete instances. Unused here, as all operations require the same
        credentials.
    Raises:
      AgentConfigurationException: If any of the required credentials are not
        present, or if the client_secrets parameter refers to a file that is not
        present on the local filesystem.
    """
        # Make sure the user has set each parameter.
        for param in self.REQUIRED_CREDENTIALS:
            if param not in parameters:
                raise AgentConfigurationException('The required parameter, {0}, was' \
                  ' not specified.'.format(param))

        # Next, make sure that the client_secrets file exists
        if not os.path.exists(self.CLIENT_SECRETS_LOCATION):
            raise AgentConfigurationException('Could not find your client_secrets ' \
              'file at {0}'.format(self.CLIENT_SECRETS_LOCATION))
Пример #2
0
    def get_params_from_args(self, args):
        """ Constructs a dict with only the parameters necessary to interact with
    Google Compute Engine (here, the client_secrets file and the image name).

    Args:
      args: A Namespace or dict that maps all of the arguments the user has
        invoked an AppScale command with their associated value.
    Returns:
      A dict containing the location of the client_secrets file and that name
      of the image to use in GCE.
    """
        if not isinstance(args, dict):
            args = vars(args)

        client_secrets = os.path.expanduser(args['client_secrets'])
        if not os.path.exists(client_secrets):
            raise AgentConfigurationException("Couldn't find your client secrets " + \
              "file at {0}".format(client_secrets))
        shutil.copy(client_secrets,
                    LocalState.get_CLIENT_SECRETS_LOCATION(args['keyname']))

        params = {
            self.PARAM_GROUP: args['group'],
            self.PARAM_IMAGE_ID: args['machine'],
            self.PARAM_INSTANCE_TYPE: args['instance_type'],
            self.PARAM_KEYNAME: args['keyname'],
            self.PARAM_PROJECT: args['project'],
            self.PARAM_SECRETS: self.CLIENT_SECRETS_LOCATION
        }

        return params
Пример #3
0
  def assert_required_parameters(self, parameters, operation):
    """
    Assert that all the parameters required for the EC2 agent are in place.
    (Also see documentation for the BaseAgent class)

    Args:
      parameters  A dictionary of parameters
      operation   Operations to be invoked using the above parameters
    """
    required_params = ()
    if operation == BaseAgent.OPERATION_RUN:
      required_params = self.REQUIRED_EC2_RUN_INSTANCES_PARAMS
    elif operation == BaseAgent.OPERATION_TERMINATE:
      required_params = self.REQUIRED_EC2_TERMINATE_INSTANCES_PARAMS

    # make sure the user set something for each parameter
    for param in required_params:
      if not self.has_parameter(param, parameters):
        raise AgentConfigurationException('no ' + param)

    # next, make sure the user actually put in their credentials
    for credential in self.REQUIRED_EC2_CREDENTIALS:
      if not self.has_parameter(credential, parameters['credentials']):
        raise AgentConfigurationException('no ' + credential)
Пример #4
0
 def assert_required_parameters(self, parameters, operation):
   """ Check whether all the parameters required to interact with Azure are
   present in the provided dict.
   Args:
     parameters: A dict containing values necessary to authenticate with the
       Azure.
     operation: A str representing the operation for which the parameters
       should be checked.
   Raises:
     AgentConfigurationException: If a required parameter is absent.
   """
   # Make sure that the user has set each parameter.
   for param in self.REQUIRED_CREDENTIALS:
     if param not in parameters:
       raise AgentConfigurationException('The required parameter, {0}, was not'
                                         ' specified.'.format(param))
Пример #5
0
  def get_params_from_args(self, args):
    """
    Searches through args to build a dict containing the parameters
    necessary to interact with Amazon EC2.

    Args:
      args: A Namespace containing the arguments that the user has
        invoked an AppScale Tool with.
    """
    # need to convert this to a dict if it is not already
    if not isinstance(args, dict):
      args = vars(args)

    params = {
      self.PARAM_CREDENTIALS : {},
      self.PARAM_GROUP : args['group'],
      self.PARAM_IMAGE_ID : args['machine'],
      self.PARAM_INSTANCE_TYPE : args['instance_type'],
      self.PARAM_KEYNAME : args['keyname'],
    }
    if 'verbose' in args:
      params['IS_VERBOSE'] = args['verbose']
    else:
      params['IS_VERBOSE'] = False

    for credential in self.REQUIRED_CREDENTIALS:
      if credential in os.environ and os.environ[credential] != '':
        params[self.PARAM_CREDENTIALS][credential] = os.environ[credential]
      else:
        raise AgentConfigurationException("Couldn't find {0} in your " \
          "environment. Please set it and run AppScale again."
          .format(credential))

    if 'use_spot_instances' in args and args['use_spot_instances'] == True:
      params[self.PARAM_SPOT] = True
    else:
      params[self.PARAM_SPOT] = False

    if params[self.PARAM_SPOT]:
      if 'max_spot_price' in args and args['max_spot_price'] is not None:
        params[self.PARAM_SPOT_PRICE] = args['max_spot_price']
      else:
        params[self.PARAM_SPOT_PRICE] = self.get_optimal_spot_price(
          self.open_connection(params), params[self.PARAM_INSTANCE_TYPE])

    return params
Пример #6
0
    def assert_required_parameters(self, parameters, operation):
        """
    Assert that all the parameters required for the EC2 agent are in place.
    (Also see documentation for the BaseAgent class)

    Args:
      parameters  A dictionary of parameters
      operation   Operations to be invoked using the above parameters
    """
        required_params = ()
        if operation == BaseAgent.OPERATION_RUN:
            required_params = self.REQUIRED_EC2_RUN_INSTANCES_PARAMS
        elif operation == BaseAgent.OPERATION_TERMINATE:
            required_params = self.REQUIRED_EC2_TERMINATE_INSTANCES_PARAMS

        for param in required_params:
            if not utils.has_parameter(param, parameters):
                raise AgentConfigurationException('no ' + param)
Пример #7
0
  def get_params_from_yaml(self, keyname):
    """Searches through the locations.yaml file to build a dict containing the
    parameters necessary to interact with Amazon EC2.

    Args:
      keyname: The name of the SSH keypair that uniquely identifies this
        AppScale deployment.
    """
    params = {
      self.PARAM_CREDENTIALS : {},
      self.PARAM_GROUP : LocalState.get_group(keyname),
      self.PARAM_KEYNAME : keyname
    }

    for credential in self.REQUIRED_CREDENTIALS:
      if os.environ[credential] and os.environ[credential] != '':
        params[self.PARAM_CREDENTIALS][credential] = os.environ[credential]
      else:
        raise AgentConfigurationException("no " + credential)

    return params
Пример #8
0
    def assert_credentials_are_valid(self, parameters):
        """Contacts GCE to see if the given credentials are valid.

    Args:
      parameters: A dict containing the credentials necessary to interact with
      GCE.

    Raises:
      AgentConfigurationException: If the given GCE credentials are invalid.
    """
        gce_service, credentials = self.open_connection(parameters)
        try:
            http = httplib2.Http()
            auth_http = credentials.authorize(http)
            request = gce_service.instances().list(
                project=parameters[self.PARAM_PROJECT],
                zone=parameters[self.PARAM_ZONE])
            response = request.execute(http=auth_http)
            AppScaleLogger.verbose(str(response),
                                   parameters[self.PARAM_VERBOSE])
            return True
        except apiclient.errors.HttpError:
            raise AgentConfigurationException("We couldn't validate your GCE" + \
              "credentials. Are your credentials valid?")
Пример #9
0
    def get_params_from_args(self, args):
        """ Constructs a dict with only the parameters necessary to interact with
    Google Compute Engine (here, the client_secrets file and the image name).

    Args:
      args: A Namespace or dict that maps all of the arguments the user has
        invoked an AppScale command with their associated value.
    Returns:
      A dict containing the location of the client_secrets file and that name
      of the image to use in GCE.
    Raises:
      AgentConfigurationException: If the caller fails to specify a
        client_secrets file, or if it doesn't exist on the local filesystem.
    """
        if not isinstance(args, dict):
            args = vars(args)

        if not args.get('client_secrets') and not args.get('oauth2_storage'):
            raise AgentConfigurationException("Please specify a client_secrets " + \
              "file or a oauth2_storage file in your AppScalefile when running " + \
              "over Google Compute Engine.")

        credentials_file = args.get('client_secrets') or args.get(
            'oauth2_storage')
        full_credentials = os.path.expanduser(credentials_file)
        if not os.path.exists(full_credentials):
            raise AgentConfigurationException("Couldn't find your credentials " + \
              "at {0}".format(full_credentials))

        if args.get('client_secrets'):
            destination = LocalState.get_client_secrets_location(
                args['keyname'])
        elif args.get('oauth2_storage'):
            destination = LocalState.get_oauth2_storage_location(
                args['keyname'])

        shutil.copy(full_credentials, destination)

        params = {
            self.PARAM_GROUP: args['group'],
            self.PARAM_IMAGE_ID: args['machine'],
            self.PARAM_INSTANCE_TYPE: args['gce_instance_type'],
            self.PARAM_KEYNAME: args['keyname'],
            self.PARAM_PROJECT: args['project'],
            self.PARAM_STATIC_IP: args.get(self.PARAM_STATIC_IP),
            self.PARAM_ZONE: args['zone']
        }

        # A zone in GCE looks like 'us-central2-a', which is in the region
        # 'us-central2'. Therefore, strip off the last two characters from the zone
        # to get the region name.
        if params[self.PARAM_ZONE]:
            params[self.PARAM_REGION] = params[self.PARAM_ZONE][:-2]
        else:
            params[self.PARAM_REGION] = self.DEFAULT_REGION

        if args.get(self.PARAM_SECRETS):
            params[self.PARAM_SECRETS] = args.get(self.PARAM_SECRETS)
        elif args.get(self.PARAM_STORAGE):
            params[self.PARAM_STORAGE] = args.get(self.PARAM_STORAGE)

        params[self.PARAM_VERBOSE] = args.get('verbose', False)
        self.assert_credentials_are_valid(params)

        return params