Ejemplo n.º 1
0
def sanitize_environment_variables_from_customer_input(
        environment_variables_input):
    """
    Returns a list of the sanitized key-value pairs in the `environment_variables_input` string,
    where pairs are comma-separated, by removing leading and trailing spaces and double quotes
    from and the key and value of each pair.

    :param environment_variables_input: a string of the form "KEY_1=VALUE_1,...,KYE_N=VALUE_N"
    :return: a list of the sanitized key-value pairs
    """
    if not environment_variables_input:
        return []

    key_value_pairs = environment_variables_input.split(',')

    environment_variables = []
    for key_value_pair in key_value_pairs:
        if '=' not in key_value_pair:
            raise InvalidSyntaxError(strings['setenv.invalidformat'])

        environment_variable, value = key_value_pair.split('=', 1)
        environment_variable = environment_variable.strip().strip('"')

        value = value.strip()
        value = __strip_leading_and_trailing_double_quotes(value)

        if not environment_variable:
            raise InvalidSyntaxError(strings['setenv.invalidformat'])

        environment_variables.append('='.join([environment_variable, value]))

    return environment_variables
Ejemplo n.º 2
0
 def _get_cfg_name(self, cmd):
     name = self.app.pargs.name
     if not name:
         io.echo('usage: eb config', cmd, '[configuration_name]')
         raise InvalidSyntaxError('too few arguments')
     else:
         return name
Ejemplo n.º 3
0
def _get_namespace_and_resource_name(namespace):
    if '.' not in namespace:
        return namespace, None
    else:
        parts = namespace.split('.')
        if len(parts) > 2:
            raise InvalidSyntaxError(prompts['update.invalidsyntax'])
        return parts[1], parts[0]
Ejemplo n.º 4
0
 def test_update_environment_configuration_bad_usr_modification(
         self, mock_fileops, mock_elasticbeanstalk, mock_env_settings,
         mock_commonops):
     mock_elasticbeanstalk.describe_configuration_settings.return_value = self.usr_model
     mock_fileops.get_environment_from_file.side_effect = InvalidSyntaxError(
         "Bad user changes")
     mock_env_settings.return_value = mock_env_settings
     mock_env_settings.convert_api_to_usr_model.return_value = self.usr_model
     mock_fileops.save_env_file.return_value = self.file_location
     configops.update_environment_configuration(self.app_name,
                                                self.env_name, self.nohang)
     mock_commonops.update_environment.assert_not_called()
Ejemplo n.º 5
0
def create_environment_variables_list(environment_variables,
                                      as_option_settings=True):
    """
    Returns a pair of environment variables to add and remove from a list of
    sanitized environment variables.

    If `as_option_settings` is set to `True`, the list of environment variables
    to add is transformed into option settings in the
    'aws:elasticbeanstalk:application:environment' namespace.

    :param environment_variables: a list of the sanitized environment variables
                                  specified in the format KEY_i=VALUE_i
    :param as_option_settings: boolean indicating whether to transform
                               `environment_variables` into option settings
    :return: a pair of environment variables to add and remove in the format
             dictated by `as_option_settings`
    """
    namespace = 'aws:elasticbeanstalk:application:environment'

    options = dict()
    options_to_remove = set()
    for environment_variable_string in environment_variables:
        if (not re.match(r'^[\w\\_.:/+@-][^="]*=.*$',
                         environment_variable_string)
                or '=' not in environment_variable_string):
            raise InvalidSyntaxError(strings['setenv.invalidformat'])

        environment_variable, value = environment_variable_string.split('=', 1)

        if value:
            options[environment_variable] = value
        else:
            options_to_remove.add(environment_variable)

    if as_option_settings:
        option_dict = options
        options = list()
        remove_list = options_to_remove
        options_to_remove = list()
        for environment_variable, value in iteritems(option_dict):
            options.append(
                dict(Namespace=namespace,
                     OptionName=environment_variable,
                     Value=value))

        for environment_variable in remove_list:
            options_to_remove.append(
                dict(Namespace=namespace, OptionName=environment_variable))

    return options, options_to_remove
Ejemplo n.º 6
0
def get_application_from_file(app_name):
    cwd = os.getcwd()
    file_name = beanstalk_directory + app_name

    try:
        ProjectRoot.traverse()
        file_ext = '.app.yml'
        path = file_name + file_ext
        if os.path.exists(path):
            with codecs.open(path, 'r', encoding='utf8') as f:
                return safe_load(f)
    except (ScannerError, ParserError):
        raise InvalidSyntaxError('The application file contains '
                                 'invalid syntax.')

    finally:
        os.chdir(cwd)
Ejemplo n.º 7
0
def get_environment_from_file(env_name):
    cwd = os.getcwd()
    file_name = beanstalk_directory + env_name

    try:
        _traverse_to_project_root()
        file_ext = '.env.yml'
        path = file_name + file_ext
        if os.path.exists(path):
            with codecs.open(path, 'r', encoding='utf8') as f:
                return load(f)
    except (ScannerError, ParserError):
        raise InvalidSyntaxError('The environment file contains '
                                 'invalid syntax.')

    finally:
        os.chdir(cwd)
Ejemplo n.º 8
0
    def test_interactive_update_lifecycle_policy_bad_user_input(
            self, mock_lifecycle_config):
        self.mock_beanstalk.describe_application.return_value = self.app_response
        mock_lifecycle_config.return_value = mock_lifecycle_config
        mock_lifecycle_config.convert_api_to_usr_model.return_value = self.usr_model
        self.mock_fileops.save_app_file.return_value = self.file_location
        self.mock_fileops.get_application_from_file.side_effect = InvalidSyntaxError(
            "Bad syntax in config.")

        lifecycleops.interactive_update_lifcycle_policy(self.app_name)

        self.mock_beanstalk.describe_application.assert_called_with(
            self.app_name)
        mock_lifecycle_config.assert_called_with(self.app_response)
        self.mock_fileops.save_app_file.assert_called_with(self.usr_model)
        self.mock_fileops.open_file_for_editing.assert_called_with(
            self.file_location)
        self.mock_fileops.get_application_from_file.assert_called_with(
            self.app_name)
        mock_lifecycle_config.collect_changes.assert_not_called()
        self.mock_fileops.delete_app_file.assert_called_with(self.app_name)
        self.mock_beanstalk.update_application_resource_lifecycle.assert_not_called(
        )