def _autoconf(env): env.log('autoconf') tmpfh = mktmp(prefix=__name__) tmpfh.write('exit 128') tmpfh.close() tmpfn = tmpfh.name() env.debug("tmpfn %s" % tmpfn) try: call("munin-node-configure --shell >%s 2>&1" % tmpfn) callCheck("/bin/sh -eu %s" % tmpfn) finally: tmpfh.unlink()
def _verify(envfn): rc = call("gpg --no-tty --no --verify %s.asc %s 2>/dev/null" % (envfn, envfn)) if rc == 0: log.msg("%s: OK" % envfn) else: log.error('env signature verify failed!') return rc
def _addUser(env, user, uid): cmd = ['useradd', '-m', '-U', '-u', str(uid)] fullname = env.settings.get("os.user.%s" % user, 'fullname', fallback='').strip() if fullname != '': cmd.append('-c') cmd.append("'%s'" % fullname) shell = env.settings.get("os.user.%s" % user, 'shell', fallback='/bin/bash').strip() cmd.append('-s') cmd.append(shell) cmd.append(user) call(cmd) env.log("%d %s user created" % (uid, user))
def _sumode(env, step): sumode = '-'.join(['--sumode', step]) log.debug("call sumode %s" % sumode) sudo = env.profile.config.get('deploy', 'sudo.command') cmd = sudo.strip().split() cmd.extend(flags.cmdline.split()) cmd.append(sumode) log.debug("sumode cmd %s" % ' '.join(cmd)) return call(cmd)
def _cmd(service, action, *args, check=True): cmd = [] cmd.extend(_ctlcmd) cmd.extend(args) cmd.append(action) cmd.append(service) if check: return callCheck(cmd) return call(' '.join(cmd))
def _check(env, opt, diff): l = [] act = None if opt == 'remove' or opt.endswith('.remove'): act = 'remove' l = env.settings.getlist('os.pkg', opt) elif opt == 'install' or opt.endswith('.install'): act = 'install' l = env.settings.getlist('os.pkg', opt) elif opt == 'prune' or opt.endswith('.prune'): act = 'prune' l = env.settings.getlist('os.pkg', opt) for pkg in l: if act == 'remove': rc = call("/usr/bin/dpkg-query -s %s >/dev/null 2>/dev/null" % pkg) if rc == 0: diff.append((opt, pkg)) elif rc > 1: raise env.error("os.pkg debian dpkg-query -s failed (rc:%d)" % rc) elif act == 'install': rc = call("/usr/bin/dpkg-query -s %s >/dev/null 2>/dev/null" % pkg) if rc == 1: diff.append((opt, pkg)) elif rc > 1: raise env.error("os.pkg debian dpkg-query -s failed (rc:%d)" % rc) elif act == 'prune': rc = call("/usr/bin/dpkg-query -s %s >/dev/null 2>/dev/null" % pkg) if rc == 0: diff.append((opt, pkg)) elif rc > 1: raise env.error("os.pkg debian dpkg-query -s failed (rc:%d)" % rc)
def dispatch(self, env=None): if env is None: env = environ.copy() try: return cmd.call(self._cmd, env=env, timeout=_TTL) except FileNotFoundError: raise PluginScriptNotFound(self._cmd[0]) except PermissionError: raise PluginScriptNoExec(self._cmd[0]) except TimeoutExpired: raise PluginScriptTimeout("%s after %s seconds" % (self._cmd[0], _TTL))
def _checkGroups(diff, env): if not env.settings.has_section('os.group'): return for group in env.settings['os.group']: gid = env.settings.getint('os.group', group) rc = call("getent group %s >%s" % (group, devnull)) if rc == 2: diff.append(('group', group, gid)) env.warn("%d %s not found" % (gid, group)) elif rc == 0: env.log("%d %s OK" % (gid, group)) else: raise env.error("getent group command failed: %d" % rc)
def loadenv(filename): envfn = path.abspath(filename) log.msg("%s: load" % envfn) rc = _check(envfn) if rc != 0: return rc if path.isfile(envfn + '.asc'): rc = _verify(envfn) if rc != 0: return rc rc = call("sha256sum -c %s" % envfn) if rc != 0: log.error('env checksum failed!') return rc _importenv(envfn) return 0
def _addUserGroup(env, user, group): call(['usermod', '-a', '-G', group, user]) env.log("%s user added to %s group" % (user, group))
def _addGroup(env, group, gid): call(['groupadd', '-g', str(gid), group]) env.log("%d %s group created" % (gid, group))