Пример #1
0
    def test_load_master_config_from_environ_var(self):
        original_environ = os.environ.copy()

        tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
        try:
            env_root_dir = os.path.join(tempdir, "foo", "env")
            os.makedirs(env_root_dir)
            env_fpath = os.path.join(env_root_dir, "config-env")

            salt.utils.fopen(env_fpath, "w").write("root_dir: {0}\n" "log_file: {1}\n".format(env_root_dir, env_fpath))

            os.environ["SALT_MASTER_CONFIG"] = env_fpath
            # Should load from env variable, not the default configuration file.
            config = sconfig.master_config("/etc/salt/master")
            self.assertEqual(config["log_file"], env_fpath)
            os.environ.clear()
            os.environ.update(original_environ)

            root_dir = os.path.join(tempdir, "foo", "bar")
            os.makedirs(root_dir)
            fpath = os.path.join(root_dir, "config")
            salt.utils.fopen(fpath, "w").write("root_dir: {0}\n" "log_file: {1}\n".format(root_dir, fpath))
            # Let's set the environment variable, yet, since the configuration
            # file path is not the default one, ie, the user has passed an
            # alternative configuration file form the CLI parser, the
            # environment variable will be ignored.
            os.environ["SALT_MASTER_CONFIG"] = env_fpath
            config = sconfig.master_config(fpath)
            self.assertEqual(config["log_file"], fpath)
            os.environ.clear()
            os.environ.update(original_environ)

        finally:
            if os.path.isdir(tempdir):
                shutil.rmtree(tempdir)
Пример #2
0
def _get_local_grains():
    """
    Return the salt grains for this host that we are running
    on.  If we support SELinux in the future this may need
    to be moved into a cthulhu RPC as the apache worker may
    not have the right capabilities to query all the grains.
    """
    # Stash grains as an attribute of this function
    if not hasattr(_get_local_grains, 'grains'):

        # >> work around salt issue #11402
        import __main__ as main
        main.__file__ = 'workaround'
        # <<

        # Use salt to get an interesting subset of the salt grains (getting
        # everything is a bit slow)
        grains = {}
        c = master_config(config.get('cthulhu', 'salt_config_path'))
        l = _create_loader(c, 'grains', 'grain')
        funcs = l.gen_functions()
        for key in [k for k in funcs.keys() if k.startswith('core.')]:
            ret = funcs[key]()
            if isinstance(ret, dict):
                grains.update(ret)
        _get_local_grains.grains = grains
    else:
        grains = _get_local_grains.grains

    return grains
Пример #3
0
    def test_issue_6714_parsing_errors_logged(self):
        try:
            tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
            test_config = os.path.join(tempdir, 'config')

            # Let's populate a master configuration file with some basic
            # settings
            salt.utils.fopen(test_config, 'w').write(
                'root_dir: {0}\n'
                'log_file: {0}/foo.log\n'.format(tempdir) +
                '\n\n\n'
                'blah:false\n'
            )

            with TestsLoggingHandler() as handler:
                # Let's load the configuration
                config = sconfig.master_config(test_config)
                for message in handler.messages:
                    if message.startswith('ERROR:Error parsing configuration'):
                        break
                else:
                    raise AssertionError(
                        'No parsing error message was logged'
                    )
        finally:
            if os.path.isdir(tempdir):
                shutil.rmtree(tempdir)
Пример #4
0
    def test_issue_6714_parsing_errors_logged(self):
        try:
            tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
            test_config = os.path.join(tempdir, 'config')

            # Let's populate a master configuration file with some basic
            # settings
            salt.utils.fopen(test_config, 'w').write(
                'root_dir: {0}\n'
                'log_file: {0}/foo.log\n'.format(tempdir) +
                '\n\n\n'
                'blah:false\n'
            )

            with TestsLoggingHandler() as handler:
                # Let's load the configuration
                config = sconfig.master_config(test_config)
                for message in handler.messages:
                    if message.startswith('ERROR:Error parsing configuration'):
                        break
                else:
                    raise AssertionError(
                        'No parsing error message was logged'
                    )
        finally:
            if os.path.isdir(tempdir):
                shutil.rmtree(tempdir)
Пример #5
0
    def test_master_confd_inclusion(self):
        try:
            tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
            master_config = os.path.join(tempdir, 'master')
            master_confd = os.path.join(tempdir, 'master.d')
            os.makedirs(master_confd)

            # Let's populate a master configuration file with some basic
            # settings
            salt.utils.fopen(master_config,
                             'w').write('blah: false\n'
                                        'root_dir: {0}\n'
                                        'log_file: {1}\n'.format(
                                            tempdir, master_config))

            # Now, let's populate an extra configuration file under master.d
            # Notice that above we've set blah as False and bellow as True.
            # Since the master.d files are loaded after the main configuration
            # file so overrides can happen, the final value of blah should be
            # True.
            extra_config = os.path.join(master_confd, 'extra.conf')
            salt.utils.fopen(extra_config, 'w').write('blah: true\n')

            # Let's load the configuration
            config = sconfig.master_config(master_config)

            self.assertEqual(config['log_file'], master_config)
            # As proven by the assertion below, blah is True
            self.assertTrue(config['blah'])
        finally:
            if os.path.isdir(tempdir):
                shutil.rmtree(tempdir)
Пример #6
0
    def test_proper_path_joining(self):
        fpath = tempfile.mktemp()
        temp_config = 'root_dir: /\n'\
                      'key_logfile: key\n'
        if salt.utils.is_windows():
            temp_config = 'root_dir: c:\\\n'\
                          'key_logfile: key\n'
        try:
            with salt.utils.fopen(fpath, 'w') as fp_:
                fp_.write(temp_config)

            config = sconfig.master_config(fpath)
            expect_path_join = os.path.join('/', 'key')
            expect_sep_join = '//key'
            if salt.utils.is_windows():
                expect_path_join = os.path.join('c:\\', 'key')
                expect_sep_join = 'c:\\\\key'

            # os.path.join behavior
            self.assertEqual(config['key_logfile'], expect_path_join)
            # os.sep.join behavior
            self.assertNotEqual(config['key_logfile'], expect_sep_join)
        finally:
            if os.path.isfile(fpath):
                os.unlink(fpath)
Пример #7
0
    def test_master_confd_inclusion(self):
        try:
            tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
            master_config = os.path.join(tempdir, 'master')
            master_confd = os.path.join(tempdir, 'master.d')
            os.makedirs(master_confd)

            # Let's populate a master configuration file with some basic
            # settings
            salt.utils.fopen(master_config, 'w').write(
                'blah: false\n'
                'root_dir: {0}\n'
                'log_file: {1}\n'.format(tempdir, master_config)
            )

            # Now, let's populate an extra configuration file under master.d
            # Notice that above we've set blah as False and bellow as True.
            # Since the master.d files are loaded after the main configuration
            # file so overrides can happen, the final value of blah should be
            # True.
            extra_config = os.path.join(master_confd, 'extra.conf')
            salt.utils.fopen(extra_config, 'w').write(
                'blah: true\n'
            )

            # Let's load the configuration
            config = sconfig.master_config(master_config)

            self.assertEqual(config['log_file'], master_config)
            # As proven by the assertion below, blah is True
            self.assertTrue(config['blah'])
        finally:
            if os.path.isdir(tempdir):
                shutil.rmtree(tempdir)
Пример #8
0
    def setup_config(self):
        opts = config.master_config(self.get_config_file_path('master'))
        user = opts.get('user', 'root')
        opts['_minion_conf_file'] = opts['conf_file']
        opts.update(config.minion_config(self.get_config_file_path('minion')))
        # Over ride the user from the master config file
        opts['user'] = user

        if 'syndic_master' not in opts:
            self.error(
                "The syndic_master needs to be configured in the salt master "
                "config, EXITING!"
            )

        from salt import utils
        # Some of the opts need to be changed to match the needed opts
        # in the minion class.
        opts['master'] = opts['syndic_master']
        opts['master_ip'] = utils.dns_check(opts['master'])

        opts['master_uri'] = 'tcp://{0}:{1}'.format(
            opts['master_ip'], str(opts['master_port'])
        )
        opts['_master_conf_file'] = opts['conf_file']
        opts.pop('conf_file')
        return opts
Пример #9
0
    def test_master_pillar_roots_glob(self):
        # Config file and stub pillar_roots.
        fpath = tempfile.mktemp()
        tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
        try:
            # Create some kown files.
            for f in 'abc':
                fpath = os.path.join(tempdir, f)
                salt.utils.fopen(fpath, 'w').write(f)

            salt.utils.fopen(fpath, 'w').write('pillar_roots:\n'
                                               '  base:\n'
                                               '    - {0}'.format(
                                                   os.path.join(tempdir, '*')))
            config = sconfig.master_config(fpath)
            base = config['pillar_roots']['base']
            self.assertEqual(
                set(base),
                set([
                    os.path.join(tempdir, 'a'),
                    os.path.join(tempdir, 'b'),
                    os.path.join(tempdir, 'c')
                ]))
        finally:
            if os.path.isfile(fpath):
                os.unlink(fpath)
            if os.path.isdir(tempdir):
                shutil.rmtree(tempdir)
Пример #10
0
    def __init__(self,main_config):
	self.opts=master_config(main_config)
	self.wheel=Wheel(self.opts)
	self.client=LocalClient()
	self.connected_minions_list=self.wheel.call_func('minions.connected')
	self.key_dict=self.wheel.call_func('key.list_all')
	self.total=len(self.key_dict['minions'])+len(self.key_dict['minions_pre'])+len(self.key_dict['minions_denied'])+len(self.key_dict['minions_rejected'])
Пример #11
0
def _master_opts(cfg='master'):
    cfg = os.environ.get(
        'SALT_MASTER_CONFIG',
        __opts__.get('conf_file',
                     os.path.join(salt.syspaths.CONFIG_DIR, cfg)))
    opts = config.master_config(cfg)
    return opts
Пример #12
0
def _get_local_grains():
    """
    Return the salt grains for this host that we are running
    on.  If we support SELinux in the future this may need
    to be moved into a cthulhu RPC as the apache worker may
    not have the right capabilities to query all the grains.
    """
    # Stash grains as an attribute of this function
    if not hasattr(_get_local_grains, 'grains'):

        # >> work around salt issue #11402
        import __main__ as main
        main.__file__ = 'workaround'
        # <<

        # Use salt to get an interesting subset of the salt grains (getting
        # everything is a bit slow)
        grains = {}
        c = master_config(config.get('cthulhu', 'salt_config_path'))
        l = _create_loader(c, 'grains', 'grain')
        funcs = l.gen_functions()
        for key in [k for k in funcs.keys() if k.startswith('core.')]:
            ret = funcs[key]()
            if isinstance(ret, dict):
                grains.update(ret)
        _get_local_grains.grains = grains
    else:
        grains = _get_local_grains.grains

    return grains
Пример #13
0
def _master_opts(cfg='master'):
    cfg = os.environ.get(
        'SALT_MASTER_CONFIG',
        __opts__.get('conf_file',
                     os.path.join(salt.syspaths.CONFIG_DIR, cfg)))
    opts = config.master_config(cfg)
    return opts
Пример #14
0
def get_pillar(minion='*', skipped=None, saltenv='base', **kwargs):
    '''
    Returns the compiled pillar either of a specific minion
    or just the global available pillars. This function assumes
    that no minion has the id ``*``.
    '''
    _o = __opts__
    try:
        fic = _o['config_dir'] + '/master'
        os.stat(fic)  # may raise OSError
        mopts = config.master_config(fic)
    except (KeyError, OSError):
        # may not have master config in masterless setups
        mopts = copy.deepcopy(_o)
    id_, grains, _ = salt.utils.minions.get_minion_data(minion, mopts)
    if not grains:
        grains = {}
    grains = copy.deepcopy(grains)
    did = {'fqdn': minion, 'id': minion}
    # for d in [grains, mopts]:
    for d in [grains]:
        d.update(did)
    pillar = salt.pillar.Pillar(mopts, grains, id_, saltenv)
    if not skipped:
        skipped = ['master']
    compiled_pillar = pillar.compile_pillar()
    for k in skipped:
        compiled_pillar.pop(k, None)
    return compiled_pillar
Пример #15
0
def _master_opts(cfg='master'):
    if 'conf_file' in __opts__:
        default_dir = os.path.dirname(__opts__['conf_file'])
    else:
        default_dir = __opts__['config_dir'],
    cfg = os.environ.get('SALT_MASTER_CONFIG', os.path.join(default_dir, cfg))
    opts = config.master_config(cfg)
    return opts
Пример #16
0
def _master_opts(cfgdir=None, cfg=None):
    if not cfgdir:
        cfgdir = salt.syspaths.CONFIG_DIR
    if not cfg:
        cfg = os.environ.get('SALT_MASTER_CONFIG', 'master')
        cfg = os.path.join(cfgdir, cfg)
    opts = config.master_config(cfg)
    return opts
Пример #17
0
    def test_load_client_config_from_environ_var(self):
        original_environ = os.environ.copy()
        try:
            tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
            env_root_dir = os.path.join(tempdir, 'foo', 'env')
            os.makedirs(env_root_dir)

            # Let's populate a master configuration file which should not get
            # picked up since the client configuration tries to load the master
            # configuration settings using the provided client configuration
            # file
            master_config = os.path.join(env_root_dir, 'master')
            with salt.utils.fopen(master_config, 'w') as fp_:
                fp_.write(
                    'blah: true\n'
                    'root_dir: {0}\n'
                    'log_file: {1}\n'.format(env_root_dir, master_config)
                )
            os.environ['SALT_MASTER_CONFIG'] = master_config

            # Now the client configuration file
            env_fpath = os.path.join(env_root_dir, 'config-env')
            with salt.utils.fopen(env_fpath, 'w') as fp_:
                fp_.write(
                    'root_dir: {0}\n'
                    'log_file: {1}\n'.format(env_root_dir, env_fpath)
                )

            os.environ['SALT_CLIENT_CONFIG'] = env_fpath
            # Should load from env variable, not the default configuration file
            config = sconfig.client_config(os.path.expanduser('~/.salt'))
            self.assertEqual(config['log_file'], env_fpath)
            self.assertTrue('blah' not in config)
            os.environ.clear()
            os.environ.update(original_environ)

            root_dir = os.path.join(tempdir, 'foo', 'bar')
            os.makedirs(root_dir)
            fpath = os.path.join(root_dir, 'config')
            with salt.utils.fopen(fpath, 'w') as fp_:
                fp_.write(
                    'root_dir: {0}\n'
                    'log_file: {1}\n'.format(root_dir, fpath)
                )
            # Let's set the environment variable, yet, since the configuration
            # file path is not the default one, i.e., the user has passed an
            # alternative configuration file form the CLI parser, the
            # environment variable will be ignored.
            os.environ['SALT_MASTER_CONFIG'] = env_fpath
            config = sconfig.master_config(fpath)
            self.assertEqual(config['log_file'], fpath)
            os.environ.clear()
            os.environ.update(original_environ)

        finally:
            if os.path.isdir(tempdir):
                shutil.rmtree(tempdir)
Пример #18
0
 def test_proper_path_joining(self):
     fpath = tempfile.mktemp()
     salt.utils.fopen(fpath, 'w').write("root_dir: /\n"
                                        "key_logfile: key\n")
     config = sconfig.master_config(fpath)
     # os.path.join behaviour
     self.assertEqual(config['key_logfile'], os.path.join('/', 'key'))
     # os.sep.join behaviour
     self.assertNotEqual(config['key_logfile'], '//key')
Пример #19
0
    def setup_config(self):
        keys_config = config.master_config(self.get_config_file_path('master'))
        if self.options.gen_keys:
            # Since we're generating the keys, some defaults can be assumed
            # or tweaked
            keys_config['key_logfile'] = os.devnull
            keys_config['pki_dir'] = self.options.gen_keys_dir

        return keys_config
Пример #20
0
Файл: lxc.py Проект: bryson/salt
def _master_opts(cfg='master'):
    if 'conf_file' in __opts__:
        default_dir = os.path.dirname(__opts__['conf_file'])
    else:
        default_dir = __opts__['config_dir'],
    cfg = os.environ.get(
        'SALT_MASTER_CONFIG', os.path.join(default_dir, cfg))
    opts = config.master_config(cfg)
    return opts
Пример #21
0
def _master_opts(cfg="master"):
    if "conf_file" in __opts__:
        default_dir = os.path.dirname(__opts__["conf_file"])
    else:
        default_dir = (__opts__["config_dir"], )
    cfg = os.environ.get("SALT_MASTER_CONFIG", os.path.join(default_dir, cfg))
    opts = config.master_config(cfg)
    opts["output"] = "quiet"
    return opts
Пример #22
0
    def test_load_client_config_from_environ_var(self):
        original_environ = os.environ.copy()
        try:
            tempdir = tempfile.mkdtemp(dir=TMP)
            env_root_dir = os.path.join(tempdir, 'foo', 'env')
            os.makedirs(env_root_dir)

            # Let's populate a master configuration file which should not get
            # picked up since the client configuration tries to load the master
            # configuration settings using the provided client configuration
            # file
            master_config = os.path.join(env_root_dir, 'master')
            with salt.utils.fopen(master_config, 'w') as fp_:
                fp_.write(
                    'blah: true\n'
                    'root_dir: {0}\n'
                    'log_file: {1}\n'.format(env_root_dir, master_config)
                )
            os.environ['SALT_MASTER_CONFIG'] = master_config

            # Now the client configuration file
            env_fpath = os.path.join(env_root_dir, 'config-env')
            with salt.utils.fopen(env_fpath, 'w') as fp_:
                fp_.write(
                    'root_dir: {0}\n'
                    'log_file: {1}\n'.format(env_root_dir, env_fpath)
                )

            os.environ['SALT_CLIENT_CONFIG'] = env_fpath
            # Should load from env variable, not the default configuration file
            config = sconfig.client_config(os.path.expanduser('~/.salt'))
            self.assertEqual(config['log_file'], env_fpath)
            self.assertTrue('blah' not in config)
            os.environ.clear()
            os.environ.update(original_environ)

            root_dir = os.path.join(tempdir, 'foo', 'bar')
            os.makedirs(root_dir)
            fpath = os.path.join(root_dir, 'config')
            with salt.utils.fopen(fpath, 'w') as fp_:
                fp_.write(
                    'root_dir: {0}\n'
                    'log_file: {1}\n'.format(root_dir, fpath)
                )
            # Let's set the environment variable, yet, since the configuration
            # file path is not the default one, i.e., the user has passed an
            # alternative configuration file form the CLI parser, the
            # environment variable will be ignored.
            os.environ['SALT_MASTER_CONFIG'] = env_fpath
            config = sconfig.master_config(fpath)
            self.assertEqual(config['log_file'], fpath)
            os.environ.clear()
            os.environ.update(original_environ)

        finally:
            if os.path.isdir(tempdir):
                shutil.rmtree(tempdir)
Пример #23
0
    def setup_config(self):
        keys_config = config.master_config(self.get_config_file_path('master'))
        if self.options.gen_keys:
            # Since we're generating the keys, some defaults can be assumed
            # or tweaked
            keys_config['key_logfile'] = os.devnull
            keys_config['pki_dir'] = self.options.gen_keys_dir

        return keys_config
Пример #24
0
    def setUp(self):
        master_config_path = self.get_config_file_path('master')
        master_config = config.master_config(master_config_path)
        if not os.path.exists(master_config['cachedir']):
            os.makedirs(master_config['cachedir'])
        if not os.path.exists(integration.TMP_CONF_DIR):
            os.makedirs(integration.TMP_CONF_DIR)

        self.local_client = client.LocalClient(mopts=master_config)
Пример #25
0
    def test_load_master_config_from_environ_var(self):
        original_environ = os.environ.copy()

        tempdir = tempfile.mkdtemp(dir=TMP)
        try:
            env_root_dir = os.path.join(tempdir, 'foo', 'env')
            os.makedirs(env_root_dir)
            env_fpath = os.path.join(env_root_dir, 'config-env')

            with salt.utils.fopen(env_fpath, 'w') as fp_:
                fp_.write(
                    'root_dir: {0}\n'
                    'log_file: {1}\n'.format(env_root_dir, env_fpath)
                )

            os.environ['SALT_MASTER_CONFIG'] = env_fpath
            # Should load from env variable, not the default configuration file.
            config = sconfig.master_config('{0}/master'.format(CONFIG_DIR))
            self.assertEqual(config['log_file'], env_fpath)
            os.environ.clear()
            os.environ.update(original_environ)

            root_dir = os.path.join(tempdir, 'foo', 'bar')
            os.makedirs(root_dir)
            fpath = os.path.join(root_dir, 'config')
            with salt.utils.fopen(fpath, 'w') as fp_:
                fp_.write(
                    'root_dir: {0}\n'
                    'log_file: {1}\n'.format(root_dir, fpath)
                )
            # Let's set the environment variable, yet, since the configuration
            # file path is not the default one, i.e., the user has passed an
            # alternative configuration file form the CLI parser, the
            # environment variable will be ignored.
            os.environ['SALT_MASTER_CONFIG'] = env_fpath
            config = sconfig.master_config(fpath)
            self.assertEqual(config['log_file'], fpath)
            os.environ.clear()
            os.environ.update(original_environ)

        finally:
            if os.path.isdir(tempdir):
                shutil.rmtree(tempdir)
Пример #26
0
    def test_load_master_config_from_environ_var(self):
        original_environ = os.environ.copy()

        tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
        try:
            env_root_dir = os.path.join(tempdir, 'foo', 'env')
            os.makedirs(env_root_dir)
            env_fpath = os.path.join(env_root_dir, 'config-env')

            with salt.utils.fopen(env_fpath, 'w') as fp_:
                fp_.write(
                    'root_dir: {0}\n'
                    'log_file: {1}\n'.format(env_root_dir, env_fpath)
                )

            os.environ['SALT_MASTER_CONFIG'] = env_fpath
            # Should load from env variable, not the default configuration file.
            config = sconfig.master_config('/etc/salt/master')
            self.assertEqual(config['log_file'], env_fpath)
            os.environ.clear()
            os.environ.update(original_environ)

            root_dir = os.path.join(tempdir, 'foo', 'bar')
            os.makedirs(root_dir)
            fpath = os.path.join(root_dir, 'config')
            with salt.utils.fopen(fpath, 'w') as fp_:
                fp_.write(
                    'root_dir: {0}\n'
                    'log_file: {1}\n'.format(root_dir, fpath)
                )
            # Let's set the environment variable, yet, since the configuration
            # file path is not the default one, i.e., the user has passed an
            # alternative configuration file form the CLI parser, the
            # environment variable will be ignored.
            os.environ['SALT_MASTER_CONFIG'] = env_fpath
            config = sconfig.master_config(fpath)
            self.assertEqual(config['log_file'], fpath)
            os.environ.clear()
            os.environ.update(original_environ)

        finally:
            if os.path.isdir(tempdir):
                shutil.rmtree(tempdir)
Пример #27
0
 def test_sha256_is_default_for_master(self):
     fpath = tempfile.mktemp()
     try:
         salt.utils.fopen(fpath, 'w').write("root_dir: /\n"
                                            "key_logfile: key\n")
         config = sconfig.master_config(fpath)
         self.assertEqual(config['hash_type'], 'sha256')
     finally:
         if os.path.isfile(fpath):
             os.unlink(fpath)
Пример #28
0
def enable(container):
    with open('/tmpwat', 'w') as what:
        print('what', file=what)
    config = sac.cloud_config('/etc/salt/cloud')
    master = sac.master_config('/etc/salt/master')
    api_key = config['providers']['my-nova']['nova']['api_key']
    ident = config['providers']['my-nova']['nova']['identity_url']
    tenant = config['providers']['my-nova']['nova']['tenant']
    user = config['providers']['my-nova']['nova']['user']
    region = config['providers']['my-nova']['nova']['compute_region']
    headers = {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    }
    payload={
      "auth": {
        "RAX-KSKEY:apiKeyCredentials": {
          "username": user,
          "apiKey": api_key
        }
      }
    }
    identity = requests.post(os.path.join(ident, 'tokens'), data=json.dumps(payload), headers=headers).json().get('access', {})
    headers['X-Auth-Token'] = identity['token']['id']
    catalog = identity['serviceCatalog']

    endpoint = _get_endpoint('object-store', region, catalog)
    cdnendpoint = _get_endpoint('rax:object-cdn', region, catalog)

    containers = requests.get(endpoint, headers=headers).json()
    cont = None
    for c in containers:
        if c['name'] == container:
            cont = c
            log.debug('Container: \n{0}'.format(pprint.pformat(cont)))
            log.debug('Container: \n{0}'.format(pprint.pformat(headers)))
            log.debug('Container: \n{0}'.format(pprint.pformat(endpoint)))
            break

    if cont is None:
        log.debug("Container doesn't exist: {0}".format(container))
        return False

    headers['X-Ttl'] = 900
    headers['X-Cdn-Enabled'] = 'True'
    ret = requests.put('{0}/{1}'.format(cdnendpoint, container), headers=headers)
    if ret.status_code == 201:
        return True
    elif ret.status_code == 202:
        log.debug('Container already enabled: {0}'.format(container))
        return None
    else:
        log.debug('Failed to enable container: {0}'.format(container))
        return False
Пример #29
0
 def test_common_prefix_stripping(self):
     tempdir = tempfile.mkdtemp()
     root_dir = os.path.join(tempdir, 'foo', 'bar')
     os.makedirs(root_dir)
     fpath = os.path.join(root_dir, 'config')
     salt.utils.fopen(fpath,
                      'w').write('root_dir: {0}\n'
                                 'log_file: {1}\n'.format(root_dir, fpath))
     config = sconfig.master_config(fpath)
     self.assertEqual(config['log_file'], fpath)
     shutil.rmtree(tempdir)
Пример #30
0
 def test_proper_path_joining(self):
     fpath = tempfile.mktemp()
     open(fpath, 'w').write(
         "root_dir: /\n"
         "key_logfile: key\n"
     )
     config = sconfig.master_config(fpath)
     # os.path.join behaviour
     self.assertEqual(config['key_logfile'], os.path.join('/', 'key'))
     # os.sep.join behaviour
     self.assertNotEqual(config['key_logfile'], '//key')
Пример #31
0
 def test_sha256_is_default_for_master(self):
     fpath = tempfile.mktemp()
     try:
         salt.utils.fopen(fpath, 'w').write(
             "root_dir: /\n"
             "key_logfile: key\n"
         )
         config = sconfig.master_config(fpath)
         self.assertEqual(config['hash_type'], 'sha256')
     finally:
         if os.path.isfile(fpath):
             os.unlink(fpath)
Пример #32
0
 def test_proper_path_joining(self):
     fpath = tempfile.mktemp()
     try:
         salt.utils.fopen(fpath, "w").write("root_dir: /\n" "key_logfile: key\n")
         config = sconfig.master_config(fpath)
         # os.path.join behaviour
         self.assertEqual(config["key_logfile"], os.path.join("/", "key"))
         # os.sep.join behaviour
         self.assertNotEqual(config["key_logfile"], "//key")
     finally:
         if os.path.isfile(fpath):
             os.unlink(fpath)
Пример #33
0
 def test_common_prefix_stripping(self):
     tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
     try:
         root_dir = os.path.join(tempdir, "foo", "bar")
         os.makedirs(root_dir)
         fpath = os.path.join(root_dir, "config")
         salt.utils.fopen(fpath, "w").write("root_dir: {0}\n" "log_file: {1}\n".format(root_dir, fpath))
         config = sconfig.master_config(fpath)
         self.assertEqual(config["log_file"], fpath)
     finally:
         if os.path.isdir(tempdir):
             shutil.rmtree(tempdir)
Пример #34
0
 def test_common_prefix_stripping(self):
     tempdir = tempfile.mkdtemp()
     root_dir = os.path.join(tempdir, 'foo', 'bar')
     os.makedirs(root_dir)
     fpath = os.path.join(root_dir, 'config')
     salt.utils.fopen(fpath, 'w').write(
         'root_dir: {0}\n'
         'log_file: {1}\n'.format(root_dir, fpath)
     )
     config = sconfig.master_config(fpath)
     self.assertEqual(config['log_file'], fpath)
     shutil.rmtree(tempdir)
Пример #35
0
 def test_proper_path_joining(self):
     fpath = tempfile.mktemp()
     try:
         with salt.utils.fopen(fpath, 'w') as fp_:
             fp_.write('root_dir: /\n' 'key_logfile: key\n')
         config = sconfig.master_config(fpath)
         # os.path.join behavior
         self.assertEqual(config['key_logfile'], os.path.join('/', 'key'))
         # os.sep.join behavior
         self.assertNotEqual(config['key_logfile'], '//key')
     finally:
         if os.path.isfile(fpath):
             os.unlink(fpath)
Пример #36
0
 def test_common_prefix_stripping(self):
     tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
     try:
         root_dir = os.path.join(tempdir, 'foo', 'bar')
         os.makedirs(root_dir)
         fpath = os.path.join(root_dir, 'config')
         with salt.utils.fopen(fpath, 'w') as fp_:
             fp_.write('root_dir: {0}\n'
                       'log_file: {1}\n'.format(root_dir, fpath))
         config = sconfig.master_config(fpath)
         self.assertEqual(config['log_file'], fpath)
     finally:
         if os.path.isdir(tempdir):
             shutil.rmtree(tempdir)
Пример #37
0
 def test_proper_path_joining(self):
     fpath = tempfile.mktemp()
     try:
         with salt.utils.fopen(fpath, 'w') as fp_:
             fp_.write(
                 'root_dir: /\n'
                 'key_logfile: key\n'
             )
         config = sconfig.master_config(fpath)
         # os.path.join behavior
         self.assertEqual(config['key_logfile'], os.path.join('/', 'key'))
         # os.sep.join behavior
         self.assertNotEqual(config['key_logfile'], '//key')
     finally:
         if os.path.isfile(fpath):
             os.unlink(fpath)
Пример #38
0
 def test_common_prefix_stripping(self):
     tempdir = tempfile.mkdtemp(dir=integration.SYS_TMP_DIR)
     try:
         root_dir = os.path.join(tempdir, 'foo', 'bar')
         os.makedirs(root_dir)
         fpath = os.path.join(root_dir, 'config')
         with salt.utils.fopen(fpath, 'w') as fp_:
             fp_.write(
                 'root_dir: {0}\n'
                 'log_file: {1}\n'.format(root_dir, fpath)
             )
         config = sconfig.master_config(fpath)
         self.assertEqual(config['log_file'], fpath)
     finally:
         if os.path.isdir(tempdir):
             shutil.rmtree(tempdir)
Пример #39
0
 def test_default_root_dir_included_in_config_root_dir(self):
     os.makedirs(os.path.join(TMP, 'tmp2'))
     tempdir = tempfile.mkdtemp(dir=os.path.join(TMP, 'tmp2'))
     try:
         root_dir = os.path.join(tempdir, 'foo', 'bar')
         os.makedirs(root_dir)
         fpath = os.path.join(root_dir, 'config')
         with salt.utils.files.fopen(fpath, 'w') as fp_:
             fp_.write('root_dir: {0}\n'
                       'log_file: {1}\n'.format(root_dir, fpath))
         with patch('salt.syspaths.ROOT_DIR', TMP):
             config = sconfig.master_config(fpath)
         self.assertEqual(config['log_file'], fpath)
     finally:
         if os.path.isdir(tempdir):
             shutil.rmtree(tempdir)
Пример #40
0
def delete():
    config = sac.cloud_config('/etc/salt/cloud')
    master = sac.master_config('/etc/salt/master')
    api_key = config['providers']['my-nova']['nova']['api_key']
    ident = config['providers']['my-nova']['nova']['identity_url']
    tenant = config['providers']['my-nova']['nova']['tenant']
    user = config['providers']['my-nova']['nova']['user']
    region = config['providers']['my-nova']['nova']['compute_region']
    loadbalancer = master['loadbalancer']['name']
    port = master['loadbalancer']['port']
    lbendpoint = 'https://{0}.loadbalancers.api.rackspacecloud.com/v1.0/{1}'.format(region, tenant)
    headers = {
      'Content-Type': 'application/json'
    }
    payload={
      "auth": {
        "RAX-KSKEY:apiKeyCredentials": {
          "username": user,
          "apiKey": api_key
        }
      }
    }
    ret = requests.post(os.path.join(ident, 'tokens'), data=json.dumps(payload), headers=headers).json()
    headers['X-Auth-Token'] = ret['access']['token']['id']

    loadbalancers = requests.get('{0}/loadbalancers'.format(lbendpoint), headers=headers).json().get('loadBalancers', {})
    lb = None
    for l in loadbalancers:
        if l['name'] == loadbalancer:
            lb = l

    if lb is None:
        log.debug("Loadbalancer doesn't exist: {0}".format(loadbalancer))
        return False

    if _wait_for_loadbalancer(lb['id'], lbendpoint, headers):
        ret = requests.delete('{0}/loadbalancers/{1}'.format(lbendpoint, lb['id']), headers=headers)
    else:
        log.debug("Loadbalancer stuck not in Active: {0}".format(loadbalancer))
        return False

    if ret.status_code == 202:
        return True
    else:
        return False
Пример #41
0
def create():
    config = sac.cloud_config('/etc/salt/cloud')
    master = sac.master_config('/etc/salt/master')
    api_key = config['providers']['my-nova']['nova']['api_key']
    ident = config['providers']['my-nova']['nova']['identity_url']
    tenant = config['providers']['my-nova']['nova']['tenant']
    user = config['providers']['my-nova']['nova']['user']
    region = config['providers']['my-nova']['nova']['compute_region']
    loadbalancer = master['loadbalancer']['name']
    port = master['loadbalancer']['port']
    lbendpoint = 'https://{0}.loadbalancers.api.rackspacecloud.com/v1.0/{1}'.format(region, tenant)
    headers = {
      'Content-Type': 'application/json'
    }
    payload={
      "auth": {
        "RAX-KSKEY:apiKeyCredentials": {
          "username": user,
          "apiKey": api_key
        }
      }
    }
    ret = requests.post(os.path.join(ident, 'tokens'), data=json.dumps(payload), headers=headers).json()
    headers['X-Auth-Token'] = ret['access']['token']['id']

    loadbalancers = requests.get('{0}/loadbalancers'.format(lbendpoint), headers=headers).json().get('loadBalancers', {})
    for l in loadbalancers:
        if l['name'] == loadbalancer:
            log.debug('Loadbalancer exists: {0}'.format(loadbalancer))
            return None

    payload = json.dumps({
        "loadBalancer": {
            "name": loadbalancer,
            "port": port,
            "protocol": "HTTP",
            "virtualIps": [
                {"type": "PUBLIC"}
            ]
        }
    })
    ret = requests.post('{0}/loadbalancers'.format(lbendpoint), headers=headers, data=payload).json().get('loadBalancer', {})
    _wait_for_loadbalancer(ret['id'], lbendpoint, headers)
    return ret
Пример #42
0
    def setup_config(self):
        opts = config.master_config(self.get_config_file_path("master"))
        opts["_minion_conf_file"] = opts["conf_file"]
        opts.update(config.minion_config(self.get_config_file_path("minion")))

        if "syndic_master" not in opts:
            self.error("The syndic_master needs to be configured in the salt master " "config, EXITING!")

        from salt import utils

        # Some of the opts need to be changed to match the needed opts
        # in the minion class.
        opts["master"] = opts["syndic_master"]
        opts["master_ip"] = utils.dns_check(opts["master"])

        opts["master_uri"] = "tcp://{0}:{1}".format(opts["master_ip"], str(opts["master_port"]))
        opts["_master_conf_file"] = opts["conf_file"]
        opts.pop("conf_file")
        return opts
Пример #43
0
def create(container):
    config = sac.cloud_config('/etc/salt/cloud')
    master = sac.master_config('/etc/salt/master')
    api_key = config['providers']['my-nova']['nova']['api_key']
    ident = config['providers']['my-nova']['nova']['identity_url']
    tenant = config['providers']['my-nova']['nova']['tenant']
    user = config['providers']['my-nova']['nova']['user']
    region = config['providers']['my-nova']['nova']['compute_region']
    headers = {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    }
    payload={
      "auth": {
        "RAX-KSKEY:apiKeyCredentials": {
          "username": user,
          "apiKey": api_key
        }
      }
    }
    identity = requests.post(os.path.join(ident, 'tokens'), data=json.dumps(payload), headers=headers).json().get('access', {})
    headers['X-Auth-Token'] = identity['token']['id']
    catalog = identity['serviceCatalog']

    endpoint = _get_endpoint('object-store', region, catalog)

    containers = requests.get(endpoint, headers=headers).json()
    for c in containers:
        if c['name'] == container:
            log.debug('Container already exists!: {1}'.format(region, container))
            fire_event('enablecontainer', {'name': container}, 'salt/{0}/enablecontainer'.format(container))
            return None

    ret = requests.put('{0}/{1}'.format(endpoint, container), headers=headers)
    if ret.status_code == 201:
        fire_event('enablecontainer', {'name': container}, 'salt/{0}/enablecontainer'.format(container))
        return True
    else:
        log.debug('Failed to create container: {0}'.format(container))
        return False
Пример #44
0
    def setup_config(self):
        opts = config.master_config(self.get_config_file_path())
        user = opts.get('user', 'root')
        opts['_minion_conf_file'] = opts['conf_file']
        opts.update(config.minion_config(self.get_config_file_path('minion')))
        # Override the user from the master config file
        opts['user'] = user
        # Override the name of the PID file.
        opts['pidfile'] = '/var/run/salt-syndic.pid'

        if not opts.get('syndic_master', None):
            self.error(
                'The syndic_master needs to be configured in the salt master '
                'config, EXITING!'
            )

        # Some of the opts need to be changed to match the needed opts
        # in the minion class.
        opts['master'] = opts.get('syndic_master', opts['master'])
        try:
            opts['master_ip'] = utils.dns_check(
                opts['master'],
                ipv6=opts['ipv6']
            )
        except exceptions.SaltSystemExit as exc:
            self.exit(
                status=exc.code,
                msg='{0}: {1}\n'.format(self.get_prog_name(), exc.message)
            )

        opts['master_uri'] = 'tcp://{0}:{1}'.format(
            opts['master_ip'], str(opts['master_port'])
        )
        opts['_master_conf_file'] = opts['conf_file']
        opts.pop('conf_file')
        return opts
Пример #45
0
 def setup_config(self):
     return config.master_config(self.get_config_file_path('master'))
Пример #46
0
 def setUp(self):
     self.master_config = master_config(self.get_config_file_path('master'))
     for entry in ('root_dir', 'cachedir'):
         if not os.path.isdir(self.master_config[entry]):
             os.makedirs(self.master_config[entry])
 def setUp(self):
     self.master_config = master_config(self.get_config_file_path('master'))
Пример #48
0
def _master_opts():
    cfg = os.environ.get('SALT_MINION_CONFIG', os.path.join(salt.syspaths.CONFIG_DIR, 'master'))
    opts = config.master_config(cfg)
    return opts
Пример #49
0
 def __init__(self):
     Base_Class.__init__(self)
     self.__opt = config.master_config('/etc/salt/master')
     self.__key = key.get_key(self.__opt)
     self.__Client = salt.client.LocalClient()
     self.timeout = 60
Пример #50
0
 def __init__(self, settings):
     super(EventListener, self).__init__()
     salt_conf = settings.get('salt.master_config', '/etc/salt/master')
     salt_opts = salt_config.master_config(salt_conf)
     self.daemon = True
     self.event = MasterEvent(salt_opts['sock_dir'])
Пример #51
0
 def setup_config(self):
     return config.master_config(self.get_config_file_path('master'))
Пример #52
0
 def __init__(self):
     self.options = DummyOptions()
     keys_config = master_config(self.get_config_file_path('master'))
     Key.__init__(self, keys_config)
Пример #53
0
 def _accepted_ids(self):
     key = salt.key.Key(config.master_config('/etc/salt/master'))
     minion_ids = key.list_keys()['minions']
     ret = set(minion_ids)
     log.debug('Minion IDs in Salt key accepted: %s', ret)
     return ret
Пример #54
0
 def __init__(self):
     self.opts = config.master_config('/etc/salt/master')
     self.key = key.get_key(self.opts)
     self.all_keys = self.key.list_keys()
Пример #55
0
def _master_opts(cfg='master'):
    cfg = os.environ.get(
        'SALT_MASTER_CONFIG',
        __opts__.get('conf_file', os.path.join(__opts__['config_dir'], cfg)))
    opts = config.master_config(cfg)
    return opts
Пример #56
0
 def __init__(self):
     self.options = DummyOptions()
     keys_config = master_config(self.get_config_file_path('master'))
     Key.__init__(self, keys_config)
Пример #57
0
 def _accepted_ids(self):
     key = salt.key.Key(config.master_config('/etc/salt/master'))
     minion_ids = key.list_keys()['minions']
     ret = set(minion_ids)
     log.debug('Minion IDs in Salt key accepted: %s', ret)
     return ret