# HG changeset patch
# User yating-l
# Date 1518796633 18000
# Node ID ce4f91831680f9367393bd5cf99643fd9d5e1d13
planemo upload for repository https://github.com/Yating-L/suite_gonramp_apollo.git commit 5367a00befb467f162d1870edb91f9face72e894
diff -r 000000000000 -r ce4f91831680 README.rst
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/README.rst Fri Feb 16 10:57:13 2018 -0500
@@ -0,0 +1,64 @@
+G-OnRamp Apollo Tools
+=====================
+
+A suite of Galaxy tools for managing a local Apollo server. It includes following tools:
+
+JBrowseHub to Apollo
+--------------------
+
+This Galaxy tool is used to create or overwrite an organism on an Apollo server with a jbrowse hub created by JBrowse Archive Creator.
+
+Apollo User Manager
+-------------------
+
+This Galaxy tool is used to manage Apollo users. The currently supported operation including:
+
+ - Create a new user
+ - Delete a user
+ - Add a user to a group (If the group doesn't exist, create the group)
+ - Remove a user to a group
+
+The tool can do these operations on one student at a time. It can also do the operations on multiple students at a time by uploading a text file, which including students information.
+
+The text file can be either CSV (comma-delimited) or Tabular (tab-delimited). It should have a header line, including names for each column. Example text files:
+
+Text file for creating multiple users:
+
+.. csv-table::
+ :header: "useremail", "firstname", "lastname", "password"
+ :widths: 20, 10, 10, 10
+
+ "test1@demo.com", "test1", "demo", "1234"
+ "test2@demo.com", "test2", "demo", "1234"
+ "test3@demo.com", "test3", "demo", "1234"
+
+
+Text file for deleting multiple users:
+
+.. csv-table::
+ :header: "useremail"
+ :widths: 20
+
+ "test1@demo.com"
+ "test2@demo.com"
+ "test3@demo.com"
+
+Text file for adding multiple users to a group:
+
+.. csv-table::
+ :header: "useremail", "group"
+ :widths: 20, 20
+
+ "test1@demo.com", "Test group"
+ "test2@demo.com", "Test group"
+ "test3@demo.com", "Test group"
+
+Text file for removing multiple users to a group:
+
+.. csv-table::
+ :header: "useremail", "group"
+ :widths: 20, 20
+
+ "test1@demo.com", "Test group"
+ "test2@demo.com", "Test group"
+ "test3@demo.com", "Test group"
diff -r 000000000000 -r ce4f91831680 apollo/ApolloInstance.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/apollo/ApolloInstance.py Fri Feb 16 10:57:13 2018 -0500
@@ -0,0 +1,80 @@
+#!/usr/bin/env python
+import os
+import json
+import shutil
+import tempfile
+import logging
+import random
+import string
+from util import subtools
+from mako.lookup import TemplateLookup
+
+from ApolloOrganism import ApolloOrganism
+from ApolloUser import ApolloUser
+
+class ApolloInstance(object):
+ def __init__(self, apollo_host, apollo_admin, tool_directory):
+ self.apollo_host = apollo_host
+ self.tool_directory = tool_directory
+ self.logger = logging.getLogger(__name__)
+ self.apollo_admin = apollo_admin
+ self.apolloTemplate = self._getApolloTemplate()
+ self._arrow_init()
+
+
+ def _arrow_init(self):
+ subtools.verify_user_login(self.apollo_admin['user_email'], self.apollo_admin['password'], self.apollo_host)
+ arrow_config = tempfile.NamedTemporaryFile(bufsize=0)
+ with open(arrow_config.name, 'w') as conf:
+ htmlMakoRendered = self.apolloTemplate.render(
+ apollo_host = self.apollo_host,
+ admin_user = self.apollo_admin['user_email'],
+ admin_pw = self.apollo_admin['password']
+ )
+ conf.write(htmlMakoRendered)
+
+ home_dir = os.path.expanduser('~')
+ arrow_config_dir = os.path.join(home_dir, '.apollo-arrow.yml')
+ shutil.copyfile(arrow_config.name, arrow_config_dir)
+ self.logger.debug("Initated arrow: apollo-arrow.yml= %s", arrow_config_dir)
+
+ #TODO: Encode admin password
+ '''
+ def _generatePassword(self, length=8):
+ chars = string.digits + string.letters
+ pw = ''.join([random.choice(chars) for _ in range(length)])
+ return pw
+ '''
+
+ def _getApolloTemplate(self):
+ mylookup = TemplateLookup(directories=[os.path.join(self.tool_directory, 'templates')],
+ output_encoding='utf-8', encoding_errors='replace')
+ apolloTemplate = mylookup.get_template("apollo-arrow.yml")
+ return apolloTemplate
+
+
+ def manageApolloOrganism(self, organism_name, organism_dir, action):
+ organism = ApolloOrganism(organism_name, organism_dir)
+ if action == "add":
+ organism.addOrganism()
+ self.logger.info("Successfully add a new organism (%s) to Apollo", organism_name)
+ elif action == "overwrite":
+ organism.overwriteOrganism()
+ self.logger.info("Successfully overwrite the organism %s", organism_name)
+ else:
+ self.logger.error("Invalid operation %s", action)
+ exit(-1)
+
+ def manageApolloUser(self, operations_dictionary = dict()):
+ for operation, users_list in operations_dictionary.items():
+ apollo_user = ApolloUser(users_list)
+ if operation == "create":
+ apollo_user.createApolloUser()
+ elif operation == "delete":
+ apollo_user.deleteApolloUser()
+ elif operation == "add":
+ apollo_user.addApolloUserToGroup()
+ elif operation == "remove":
+ apollo_user.removeApolloUserFromeGroup()
+
+
diff -r 000000000000 -r ce4f91831680 apollo/ApolloOrganism.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/apollo/ApolloOrganism.py Fri Feb 16 10:57:13 2018 -0500
@@ -0,0 +1,58 @@
+#!/usr/bin/env python
+
+import json
+import logging
+from util import subtools
+
+class ApolloOrganism(object):
+
+ def __init__(self, organism_name, organism_dir):
+ self.organism_name = organism_name
+ self.organism_dir = organism_dir
+ self.logger = logging.getLogger(__name__)
+
+ def addOrganism(self):
+ exist = subtools.arrow_get_organism(self.organism_name)
+ if not exist:
+ self.logger.debug("The organism does not exist.")
+ p = subtools.arrow_add_organism(self.organism_name, self.organism_dir)
+ if not p:
+ self.logger.error("The user is not authorized to add organism")
+ exit(-1)
+ organism = json.loads(p)
+ organism_id = organism['id']
+ self.logger.debug("A new organism %s was added to Apollo instance", p)
+ return organism_id
+ else:
+ self.logger.error("The organism %s is already on Apollo instance! Rerun the tool to use a different species name or choose to overwrite the organism", self.organism_name)
+ exit(-1)
+
+ #TODO: the JSON dictionary return by deleteOrganism still contains the deleted organism. Improve the API.
+ def deleteOrganism(self):
+ organism_id = subtools.arrow_get_organism(self.organism_name)
+ if organism_id:
+ self.logger.debug("Deleting the organism %s", self.organism_name)
+ subtools.arrow_delete_organism(organism_id)
+ if not subtools.arrow_get_organism(self.organism_name):
+ self.logger.debug("Organism %s has been deleted", self.organism_name)
+ else:
+ self.logger.error("Organism %s cannot be deleted", self.organism_name)
+ exit(-1)
+ else:
+ self.logger.error("Organism %s doesn't exist", self.organism_name)
+ exit(-1)
+
+ #TODO: API update_organism not working. Improve the API to enable updating directory.
+ def overwriteOrganism(self):
+ self.deleteOrganism()
+ p = subtools.arrow_add_organism(self.organism_name, self.organism_dir)
+ if not p:
+ self.logger.error("The user is not authorized to add organism")
+ exit(-1)
+ organism = json.loads(p)
+ organism_id = organism['id']
+ self.logger.debug("A new organism %s has been added to Apollo instance", p)
+ return organism_id
+
+
+
\ No newline at end of file
diff -r 000000000000 -r ce4f91831680 apollo/ApolloOrganism.pyc
Binary file apollo/ApolloOrganism.pyc has changed
diff -r 000000000000 -r ce4f91831680 apollo/ApolloUser.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/apollo/ApolloUser.py Fri Feb 16 10:57:13 2018 -0500
@@ -0,0 +1,98 @@
+#!/usr/bin/python
+
+
+import os
+import sys
+import json
+import logging
+from util import subtools
+
+class ApolloUser(object):
+ """
+ This class is to manage Apollo users, such as create, delete, add users to a group or delete users from a group
+
+ """
+
+ def __init__(self, users_list):
+ self.users_list = users_list
+ self.logger = logging.getLogger(__name__)
+
+
+ def createApolloUser(self):
+ for user in self.users_list:
+ if user['batch'] == "false":
+ subtools.arrow_create_user(user['useremail'], user['firstname'], user['lastname'], user['password'])
+ elif user['batch'] == "true":
+ users = self.parseUserInfoFile(user['format'], user['false_path'])
+ for u in users:
+ if not 'useremail' in u:
+ self.logger.error("Cannot find useremail in the text file, make sure you use the correct header, see README file for examples.")
+ if not 'firstname' in u:
+ self.logger.error("Cannot find firstname in the text file, make sure you use the correct header, see README file for examples.")
+ if not 'lastname' in u:
+ self.logger.error("Cannot find lastname in the text file, make sure you use the correct header, see README file for examples.")
+ if not 'password' in u:
+ self.logger.error("Cannot find password in the text file, make sure you use the correct header, see README file for examples.")
+ subtools.arrow_create_user(u['useremail'], u['firstname'], u['lastname'], u['password'])
+
+
+ def parseUserInfoFile(self, file_format, filename):
+ if file_format == "tab":
+ delimiter = '\t'
+ elif file_format == "csv":
+ delimiter = ','
+ else:
+ self.logger.error("The %s format is not supported!", file_format)
+ with open(filename, 'r') as f:
+ lines = f.readlines()
+ headers = lines[0].split(delimiter)
+ users = []
+ lines = lines[1:]
+ for l in lines:
+ l = l.split(delimiter)
+ info = dict()
+ fields = len(l)
+ for i in range(fields):
+ title = headers[i].rstrip()
+ info[title] = l[i].rstrip()
+ users.append(info)
+ return users
+
+ def deleteApolloUser(self):
+ for user in self.users_list:
+ if user['batch'] == "false":
+ subtools.arrow_delete_user(user['useremail'])
+ elif user['batch'] == "true":
+ users = self.parseUserInfoFile(user['format'], user['false_path'])
+ for u in users:
+ if not 'useremail' in u:
+ self.logger.error("Cannot find useremail in the text file, make sure you use the correct header, see README file for examples.")
+ subtools.arrow_delete_user(u['useremail'])
+
+ def addApolloUserToGroup(self):
+ for user in self.users_list:
+ if user['batch'] == "false":
+ subtools.arrow_add_to_group(user['group'], user['useremail'])
+ elif user['batch'] == "true":
+ users = self.parseUserInfoFile(user['format'], user['false_path'])
+ for u in users:
+ if not 'useremail' in u:
+ self.logger.error("Cannot find useremail in the text file, make sure you use the correct header, see README file for examples.")
+ if not 'group' in u:
+ self.logger.error("Cannot find group in the text file, make sure you use the correct header, see README file for examples.")
+ subtools.arrow_add_to_group(u['group'], u['useremail'])
+
+ def removeApolloUserFromeGroup(self):
+ for user in self.users_list:
+ if user['batch'] == "false":
+ subtools.arrow_remove_from_group(user['group'], user['useremail'])
+ elif user['batch'] == "true":
+ users = self.parseUserInfoFile(user['format'], user['false_path'])
+ for u in users:
+ if not 'useremail' in u:
+ self.logger.error("Cannot find useremail in the text file, make sure you use the correct header, see README file for examples.")
+ if not 'group' in u:
+ self.logger.error("Cannot find group in the text file, make sure you use the correct header, see README file for examples.")
+ subtools.arrow_remove_from_group(u['group'], u['useremail'])
+
+
diff -r 000000000000 -r ce4f91831680 apollo/__init__.py
diff -r 000000000000 -r ce4f91831680 apollo/__init__.pyc
Binary file apollo/__init__.pyc has changed
diff -r 000000000000 -r ce4f91831680 apollo/__pycache__/test_apolloInstance.cpython-27-PYTEST.pyc
Binary file apollo/__pycache__/test_apolloInstance.cpython-27-PYTEST.pyc has changed
diff -r 000000000000 -r ce4f91831680 apolloUserManager.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/apolloUserManager.py Fri Feb 16 10:57:13 2018 -0500
@@ -0,0 +1,63 @@
+#!/usr/bin/env python
+import os
+import sys
+import argparse
+import json
+import logging
+import socket
+from apollo.ApolloInstance import ApolloInstance
+from apollo.ApolloUser import ApolloUser
+from util.Reader import Reader
+from util.Logger import Logger
+
+
+def main(argv):
+ parser = argparse.ArgumentParser(description='Upload a hub to display on Apollo.')
+ parser.add_argument('-j', '--data_json', help='JSON file containing the metadata of the inputs')
+ parser.add_argument('-o', '--output', help='HTML output')
+
+ # Get the args passed in parameter
+ args = parser.parse_args()
+ json_inputs_data = args.data_json
+ outputFile = args.output
+
+ ##Parse JSON file with Reader
+ reader = Reader(json_inputs_data)
+
+ # Begin init variables
+
+ apollo_port = reader.getPortNum()
+ apollo_host = "http://localhost:"+ apollo_port + "/apollo"
+ apollo_admin_user = reader.getAdminUser()
+ toolDirectory = reader.getToolDir()
+ extra_files_path = reader.getExtFilesPath()
+ debug_mode = reader.getDebugMode()
+ operations_dictionary = reader.getOperationList()
+
+
+
+
+ #### Logging management ####
+ # If we are in Debug mode, also print in stdout the debug dump
+ log = Logger(tool_directory=toolDirectory, debug=debug_mode, extra_files_path=extra_files_path)
+ log.setup_logging()
+
+ logging.info("#### Apollo User Manager: Start on Apollo instance: %s #### ", apollo_host)
+ logging.debug('JSON parameters: %s\n\n', json.dumps(reader.args))
+
+ # Set up apollo
+ apollo = ApolloInstance(apollo_host, apollo_admin_user, toolDirectory)
+ apollo.manageApolloUser(operations_dictionary)
+ outHtml(outputFile, apollo_host)
+ logging.info('#### Apollo User Manager: Congratulation! ####\n')
+
+def outHtml(outputFile, host_name):
+ with open(outputFile, 'w') as htmlfile:
+ htmlstr = 'The Apollo User Manager has done with operations on Apollo: '
+ jbrowse_hub = '