Esempio n. 1
0
def _update_trex_process(package_path):
    if not os.path.exists(tmp_dir):
        os.makedirs(tmp_dir)
    file_name = 'trex_package.tar.gz'

    # getting new package
    if package_path.startswith('http'):
        ret_code, stdout, stderr = run_command('wget %s -O %s' % (package_path, os.path.join(tmp_dir, file_name)), timeout = 600)
    else:
        ret_code, stdout, stderr = run_command('rsync -Lc %s %s' % (package_path, os.path.join(tmp_dir, file_name)), timeout = 300)
    if ret_code:
        raise Exception('Could not get requested package. Result: %s' % [ret_code, stdout, stderr])

    # calculating hash
    ret_code, stdout, stderr = run_command('sha1sum -b %s' % os.path.join(tmp_dir, file_name), timeout = 30)
    if ret_code:
        raise Exception('Could not calculate hash of package. Result: %s' % [ret_code, stdout, stderr])
    package_sha1 = stdout.strip().split()[0]

    # clean old unpacked dirs
    tmp_files = glob(os.path.join(tmp_dir, '*'))
    for tmp_file in tmp_files:
        if os.path.isdir(tmp_file) and not os.path.islink(tmp_file):
            shutil.rmtree(tmp_file)

    # unpacking
    ret_code, stdout, stderr = run_command('tar -xzf %s' % os.path.join(tmp_dir, file_name), timeout = 120, cwd = tmp_dir)
    if ret_code:
        raise Exception('Could not untar the package. %s' % [ret_code, stdout, stderr])
    tmp_files = glob(os.path.join(tmp_dir, '*'))
    unpacked_dirs = []
    for tmp_file in tmp_files:
        if os.path.isdir(tmp_file) and not os.path.islink(tmp_file):
            unpacked_dirs.append(tmp_file)
    if len(unpacked_dirs) != 1:
        raise Exception('Should be exactly one unpacked directory, got: %s' % unpacked_dirs)
    os.chmod(unpacked_dirs[0], 0o777) # allow core dumps to be written
    cur_dir = args.trex_dir
    if os.path.islink(cur_dir) or os.path.isfile(cur_dir):
        os.unlink(cur_dir)
    if not os.path.exists(cur_dir):
        os.makedirs(cur_dir)
        os.chmod(cur_dir, 0o777)
    bu_dir = '%s_BU%i' % (cur_dir, int(time.time()))
    try:
        # bu current dir
        shutil.move(cur_dir, bu_dir)
        shutil.move(unpacked_dirs[0], cur_dir)
        CUpdate.info = {'path': package_path, 'sha1': package_sha1}
        logging.info('Done updating, success')
    except BaseException as e: # something went wrong, return backup dir
        logging.error('Error while updating: %s' % e)
        if os.path.exists(cur_dir):
            shutil.rmtree(cur_dir)
        shutil.move(bu_dir, cur_dir)
        raise
    finally:
        if os.path.exists(bu_dir):
            shutil.rmtree(bu_dir)
Esempio n. 2
0
 def get_trex_cmds(self):
     logger.info('Processing get_trex_cmds() command.')
     ret_code, stdout, stderr = run_command('ps -u root --format pid,comm,cmd')
     if ret_code:
         raise Exception('Failed to determine running processes, stderr: %s' % stderr)
     trex_cmds_list = []
     for line in stdout.splitlines():
         pid, proc_name, full_cmd = line.strip().split(' ', 2)
         pid = pid.strip()
         full_cmd = full_cmd.strip()
         if proc_name.find('t-rex-64') >= 0:
             trex_cmds_list.append((pid, full_cmd))
     return trex_cmds_list
Esempio n. 3
0
 def get_trex_version (self, base64 = True):
     try:
         logger.info("Processing get_trex_version() command.")
         if not self.trex_version:
             ret_code, stdout, stderr = run_command('./t-rex-64 --help', cwd = self.TREX_PATH)
             search_result = re.search('\n\s*(Version\s*:.+)', stdout, re.DOTALL)
             if not search_result:
                 raise Exception('Could not determine version from ./t-rex-64 --help')
             self.trex_version = binascii.b2a_base64(search_result.group(1).encode(errors='replace'))
         if base64:
             return self.trex_version.decode(errors='replace')
         else:
             return binascii.a2b_base64(self.trex_version).decode(errors='replace')
     except Exception as e:
         err_str = "Can't get trex version, error: %s" % e
         logger.error(err_str)
         return Fault(-33, err_str)
Esempio n. 4
0
# returns True if given path is under current dir or /tmp
def _check_path_under_current_or_temp(path):
    if not os.path.relpath(path, '/tmp').startswith(os.pardir):
        return True
    if not os.path.relpath(path, os.getcwd()).startswith(os.pardir):
        return True
    return False


### Main ###

if os.getuid() != 0:
    fail('Please run this program as root/with sudo')

pid = os.getpid()
ret, out, err = run_command('taskset -pc 0 %s' % pid)
if ret:
    fail('Could not set self affinity to core zero. Result: %s' %
         [ret, out, err])

daemon_actions = OrderedDict([
    ('start', 'start the daemon'), ('stop', 'exit the daemon process'),
    ('show', 'prompt the status of daemon process (running / not running)'),
    ('restart', 'stop, then start again the daemon process')
])

actions_help = 'Specify action command to be applied on master daemon.\n' +\
               '\n'.join(['    (*) %-11s: %s' % (key, val) for key, val in daemon_actions.items()])

daemons = {}.fromkeys(['master_daemon', 'trex_daemon_server', 'stl_rpc_proxy'])