예제 #1
0
def _mod_deploy():
    """Deploy an installed OnRamp educational module.

    Usage: ./onramp_pce_service.py moddeploy [-h] [-v] mod_id

    positional arguments:
      mod_id         Id of the module

      optional arguments:
        -h, --help     show this help message and exit
        -v, --verbose  increase output verbosity

    """
    descrip = 'Deploy an installed OnRamp educational module.'
    parser = argparse.ArgumentParser(prog='onramp_pce_service.py moddeploy',
                                     description=descrip)
    parser.add_argument('-v', '--verbose', action='store_true',
                        help='increase output verbosity')
    parser.add_argument('mod_id', help='Id of the module', type=int)
    args = parser.parse_args(args=sys.argv[2:])

    result, msg = deploy_module(args.mod_id, verbose=args.verbose)

    if result != 0:
        sys.stderr.write(msg + '\n')
    else:
        print msg

    sys.exit(result)
예제 #2
0
def _mod_deploy():
    """Deploy an installed OnRamp educational module.

    Usage: ./onramp_pce_service.py moddeploy [-h] [-v] mod_id

    positional arguments:
      mod_id         Id of the module

      optional arguments:
        -h, --help     show this help message and exit
        -v, --verbose  increase output verbosity

    """
    descrip = 'Deploy an installed OnRamp educational module.'
    parser = argparse.ArgumentParser(prog='onramp_pce_service.py moddeploy',
                                     description=descrip)
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        help='increase output verbosity')
    parser.add_argument('mod_id', help='Id of the module', type=int)
    args = parser.parse_args(args=sys.argv[2:])

    result, msg = deploy_module(args.mod_id, verbose=args.verbose)

    if result != 0:
        sys.stderr.write(msg + '\n')
    else:
        print msg

    sys.exit(result)
예제 #3
0
def _mod_test():
    """Test contents of OnRamp Educational module.

    Usage: onramp_pce_service.py modtest [-h] [-v] mod_cfg_file

    positional arguments:
        mod_cfg_file   module's modtest configuration file

    optional arguments:
        -h, --help     show help message and exit
        -v, --verbose  increase output verbosity
    """
    pce_dir = 'onramp'
    batch_script_name = 'script.sh'
    default_sleep_time = 5.0
    job_output_file = 'output.txt'
    descrip = 'Test contents of OnRamp educational module.'
    username = '******'
    module_id = 1
    job_id = 1
    ret_dir = os.getcwd()

    parser = argparse.ArgumentParser(prog='onramp_pce_service.py modtest',
                                     description=descrip)
    parser.add_argument('-v', '--verbose', action='store_true',
                        help='increase output verbosity')
    parser.add_argument('mod_cfg_file',
                        help="module's modtest configuration file")
    args = parser.parse_args(args=sys.argv[2:])

    env_py = os.path.join(pce_root, 'src', 'env', 'bin', 'python')

    conf = ConfigObj(args.mod_cfg_file,
                     configspec=os.path.join(pce_root, 'src', 'configspecs',
                                             'modtest.cfgspec'))
    conf.validate(Validator())

    module_path = abspath(expanduser(conf['module_path']))
    deploy_path_parent = abspath(expanduser(conf['deploy_path']))
    module_name = conf['module_name']
    deploy_path = os.path.join(deploy_path_parent,
                               '%s_%d' % (module_name, module_id))
    mod_state_f = mkstemp()
    job_state_f = mkstemp()
    post_deploy_test = ('post_deploy_test',
                        abspath(expanduser(os.path.join(
                        deploy_path, conf['post_deploy_test']))))
    post_preprocess_test = ('post_preprocess_test',
                            abspath(expanduser(os.path.join(
                            deploy_path, conf['post_preprocess_test']))))
    post_launch_test = ('post_launch_test',
                        abspath(expanduser(os.path.join(
                        deploy_path, conf['post_launch_test']))))
    post_status_test = ('post_status_test',
                        abspath(expanduser(os.path.join(
                        deploy_path, conf['post_status_test']))))
    post_postprocess_test = ('post_postprocess_test',
                             abspath(expanduser(os.path.join(
                             deploy_path, conf['post_postprocess_test']))))
     
    def finish(conf, error=False):
        path = deploy_path
        results = job_output_file
        params = args

        os.close(mod_state_f[0])
        os.close(job_state_f[0])
        os.remove(mod_state_f[1])
        os.remove(job_state_f[1])

        if error:
            if conf['cleanup']:
                print ('modtest is configured to cleanup job files on '
                       'exit, but there has been an error. Would you like '
                       'to keep the files for troubleshooting?')
                response = raw_input('(Y)es or (N)o? ')
                if response == 'Y' or response == 'y':
                    conf['cleanup'] = False

        if conf['cleanup']:
            if params.verbose:
                print 'Removing %s' % deploy_path
            shutil.rmtree(path)
        else:
            if os.path.isfile(results):
                print ('Output file from job: %s'
                       % os.path.join(deploy_path, results))
            else:
                print 'No job output file found.'

        os.chdir(ret_dir)
        return

    def run_test(ret_val=None, test=None):
        py = env_py
        cfg = conf
        params = args

        if ret_val is not None and ret_val[0] != 0:
            print 'Error: %s' % ret[1]
            finish(cfg, error=True)
            return -1

        if test is not None and cfg[test[0]]:
            if params.verbose:
                print 'Running %s' % test[0]
            if 0 != call([py, test[1]]):
                print '%s failed.' % test[0]
                finish(cfg, error=True)
                return -1

        return 0

    # Install.
    if args.verbose:
        print 'Installing module %s to %s' % (module_name, deploy_path)
    if os.path.exists(deploy_path):
        print ('The deploy path exists. Would you like to remove the old path '
               'and continue?')
        response = raw_input('(Y)es or (N)o? ')
        if response == 'Y' or response == 'y':
            shutil.rmtree(deploy_path)
        else:
            sys.exit('Aborted')

    ret = install_module('local', module_path, deploy_path_parent, module_id,
                         module_name, mod_state_file=mod_state_f[1])
    if 0 != run_test(ret_val=ret):
        return

    os.chdir(deploy_path)

    # Deploy.
    if args.verbose:
        print 'Deploying module'
    ret = deploy_module(module_id, mod_state_file=mod_state_f[1])
    if 0 != run_test(ret_val=ret, test=post_deploy_test):
        return

    # Init job state.
    if args.verbose:
        print 'Initializing job state'
    custom_runparams = ConfigObj(abspath(expanduser(conf['custom_runparams'])))
    ret = job_init_state(job_id, module_id, username, module_name,
                         custom_runparams, job_state_file=job_state_f[1],
                         mod_state_file=mod_state_f[1], run_dir=deploy_path)
    if 0 != run_test(ret_val=ret):
        return

    # Preprocess.
    if args.verbose:
        print 'Preprocessing job'
    ret = job_preprocess(job_id, job_state_file=job_state_f[1])
    if 0 != run_test(ret_val=ret, test=post_preprocess_test):
        return

    # Schedule.
    if args.verbose:
        print 'Deploying module'
    ret = job_run(job_id, job_state_file=job_state_f[1])
    if 0 != run_test(ret_val=ret, test=post_launch_test):
        return

    # Wait for job to finish, call onramp_status.py when appropriate.
    if args.verbose:
        print 'Waiting/polling job state for completion'
        
    sleep_time = conf['results_check_sleep']
    if not sleep_time:
        sleep_time = default_sleep_time
    job_state = 'Queued'

    while job_state != 'Done':
        time.sleep(sleep_time)
        job = get_jobs(job_id, job_state_file=job_state_f[1])
        job_state = job['state']

        if job_state == 'Running':
            if job['mod_status_output'] != '' and args.verbose:
                print job['mod_status_output']
            if 0 != run_test(test=post_status_test):
                return

    # Postprocess.
    if args.verbose:
        print 'Testing job postprocess'
    if 0 != run_test(test=post_postprocess_test):
        return

    print 'No errors found.'
    finish(conf, error=False)
예제 #4
0
def _mod_test():
    """Test contents of OnRamp Educational module.

    Usage: onramp_pce_service.py modtest [-h] [-v] mod_cfg_file

    positional arguments:
        mod_cfg_file   module's modtest configuration file

    optional arguments:
        -h, --help     show help message and exit
        -v, --verbose  increase output verbosity
    """
    pce_dir = 'onramp'
    batch_script_name = 'script.sh'
    default_sleep_time = 5.0
    job_output_file = 'output.txt'
    descrip = 'Test contents of OnRamp educational module.'
    username = '******'
    module_id = 1
    job_id = 1
    ret_dir = os.getcwd()

    parser = argparse.ArgumentParser(prog='onramp_pce_service.py modtest',
                                     description=descrip)
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        help='increase output verbosity')
    parser.add_argument('mod_cfg_file',
                        help="module's modtest configuration file")
    args = parser.parse_args(args=sys.argv[2:])

    env_py = os.path.join(pce_root, 'src', 'env', 'bin', 'python')

    conf = ConfigObj(args.mod_cfg_file,
                     configspec=os.path.join(pce_root, 'src', 'configspecs',
                                             'modtest.cfgspec'))
    conf.validate(Validator())

    module_path = abspath(expanduser(conf['module_path']))
    deploy_path_parent = abspath(expanduser(conf['deploy_path']))
    module_name = conf['module_name']
    deploy_path = os.path.join(deploy_path_parent,
                               '%s_%d' % (module_name, module_id))
    mod_state_f = mkstemp()
    job_state_f = mkstemp()
    post_deploy_test = ('post_deploy_test',
                        abspath(
                            expanduser(
                                os.path.join(deploy_path,
                                             conf['post_deploy_test']))))
    post_preprocess_test = ('post_preprocess_test',
                            abspath(
                                expanduser(
                                    os.path.join(
                                        deploy_path,
                                        conf['post_preprocess_test']))))
    post_launch_test = ('post_launch_test',
                        abspath(
                            expanduser(
                                os.path.join(deploy_path,
                                             conf['post_launch_test']))))
    post_status_test = ('post_status_test',
                        abspath(
                            expanduser(
                                os.path.join(deploy_path,
                                             conf['post_status_test']))))
    post_postprocess_test = ('post_postprocess_test',
                             abspath(
                                 expanduser(
                                     os.path.join(
                                         deploy_path,
                                         conf['post_postprocess_test']))))

    def finish(conf, error=False):
        path = deploy_path
        results = job_output_file
        params = args

        os.close(mod_state_f[0])
        os.close(job_state_f[0])
        os.remove(mod_state_f[1])
        os.remove(job_state_f[1])

        if error:
            if conf['cleanup']:
                print(
                    'modtest is configured to cleanup job files on '
                    'exit, but there has been an error. Would you like '
                    'to keep the files for troubleshooting?')
                response = raw_input('(Y)es or (N)o? ')
                if response == 'Y' or response == 'y':
                    conf['cleanup'] = False

        if conf['cleanup']:
            if params.verbose:
                print 'Removing %s' % deploy_path
            shutil.rmtree(path)
        else:
            if os.path.isfile(results):
                print('Output file from job: %s' %
                      os.path.join(deploy_path, results))
            else:
                print 'No job output file found.'

        os.chdir(ret_dir)
        return

    def run_test(ret_val=None, test=None):
        py = env_py
        cfg = conf
        params = args

        if ret_val is not None and ret_val[0] != 0:
            print 'Error: %s' % ret[1]
            finish(cfg, error=True)
            return -1

        if test is not None and cfg[test[0]]:
            if params.verbose:
                print 'Running %s' % test[0]
            if 0 != call([py, test[1]]):
                print '%s failed.' % test[0]
                finish(cfg, error=True)
                return -1

        return 0

    # Install.
    if args.verbose:
        print 'Installing module %s to %s' % (module_name, deploy_path)
    if os.path.exists(deploy_path):
        print(
            'The deploy path exists. Would you like to remove the old path '
            'and continue?')
        response = raw_input('(Y)es or (N)o? ')
        if response == 'Y' or response == 'y':
            shutil.rmtree(deploy_path)
        else:
            sys.exit('Aborted')

    ret = install_module('local',
                         module_path,
                         deploy_path_parent,
                         module_id,
                         module_name,
                         mod_state_file=mod_state_f[1])
    if 0 != run_test(ret_val=ret):
        return

    os.chdir(deploy_path)

    # Deploy.
    if args.verbose:
        print 'Deploying module'
    ret = deploy_module(module_id, mod_state_file=mod_state_f[1])
    if 0 != run_test(ret_val=ret, test=post_deploy_test):
        return

    # Init job state.
    if args.verbose:
        print 'Initializing job state'
    custom_runparams = ConfigObj(abspath(expanduser(conf['custom_runparams'])))
    ret = job_init_state(job_id,
                         module_id,
                         username,
                         module_name,
                         custom_runparams,
                         job_state_file=job_state_f[1],
                         mod_state_file=mod_state_f[1],
                         run_dir=deploy_path)
    if 0 != run_test(ret_val=ret):
        return

    # Preprocess.
    if args.verbose:
        print 'Preprocessing job'
    ret = job_preprocess(job_id, job_state_file=job_state_f[1])
    if 0 != run_test(ret_val=ret, test=post_preprocess_test):
        return

    # Schedule.
    if args.verbose:
        print 'Deploying module'
    ret = job_run(job_id, job_state_file=job_state_f[1])
    if 0 != run_test(ret_val=ret, test=post_launch_test):
        return

    # Wait for job to finish, call onramp_status.py when appropriate.
    if args.verbose:
        print 'Waiting/polling job state for completion'

    sleep_time = conf['results_check_sleep']
    if not sleep_time:
        sleep_time = default_sleep_time
    job_state = 'Queued'

    while job_state != 'Done':
        time.sleep(sleep_time)
        job = get_jobs(job_id, job_state_file=job_state_f[1])
        job_state = job['state']

        if job_state == 'Running':
            if job['mod_status_output'] != '' and args.verbose:
                print job['mod_status_output']
            if 0 != run_test(test=post_status_test):
                return

    # Postprocess.
    if args.verbose:
        print 'Testing job postprocess'
    if 0 != run_test(test=post_postprocess_test):
        return

    print 'No errors found.'
    finish(conf, error=False)