def edit_subgroup(group_name): access_token = get_user_access_token(session) query = {'token': access_token} if request.method == 'GET': sciences = requests.get( ciconnect_api_endpoint + '/v1alpha1/fields_of_science') sciences = sciences.json()['fields_of_science'] group = get_group_info(group_name, session) return render_template('groups_edit.html', sciences=sciences, group_name=group_name, group=group) elif request.method == 'POST': display_name = request.form['display-name'] email = request.form['email'] phone = request.form['phone'] description = request.form['description'] put_query = {"apiVersion": 'v1alpha1', 'metadata': {'display_name': display_name, 'email': email, 'phone': phone, 'description': description}} r = requests.put( ciconnect_api_endpoint + '/v1alpha1/groups/' + group_name, params=query, json=put_query) if r.status_code == requests.codes.ok: flash_message = flash_message_parser('edit_subgroup') flash(flash_message, 'success') return redirect(url_for('view_group', group_name=group_name)) else: err_message = r.json()['message'] flash('Failed to update subgroup information: {}'.format( err_message), 'warning') return redirect(url_for('edit_subgroup', group_name=group_name))
def list_clusters_dict_request(session): """ - Get Clusters and Status on SLATE """ access_token = get_user_access_token(session) query = {'token': access_token} # Sorted ordered list of clusters on slate slate_clusters = list_clusters_request() slate_clusters.sort(key=lambda e: e['metadata']['name']) multiplexJson = {} for cluster in slate_clusters: cluster_name = cluster['metadata']['name'] cluster_status_query = "/v1alpha3/clusters/" + cluster_name + "/ping?token=" + query[ 'token'] + "&cache" multiplexJson[cluster_status_query] = {"method": "GET"} # POST request for multiplex return multiplex = requests.post(slate_api_endpoint + '/v1alpha3/multiplex', params=query, json=multiplexJson) multiplex = multiplex.json() cluster_status_dict = {} for cluster in multiplex: cluster_name = cluster.split('/')[3] cluster_status_dict[cluster_name] = json.loads( multiplex[cluster]['body'])['reachable'] return slate_clusters, cluster_status_dict
def delete_instance(name): access_token = get_user_access_token(session) query = {'token': access_token} r = requests.delete(slate_api_endpoint + '/v1alpha3/instances/' + name, params=query) if r.status_code == requests.codes.ok: flash('Successfully deleted instance', 'success') else: flash('Failed to delete instance', 'warning') return redirect(url_for('list_instances'))
def edit_subgroup_requests(group_name): access_token = get_user_access_token(session) query = {'token': access_token} enclosing_group_name = '.'.join(group_name.split('.')[:-1]) if request.method == 'GET': sciences = requests.get( ciconnect_api_endpoint + '/v1alpha1/fields_of_science') sciences = sciences.json()['fields_of_science'] group = get_group_info(group_name, session) return render_template('groups_requests_edit.html', sciences=sciences, group_name=group_name, group=group) elif request.method == 'POST': name = request.form['name'] display_name = request.form['display-name'] email = request.form['email'] phone = request.form['phone'] description = request.form['description'] new_unix_name = enclosing_group_name + '.' + name if new_unix_name == group_name: put_query = {"apiVersion": 'v1alpha1', 'metadata': {'display_name': display_name, 'email': email, 'phone': phone, 'description': description}} else: put_query = {"apiVersion": 'v1alpha1', 'metadata': {'name': name, 'display_name': display_name, 'email': email, 'phone': phone, 'description': description}} r = requests.put( ciconnect_api_endpoint + '/v1alpha1/groups/' + group_name, params=query, json=put_query) enclosing_group_name = '.'.join(group_name.split('.')[:-1]) if r.status_code == requests.codes.ok: flash_message = flash_message_parser('edit_subgroup_requests') flash(flash_message, 'success') return redirect(url_for('users_groups_pending')) else: err_message = r.json()['message'] flash('Failed to edit subgroup request: {}'.format( err_message), 'warning') return redirect(url_for('edit_subgroup_requests', group_name=group_name, name=name, display_name=display_name, email=email, phone=phone, description=description))
def get_cluster_status(cluster_name): """ - Get Clusters and Status on SLATE """ access_token = get_user_access_token(session) query = {'token': access_token, 'cache': 'cache'} cluster_status_query = requests.get( slate_api_endpoint + '/v1alpha3/clusters/' + cluster_name + '/ping', params=query) cluster_status_query = cluster_status_query.json() cluster_status = cluster_status_query['reachable'] return cluster_status
def restart_instance(name): access_token = get_user_access_token(session) query = {'token': access_token} r = requests.put(slate_api_endpoint + '/v1alpha3/instances/' + name + '/restart', params=query) if r.status_code == requests.codes.ok: flash('Successfully restarted instance', 'success') else: flash('Failed to restart instance', 'warning') print(r) return redirect(url_for('view_instance', name=name))
def list_public_clusters_request(session, name): """ Request query to get public cluster's information """ access_token = get_user_access_token(session) query = {'token': access_token} # Get cluster whitelist and parse allowed groups print("Querying cluster whitelist...") whitelist = list_cluster_whitelist(name) print("Query Results: {}".format(whitelist)) allowed_groups = [item for item in whitelist['items']] # Get cluster info and parse below cluster = get_cluster_info(name, nodes=True) print("Query Results: {}".format(cluster)) if cluster == 504: cluster = {} storageClasses = {} priorityClasses = {} owningGroupEmail = '' cluster_status = "False" timeout = "true" else: # Get owning group information for contact info print("Setting owning group...") owningGroupName = cluster['metadata']['owningGroup'] print("Querying owning group info for email info...") owningGroup = requests.get(slate_api_endpoint + '/v1alpha3/groups/' + owningGroupName, params=query) print("Query Response: {}".format(owningGroup)) owningGroup = owningGroup.json() owningGroupEmail = owningGroup['metadata']['email'] storageClasses = cluster['metadata']['storageClasses'] priorityClasses = cluster['metadata']['priorityClasses'] timeout = "false" # Get Cluster status and return as string for flask template print("Querying for cluster status") cluster_status = get_cluster_status(name) print("Cluster Status Response: {}".format(cluster_status)) cluster_status = str(cluster_status) print("Timeout Status: {}".format(timeout)) return cluster, owningGroupEmail, allowed_groups, cluster_status, storageClasses, priorityClasses, timeout
def approve_subgroup(group_name, subgroup_name): access_token = get_user_access_token(session) query = {'token': access_token} if request.method == 'GET': r = requests.put( ciconnect_api_endpoint + '/v1alpha1/groups/' + group_name + '/subgroup_requests/' + subgroup_name + '/approve', params=query) if r.status_code == requests.codes.ok: flash_message = flash_message_parser('approve_subgroup') flash(flash_message, 'success') return redirect(url_for('view_group_subgroups_requests', group_name=group_name)) else: err_message = r.json()['message'] flash('Failed to approve subgroup creation: {}'.format( err_message), 'warning') return redirect(url_for('view_group_subgroups_requests', group_name=group_name))
def get_instance_container_log(name, container, lines): access_token = get_user_access_token(session) query = {'token': access_token} # Initialize instance log query for multiplex request instance_log_query = '/v1alpha3/instances/' + name + '/logs' + '?token=' + query[ 'token'] + '&container=' + container + '&max_lines=' + lines # Set up multiplex JSON multiplexJson = {instance_log_query: {"method": "GET"}} # POST request for multiplex return multiplex = requests.post(slate_api_endpoint + '/v1alpha3/multiplex', params=query, json=multiplexJson) multiplex = multiplex.json() # Parse post return for instance, instance details, and instance logs instance_log = json.loads(multiplex[instance_log_query]['body']) print("insTANCE LOGS FROM XHR: {}".format(instance_log)) return jsonify(instance_log['logs'])
def create_application(name, group_name): """ View form to install new application """ if request.method == 'GET': return render_template('applications_create_final.html', name=name, group_name=group_name, minislate_user=minislate_user) elif request.method == 'POST': access_token = get_user_access_token(session) query = {'token': access_token} group = group_name cluster = request.form["cluster"] configuration = request.form["config"] install_app = { "apiVersion": 'v1alpha3', "group": group, "cluster": cluster, "configuration": configuration } # Post query to install application config app_install = requests.post(slate_api_endpoint + '/v1alpha3/apps/' + name, params=query, json=install_app) print("APP INSTALL STATUS: {}".format(app_install)) print("APP NAME: {}".format(name)) if app_install.status_code == 200: app_id = app_install.json()['metadata']['id'] flash('You have successfully installed an application instance', 'success') return redirect(url_for('view_instance', name=app_id)) else: err_message = app_install.json()['message'] flash( 'Failed to install application instance: {}'.format( err_message), 'warning') return redirect(url_for('view_application', name=name))
def deny_subgroup(group_name, subgroup_name): access_token = get_user_access_token(session) query = {'token': access_token} if request.method == 'POST': message = request.form['denial-message'] denial_message = {'message': message} r = requests.delete( ciconnect_api_endpoint + '/v1alpha1/groups/' + group_name + '/subgroup_requests/' + subgroup_name, params=query, json=denial_message) if r.status_code == requests.codes.ok: flash_message = flash_message_parser('deny_subgroup') flash(flash_message, 'success') return redirect(url_for('view_group_subgroups_requests', group_name=group_name)) else: err_message = r.json()['message'] flash('Failed to deny subgroup request: {}'.format( err_message), 'warning') return redirect(url_for('view_group_subgroups_requests', group_name=group_name))
def remove_group_member(group_name, unix_name): if request.method == 'POST': access_token = get_user_access_token(session) query = {'token': access_token} try: message = request.form['denial-message'] denial_message = {'message': message} remove_user = requests.delete( ciconnect_api_endpoint + '/v1alpha1/groups/' + group_name + '/members/' + unix_name, params=query, json=denial_message) except: remove_user = requests.delete( ciconnect_api_endpoint + '/v1alpha1/groups/' + group_name + '/members/' + unix_name, params=query) if remove_user.status_code == requests.codes.ok: flash_message = flash_message_parser('remove_group_member') flash(flash_message, 'success') return redirect(url_for('view_group_members', group_name=group_name)) else: err_message = remove_user.json()['message'] flash('Failed to remove member from group: {}'.format( err_message), 'warning') return redirect(url_for('view_group_members', group_name=group_name))
def create_group(): """Create groups""" access_token = get_user_access_token(session) query = {'token': access_token} print(query) if request.method == 'GET': sciences = requests.get( ciconnect_api_endpoint + '/v1alpha1/fields_of_science') sciences = sciences.json()['fields_of_science'] return render_template('groups_create.html', sciences=sciences) elif request.method == 'POST': name = request.form['name'] email = request.form['email'] phone = request.form['phone'] description = request.form['description'] try: # Purpose/Field of Science for CMS will always be High Energy Physics field_of_science = request.form['field_of_science'] except: field_of_science = "High Energy Physics" put_group = {"apiVersion": 'v1alpha1', "kind": "Group", 'metadata': {'name': name, 'field_of_science': field_of_science, 'email': email, 'phone': phone, 'description': description}} create_group = requests.put( ciconnect_api_endpoint + '/v1alpha1/groups/root/subgroups/' + name, params=query, json=put_group) if create_group.status_code == requests.codes.ok: flash_message = flash_message_parser('create_group') flash(flash_message, 'success') return redirect(url_for('groups')) else: err_message = create_group.json()['message'] flash('Failed to create group: {}'.format(err_message), 'warning') return redirect(url_for('groups'))
def view_public_group(name): access_token = get_user_access_token(session) query = {'token': access_token} if request.method == 'GET': return render_template('groups_public_profile.html', name=name)
def edit_profile(unix_name): identity_id = session.get('primary_identity') query = {'token': ciconnect_api_token, 'globus_id': identity_id} user = get_user_info(session) expected_unix_name = user['metadata']['unix_name'] try: unix_name == expected_unix_name except Exception: return redirect(url_for('handle_exception', e=Exception)) if request.method == 'GET': # Get user info, pass through as args, convert to json and load input fields profile = get_user_profile(unix_name) profile = profile['metadata'] return render_template('profile_edit.html', profile=profile, unix_name=unix_name) elif request.method == 'POST': name = request.form['name'] email = request.form['email'] phone = request.form['phone-number'] institution = request.form['institution'] public_key = request.form['sshpubstring'] globus_id = session['primary_identity'] x509dn = request.form['x509dn'] access_token = get_user_access_token(session) query = {'token': access_token, 'globus_id': identity_id} # Schema and query for adding users to CI Connect DB if public_key != ' ': post_user = {"apiVersion": 'v1alpha1', 'metadata': {'name': name, 'email': email, 'phone': phone, 'institution': institution, 'public_key': public_key, 'X.509_DN': x509dn}} else: post_user = {"apiVersion": 'v1alpha1', 'metadata': {'name': name, 'email': email, 'phone': phone, 'institution': institution, 'X.509_DN': x509dn}} # PUT request to update user information r = requests.put(ciconnect_api_endpoint + '/v1alpha1/users/' + unix_name, params=query, json=post_user) session['name'] = name session['email'] = email session['phone'] = phone session['institution'] = institution flash_message = flash_message_parser('edit_profile') flash(flash_message, 'success') if 'next' in session: redirect_to = session['next'] session.pop('next') else: redirect_to = url_for('profile') return redirect(url_for('profile'))
def create_subgroup(group_name): access_token = get_user_access_token(session) query = {'token': access_token} if request.method == 'GET': sciences = requests.get( ciconnect_api_endpoint + '/v1alpha1/fields_of_science') sciences = sciences.json()['fields_of_science'] # Get group members group_members = get_group_members(group_name, session) # Return list of admins of group try: group_admins = [ member for member in group_members if member['state'] == 'admin'] except: group_admins = [] # Check if user status of root connect group specifically connect_group = session['url_host']['unix_name'] unix_name = session['unix_name'] user_status = get_user_connect_status(unix_name, connect_group) # Get group information group = get_group_info(group_name, session) return render_template('groups_create.html', sciences=sciences, group_name=group_name, group_admins=group_admins, user_status=user_status, group=group) elif request.method == 'POST': name = request.form['name'] display_name = request.form['display-name'] email = request.form['email'] phone = request.form['phone'] description = request.form['description'] try: # Purpose/Field of Science for CMS will always be High Energy Physics field_of_science = request.form['field_of_science'] except: field_of_science = "High Energy Physics" put_query = {"apiVersion": 'v1alpha1', 'metadata': {'name': name, 'display_name': display_name, 'purpose': field_of_science, 'email': email, 'phone': phone, 'description': description}} r = requests.put( ciconnect_api_endpoint + '/v1alpha1/groups/' + group_name + '/subgroup_requests/' + name, params=query, json=put_query) full_created_group_name = group_name + '.' + name # Check if user status of root connect group specifically connect_group = session['url_host']['unix_name'] unix_name = session['unix_name'] user_status = get_user_connect_status(unix_name, connect_group) if r.status_code == requests.codes.ok: if user_status == 'admin': flash_message = flash_message_parser('create_subgroup') flash(flash_message, 'success') return redirect(url_for('view_group', group_name=full_created_group_name)) else: flash( "The support team has been notified of your requested subgroup.", 'success') return redirect(url_for('users_groups_pending')) else: err_message = r.json()['message'] flash('Failed to request project creation: {}'.format( err_message), 'warning') return redirect(url_for('view_group_subgroups_requests', group_name=group_name))