<reponame>alekseyrybalkin/monorepo
import argparse

import mr.config
import mr.shell as shell
import mr.util.hostconf


class Mercenary:
    def __init__(self):
        self.args = self.parse_args()
        self.config = mr.config.Config('merc').read()

    def parse_args(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('preset', type=str)
        return parser.parse_args()

    def main(self):
        preset = self.config['presets'][self.args.preset]
        user = preset['user']
        app = preset['app']
        foreground = preset.get('foreground', False)
        local_for = preset.get('local_for', [])
        kernel = mr.util.hostconf.HostConf().get_option('kernel')

        env = ' '.join(f'{k}={v.format(user=user)}' for k, v in self.config['env'].items())
        keep_env = '--preserve-env=' + ','.join(self.config['preserve_env'])

        if kernel in local_for:
            if foreground:
                command = f'{env} sudo {keep_env} -u {user} {app}'
            else:
                command = f'{env} sudo {keep_env} -b -u {user} {app} >/dev/null 2>&1'
        else:
            if foreground:
                command = f'{env} sudo {keep_env} chroot-enter sudo {keep_env} -u {user} {app}'
            else:
                command = f'{env} sudo {keep_env} -b chroot-enter sudo {keep_env} -u {user} {app} >/dev/null 2>&1'

        shell.run(command, shell=True)


def main():
    Mercenary().main()


if __name__ == '__main__':
    main()

import argparse

import mr.config
import mr.shell as shell
import mr.util.hostconf


class Mercenary:
    def __init__(self):
        self.args = self.parse_args()
        self.config = mr.config.Config('merc').read()

    def parse_args(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('preset', type=str)
        return parser.parse_args()

    def main(self):
        preset = self.config['presets'][self.args.preset]
        user = preset['user']
        app = preset['app']
        foreground = preset.get('foreground', False)
        local_for = preset.get('local_for', [])
        kernel = mr.util.hostconf.HostConf().get_option('kernel')

        env = ' '.join(f'{k}={v.format(user=user)}' for k, v in self.config['env'].items())
        keep_env = '--preserve-env=' + ','.join(self.config['preserve_env'])

        if kernel in local_for:
            if foreground:
                command = f'{env} sudo {keep_env} -u {user} {app}'
            else:
                command = f'{env} sudo {keep_env} -b -u {user} {app} >/dev/null 2>&1'
        else:
            if foreground:
                command = f'{env} sudo {keep_env} chroot-enter sudo {keep_env} -u {user} {app}'
            else:
                command = f'{env} sudo {keep_env} -b chroot-enter sudo {keep_env} -u {user} {app} >/dev/null 2>&1'

        shell.run(command, shell=True)


def main():
    Mercenary().main()


if __name__ == '__main__':
    main()