Beispiel #1
0
def main(args):

    import_data = yaml.safe_load(args.input)
    groups = {k['group']['name']:k['group'] for k in import_data if 'group' in k}
    programs = {k['program']['name']:k['program'] for k in import_data if 'program' in k}

    for group in groups.values():
        group_dir = path.join(config.dirs.user_data_dir, 'groups')
        if not path.isdir(group_dir): os.makedirs(group_dir)

        group_path = path.join(group_dir, '%s.yaml' % group['name'])
        log.info("Writing group %s to %s" % (group['name'], group_path))
        with open(group_path, 'w') as gf:
            yaml.safe_dump(group, gf, default_flow_style=False)

    for defn in programs.values():
        if 'name' not in defn:
            raise errors.ChalmersError("Import definition requires a name field")

        prog = Program(defn['name'])
        if prog.exists():
            log.warn("Program '%s' already exists, not importing" % defn['name'])
            continue

        prog.raw_data.update(defn)
        prog.mk_data()
Beispiel #2
0
def main(args):

    prog = Program(args.name)

    if not prog.exists():
        raise errors.ProgramNotFound("program '{}' not found".format(args.name))

    if args.definition:
        print(prog.raw_data.filename)
        return
    if args.state:
        print(prog.state.filename)
        return
    if args.command:
        print(' '.join(prog.raw_data['command']))
        return

    print("Definition file:\n\t%s" % prog.raw_data.filename)
    print("State file:\n\t%s" % prog.state.filename)
    print('State')
    for key, value in prog.state.items():
        print("%12s: %s" % (key, value))

    if args.raw:
        data = prog.raw_data.copy()
    else:
        data = prog.data.copy()

    for category, opts in Program.OPTIONS:
        print_opts(category, data, opts)

    print_opts('Other Options', data, data.keys())
Beispiel #3
0
def select_programs(args, filter_paused=True, force=False):
    """
    Return a list of selected programs from command line arguments
    """

    if not (args.all ^ bool(args.names)):
        if args.all:
            log.error(
                "You may not specify a program name when you use the -a/--all option (See -h/--help for more details)"
            )
        else:
            log.error(
                "You must select at least one program from the command line (See -h/--help for more details)"
            )
        raise SystemExit(1)

    if args.all:
        programs = list(Program.find_for_user(force=force))
        if filter_paused:
            programs = [prog for prog in programs if not prog.is_paused]

    else:
        programs = [Program(name, force=force) for name in args.names]

    erorrs = 0
    for program in programs:
        if not program.exists():
            erorrs += 1
            log.error("Program '%s' does not exist" % program.name)
    if erorrs:
        raise SystemExit(1)

    return list(programs)
Beispiel #4
0
def main(args):

    prog = Program(args.name)

    if not prog.is_running:
        raise errors.StateError("Program is not running")

    prog.send('bg')
    print("Process is now a background process")
Beispiel #5
0
def main(args):

    prog = Program(args.name)

    if not prog.is_running:
        raise errors.StateError("Program is not running")

    prog.send('bg')
    print("Process is now a background process")
Beispiel #6
0
def start_program(name):
    program = Program(name)
    # Remove base logging handlers, this is read from the log files that are
    # set up after the start method
    logger = logging.getLogger('chalmers')
    logger.handlers = []
    logger.handlers.append(logging.NullHandler())
    try:
        program.start(daemon=False)
    except KeyboardInterrupt:
        log.error('Program %s is shutting down' % program.name)
Beispiel #7
0
def main(args):
    if args.name:
        prog = Program(args.name)

        logfile = prog.data.get(args.logfile)
    else:
        logfile = config.main_logfile()
        prog = None

    if prog and not prog.exists():
        raise errors.ProgramNotFound("program '{}' not found".format(
            args.name))

    if args.showfile:
        print(logfile)
        return
    if args.rm:
        if os.path.isfile(logfile):
            log.info("Removing log file '%s'" % logfile)
            os.unlink(logfile)
        else:
            log.warn("File '%s' does not exist" % logfile)
        return

    if logfile is None:
        log.warn("The logfile for key '%s' does not exist" % args.logfile)
        return

    with open(logfile) as fd:
        if args.n:
            lines = fd.readlines()
            if args.n > 0:
                lines = lines[:args.n]
            else:
                lines = lines[args.n:]

            sys.stdout.write(''.join(lines))

        else:
            sys.stdout.write(fd.read())

        while args.append and (not prog or prog and prog.is_running):
            pos = fd.tell()
            if pos < os.fstat(fd.fileno()).st_size:
                fd.seek(pos)
                sys.stdout.write(fd.read())
            else:
                time.sleep(1)
Beispiel #8
0
def main(args):

    if args.cmd and args.command:
        raise errors.ChalmersError('Unknow arguments %r' % args.command)
    elif not (args.cmd or args.command):
        raise errors.ChalmersError('Must specify a command to add')
    if args.cmd:
        args.command = args.cmd

    if not args.name:
        args.name = args.command[0]

    env = {}
    for env_var in args.save_env:
        if env_var in os.environ:
            env[env_var] = os.environ[env_var]
        else:
            log.warn("Environment variable %s does not exist (from -e/--save-env)" % env_var)

    program = Program.add(
        args.name, args.command,
        paused=args.paused, cwd=args.cwd,
        stdout=args.stdout, stderr=args.stderr,
        daemon_log=args.daemon_log, redirect_stderr=args.redirect_stderr,
        env=env
    )

    log.info('Added program {args.name}'.format(args=args))

    if args.run_now:
        log.info('Running program {args.name}'.format(args=args))
        program.start(daemon=not args.wait)
Beispiel #9
0
def main():

    parser = ArgumentParser(description=__doc__)
    parser.add_argument('--root', help='chalmers root config directory')
    parser.add_argument('name', help='name of program to run')
    args = parser.parse_args()

    if args.root:
        config.set_relative_dirs(args.root)


    logfile = config.main_logfile()
    setup_logging(logger, logging.INFO, use_color='never', logfile=logfile, show_tb=True)
    cli_logger.error("Starting program: %s" % args.name)
    prog = Program(args.name)
    prog.start_sync()
Beispiel #10
0
 def start_all(self):
     for prog in Program.find_for_user():
         if not prog.is_paused:
             self.dispatch_start(prog.name)
         else:
             log.info("Not starting program %s (it is paused)" %
                      (prog.name))
Beispiel #11
0
def select_programs(args, filter_paused=True, force=False):
    """
    Return a list of selected programs from command line arguments
    """

    if not (args.all ^ bool(args.names)):
        if args.all:
            log.error("You may not specify a program name when you use the -a/--all option (See -h/--help for more details)")
        else:
            log.error("You must select at least one program from the command line (See -h/--help for more details)")
        raise  SystemExit(1)

    if args.all:
        programs = list(Program.find_for_user(force=force))
        if filter_paused:
            programs = [prog for prog in programs if not prog.is_paused]

    else:
        programs = [Program(name, force=force) for name in args.names]

    erorrs = 0
    for program in programs:
        if not program.exists():
            erorrs += 1
            log.error("Program '%s' does not exist" % program.name)
    if erorrs:
        raise SystemExit(1)

    return list(programs)
Beispiel #12
0
def main(args):

    config.set_relative_dirs(path.abspath('.chalmers'))
    if not os.path.isfile(args.procfile):
        raise errors.ChalmersError("procfile '{}' does not exist".format(
            args.procfile))

    with open(args.procfile) as fd:
        procs = yaml.load(fd)
    print('procs', procs)
    programs = []
    for name, command in procs.items():
        definition = {
            'name': name,
            'command': split(command),
        }
        program = Program(name)
        pprint(definition)
        program.raw_data.update(definition)
        programs.append(program)

    pool = MultiPlexIOPool(stream=True, use_color=args.color)

    for prog in programs:
        pool.append(prog)

    pool.join()
Beispiel #13
0
    def start(self):

        programs = list(Program.find_for_user())
        programs = [p for p in programs if not p.is_paused]

        for prog in programs:
            if not prog.is_running:
                prog.start(True)
Beispiel #14
0
def main(args):

    export_data = []

    for prog in Program.find_for_user():
        export_data.append({'program': prog.raw_data})

    yaml.safe_dump(export_data, args.output, default_flow_style=False)
Beispiel #15
0
def main(args):

    proc = Program(args.name)

    if not proc.exists():
        raise errors.ProgramNotFound("program '{}' not found".format(args.name))

    if proc.is_running:
        log.warning("Program is running: Updates will not be reflected until a restart is done")
    with proc.raw_data.transaction():
        for key, value in args.items:
            if key == 'name':
                raise errors.ChalmersError("Can not set program name")

            set_nested_key(proc.raw_data, key, value)
            print("Set '%s' to %r for program %s" % (key, value, args.name))

    print("done")
Beispiel #16
0
def restart_main(args):

    if args.all:
        programs = Program.find_for_user()
    else:
        programs = [Program(name) for name in args.names]

    for prog in programs:
        prog.restart()
Beispiel #17
0
def main(args):

    proc = Program(args.name)

    if proc.is_running:
        log.warning("Program is running: Updates will not be reflected until a restart is done")

    for key, value in args.items:
        if key == 'name':
            raise errors.ChalmersError("Can not set program name")

        set_nested_key(proc.raw_data, key, value)
        log.info("Set '%s' to %r for program %s" % (key, value, args.name))

    proc.mk_data()
    proc.save()

    log.info("done")
Beispiel #18
0
    def stop(self):

        self.log("Stop Called")
        for prog in Program.find_for_user():
            if prog.is_running:
                self.log("Stopping program %s" % prog.name)
                prog.stop()

        self.log("Sending chalmers manager exit signal")
        send_action(ProgramManager.NAME, "exitloop")
Beispiel #19
0
def main():

    parser = ArgumentParser(description=__doc__)
    parser.add_argument('--root', help='chalmers root config directory')
    parser.add_argument('name', help='name of program to run')
    args = parser.parse_args()

    if args.root:
        config.set_relative_dirs(args.root)

    logfile = config.main_logfile()
    setup_logging(logger,
                  logging.INFO,
                  use_color='never',
                  logfile=logfile,
                  show_tb=True)
    cli_logger.error("Starting program: %s" % args.name)
    prog = Program(args.name)
    prog.start_sync()
Beispiel #20
0
def main(args):

    if args.cmd and args.command:
        raise errors.ChalmersError('Unknow arguments %r' % args.command)
    if args.cmd:
        args.command = args.cmd

    program_dir = path.join(dirs.user_data_dir, 'programs')

    if not args.name:
        args.name = args.command[0]

    if not path.isdir(program_dir):
        os.makedirs(program_dir)

    program_defn = path.join(program_dir, '{args.name}.yaml'.format(args=args))
    if path.isfile(program_defn):
        raise errors.ChalmersError("Program with name '{args.name}' already exists.  \n"
                                   "Use the -n/--name option to change the name or \n"
                                   "Run 'chalmers remove {args.name}' to remove it \n"
                                   "or 'chalmers set' to update the parameters".format(args=args))

    definition = {
                    'name': args.name,
                    'command': args.command,
                    'cwd': args.cwd,
    }

    if args.stdout:
        definition['stdout'] = args.stdout

    if args.daemon_log:
        definition['daemon_log'] = args.daemon_log

    if args.redirect_stderr is not None:
        definition['redirect_stderr'] = args.redirect_stderr

    if args.stderr is not None:
        definition['stderr'] = args.stderr

    state = {'paused': args.paused}

    prog = Program.create(args.name, definition, state)
    prog.save_state()

    if not args.paused:
        log.info('Starting program {args.name}'.format(args=args))
        prog.start(daemon=args.daemon)

    if args.daemon:
        prog.save()
        log.info('Added program {args.name}'.format(args=args))
    else:
        log.info('Program {args.name} exited'.format(args=args))
Beispiel #21
0
def unpause_main(args):

    if args.all:
        programs = Program.find_for_user()
    else:
        programs = [Program(name) for name in args.names]

    for prog in programs:
        log.info("Unpausing program %s" % (prog.name))
        prog.update_state(paused=False)
        if not prog.is_running:
            log.warn("%s is not running and will start on system reboot" % (prog.name))
    def test_manager(self):
        prog = Program.add('name', ['echo', 'name'])

        pm = program_manager.ProgramManager(setup_logging=False)
        pm.start_all()
        self.assertEqual(len(pm.processes), 1)

        time.sleep(1)
        prog.state.reload()

        self.assertEqual(prog.text_status, 'STOPPED')
        self.assertEqual(prog.state['reason'], 'Program exited gracefully')
    def test_logging(self, MockProgram):

        Program.add('name', ['echo', 'name'])

        log = logging.getLogger('chalmers')
        stream = io.StringIO()
        handler = logging.StreamHandler(stream)
        handler.setFormatter(logging.Formatter('%(msg)s'))
        log.addHandler(handler)

        def log_something():

            log.info('hello')

        MockProgram().start_sync.side_effect = log_something

        program_manager.start_program('name', setup_logging=True)

        MockProgram().start_sync.assert_called_once_with()

        self.assertEqual(stream.getvalue().strip(), '[name] hello')
Beispiel #24
0
def main(args):

    proc = Program(args.name)

    if not proc.exists():
        raise errors.ProgramNotFound("program '{}' not found".format(
            args.name))

    if proc.is_running:
        log.warning(
            "Program is running: Updates will not be reflected until a restart is done"
        )
    with proc.raw_data.transaction():
        for key, value in args.items:
            if key == 'name':
                raise errors.ChalmersError("Can not set program name")

            set_nested_key(proc.raw_data, key, value)
            print("Set '%s' to %r for program %s" % (key, value, args.name))

    print("done")
Beispiel #25
0
def main(args):

    EDITOR = os.environ.get('EDITOR', None)
    if EDITOR is None:
        raise errors.ChalmersError(
            "Environment variable 'EDITOR' needs to be set")

    prog = Program(args.name, force=True)

    if prog and not prog.exists():
        raise errors.ProgramNotFound("program '{}' not found".format(
            args.name))

    cmd = '%s %s' % (EDITOR, pipes.quote(prog.raw_data.filename))

    print(cmd)
    if subprocess.call(cmd, shell=True):
        raise errors.ChalmersError('Command "%s" exited with non zero status' %
                                   cmd)

    if prog.is_running:
        log.info("Changes to program %s will take effect on the next restart" %
                 prog.name)
Beispiel #26
0
def start_program(name, setup_logging=True):

    logger = logging.getLogger('chalmers')
    logger.setLevel(logging.INFO)

    prefix = '[%s] ' % name

    if setup_logging and not logger.handlers:
        shndlr = logging.StreamHandler()
        shndlr.setLevel(logging.INFO)
        logger.setLevel(logging.INFO)
        logger.addHandler(shndlr)

    if setup_logging:
        for h in logger.handlers:
            FormatterWrapper.wrap(prefix, h)

    sys.excepthook = log_unhandled_exception(logger)

    prog = Program(name)

    assert prog.exists()

    prog.start_sync()
def start_program(name, setup_logging=True):

    logger = logging.getLogger('chalmers')
    logger.setLevel(logging.INFO)

    prefix = '[%s] ' % name

    if setup_logging and not logger.handlers:
        shndlr = logging.StreamHandler()
        shndlr.setLevel(logging.INFO)
        logger.setLevel(logging.INFO)
        logger.addHandler(shndlr)

    if setup_logging:
        for h in logger.handlers:
            FormatterWrapper.wrap(prefix, h)

    sys.excepthook = log_unhandled_exception(logger)

    prog = Program(name)

    assert prog.exists()

    prog.start_sync()
Beispiel #28
0
def main(args):

    if args.all:
        programs = Program.find_for_user()
    else:
        programs = [Program(name) for name in args.names]

    for prog in programs:
        if prog.is_running:
            print("Stopping program %s ..." % prog.name, end=''); sys.stdout.flush()
            try:
                prog.stop()
            except errors.StateError as err:
                log.error(err.message)
            else:
                print("stopped ")
        else:
            print("Program %s is already stopped" % prog.name)
Beispiel #29
0
def main(args):

    if args.all:
        programs = list(Program.find_for_user())
    else:
        programs = [Program(name) for name in args.names]

    if args.daemon:
        for prog in programs:
            print("Starting program %s (daemon:%s) ... " % (prog.name, args.daemon), end=''); sys.stdout.flush()
            prog.start(args.daemon)
            print("started")
    else:
        mgr = ProgramManager(exit_on_first_failure=True, use_color=args.color)

        for prog in programs:
            mgr.dispatch_start(prog.name)


        for process in mgr.processes:
            process.join()
Beispiel #30
0
def list_loaded(args):

    programs = sorted(Program.find_for_user(), key=sort_key)

    for prog in programs:

        if prog.is_running:
            start_time = prog.state.get('start_time')
            if start_time:
                td = str(timedelta(seconds=(time.time() - start_time))).rsplit(
                    '.', 1)[0]
            else:
                td = '?'
            ctx = (
                prog.data['name'][:24],
                prog.text_status,
                prog.state.get('child_pid'),
                td,
            )

            print('%-25s %-15s pid %-6s, uptime %s' % ctx)
        else:
            stop_time = prog.state.get('stop_time')
            if stop_time:
                td = 'downtime ' + str(
                    timedelta(seconds=(time.time() - stop_time))).rsplit(
                        '.', 1)[0]
            else:
                td = ''

            reason = prog.state.get('reason', '')
            ctx = (prog.data['name'][:35], prog.text_status, reason)

            print('%-25s %-15s %s' % ctx)

            if td:
                print(' ' * 41, td)

    if not programs:
        print('No programs added')
Beispiel #31
0
def list_loaded(args):

    programs = sorted(Program.find_for_user(), key=sort_key)

    for prog in programs:

        if prog.is_running:
            start_time = prog.state.get('start_time')
            if start_time:
                td = str(timedelta(seconds=(time.time() - start_time))).rsplit('.', 1)[0]
            else:
                td = '?'
            ctx = (prog.data['name'][:24], prog.text_status,
                   prog.state.get('child_pid'),
                   td,
                   )

            print('%-25s %-15s pid %-6s, uptime %s' % ctx)
        else:
            stop_time = prog.state.get('stop_time')
            if stop_time:
                td = 'downtime ' + str(timedelta(seconds=(time.time() - stop_time))).rsplit('.', 1)[0]
            else:
                td = ''

            reason = prog.state.get('reason', '')
            ctx = (prog.data['name'][:35], prog.text_status,
                   reason

                  )

            print('%-25s %-15s %s' % ctx)

            if td:
                print(' ' * 41, td)

    if not programs:
        print('No programs added')
Beispiel #32
0
def main(args):

    programs = list(Program.find_for_user())

    for prog in programs:

        if prog.is_running:
            start_time = prog.state.get('start_time')
            td = str(timedelta(seconds=(time.time() - start_time))).rsplit('.', 1)[0]
            ctx = (prog.data['name'][:35], prog.text_status,
                   prog.state.get('child_pid'),
                   td,
                )

            print('%-35s %-15s pid %-6s, uptime %s' % ctx)
        else:
            ctx = (prog.data['name'][:35], prog.text_status,
                   prog.state.get('reason')
                  )

            print('%-35s %-15s %s' % ctx)

    if not programs:
        print('No programs added')
Beispiel #33
0
def main(args):

    if args.cmd and args.command:
        raise errors.ChalmersError('Unknow arguments %r' % args.command)
    elif not (args.cmd or args.command):
        raise errors.ChalmersError('Must specify a command to add')
    if args.cmd:
        args.command = args.cmd

    if not args.name:
        args.name = args.command[0]

    env = {}
    for env_var in args.save_env:
        if env_var in os.environ:
            env[env_var] = os.environ[env_var]
        else:
            log.warn(
                "Environment variable %s does not exist (from -e/--save-env)" %
                env_var)

    program = Program.add(args.name,
                          args.command,
                          paused=args.paused,
                          cwd=args.cwd,
                          stdout=args.stdout,
                          stderr=args.stderr,
                          daemon_log=args.daemon_log,
                          redirect_stderr=args.redirect_stderr,
                          env=env)

    log.info('Added program {args.name}'.format(args=args))

    if args.run_now:
        log.info('Running program {args.name}'.format(args=args))
        program.start(daemon=not args.wait)
Beispiel #34
0
def main(args):

    prog = Program(args.name)
    prog.delete()
    log.info("Program %s removed" % args.name)
 def start_all(self):
     for prog in Program.find_for_user():
         if not prog.is_paused:
             self.dispatch_start(prog.name)
         else:
             log.info("Not starting program %s (it is paused)" % (prog.name))