Example #1
0
def from_remote_to_launcher(command_id, client, paths, targetpath, bwlimit,
                            wrapper_timeout):
    """
    Recursive copy of a directory from a client to the launcher using scp.
    """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)

    if not LauncherConfig().is_scp_available:
        logging.getLogger().warn(
            "Can't do remote to launcher because scp is not available")
        return False

    real_command = [LauncherConfig().scp_path]
    real_command += client['transp_args']
    if bwlimit:
        real_command += ['-l'] + [str(bwlimit)]
    real_command += ['-r']
    for path in paths:
        real_command += ["%s@%s:%s" % (client['user'], client['host'], path)]
    real_command += [targetpath]
    # The following ssh options are not used by scp, so we remove them
    real_command.remove('-T')
    real_command.remove('-a')

    # Built "thru" command
    thru_command_list = [LauncherConfig().ssh_path]
    thru_command_list += client['transp_args']
    thru_command_list += ["%s@%s" % (client['user'], client['host'])]

    # Build final command line
    command_list = [
        LauncherConfig().wrapper_path, '--max-log-size',
        str(LauncherConfig().wrapper_max_log_size), '--max-exec-time',
        str(wrapper_timeout), '--exec',
        PULSE2_WRAPPER_ARG_SEPARATOR.join(real_command), '--thru',
        PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
        '--exec-server-side'
    ]

    # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
    if client['client_check']:
        command_list += [
            '--check-client-side', ','.join(
                map((lambda x: '='.join(x)), client['client_check'].items()))
        ]
    if client['server_check']:
        command_list += [
            '--check-server-side', ','.join(
                map((lambda x: '='.join(x)), client['server_check'].items()))
        ]
    if client['action']:
        command_list += ['--action', client['action']]

    result = pulse2.launcher.process_control.commandRunner(
        command_list, __cb_sync_process_end)
    if not result:
        logging.getLogger().warn("Remote to launcher failed for CoH #%d" %
                                 command_id)
    return result
Example #2
0
def remote_push(command_id, client, files_list, mode, wrapper_timeout):
    """ Handle remote copy (push) """
    target_path = os.path.join(LauncherConfig().target_path, pulse2.launcher.utils.getTempFolderName(command_id, client['uuid']))
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    if client['protocol'] == "rsyncssh":
        # command is issued though our wrapper, time to build it
        real_files_list = files_list

        if not LauncherConfig().is_rsync_available:
            logging.getLogger().warn('Can\'t do remote push because rsync is not available')
            return False
        # Build "exec" command
        real_command  = [LauncherConfig().rsync_path]
        real_command += client['proto_args']
        real_command += real_files_list
        real_command += [ '%s@%s:%s/' % (client['user'], client['host'], target_path)]

        # Build "thru" command
        thru_command_list  = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [ "%s@%s" % (client['user'], client['host'])]

        # Define environnement
        envir = {
                    'SSHPATH': LauncherConfig().ssh_path,
                    'SSHARGS': client['transp_args'],
                    'SSHTARGET' : "%s@%s" % (client['user'], client['host']),
                    'STEP': 'PUSH'
                }

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--exec',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(real_command),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec-server-side'
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += ['--check-client-side', ','.join(map((lambda x: '='.join(x)), client['client_check'].items()))]
        if client['server_check']:
            command_list += ['--check-server-side', ','.join(map((lambda x: '='.join(x)), client['server_check'].items()))]
        if client['action']:
            command_list += ['--action', client['action']]

        return get_command_result(command_id, command_list, mode, client['group'], envir, 'push', 'Remote push (rsyncssh/async)')    

    logging.getLogger().warn("Remote push failed for CoH #%d" % command_id)
    return None
Example #3
0
def remote_exec(command_id, client, command, mode, wrapper_timeout):
    """ Handle remote execution on target """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    target_path = os.path.join(LauncherConfig().target_path, pulse2.launcher.utils.getTempFolderName(command_id, client['uuid']))
    if client['protocol'] == "ssh":
        # command is issued though our wrapper, time to build it

        if not LauncherConfig().is_ssh_available:
            return False

        # Built "thru" command
        thru_command_list  = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [client['host']]

        # Build "exec" command
        real_command  = ['cd', target_path, ';', command]

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(real_command),
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += ['--check-client-side', ','.join(map((lambda x: '='.join(x)), client['client_check'].items()))]
        if client['server_check']:
            command_list += ['--check-server-side', ','.join(map((lambda x: '='.join(x)), client['server_check'].items()))]
        if client['action']:
            command_list += ['--action', client['action']]

        if mode == 'async':
            return pulse2.launcher.process_control.commandForker(
                command_list,
                __cb_async_process_end,
                command_id,
                LauncherConfig().defer_results,
                'completed_execution',
                LauncherConfig().max_command_age,
                client['group'],
                'exec'
            )
        elif mode == 'sync':
            return pulse2.launcher.process_control.commandRunner(
                command_list,
                __cb_sync_process_end
            )
    return None
Example #4
0
def from_remote_to_launcher(command_id, client, paths, targetpath, bwlimit, wrapper_timeout):
    """
    Recursive copy of a directory from a client to the launcher using scp.
    """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)

    if not LauncherConfig().is_scp_available:
        logging.getLogger().warn("Can't do remote to launcher because scp is not available")
        return False

    real_command = [LauncherConfig().scp_path]
    real_command += client['transp_args']
    if bwlimit:
        real_command += ['-l'] + [str(bwlimit)]
    real_command += ['-r']
    for path in paths:
        real_command += [ "%s@%s:%s" % (client['user'], client['host'], path)]
    real_command += [targetpath]
    # The following ssh options are not used by scp, so we remove them
    real_command.remove('-T')
    real_command.remove('-a')

    # Built "thru" command
    thru_command_list  = [LauncherConfig().ssh_path]
    thru_command_list += client['transp_args']
    thru_command_list += [ "%s@%s" % (client['user'], client['host'])]

    # Build final command line
    command_list = [
        LauncherConfig().wrapper_path,
        '--max-log-size',
        str(LauncherConfig().wrapper_max_log_size),
        '--max-exec-time',
        str(wrapper_timeout),
        '--exec',
        PULSE2_WRAPPER_ARG_SEPARATOR.join(real_command),
        '--thru',
        PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
        '--exec-server-side'
        ]

    # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
    if client['client_check']:
        command_list += ['--check-client-side', ','.join(map((lambda x: '='.join(x)), client['client_check'].items()))]
    if client['server_check']:
        command_list += ['--check-server-side', ','.join(map((lambda x: '='.join(x)), client['server_check'].items()))]
    if client['action']:
        command_list += ['--action', client['action']]

    result = pulse2.launcher.process_control.commandRunner(
        command_list,
        __cb_sync_process_end)
    if not result:
        logging.getLogger().warn("Remote to launcher failed for CoH #%d" % command_id)
    return result
Example #5
0
def remote_exec(command_id, client, command, mode, wrapper_timeout):
    """ Handle remote execution on target """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    target_path = os.path.join(LauncherConfig().target_path, pulse2.launcher.utils.getTempFolderName(command_id, client['uuid']))
    if client['protocol'] == "ssh":
        # command is issued though our wrapper, time to build it

        if not LauncherConfig().is_ssh_available:
            logging.getLogger().warn("Can't do remote exec because ssh is not available")
            return False

        # Built "thru" command
        thru_command_list  = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [client['host']]

        # Define environnement
        envir = {
                    'SSHPATH': LauncherConfig().ssh_path,
                    'SSHARGS': client['transp_args'],
                    'SSHTARGET' : client['host'],
                    'STEP': 'EXEC'
                }


        # Build "exec" command
        real_command  = ['cd', target_path, ';', command]

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(real_command),
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += ['--check-client-side', ','.join(map((lambda x: '='.join(x)), client['client_check'].items()))]
        if client['server_check']:
            command_list += ['--check-server-side', ','.join(map((lambda x: '='.join(x)), client['server_check'].items()))]
        if client['action']:
            command_list += ['--action', client['action']]

        return get_command_result(command_id, command_list, mode, client['group'], envir, 'exec', 'Remote exec (ssh/async)')    
    logging.getLogger().warn("Remote exec failed for CoH #%d" % command_id) 
    return None
Example #6
0
def remote_direct(command_id, client, command, mode, max_log_size, wrapper_timeout):
    """ Handle remote direct stuff on target """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    if client['protocol'] == "ssh":
        # command is issued though our wrapper, time to build it

        if not LauncherConfig().is_ssh_available:
            logging.getLogger().warn("Can't do remote direct because ssh is not available")
            return False

        # Built "thru" command
        thru_command_list  = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [client['host']]

        # Define environnement
        envir = {
                    'SSHPATH': LauncherConfig().ssh_path,
                    'SSHARGS': client['transp_args'],
                    'SSHTARGET' : client['host'],
                    'STEP': 'REMOTEDIRECT'
                }


        # Build "exec" command
        real_command = command

        if max_log_size == None:
            # If no max_log_size set, use the value from the configuration file
            max_log_size = LauncherConfig().wrapper_max_log_size
        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            real_command, # we do not use the PULSE2_WRAPPER_ARG_SEPARATOR here, as the command is send "as is"
            '--no-wrap',
            '--only-stdout',
            '--remove-empty-lines'
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += ['--check-client-side', ','.join(map((lambda x: '='.join(x)), client['client_check'].items()))]
        if client['server_check']:
            command_list += ['--check-server-side', ','.join(map((lambda x: '='.join(x)), client['server_check'].items()))]
        if client['action']:
            command_list += ['--action', client['action']]

        return get_command_result(command_id, command_list, mode, client['group'], envir, 'direct', 'Remote direct (ssh/async)')    

    logging.getLogger().warn("Remote direct failed for CoH #%d" % command_id)
    return None
Example #7
0
def remote_pull(command_id, client, files_list, mode, wrapper_timeout):
    """ Handle remote copy (pull) on target """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    target_path = os.path.join(LauncherConfig().target_path, pulse2.launcher.utils.getTempFolderName(command_id, client['uuid']))
    if client['protocol'] == "wget":
        # command is issued though our wrapper, time to build it

        if not LauncherConfig().is_ssh_available:
            logging.getLogger().warn("Can't do remote pull because ssh is not available")
            return False
        # Built "thru" command
        thru_command_list  = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [ "%s@%s" % (client['user'], client['host'])]

        # Define environnement
        envir = {
                    'SSHPATH': LauncherConfig().ssh_path,
                    'SSHARGS': client['transp_args'],
                    'SSHTARGET' : "%s@%s" % (client['user'], client['host']),
                    'STEP': 'PUSHPULL'
                }

        # Build "exec" command
        real_command  = [LauncherConfig().wget_path]
        real_command += client['proto_args']
        real_command += ['-N']
        real_command += files_list
        real_command += ['-P']
        real_command += [target_path]
        # Make downloaded files executable
        real_command += ['&&']
        real_command += ['chmod']
        real_command += ['u+x']
        real_command += ['-R']
        real_command += [target_path]
        # Check files integrity with MD5SUMS file if available AND size > 0
        real_command += ['&&']
        real_command += ['cd', target_path]
        real_command += ['&&']
        real_command += ['if', '[', '-s', 'MD5SUMS', ']', ';']  # '-s' : size > 0
        real_command += ['then']
        real_command += ['md5sum', '-c', 'MD5SUMS', ';']
        real_command += ['fi']

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(real_command),
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += ['--check-client-side', ','.join(map((lambda x: '='.join(x)), client['client_check'].items()))]
        if client['server_check']:
            command_list += ['--check-server-side', ','.join(map((lambda x: '='.join(x)), client['server_check'].items()))]
        if client['action']:
            command_list += ['--action', client['action']]

        if mode == 'async':
            result = pulse2.launcher.process_control.commandForker(
                command_list,
                __cb_async_process_end,
                command_id,
                LauncherConfig().defer_results,
                'completed_pull',
                LauncherConfig().max_command_age,
                client['group'],
                'pull',
                env_=envir
            )
            if not result :
                logging.getLogger().warn("Remote pull (wget/async) failed for CoH #%d" % command_id)
            return result
        elif mode == 'sync':
            result = pulse2.launcher.process_control.commandRunner(
                command_list,
                __cb_sync_process_end,
                env_=envir
            )
            if not result :
                logging.getLogger().warn("Remote pull (wget/sync) failed for CoH #%d" % command_id)
            return result
    elif client['protocol'] == "rsyncproxy":
        # Built "thru" command
        thru_command_list  = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [ "%s@%s" % (client['user'], client['host'])]

        # Define environnement
        envir = {
                    'SSHPATH': LauncherConfig().ssh_path,
                    'SSHARGS': client['transp_args'],
                    'SSHTARGET' : "%s@%s" % (client['user'], client['host']),
                    'STEP': 'PUSHPULL'
                }

        # Build "exec" command
        src_path = pulse2.launcher.utils.getTempFolderName(client['proxy']['command_id'], client['proxy']['uuid'])

        real_command  = ['rsync']
        real_command += client['proto_args']

        real_command += ['%s@%s:"%s"' % (client['user'], client['proxy']['host'], ' '.join(map(lambda x: "%s/%s/'%s'" % (LauncherConfig().target_path, src_path, x), files_list)))]
        real_command += [target_path]

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(real_command),
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += ['--check-client-side', ','.join(map((lambda x: '='.join(x)), client['client_check'].items()))]
        if client['server_check']:
            command_list += ['--check-server-side', ','.join(map((lambda x: '='.join(x)), client['server_check'].items()))]
        if client['action']:
            command_list += ['--action', client['action']]

        if mode == 'async':
            result = pulse2.launcher.process_control.commandForker(
                command_list,
                __cb_async_process_end,
                command_id,
                LauncherConfig().defer_results,
                'completed_pull',
                LauncherConfig().max_command_age,
                client['group'],
                'pull',
                env_=envir
            )
            if not result :
                logging.getLogger().warn("Remote pull (rsyncproxy/async) failed for CoH #%d" % command_id)
            return result
        elif mode == 'sync':
            result = pulse2.launcher.process_control.commandRunner(
                command_list,
                __cb_sync_process_end,
                env_=envir
            )
            if not result :
                logging.getLogger().warn("Remote pull (rsyncproxy/sync) failed for CoH #%d" % command_id)
            return result

    logging.getLogger().warn("Remote pull failed for CoH #%d" % command_id)
    return None
Example #8
0
def remote_halt(command_id, client, mode, wrapper_timeout):
    """ Handle remote halt on target
    """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    halt_command = LauncherConfig().halt_command
    if client['protocol'] == "ssh":
        # command is issued though our wrapper, time to build it

        if not LauncherConfig().is_ssh_available:
            logging.getLogger().warn(
                "Can't do remote halt because ssh is not available")
            return False

        # Built "thru" command
        thru_command_list = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [client['host']]

        # Define environnement
        envir = {
            'SSHPATH': LauncherConfig().ssh_path,
            'SSHARGS': client['transp_args'],
            'SSHTARGET': client['host'],
            'STEP': 'HALT'
        }

        # Build "exec" command
        real_command = halt_command

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            real_command,  # we do not use the PULSE2_WRAPPER_ARG_SEPARATOR here, as the command is send "as is"
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += [
                '--check-client-side', ','.join(
                    map((lambda x: '='.join(x)),
                        client['client_check'].items()))
            ]
        if client['server_check']:
            command_list += [
                '--check-server-side', ','.join(
                    map((lambda x: '='.join(x)),
                        client['server_check'].items()))
            ]
        if client['action']:
            command_list += ['--action', client['action']]

        return get_command_result(command_id, command_list, mode,
                                  client['group'], envir, 'halt',
                                  'Remote halt (ssh/async)')
    logging.getLogger().warn("Remote halt failed for CoH #%d" % command_id)
    return None
Example #9
0
def remote_push(command_id, client, files_list, mode, wrapper_timeout):
    """ Handle remote copy (push) """
    target_path = os.path.join(
        LauncherConfig().target_path,
        pulse2.launcher.utils.getTempFolderName(command_id, client['uuid']))
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    if client['protocol'] == "rsyncssh":
        # command is issued though our wrapper, time to build it
        real_files_list = files_list

        if not LauncherConfig().is_rsync_available:
            logging.getLogger().warn(
                'Can\'t do remote push because rsync is not available')
            return False
        # Build "exec" command
        real_command = [LauncherConfig().rsync_path]
        real_command += client['proto_args']
        real_command += real_files_list
        real_command += [
            '%s@%s:%s/' % (client['user'], client['host'], target_path)
        ]

        # Build "thru" command
        thru_command_list = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += ["%s@%s" % (client['user'], client['host'])]

        # Define environnement
        envir = {
            'SSHPATH': LauncherConfig().ssh_path,
            'SSHARGS': client['transp_args'],
            'SSHTARGET': "%s@%s" % (client['user'], client['host']),
            'STEP': 'PUSH'
        }

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path, '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size), '--max-exec-time',
            str(wrapper_timeout), '--exec',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(real_command), '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec-server-side'
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += [
                '--check-client-side', ','.join(
                    map((lambda x: '='.join(x)),
                        client['client_check'].items()))
            ]
        if client['server_check']:
            command_list += [
                '--check-server-side', ','.join(
                    map((lambda x: '='.join(x)),
                        client['server_check'].items()))
            ]
        if client['action']:
            command_list += ['--action', client['action']]

        return get_command_result(command_id, command_list, mode,
                                  client['group'], envir, 'push',
                                  'Remote push (rsyncssh/async)')

    logging.getLogger().warn("Remote push failed for CoH #%d" % command_id)
    return None
Example #10
0
def remote_delete(command_id, client, files_list, mode, wrapper_timeout):
    """ Handle remote deletion on target """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    target_path = os.path.join(
        LauncherConfig().target_path,
        pulse2.launcher.utils.getTempFolderName(command_id, client['uuid']))
    if client['protocol'] == "ssh":
        # command is issued through our wrapper, time to build it

        if not LauncherConfig().is_ssh_available:
            logging.getLogger().warn(
                "Can't do remote delete because ssh is not available")
            return False

        # Built "thru" command
        thru_command_list = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [client['host']]

        # Define environnement
        envir = {
            'SSHPATH': LauncherConfig().ssh_path,
            'SSHARGS': client['transp_args'],
            'SSHTARGET': client['host'],
            'STEP': 'DELETE'
        }

        # Build "exec" command
        # The permissions need to be modified, else the directory can't be
        # deleted.
        real_command = ['if', 'id', 'SYSTEM', '>', '/dev/null', ';', 'then']
        real_command += ['chown', 'SYSTEM.SYSTEM', '"%s"' % target_path, ';']
        real_command += ['fi', ';']

        if LauncherConfig().is_smart_cleaner_available:
            real_command += [
                'if', '[', '-x',
                LauncherConfig().smart_cleaner_path, ']', ';'
            ]
            real_command += ['then']
            real_command += [LauncherConfig().smart_cleaner_path]
            real_command += ['--directory', '"%s"' % target_path]
            real_command += ['--files', '"%s"' % ','.join(files_list)]
            real_command += LauncherConfig().smart_cleaner_options
            real_command += [';']
            real_command += ['else']

        real_command += ['rm']
        real_command += map(lambda (a): '"%s"' % os.path.join(target_path, a),
                            files_list)
        real_command += ['&&', 'if', '!', 'rmdir', target_path, ';']
        real_command += ['then']
        # Use the dellater command if available
        real_command += ['if', '[', '-x', '/usr/bin/dellater.exe', ']', ';']
        real_command += ['then']
        # The mount/grep/sed stuff is needed to get the directory name for
        # Windows.
        real_command += [
            'dellater',
            '"$(mount | grep " on / type" | sed "s| on / type.*$||")"' +
            target_path, ';'
        ]
        real_command += ['fi', ';']
        real_command += ['fi', ';']
        if LauncherConfig().is_smart_cleaner_available:
            real_command += ['fi', ';']

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(real_command),
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += [
                '--check-client-side', ','.join(
                    map((lambda x: '='.join(x)),
                        client['client_check'].items()))
            ]
        if client['server_check']:
            command_list += [
                '--check-server-side', ','.join(
                    map((lambda x: '='.join(x)),
                        client['server_check'].items()))
            ]
        if client['action']:
            command_list += ['--action', client['action']]

        return get_command_result(command_id, command_list, mode,
                                  client['group'], envir, 'delete',
                                  'Remote delete (ssh/async)')
    logging.getLogger().warn("Remote delete failed for CoH #%d" % command_id)
    return None
Example #11
0
def remote_pull(command_id, client, files_list, mode, wrapper_timeout):
    """ Handle remote copy (pull) on target """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    target_path = os.path.join(
        LauncherConfig().target_path,
        pulse2.launcher.utils.getTempFolderName(command_id, client['uuid']))
    if client['protocol'] == "wget":
        # command is issued though our wrapper, time to build it

        if not LauncherConfig().is_ssh_available:
            logging.getLogger().warn(
                "Can't do remote pull because ssh is not available")
            return False
        # Built "thru" command
        thru_command_list = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += ["%s@%s" % (client['user'], client['host'])]

        # Define environnement
        envir = {
            'SSHPATH': LauncherConfig().ssh_path,
            'SSHARGS': client['transp_args'],
            'SSHTARGET': "%s@%s" % (client['user'], client['host']),
            'STEP': 'PUSHPULL'
        }

        # Build "exec" command
        real_command = [LauncherConfig().wget_path]
        real_command += client['proto_args']
        real_command += ['-N']
        real_command += files_list
        real_command += ['-P']
        real_command += [target_path]
        # Make downloaded files executable
        real_command += ['&&']
        real_command += ['chmod']
        real_command += ['u+x']
        real_command += ['-R']
        real_command += [target_path]
        # Check files integrity with MD5SUMS file if available AND size > 0
        real_command += ['&&']
        real_command += ['cd', target_path]
        real_command += ['&&']
        real_command += ['if', '[', '-s', 'MD5SUMS', ']',
                         ';']  # '-s' : size > 0
        real_command += ['then']
        real_command += ['md5sum', '-c', 'MD5SUMS', ';']
        real_command += ['fi']

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(real_command),
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += [
                '--check-client-side', ','.join(
                    map((lambda x: '='.join(x)),
                        client['client_check'].items()))
            ]
        if client['server_check']:
            command_list += [
                '--check-server-side', ','.join(
                    map((lambda x: '='.join(x)),
                        client['server_check'].items()))
            ]
        if client['action']:
            command_list += ['--action', client['action']]

        return get_command_result(command_id, command_list, mode,
                                  client['group'], envir, 'pull',
                                  'Remote pull (wget/async)')
    elif client['protocol'] == "rsyncproxy":
        # Built "thru" command
        thru_command_list = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += ["%s@%s" % (client['user'], client['host'])]

        # Define environnement
        envir = {
            'SSHPATH': LauncherConfig().ssh_path,
            'SSHARGS': client['transp_args'],
            'SSHTARGET': "%s@%s" % (client['user'], client['host']),
            'STEP': 'PUSHPULL'
        }

        # Build "exec" command
        src_path = pulse2.launcher.utils.getTempFolderName(
            client['proxy']['command_id'], client['proxy']['uuid'])

        real_command = ['rsync']
        real_command += client['proto_args']

        real_command += [
            '%s@%s:"%s"' % (client['user'], client['proxy']['host'], ' '.join(
                map(
                    lambda x: "%s/%s/'%s'" %
                    (LauncherConfig().target_path, src_path, x), files_list)))
        ]
        real_command += [target_path]

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(real_command),
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += [
                '--check-client-side', ','.join(
                    map((lambda x: '='.join(x)),
                        client['client_check'].items()))
            ]
        if client['server_check']:
            command_list += [
                '--check-server-side', ','.join(
                    map((lambda x: '='.join(x)),
                        client['server_check'].items()))
            ]
        if client['action']:
            command_list += ['--action', client['action']]

        return get_command_result(command_id, command_list, mode,
                                  client['group'], envir, 'pull',
                                  'Remote pull (rsyncproxy/async)')

    logging.getLogger().warn("Remote pull failed for CoH #%d" % command_id)
    return None
Example #12
0
def remote_direct(command_id, client, command, mode, max_log_size, wrapper_timeout):
    """ Handle remote direct stuff on target """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    if client['protocol'] == "ssh":
        # command is issued though our wrapper, time to build it

        if not LauncherConfig().is_ssh_available:
            return False

        # Built "thru" command
        thru_command_list  = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [client['host']]

        # Build "exec" command
        real_command = command

        if max_log_size == None:
            # If no max_log_size set, use the value from the configuration file
            max_log_size = LauncherConfig().wrapper_max_log_size
        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            real_command, # we do not use the PULSE2_WRAPPER_ARG_SEPARATOR here, as the command is send "as is"
            '--no-wrap',
            '--only-stdout',
            '--remove-empty-lines'
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += ['--check-client-side', ','.join(map((lambda x: '='.join(x)), client['client_check'].items()))]
        if client['server_check']:
            command_list += ['--check-server-side', ','.join(map((lambda x: '='.join(x)), client['server_check'].items()))]
        if client['action']:
            command_list += ['--action', client['action']]

        if mode == 'async':
            return pulse2.launcher.process_control.commandForker(
                command_list,
                __cb_async_process_end,
                command_id,
                LauncherConfig().defer_results,
                'completed_direct',
                LauncherConfig().max_command_age,
                client['group'],
                'direct'
            )
        elif mode == 'sync':
            return pulse2.launcher.process_control.commandRunner(
                command_list,
                __cb_sync_process_end
            )
    return None
Example #13
0
def remote_halt(command_id, client, mode, wrapper_timeout):
    """ Handle remote halt on target
    """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    halt_command = LauncherConfig().halt_command
    if client['protocol'] == "ssh":
        # command is issued though our wrapper, time to build it

        if not LauncherConfig().is_ssh_available:
            logging.getLogger().warn(
                "Can't do remote halt because ssh is not available")
            return False

        # Built "thru" command
        thru_command_list = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [client['host']]

        # Build "exec" command
        real_command = halt_command

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            real_command,  # we do not use the PULSE2_WRAPPER_ARG_SEPARATOR here, as the command is send "as is"
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += [
                '--check-client-side', ','.join(
                    map((lambda x: '='.join(x)),
                        client['client_check'].items()))
            ]
        if client['server_check']:
            command_list += [
                '--check-server-side', ','.join(
                    map((lambda x: '='.join(x)),
                        client['server_check'].items()))
            ]
        if client['action']:
            command_list += ['--action', client['action']]

        if mode == 'async':
            result = pulse2.launcher.process_control.commandForker(
                command_list, __cb_async_process_end, command_id,
                LauncherConfig().defer_results, 'completed_halt',
                LauncherConfig().max_command_age, client['group'], 'halt')
            if not result:
                logging.getLogger().warn(
                    "Remote halt (ssh/async) failed for CoH #%d" % command_id)
            return result
        elif mode == 'sync':
            result = pulse2.launcher.process_control.commandRunner(
                command_list, __cb_sync_process_end)
            if not result:
                logging.getLogger().warn(
                    "Remote halt (ssh/sync) failed for CoH #%d" % command_id)
            return result
    logging.getLogger().warn("Remote halt failed for CoH #%d" % command_id)
    return None
Example #14
0
def remote_inventory(command_id, client, mode, wrapper_timeout):
    """ Handle remote inventoring on target, sync mode

    This function will simply run the inventory on the othe side
    client is the method used to connect to the client

    TODO: same as sync_remote_push

    Return: same as sync_remote_push
    """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    inventory_command = LauncherConfig().inventory_command
    if client['protocol'] == "ssh":
        # command is issued though our wrapper, time to build it

        if not LauncherConfig().is_ssh_available:
            return False

        # Built "thru" command
        thru_command_list  = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [client['host']]

        # Build "exec" command
        real_command = inventory_command

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            real_command, # we do not use the PULSE2_WRAPPER_ARG_SEPARATOR here, as the command is send "as is"
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += ['--check-client-side', ','.join(map((lambda x: '='.join(x)), client['client_check'].items()))]
        if client['server_check']:
            command_list += ['--check-server-side', ','.join(map((lambda x: '='.join(x)), client['server_check'].items()))]
        if client['action']:
            command_list += ['--action', client['action']]

        if mode == 'async':
            return pulse2.launcher.process_control.commandForker(
                command_list,
                __cb_async_process_end,
                command_id,
                LauncherConfig().defer_results,
                'completed_inventory',
                LauncherConfig().max_command_age,
                client['group'],
                'inventory'
            )
        elif mode == 'sync':
            return pulse2.launcher.process_control.commandRunner(
                command_list,
                __cb_sync_process_end
            )
    return None
Example #15
0
def remote_push(command_id, client, files_list, mode, wrapper_timeout):
    """ Handle remote copy (push) """
    target_path = os.path.join(LauncherConfig().target_path, pulse2.launcher.utils.getTempFolderName(command_id, client['uuid']))
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    if client['protocol'] == "rsyncssh":
        # command is issued though our wrapper, time to build it
        real_files_list = files_list

        if not LauncherConfig().is_rsync_available:
            logging.getLogger().warn('Can\'t do remote push because rsync is not available')
            return False
        # Build "exec" command
        real_command  = [LauncherConfig().rsync_path]
        real_command += client['proto_args']
        real_command += real_files_list
        real_command += [ '%s@%s:%s/' % (client['user'], client['host'], target_path)]

        # Build "thru" command
        thru_command_list  = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [ "%s@%s" % (client['user'], client['host'])]

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--exec',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(real_command),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec-server-side'
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += ['--check-client-side', ','.join(map((lambda x: '='.join(x)), client['client_check'].items()))]
        if client['server_check']:
            command_list += ['--check-server-side', ','.join(map((lambda x: '='.join(x)), client['server_check'].items()))]
        if client['action']:
            command_list += ['--action', client['action']]

        if mode == 'async':
            return pulse2.launcher.process_control.commandForker(
                command_list,
                __cb_async_process_end,
                command_id,
                LauncherConfig().defer_results,
                'completed_push',
                LauncherConfig().max_command_age,
                client['group'],
                'push'
            )
        elif mode == 'sync':
            return pulse2.launcher.process_control.commandRunner(
                command_list,
                __cb_sync_process_end
            )
    return None
Example #16
0
def remote_delete(command_id, client, files_list, mode, wrapper_timeout):
    """ Handle remote deletion on target """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    target_path = os.path.join(LauncherConfig().target_path, pulse2.launcher.utils.getTempFolderName(command_id, client['uuid']))
    if client['protocol'] == "ssh":
        # command is issued through our wrapper, time to build it

        if not LauncherConfig().is_ssh_available:
            logging.getLogger().warn("Can't do remote delete because ssh is not available")
            return False

        # Built "thru" command
        thru_command_list  = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [client['host']]

        # Define environnement
        envir = {
                    'SSHPATH': LauncherConfig().ssh_path,
                    'SSHARGS': client['transp_args'],
                    'SSHTARGET' : client['host'],
                    'STEP': 'DELETE'
                }

        # Build "exec" command
        # The permissions need to be modified, else the directory can't be
        # deleted.
        real_command = ['if', 'id', 'SYSTEM', '>', '/dev/null', ';', 'then']
        real_command += ['chown', 'SYSTEM.SYSTEM', '"%s"' % target_path, ';']
        real_command += ['fi', ';']

        if LauncherConfig().is_smart_cleaner_available:
            real_command += ['if', '[', '-x', LauncherConfig().smart_cleaner_path, ']', ';']
            real_command += ['then']
            real_command += [ LauncherConfig().smart_cleaner_path ]
            real_command += ['--directory', '"%s"' % target_path]
            real_command += ['--files', '"%s"' % ','.join(files_list)]
            real_command += LauncherConfig().smart_cleaner_options
            real_command += [';']
            real_command += ['else']

        real_command += ['rm']
        real_command += map(lambda(a): '"%s"' % os.path.join(target_path, a), files_list)
        real_command += ['&&', 'if', '!', 'rmdir', target_path, ';']
        real_command += ['then']
        # Use the dellater command if available
        real_command += ['if', '[', '-x', '/usr/bin/dellater.exe', ']', ';']
        real_command += ['then']
        # The mount/grep/sed stuff is needed to get the directory name for
        # Windows.
        real_command += ['dellater', '"$(mount | grep " on / type" | sed "s| on / type.*$||")"' + target_path, ';']
        real_command += ['fi', ';']
        real_command += ['fi', ';']
        if LauncherConfig().is_smart_cleaner_available:
            real_command += ['fi', ';']

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(real_command),
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += ['--check-client-side', ','.join(map((lambda x: '='.join(x)), client['client_check'].items()))]
        if client['server_check']:
            command_list += ['--check-server-side', ','.join(map((lambda x: '='.join(x)), client['server_check'].items()))]
        if client['action']:
            command_list += ['--action', client['action']]

        if mode == 'async':
            result = pulse2.launcher.process_control.commandForker(
                command_list,
                __cb_async_process_end,
                command_id,
                LauncherConfig().defer_results,
                'completed_deletion',
                LauncherConfig().max_command_age,
                client['group'],
                'delete',
                env_=envir
            )
            if not result :
                logging.getLogger().warn("Remote delete (ssh/async) failed for CoH #%d" % command_id)
            return result
        elif mode == 'sync':
            result = pulse2.launcher.process_control.commandRunner(
                command_list,
                __cb_sync_process_end,
                env_=envir
            )
            if not result :
                logging.getLogger().warn("Remote delete (ssh/sync) failed for CoH #%d" % command_id)
            return result
    logging.getLogger().warn("Remote delete failed for CoH #%d" % command_id)
    return None
Example #17
0
def remote_halt(command_id, client, mode, wrapper_timeout):
    """ Handle remote halt on target
    """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    halt_command = LauncherConfig().halt_command
    if client['protocol'] == "ssh":
        # command is issued though our wrapper, time to build it

        if not LauncherConfig().is_ssh_available:
            logging.getLogger().warn("Can't do remote halt because ssh is not available")
            return False

        # Built "thru" command
        thru_command_list  = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [client['host']]

        # Define environnement
        envir = {
                    'SSHPATH': LauncherConfig().ssh_path,
                    'SSHARGS': client['transp_args'],
                    'SSHTARGET' : client['host'],
                    'STEP': 'HALT'
                }

        # Build "exec" command
        real_command = halt_command

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            real_command, # we do not use the PULSE2_WRAPPER_ARG_SEPARATOR here, as the command is send "as is"
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += ['--check-client-side', ','.join(map((lambda x: '='.join(x)), client['client_check'].items()))]
        if client['server_check']:
            command_list += ['--check-server-side', ','.join(map((lambda x: '='.join(x)), client['server_check'].items()))]
        if client['action']:
            command_list += ['--action', client['action']]

        if mode == 'async':
            result = pulse2.launcher.process_control.commandForker(
                command_list,
                __cb_async_process_end,
                command_id,
                LauncherConfig().defer_results,
                'completed_halt',
                LauncherConfig().max_command_age,
                client['group'],
                'halt',
                env_=envir
            )
            if not result :
                logging.getLogger().warn("Remote halt (ssh/async) failed for CoH #%d" % command_id)
            return result
        elif mode == 'sync':
            result = pulse2.launcher.process_control.commandRunner(
                command_list,
                __cb_sync_process_end,
                env_=envir
            )
            if not result :
                logging.getLogger().warn("Remote halt (ssh/sync) failed for CoH #%d" % command_id)
            return result
    logging.getLogger().warn("Remote halt failed for CoH #%d" % command_id)
    return None
Example #18
0
def remote_inventory(command_id, client, mode, wrapper_timeout):
    """ Handle remote inventoring on target, sync mode

    This function will simply run the inventory on the othe side
    client is the method used to connect to the client

    TODO: same as sync_remote_push

    Return: same as sync_remote_push
    """
    client = pulse2.launcher.utils.setDefaultClientOptions(client)
    inventory_command = LauncherConfig().inventory_command
    if client['protocol'] == "ssh":
        # command is issued though our wrapper, time to build it

        if not LauncherConfig().is_ssh_available:
            logging.getLogger().warn("Can't do remote inventory because ssh is not available")
            return False

        # Built "thru" command
        thru_command_list  = [LauncherConfig().ssh_path]
        thru_command_list += client['transp_args']
        thru_command_list += [client['host']]

        # Define environnement
        envir = {
                    'SSHPATH': LauncherConfig().ssh_path,
                    'SSHARGS': client['transp_args'],
                    'SSHTARGET' : client['host'],
                    'STEP': 'INVENTORY'
                }


        # Build "exec" command
        real_command = inventory_command

        # Build final command line
        command_list = [
            LauncherConfig().wrapper_path,
            '--max-log-size',
            str(LauncherConfig().wrapper_max_log_size),
            '--max-exec-time',
            str(wrapper_timeout),
            '--thru',
            PULSE2_WRAPPER_ARG_SEPARATOR.join(thru_command_list),
            '--exec',
            real_command, # we do not use the PULSE2_WRAPPER_ARG_SEPARATOR here, as the command is send "as is"
        ]

        # from {'a': 'b', 'c: 'd'} to 'a=b,c=d'
        if client['client_check']:
            command_list += ['--check-client-side', ','.join(map((lambda x: '='.join(x)), client['client_check'].items()))]
        if client['server_check']:
            command_list += ['--check-server-side', ','.join(map((lambda x: '='.join(x)), client['server_check'].items()))]
        if client['action']:
            command_list += ['--action', client['action']]

        return get_command_result(command_id, command_list, mode, client['group'], envir, 'inventory', 'Remote inventory (ssh/async)')    

    logging.getLogger().warn("Remote inventory failed for CoH #%d" % command_id) 
    return None