Esempio n. 1
0
def remove_progs():
    log.info('Removing programs')
    log.debug('Removing programs: {}'.format(dashboard.prog_to_remove))
    stop_programs(None, dashboard.prog_to_remove)
    for prog_name in dashboard.prog_to_remove:
        prog = dashboard.programs.pop(prog_name, None)
        for process in prog.processes:
            dashboard.name_procs.pop(process.name)
Esempio n. 2
0
def serve_relaod(cs, query_list):
    log.info('serving: reload: {0}'.format(query_list))
    if len(query_list) < 2:
        utils.socket_send(cs, 'err: config file is required')
        return
    config_server = tm_config.ConfigServer(query_list[1])
    if not config_server.valid:
        log.debug('invalid config has occurred')
        utils.socket_send(cs, 'warn: the config is invalid')
        return
    new_programs = mod_dash.load_programs(config_server.data)
    utils.thread_start(reload_launch_manager, (new_programs, ))
Esempio n. 3
0
def kill_process(process: Process, stopsig=signal.SIGKILL):
    log.debug('killing process {0}'.format(process.name))
    if process.state != ProcessState.STARTING \
            and process.state != ProcessState.BACKOFF \
            and process.state != ProcessState.RUNNING:
        return 1
    process.state = ProcessState.STOPPING
    try:
        os.kill(process.pid, stopsig)  # get signal from data
        log.debug('killing signal has been sent to {0}'.format(process.pid))
    except OSError:
        log.error('failed to kill process {0}:[{1}]'.format(
            process.name, process.pid))
Esempio n. 4
0
def serve_status(cs, query_list):
    log.info('serving: status: {0}'.format(query_list))
    if len(query_list) > 1:
        prog_names = query_list[1:]
    else:
        prog_names = list(dashboard.programs.keys())
        log.debug('something good')
    for prog_name in prog_names:
        utils.socket_send(cs, 'program status: {0}'.format(prog_name))
        program = dashboard.programs.get(prog_name)
        for process in program.processes:
            utils.socket_send(
                cs, '\t{0} status {1}'.format(process.name, process.state))
Esempio n. 5
0
def buff_manager():
    """\
    Buffer Manager is responsible for dispatching
    standard out/err flow of running processes by
    by selecting open fds which are ready to read from
    and transport the data to the appropriate files.
    """
    log.info('starting buff_manager')

    while 1:
        time.sleep(TIME_SLEEP)
        log.debug('checking fds')
        fds = list(dashboard.fds_buff.keys())
        if not len(fds):
            log.debug('empty fd list')
            continue
        rfds = []
        try:  # select open fds which are ready to read from
            rfds, wfds, xfds = select(fds, [], [])
        except OSError:
            log.error('error occurred upon select fds')
            continue
        log.info('found some fds: {0}'.format(rfds))
        for fd in rfds:  # transporting data from each open fd
            log.info('fd={0} ready to write in {1}'.format(
                fd, dashboard.fds_buff.get(fd)))
            data = None
            try:
                data = os.read(fd, BUFF_SIZE)
            except OSError as err:
                log.error('Failed to read from fd={0}'.format(fd))
            if not data:
                if fd in dashboard.fds_zombie:  # close and remove the fd if it's in zombie list
                    dashboard.fds_zombie.remove(fd)
                    os.close(fd)
                    dashboard.fds_buff.pop(fd)
                continue
            file = dashboard.fds_buff.get(
                fd)  # get the linked file from the dashboard
            if isinstance(file, pathlib.Path):
                if not file.exists():
                    file.touch(exist_ok=True)
                with file.open('a') as f:
                    f.write(data.decode(DECODE_FORMAT))
Esempio n. 6
0
 def authenticate(self) -> bool:
     if not self.config.username:
         self.config.username = input("Enter username: "******"Enter password: "******"Enter password: ")
     query = 'auth\r\n{0}\r\n{1}\r\n'\
         .format(self.config.username, self.config.password)
     self.csocket.send(query.encode('utf-8'))
     response = self.csocket.recv(1024).decode('utf-8')
     log.debug('received {0} from server'.format(response))
     response = response.rsplit('\r\n')
     log.debug('response list {0}'.format(response))
     if response[0] != 'OK':
         log.info('failed to authenticate with response: {0}'.format(
             response[0]))
         self.config.password = None
         return False
     return True
Esempio n. 7
0
def authenticate_client(cs, addr, configServer) -> (str, bool):
    # username = cs.recv(256).decode('utf-8')
    # password = cs.recv(256).decode('utf-8')
    auth_query = cs.recv(1024).decode('utf-8')
    log.debug('received auth query: {0}'.format(auth_query))
    auth_list = auth_query.rsplit('\r\n')
    log.debug('auth list: {0}'.format(auth_list))
    if len(auth_list) < 3 or auth_list[0] != 'auth':
        cs.send('KO-1'.encode('utf-8'))
        return None, False
    elif auth_list[1] not in configServer.clients.keys():
        cs.send('KO-2'.encode('utf-8'))
        return auth_list[1], False
    elif auth_list[2] != configServer.clients.get(auth_list[1]):
        cs.send('KO-3'.encode('utf-8'))
        return auth_list[1], False
    else:
        cs.send('OK'.encode('utf-8'))
        return auth_list[1], True
Esempio n. 8
0
def launch_program(program: Program, force_start=False):
    """
    Launches a program by launching all the processes created by this program
    if the start is forced by client or autostart is enabled then
    Otherwise, all processes will be moved to STOPPED state.
    :param program: Program object to be launched.
    :param force_start: True if client requesting a start.
    :return:
    """
    if not program or not isinstance(program, Program):
        return
    log.info('launching program {0}, umask={1}'.format(program.name,
                                                       program.umask))
    log.info('launching {0}'.format(program.cmd.split(' ')))
    for process in program.processes:
        log.debug('Starting new process !')
        # dashboard.name_procs[process.name] = process # move to higher level
        log.debug('AUTO_START -------- {} === {}'.format(
            program.autostart, configmap.Start.NO))
        if program.autostart is False:
            process.state = ProcessState.STOPPED
        else:
            launch_process(program, process)
Esempio n. 9
0
def serve_start(cs, query_list):
    log.info('serving: start: {0}'.format(query_list))
    # utils.socket_send(cs, 'something there from start')
    # utils.socket_send(cs, '\r')
    prog_names = query_list[1:]
    log.debug('prog_names: {0}'.format(prog_names))
    for prog_name in prog_names:
        if prog_name not in dashboard.programs.keys():
            utils.socket_send(cs, 'program {0} not found'.format(prog_name))
        else:
            utils.socket_send(cs, 'starting {0}'.format(prog_name))
            program = dashboard.programs.get(prog_name)
            changed = 0
            for process in program.processes:
                utils.socket_send(
                    cs, 'process {0} is in {1} state'.format(
                        process.name, process.state))
                if process.state != ProcessState.STARTING \
                        and process.state != ProcessState.BACKOFF \
                        and process.state != ProcessState.RUNNING:
                    # see if it needs to reset retries
                    utils.socket_send(
                        cs, 'starting process: {0}'.format(process.name))
                    launch_process(program, process)
Esempio n. 10
0
def clean_proccess(process: Process, state=None):
    if state:
        process.state = state
    process.dtime = time.time()
    dashboard.pid_procs.pop(process.pid)
    process.pid = -1
    log.debug('cleaning process fds: {0}'.format(process.fds))
    fds = process.fds[:]
    for fd in fds:
        try:
            log.debug('moving fd={0} to zombie list'.format(fd))
            process.fds.remove(fd)
            dashboard.fds_zombie.append(fd)
        except OSError as err:
            log.error('failed to close fd={0}'.format(fd))
    log.debug('fds_zombies: {0}'.format(dashboard.fds_zombie))