示例#1
0
def get_folder(request):

    if request.method == 'POST':
        folder = json.loads(request.body)['file']
        try:
            cred = Credential.objects.get(
                site_user_name=request.user, service='velo')
            if not cred:
                print "Unable to find user in credential store"
                return HttpResponse(status=500)
            if folder == '/User Documents/':
                folder += cred.service_user_name
            # request = {
            #     'velo_user': cred.service_user_name,
            #     'velo_pass': cred.password,
            #     'command': 'get_folder',
            #     'folder': folder
            # }
            request = {
                'velo_user': '******',
                'velo_pass': '******',
                'command': 'get_folder',
                'folder': folder
            }
            out = velo_request(request)
            out = out.split(',')
            out.insert(0, folder)
            out = [o for o in out if o != '']
            return HttpResponse(json.dumps(out))

        except Exception as e:
            print_debug(e)
            return HttpResponse(status=500)
    else:
        return HttpResponse(status=404)
示例#2
0
def check_credentials(request):
    if request.method == 'POST':
        try:
            data = json.loads(request.body)
            response = {}
            creds = Credential.objects.filter(site_user_name=request.user)
            if len(creds) != 0:
                for c in creds:
                    try:
                        if c.service == 'esgf':
                            import pyesgf
                            from pyesgf.logon import LogonManager
                            lm = LogonManager()
                            lm.logon_with_openid(
                                c.service_user_name, c.password)
                            if lm.is_logged_on():
                                response[s] = 'success'
                                print 'esgf log in successful'
                            else:
                                print 'esgf log in failed'
                                response[s] = 'fail'
                        if c.service == 'velo':
                            user, password = c.service_user_name, c.password
                            velo_creds = {
                                "velo_user": user,
                                "velo_pass": password,
                                "command": "init"
                            }
                            result = velo_request(velo_creds)
                            # TODO: Extract values out to CAPITAL_NAMED_CONSTANTS
                            if result == "Success":
                                print "velo login successful"
                            else:
                                print "velo login failed"
                                response[s] = "fail"

                        if c.service == 'github':
                            import github3
                            from github3 import login
                            gh = login(c.site_user_name, password=c.password)
                            if gh.user() == c.site_user_name:
                                print 'Github login successful'
                                response[s] = 'success'
                            else:
                                print 'Github login failure'
                                response[s] = 'fail'

                        if c.service == 'jira':
                            print 'Working on jira....'
                    except:
                        print_debug(c)
                        return HttpResponse(status=500)

            return HttpResponse(json.dumps(response))

        except Exception as e:
            print_debug(e)
            return HttpResponse(status=500)
    else:
        return HttpResponse(status=404)
示例#3
0
def load_layout(request):
    if request.method == 'POST':
        try:
            data = json.loads(request.body)
            layout = TileLayout.objects.filter(layout_name=data['layout_name'])
            if len(layout) == 0:
                return HttpResponse(status=500)

            j = {}
            for i in layout:
                j['board_layout'] = json.loads(i.board_layout)
                j['mode'] = i.mode
                return HttpResponse(json.dumps(j), status=200, content_type="application/json")
        except Exception as e:
            print_debug(e)
            return HttpResponse(status=500)
    elif request.method == 'GET':
        try:
            all_layouts = TileLayout.objects.filter(user_name=request.user)
            layouts = []
            for layout in all_layouts:
                curlayout = {}
                curlayout['name'] = layout.layout_name
                curlayout['default'] = layout.default
                curlayout['layout'] = json.loads(layout.board_layout)
                curlayout['mode'] = layout.mode
                layouts.append(curlayout)
            return HttpResponse(json.dumps(layouts))
        except Exception as e:
            print_debug(e)
            return HttpResponse(status=500)
示例#4
0
def nersc_login(request):
    try:
        data = json.loads(request.body)
    except Exception as e:
        print_message('Unable to decode request.body: {}'.format(request.body))
        return HttpResponse(status=400)

    user = str(request.user)
    username = data.get('username')
    password = data.get('password')
    if not username or not password:
        print_message('No username or password')
        return HttpResponse(status=400)

    try:
        client = paramiko.SSHClient()
        client.load_system_host_keys()
        client.connect('edison.nersc.gov',
                       port=22,
                       username=username,
                       password=password)
    except Exception as e:
        print_message('Error connecting to nersc via ssh')
        print_debug(e)
        return HttpResponse(status=500)
    else:
        print_message('saving credentials to session')
        request.session['nersc_username'] = username
        request.session['nersc_password'] = password
        print_message('session user={}, pass={}'.format(request.session.get('nersc_username'), request.session.get('nersc_password')))
    print_message('login successful')
    return HttpResponse()
示例#5
0
def stop_run(request):
    data = json.loads(request.body)
    job_id = data.get('job_id')
    if not job_id:
        print_message('No job_id given')
        return HttpResponse(status=400)

    request = mydict()
    request.body = json.dumps({
        'job_id': job_id,
        'request': 'stop'
    })
    request.method = 'POST'
    # url = ''.join([POLLER_HOST, ':', request.META['SERVER_PORT'], POLLER_SUFFIX])
    try:
        # r = requests.post(url, request)
        print_message('sending request to poller with data {}'.format(request.body), 'ok')
        r = poller_update(request)
        if r.status_code == 200:
            return HttpResponse()
        else:
            return HttpResponse(status=500)

    except Exception as e:
        print_message("Failed to stop job")
        print_debug(e)

    return HttpResponse(status=500)
示例#6
0
def post_stop(job_id):
    try:
        job = UserRuns.objects.get(id=job_id)
        job.status = 'stopped'
        job.save()
        options = json.loads(job.config_options)
    except Exception as e:
        print_debug(e)
        return HttpResponse(status=500)
    message = {
        'type': 'stopped',
        'timestamp': datetime.datetime.now().strftime(timeformat)
    }
    group_job_update(job_id, job.user, message)
    try:
        note = Notification.objects.get(user=job.user)
        new_notification = json.dumps({
            'job_id': job_id,
            'run_type': 'stop',
            'optional_message': {
                'run_name': options.get('run_name'),
                'run_type': options.get('run_type')
            },
            'status': 'stopped',
            'timestamp': datetime.datetime.now().strftime(timeformat)
        })
        note.notification_list += new_notification + ' -|- '
        note.save()
    except Exception, e:
        raise
示例#7
0
def read_output_script(request):
    script_name = request.GET.get('script_name')
    run_name = request.GET.get('run_name')
    user = str(request.user)
    job_id = str(request.GET.get('job_id'))
    print_message(request.GET, 'ok')
    if not script_name:
        print_message('No script name given', 'error')
        return HttpResponse(status=400)

    if not run_name:
        print_message('No run config folder given', 'error')
        return HttpResponse(status=400)

    if not job_id:
        print_message('No job id given', 'error')
        return HttpResponse(status=400)

    try:
        run = UserRuns.objects.get(id=job_id)
    except Exception as e:
        print_debug(e)
        print_message('Error looking up job with id: {}'.format(job_id))
        return HttpResponse(status=500)

    contents = run.output
    return JsonResponse({'script': contents})
示例#8
0
def add_credentials(request):
    if request.method == 'POST':
        print request.body
        try:
            data = json.loads(request.body)
            if len(data) != 0:
                for s in data:
                    creds = Credential.objects.filter(
                        service=s, site_user_name=str(request.user))
                    if len(creds) != 0:
                        for i in creds:
                            i.password = data[s]['password']
                            i.service_user_name = data[s]['username']
                            i.save()
                    else:
                        c = Credential(service_user_name=data[s]['username'], password=data[s][
                                       'password'], service=s, site_user_name=str(request.user))
                        c.save()
                res = render_template(request, 'web_fe/add_credentials.html', {'added': 'true'})
                return HttpResponse(res)
            else:
                res = render_template(request, 'web_fe/add_credentials.html', {'added': 'false'})
                return HttpResponse(res)
        except Exception as e:
            print_debug(e)
            return HttpResponse(status=500)
    else:
        creds = Credential.objects.filter(site_user_name=str(request.user))
        stored_credentials = []
        for s in creds:
            stored_credentials.append(s.service)
        response = {'added': 'false', 'stored_credentials': stored_credentials}
        return HttpResponse(render_template(request, 'web_fe/add_credentials.html', response))
示例#9
0
def get_data_type_folders(request):
    user = str(request.user)
    try:
        data = json.loads(request.body)
    except Exception as e:
        print_message("Unable to load request body")
        print_debug(e)
        return HttpResponse(status=400)
    data_type = data.get('type')
    if not data_type:
        print_message('No data type given')
        return HttpResponse(status=400)

    folder_path = '{project_root}/userdata/{user}/'.format(
        project_root=project_root(),
        user=user)
    if data_type == 'diagnostic':
        folder_path += 'diagnostic_output'
    elif data_type == 'model':
        folder_path += 'model_output'
    elif data_type == 'observation':
        folder_path += 'observations'
    else:
        print_message('Invalid data_type {}'.format(data_type))
        return HttpResponse(status=400)

    print_message('Looking for folders in {}'.format(folder_path))
    folder_contents = os.listdir(folder_path)
    return HttpResponse(json.dumps(folder_contents))
示例#10
0
def get_diagnostic_by_name(request):
    """
    Looks up a specific config and returns its information
    inputs: user, the user making the request
            name, the name of the config to lookup
            version, an optional argument for the version to look for, default is latest
    """
    user = str(request.user)
    print_message(request.body)
    data = json.loads(request.body)
    version = data.get('version')
    name = data.get('name')
    version = version if version else 'latest'
    if not name:
        print_message('No name given')
        return HttpResponse(status=400)

    try:
        if version == 'latest':
            print_message('Looking for config with name={name}, user={user}'.format(name=name, user=user))
            config = DiagnosticConfig.objects.filter(
                user=user,
                name=name)
            if config:
                config = config.latest()
        else:
            print_message('Looking for config with name={name}, user={user}, version={version}'.format(name=name, user=user, version=version))
            config = DiagnosticConfig.objects.filter(
                user=user,
                name=name,
                version=version)
            if config:
                print_message('config was found')
                config.extra(order_by=['version'])
                config = config[0]
            else:
                for c in DiagnosticConfig.objects.all():
                    print_message(c.__dict__)
                print_message('error finding config')
    except Exception as e:
        print_message('Error looking up config with user: {user}, name: {name}, version: {version}'.format(user=user, name=name, version=version))
        print_debug(e)
        return HttpResponse(status=400)

    if not config:
        print_message('No config matching with name={name} and version={version} was found'.format(name=name, version=version))
        return HttpResponse(status=400)

    response = json.dumps({
        'version': config.version,
        'name': config.name,
        'model_path': config.model_path,
        'obs_path': config.obs_path,
        'output_path': config.output_path,
        'allowed_users': config.allowed_users,
        'set': config.diag_set
    })
    return HttpResponse(response)
示例#11
0
def view_remote_directory(request):
    try:
        data = json.loads(request.body)
    except Exception as e:
        print_message('Unable to decode request.body: {}'.format(request.body))
        return HttpResponse(status=400)

    user = str(request.user)
    remote_dir = data.get('remote_dir')
    if not remote_dir:
        remote_dir = ''
    remote_dir.replace('\n', ' ')
    remote_dir.replace('&', ' ')
    remote_dir = '/project/projectdirs/acme/{}'.format(remote_dir.split(' ')[0])

    username = data.get('username')
    password = data.get('password')
    if not username or not password:
        print_message('No username or password in request.body, using session data')
        try:
            username = request.session.get('nersc_username')
            password = request.session.get('nersc_password')
        except Exception as e:
            print_message('could not extract username from session')
            return HttpResponse(status=400)
    if not username or not password:
        print_message('No username or password')
        return HttpResponse(status=400)
    try:
        print_message('connecting', 'ok')
        client = paramiko.SSHClient()
        client.load_system_host_keys()
        client.connect('edison.nersc.gov',
                       port=22,
                       username=username,
                       password=password)
        print_message('connected!', 'ok')
    except Exception as e:
        print_message('Error connecting to publication server')
        print_debug(e)
        return HttpResponse(status=500)

    print_message('Looking up remote folder {}'.format(remote_dir))
    command = "python ~/scripts/get_dir_contents.py {}".format(remote_dir)
    try:
        stdin, stdout, stderr = client.exec_command(command)
    except Exception as e:
        print_debug(e)
        print_message('Error executing command {}'.format(command))
        return HttpResponse(status=500)
    response = {
        'error': stderr.read(),
        'out': stdout.read()
    }
    return HttpResponse(json.dumps(response))
示例#12
0
def get_file(request):
    if request.method == 'POST':
        try:
            data = json.loads(request.body)
            filename = data['filename']
            remote_file_path = os.path.join(data['path'], filename)
            cred = Credential.objects.get(
                site_user_name=request.user, service='velo')

            local_path = os.path.join(os.getcwd(), 'userdata', cred.service_user_name)
            # Create the local directories if they dont exist
            remote_path = remote_file_path.split('/')
            prefix = local_path[
                :local_path.index(cred.service_user_name) + len(cred.service_user_name)] + '/'
            if not os.path.isdir(prefix):
                os.makedirs(prefix)

            for i in range(remote_path.index(cred.service_user_name) + 1, len(remote_path) - 1):
                if not os.path.isdir(prefix + remote_path[i]):
                    prefix += remote_path[i] + '/'
                    os.makedirs(prefix)

            request = {
                'velo_user': cred.service_user_name,
                'velo_pass': cred.password,
                'command': 'get_file',
                'remote_path': remote_file_path,
                'local_path': prefix,
                'filename': filename
            }
            response = velo_request(request)
            if 'Failed to download file' in response:
                print response
                return HttpResponse(status=500)

            path = prefix + filename
            path_components = path.split("/")
            path = os.path.join(
                path_components[path_components.index(cred.service_user_name) + 1:])
            if filename.split('.').pop() == 'png':
                response = {
                    'type': 'image',
                    'location': path
                }
                return HttpResponse(json.dumps(response))
            else:
                out = response.splitlines(True)
                return HttpResponse(out, content_type='text/plain')

        except Exception as e:
            print_debug(e)
            return HttpResponse(status=500)
    else:
        return HttpResponse(status=404)
示例#13
0
def save_diagnostic_config(request):
    """
    Saves the parameters of a diagnostic run to the db
    input:
        set: the set of diagnostic sets to run,
        user: the user creating the config,
        obs: the name of the obs data folder to use (can also be model data),
        model: the name of the model data to use (can also be obs, even though obs/obs comparisons dont make a lot of sense),
        name: the name of the config,
        shared_users: any other users who should have access to the config
    """
    if request.method != 'POST':
        print_message('invalid HTTP verb')
        return HttpResponse(status=404)
    user = str(request.user)
    try:
        data = json.loads(request.body)
    except Exception as e:
        print_message('Error loading request json from body: {}'.format(request.body))
        print_debug(e)
        return HttpResponse(status=400)

    diag_set = data.get('set')
    obs_data = data.get('obs')
    model_data = data.get('model')
    name = data.get('name')
    shared_users = data.get('shared_users')
    if not diag_set:
        print_message('No diag set')
        return HttpResponse(status=400)
    if not obs_data:
        print_message('no obs data folder')
        return HttpResponse(status=400)
    if not model_data:
        print_message('no model data folder')
        return HttpResponse(status=400)
    if not name:
        print_message('no name')
        return HttpResponse(status=400)
    if not shared_users:
        shared_users = user
    else:
        shared_users = '{},{}'.format(shared_users, user)

    version = None
    try:
        config = DiagnosticConfig.objects.filter(user=user, name=data.get('name')).extra(order_by=['version'])
        latest = config[len(config) - 1].version + 1
        print_message('Looking for latest {}'.format(latest))
    except Exception, e:
        latest = 1
示例#14
0
def start_run(request):
    user = str(request.user)
    data = json.loads(request.body)
    run_name = data.get('run_name')
    if not run_name:
        print_message('No run name given', 'error')
        return HttpResponse(status=400)

    conf = DiagnosticConfig.objects.filter(user=user, name=run_name)
    if not conf:
        print_message('Unable to find config {} for user {}'.format(run_name, user))
        return HttpResponse(400)

    conf = conf.latest()
    request = mydict()
    request.body = {
        'user': user,
        'request': 'new',
        'run_name': run_name,
        'run_type': 'diagnostic'
    }
    request.method = 'POST'
    request.body.update(model_to_dict(conf))

    if not os.path.exists(request.body.get('obs_path')):
        print_message(request.body)
        print_message('could not find {}'.format(request.body.get('obs_path')))
        return HttpResponse(json.dumps({
            'error': 'Invalid observation path'
        }))
    if not os.path.exists(request.body.get('model_path')):
        print_message('could not find {}'.format(request.body.get('model_path')))
        return HttpResponse(json.dumps({
            'error': 'Invalid model path'
        }))

    request.body = json.dumps(request.body)
    try:
        r = poller_update(request)
        if(r.status_code != 200):
            print_message('Error communicating with poller')
            return HttpResponse(status=500)
        print_message("poller returning content: {}".format(r.content), 'ok')
        return HttpResponse(r.content)
    except Exception as e:
        print_message('Error making request to poller')
        print_debug(e)
        return HttpResponse(status=500)

    return HttpResponse()
示例#15
0
def update_script(request):
    data = json.loads(request.body)
    script_name = data.get('script_name')
    run_name = data.get('run_name')
    contents = data.get('contents')
    user = str(request.user)
    if not script_name:
        print_message('No script name given', 'error')
        return HttpResponse(status=400)

    if not run_name:
        print_message('No run name given', 'error')
        return HttpResponse(status=400)
    if not contents:
        print_message('No contents given', 'error')
        return HttpResponse(status=400)
    path = os.path.abspath(os.path.dirname(__file__))
    run_directory = path + RUN_SCRIPT_PATH + user + '/' + run_name
    if not os.path.exists(run_directory):
        print_message('Run directory not found {}'.format(run_directory), 'error')
        return HttpResponse(status=400)

    try:
        run_scripts = RunScript.objects.filter(user=user, name=script_name, run=run_name)
        if not run_scripts:
            return HttpResponse(status=404)
        latest = run_scripts.latest()
        latest.version += 1
        latest.save()
    except Exception as e:
        print_message('Error finding latest script {}'.format(script_name), 'error')
        print_debug(e)
        return HttpResponse(status=500)

    script_path = run_directory + '/' + script_name
    if not os.path.exists(script_path + '_' + str(latest.version - 1)):
        print_message('Run script {} cannot be updated as it doesn\'t exist'.format(script_name), 'error')
        return HttpResponse(status=403)
    script_path = script_path + '_' + str(latest.version)
    try:
        f = open(script_path, 'w+')
        f.write(contents)
        f.close()
    except Exception as e:
        print_message('Error writing script to file {}'.format, 'error')
        print_debug(e)
        return HttpResponse(status=500)

    return HttpResponse()
示例#16
0
def credential_check_existance(request):
    if request.method == 'POST':
        try:
            service = json.loads(request.body)['service']
            cred = Credential.objects.filter(
                site_user_name=request.user, service=service)
            if len(cred) != 0:
                return HttpResponse(status=200)
            else:
                return HttpResponse(status=500)
        except Exception as e:
            print_debug(e)
            return HttpResponse(status=500)
    else:
        return HttpResponse(status=404)
示例#17
0
def set_favorite_plot(request):
    user = str(request.user)
    data = json.loads(request.body)
    plot = data.get('plot')
    if not plot:
        print_message('no plot given')
        return HttpResponse(status=400)

    new_fav = FavoritePlot(user=user, plot=plot)
    try:
        new_fav.save()
    except Exception as e:
        print_debug(e)
        return HttpResponse(status=500)
    return HttpResponse()
示例#18
0
def get_job(job_id):
    if not job_id or not job_id.isdigit():
        return HttpResponse(status=400)
    try:
        data = UserRuns.objects.get(id=job_id)
        config = json.loads(data.config_options)
        obj = {
            'job_id': data.id,
            'user': data.user,
            'status': data.status,
        }
        obj.update(config)
        return JsonResponse(obj, safe=False)
    except Exception as e:
        print_debug(e)
        return JsonResponse({}, status=500)
示例#19
0
def upload_to_viewer(request):
    if request.method != 'POST':
        return HttpResponse(status=404)
    user = str(request.user)
    expected_params = [
        'run_name',
        'username',
        'password',
        'server',
    ]
    params = check_params(json.loads(request.body), expected_params)
    for p in expected_params:
        if not params[p]:
            return HttpResponse(status=400)

    run_directory = '{prefix}{user}/diagnostic_output/{run_name}/amwg/'.format(
        prefix=USER_DATA_PREFIX,
        user=user,
        run_name=params.get('run_name', 'default'))

    request = mydict()
    request.body = json.dumps({
        'user': user,
        'request': 'new',
        'run_type': 'upload_to_viewer',
        'request_attr': {
            'run_name': params['run_name'],
            'username': params['username'],
            'password': params['password'],
            'server': params['server'],
            'path': run_directory
        }
    })
    request.method = 'POST'
    try:
        r = poller_update(request)
        if(r.status_code != 200):
            print_message('Error communicating with poller')
            return HttpResponse(status=500)
        return HttpResponse(r.content)
    except Exception as e:
        print_message('Error making request to poller')
        print_debug(e)
        return HttpResponse(status=500)
    return HttpResponse(status=400)
示例#20
0
def get_provenance(request):
    user = str(request.user)
    try:
        data = json.loads(request.body)
    except Exception as e:
        raise

    file_name = data.get('file_name')
    run_name = data.get('run_name')
    run_id = data.get('run_id')
    variable = data.get('variable')
    if not file_name:
        print_message('No file name')
        return HttpResponse(status=400)
    if not run_name:
        print_message('No run_name')
        return HttpResponse(status=400)
    if not run_id:
        print_message('No run_id')
        return HttpResponse(status=400)
    if not variable:
        print_message('No variable')
        return HttpResponse(status=400)

    print_message('Looking up filepath for file={file}, user={user}, run_name={run_name}, id={id}'.format(
        file=file_name,
        user=user,
        run_name=run_name,
        id=run_id), 'ok')

    filepath = find_filepath(file_name, user, run_name, run_id)
    try:
        file = cdms2.open(filepath)
    except Exception as e:
        print_message('Error opening {file} with cdms2'.format(file=filepath))
        print_debug(e)
        return HttpResponse(status=500)

    try:
        provenance = file(variable).exportProvenance()
    except Exception as e:
        print_message('Error getting information from {file} with variable={var}'.format(file=filepath, var=variable))
        print_debug(e)
        return HttpResponse(status=500)
    return HttpResponse(json.dumps(provenance))
示例#21
0
def create_script(request):
    data = json.loads(request.body)
    script_name = data.get('script_name')
    run_name = data.get('run_name')
    contents = data.get('contents')
    user = str(request.user)
    if not script_name:
        print_message('No script name given', 'error')
        return HttpResponse(status=400)
    if not run_name:
        print_message('No run name given', 'error')
        return HttpResponse(status=400)
    if not contents:
        print_message('No contents given', 'error')
        return HttpResponse(status=400)

    path = os.path.abspath(os.path.dirname(__file__))
    run_directory = path + RUN_SCRIPT_PATH + user + '/' + run_name
    if not os.path.exists(run_directory):
        print_message('Run directory not found {}'.format(run_directory), 'error')
        return HttpResponse(status=400)

    script_path = run_directory + '/' + script_name + '_1'
    if os.path.exists(script_path):
        print_message('Attempting to overwrite script {}'.format(script_path), 'error')
        return HttpResponse(status=403)

    try:
        newScript = RunScript(user=user, version=1, name=script_name, run=run_name)
        newScript.save()
    except Exception as e:
        print_message('Error saving model for script {}'.format(script_name), 'error')
        print_debug(e)
        return HttpResponse(status=500)

    try:
        f = open( script_path, 'w+')
        f.write(contents)
        f.close()
    except Exception as e:
        print_message('Error writing script to file {}'.format, 'error')
        print_debug(e)
        return HttpResponse(status=500)

    return HttpResponse()
示例#22
0
def copy_template(request):
    data = json.loads(request.body)
    user = str(request.user)
    template = data.get('template')
    new_template = data.get('new_template')
    if not template:
        print_message('No template name given', 'error')
        return HttpResponse(status=400)
    if not new_template:
        print_message('No new template name given', 'error')
        return HttpResponse(status=400)

    if user in template:
        template_search_dirs = [user]
    else:
        template_search_dirs = [user, 'global']

    path = os.path.abspath(os.path.dirname(__file__))
    template_directory = path + '/resources/'
    template = template.split('/')[-1]
    found_template = False
    template_path = False
    template_search_dirs = [ str(template_directory + x) for x in template_search_dirs]
    for directory in template_search_dirs:
        if os.path.exists(directory):
            if template in os.listdir(directory):
                found_template = True
                template_path = directory + '/' + template
        else:
            os.mkdir(directory)

    if found_template:
        try:
            print_message('copying {} to {}'.format(template_path, template_directory + user + '/' + template), 'ok')
            shutil.copy(template_path, template_directory + user + '/' + new_template)
        except Exception as e:
            print_message("Error saving template {} for user {}".format(template, request.user), 'error')
            print_debug(e)
            return JsonResponse({'error': 'template not saved'})
        return JsonResponse({'template': 'template saved'})
    else:
        return JsonResponse({'error': 'template not found'})

    return HttpResponse()
示例#23
0
def save_file(request):
    if request.method == 'POST':
        try:
            incoming_file = json.loads(request.body)
            filename = incoming_file['filename']
            remote_path = incoming_file['remote_path']
            text = incoming_file['text']

            cred = Credential.objects.get(
                site_user_name=request.user, service="velo")

            local_path = os.path.join(
                os.getcwd(), 'userdata', cred.service_user_name)
            remote_path = remote_path[:remote_path.index(filename)]
            print 'filename:', filename, 'remote_path:', remote_path

            try:
                f = open(os.path.join(local_path, filename), 'w')
                f.write(text)
                f.close()
            except Exception as e:
                print_debug(e)
                print 'I/O failure when saving file for velo'
                return HttpResponse(status=500)

            data = {
                'velo_user': cred.service_user_name,
                'velo_pass': cred.password,
                'remote_path': remote_path,
                'local_path': local_path,
                'filename': filename,
                'command': 'save_file'
            }
            if velo_request(data) >= 0:
                return HttpResponse(status=200)
            else:
                print out, err
                return HttpResponse(status=500)
        except Exception as e:
            print_debug(e)
            return HttpResponse(status=500)
    else:
        return HttpResponse(status=404)
示例#24
0
def get_all(user=None):
    if user:
        data = UserRuns.objects.filter(user=user)
    else:
        data = UserRuns.objects.all()
    return data

    try:
        runs = UserRuns.objects.filter(status='new')
        if len(runs) == 0:
            return {}
        data = runs.earliest()
        data.status = 'in_progress'
        data.save()
    except Exception as e:
        print_message('Error getting next job from database')
        print_debug(e)
        return {}
    if not data:
        return {}

    config = json.loads(data.config_options)
    r = {
        'timestamp': datetime.datetime.now().strftime(timeformat),
        'job_id': data.id,
        'user': data.user
    }
    r.update(config)
    group_job_update(data.id, data.user, 'in_progress', optional_message=r)
    try:
        note = Notification.objects.get(user=data.user)
        new_notification = json.dumps({
            'job_id': data.get('id'),
            'run_type': data.get('run_type'),
            'optional_message': r,
            'status': data.get('status'),
            'timestamp': datetime.datetime.now().strftime(timeformat)
        })
        note.notification_list += new_notification + ' -|- '
        note.save()
    except Exception, e:
        raise
示例#25
0
def create_run(request):
    print request.body
    data = json.loads(request.body)
    user = str(request.user)

    new_run = data.get('run_name')
    if not new_run:
        print_message('No new run_name given', 'error')
        return HttpResponse(status=400)

    run_type = data.get('run_type')
    if not run_type:
        print_message('No run_type specied', 'error')
        return HttpResponse(status=400)

    if run_type == 'diagnostic':
        # Check to see if this config already exists
        try:
            conf = DiagnosticConfig.objects.get(user=user, name=new_run)
        except Exception as e:
            pass
        else:
            print_message('Run config already exists')
            return HttpResponse(status=409)

        # create diag config object
        conf = DiagnosticConfig(
            user=user,
            name=new_run)
        try:
            conf.save()
        except Exception as e:
            print_message('Error saving diagnostic config')
            print_debug(e)
            return HttpResponse(status=500)
    elif run_type == 'model':
        # create model config object
        print_message('Got a model create request')
    else:
        print_message('Unrecognized run_type {}'.format(run_type))
        return HttpResponse(status=400)
    return HttpResponse()
示例#26
0
def run_status(request):
    user = str(request.user)
    params = {'user': user, 'request': 'all', 'method': 'GET'}

    params = mydict()
    params.GET = {'user': user, 'request': 'all'}
    params.method = 'GET'

    try:
        r = poller_update(params)
        # r = requests.get(url, params={'user': user, 'request': 'all'})
        if(r.status_code != 200):
            print_message('Poller error with params {}'.format(params))
            return HttpResponse(status=500)
        return HttpResponse(r.content)
    except Exception as e:
        print_message("Error getting run status with url: {}".format(url))
        print_debug(e)

    return HttpResponse(status=500)
示例#27
0
def get_variables(request):
    user = str(request.user)
    try:
        data = json.loads(request.body)
    except Exception as e:
        raise

    file_name = data.get('file_name')
    run_name = data.get('run_name')
    run_id = data.get('run_id')
    if not file_name:
        print_message('No file name')
        return HttpResponse(status=400)
    if not run_name:
        print_message('No run_name')
        return HttpResponse(status=400)
    if not run_id:
        print_message('No run_id')
        return HttpResponse(status=400)

    print_message('Looking up filepath for file={file}, user={user}, run_name={run_name}, id={id}'.format(
        file=file_name,
        user=user,
        run_name=run_name,
        id=run_id), 'ok')
    filepath = find_filepath(file_name, user, run_name, run_id)
    print_message('filepath={filepath}'.format(filepath=filepath), 'ok')
    try:
        file = cdms2.open(filepath)
    except Exception as e:
        print_message('Error opening {file} with cdms2'.format(file=filepath))
        print_debug(e)
        return HttpResponse(status=500)

    variables = []
    bounds_arrays = [a.bounds for _, a in file.axes.items() if hasattr(a, "bounds")]
    for v in file.variables:
        if v in bounds_arrays:
            continue
        variables.append(v)
    return HttpResponse(json.dumps(variables))
示例#28
0
def get_output_zip(request):
    user = str(request.user)
    run_name = request.GET.get('run_name')
    run_type = request.GET.get('run_type')

    if not run_name:
        print_message('No run name given')
        return HttpResponse(status=400)

    if not run_type:
        print_message('No run type given')
        return HttpResponse(status=400)

    if run_type == 'diagnostic':
        run_directory = DIAG_OUTPUT_PREFIX \
            + user \
            + '/diagnostic_output/' \
            + run_name
    elif run_type == 'model':
        # TODO: setup this so it works with models
        run_directory = ''
    else:
        print_message('Unrecognized run_type {}'.format(run_type))
        return HttpResponse(status=403)

    output_filename = DIAG_OUTPUT_PREFIX \
        + user + '/' \
        + 'run_archives/' \
        + run_name + '/' \
        + 'output_archive'

    if not os.path.exists(output_filename + '.tar.gz'):
        try:
            print_message('creating output archive {}'.format(output_filename))
            shutil.make_archive(output_filename, 'gztar', run_directory)
        except Exception as e:
            print_message('Failed to create zip archive {}'.format(output_filename))
            print_debug(e)

    return sendfile(request, output_filename + '.tar.gz')
示例#29
0
def delete_diagnostic_config(request):
    if request.method != 'POST':
        print_message('invalid HTTP verb')
        return HttpResponse(status=404)
    user = str(request.user)
    try:
        data = json.loads(request.body)
    except Exception as e:
        print_message('Error loading request json from body: {}'.format(request.body))
        print_debug(e)
        return HttpResponse(status=400)
    name = data.get('name')
    if not name:
        print_message('no name given in delete request')
        return HttpResponse(status=400)
    try:
        DiagnosticConfig.objects.filter(user=user, name=name).delete()
    except Exception as e:
        print_message('Error deleting diagnostic configs with name: {}'.format(name))
        print_debug(e)
        return HttpResponse(status=500)
    return HttpResponse()
示例#30
0
def get_run_output(request):
    """
    Looks up a runs output and returns it to the front end
    input: user, the user making the request
           job_id, the id of the job to get the output for
    """
    user = str(request.user)
    job_id = request.GET.get('job_id')
    if not job_id:
        print_message('No job_id in output request')
        return HttpResponse(status=400)
    try:
        job = UserRuns.objects.get(id=job_id)
    except Exception as e:
        print_debug(e)
        print_message('Error looking up job with id: {}'.format(job_id))
        return HttpResponse(status=500)

    response = {
        'output': job.output.split('u\'').pop()
    }
    return HttpResponse(json.dumps(response))