def test_roster_defaults_flat(self): """ Test Roster Defaults on the flat roster """ tempdir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP) expected = { "self": {"host": "0.0.0.0", "user": "******", "port": 42}, "localhost": {"host": "127.0.0.1", "user": "******", "port": 2827}, } 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( """ roster_defaults: user: daniel """ ) opts = salt.config.master_config(fpath) with patch( "salt.roster.get_roster_file", MagicMock(return_value=self.roster) ): with patch( "salt.template.compile_template", MagicMock(return_value=salt.utils.yaml.safe_load(self.roster)), ): roster = salt.roster.Roster(opts=opts) self.assertEqual(roster.targets("*", "glob"), expected) finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def check_minions(self, expr, tgt_type="glob", delimiter=DEFAULT_TARGET_DELIM, greedy=True): """ Check the passed regex against the available minions' public keys stored for authentication. This should return a set of ids which match the regex, this will then be used to parse the returns to make sure everyone has checked back in. """ try: if expr is None: expr = "" check_func = getattr(self, "_check_{0}_minions".format(tgt_type), None) if tgt_type in ( "grain", "grain_pcre", "pillar", "pillar_pcre", "pillar_exact", "compound", "compound_pillar_exact", ): # pylint: disable=not-callable _res = check_func(expr, delimiter, greedy) # pylint: enable=not-callable else: _res = check_func(expr, greedy) # pylint: disable=not-callable _res["ssh_minions"] = False if self.opts.get("enable_ssh_minions", False) is True and isinstance( "tgt", six.string_types): roster = salt.roster.Roster(self.opts, self.opts.get("roster", "flat")) ssh_minions = roster.targets(expr, tgt_type) if ssh_minions: _res["minions"].extend(ssh_minions) _res["ssh_minions"] = True except Exception: # pylint: disable=broad-except log.exception( "Failed matching available minions with %s pattern: %s", tgt_type, expr) _res = {"minions": [], "missing": []} return _res
def check_minions(self, expr, tgt_type='glob', delimiter=DEFAULT_TARGET_DELIM, greedy=True): ''' Check the passed regex against the available minions' public keys stored for authentication. This should return a set of ids which match the regex, this will then be used to parse the returns to make sure everyone has checked back in. ''' try: if expr is None: expr = '' check_func = getattr(self, '_check_{0}_minions'.format(tgt_type), None) if tgt_type in ('grain', 'grain_pcre', 'pillar', 'pillar_pcre', 'pillar_exact', 'compound', 'compound_pillar_exact'): _res = check_func(expr, delimiter, greedy) else: _res = check_func(expr, greedy) _res['ssh_minions'] = False if self.opts.get('enable_ssh_minions', False) is True and isinstance('tgt', six.string_types): roster = salt.roster.Roster(self.opts, self.opts.get('roster', 'flat')) ssh_minions = roster.targets(expr, tgt_type) if ssh_minions: _res['minions'].extend(ssh_minions) _res['ssh_minions'] = True except Exception: log.exception( 'Failed matching available minions with %s pattern: %s', tgt_type, expr) _res = {'minions': [], 'missing': []} return _res
def test_roster_defaults_flat(self): ''' Test Roster Defaults on the flat roster ''' tempdir = tempfile.mkdtemp(dir=TMP) expected = { 'self': { 'host': '0.0.0.0', 'user': '******', 'port': 42, }, 'localhost': { 'host': '127.0.0.1', 'user': '******', 'port': 2827, }, } 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(''' roster_defaults: user: daniel ''') opts = salt.config.master_config(fpath) with patch('salt.roster.get_roster_file', MagicMock(return_value=ROSTER)): with patch( 'salt.template.compile_template', MagicMock( return_value=salt.utils.yaml.safe_load(ROSTER))): roster = salt.roster.Roster(opts=opts) self.assertEqual(roster.targets('*', 'glob'), expected) finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)