diff apollo/ApolloInstance.py @ 1:2ae1e96a8380 draft

planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git commit a8c47ae0025953ef398bdc689dc5df5516edf686-dirty
author yating-l
date Tue, 24 Oct 2017 18:24:40 -0400
parents bc00f5c4c59e
children 8ff4b84d709f
line wrap: on
line diff
--- a/apollo/ApolloInstance.py	Tue Oct 17 17:28:05 2017 -0400
+++ b/apollo/ApolloInstance.py	Tue Oct 24 18:24:40 2017 -0400
@@ -4,27 +4,30 @@
 import shutil
 import tempfile
 import logging
+import random
+import string
 from util import subtools
 from mako.lookup import TemplateLookup
 
 
 class ApolloInstance(object):
-    def __init__(self, apollo_host, tool_directory, user_email):
+    def __init__(self, apollo_host, apollo_admin, tool_directory):
         self.apollo_host = apollo_host
         self.tool_directory = tool_directory
-        self.default_user = user_email
         self.logger = logging.getLogger(__name__)
+        self.apollo_admin =  apollo_admin
         self.apolloTemplate = self._getApolloTemplate()
         self._arrow_init()
     
-    #TODO: Encode password
+    
     def _arrow_init(self):
+        subtools.verify_user_login(self.apollo_admin.user_email, self.apollo_admin.password)
         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.default_user,
-            admin_pw = '1234'
+            admin_user = self.apollo_admin.user_email,
+            admin_pw = self.apollo_admin.password
         )
             conf.write(htmlMakoRendered)
 
@@ -32,7 +35,14 @@
         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')],
@@ -40,17 +50,15 @@
         apolloTemplate = mylookup.get_template("apollo-arrow.yml")
         return apolloTemplate
 
-    def getHost(self):
-        return self.apollo_host
-
     def createApolloUser(self, apollo_user, admin=None):
         p = subtools.arrow_create_user(apollo_user.user_email, apollo_user.firstname, apollo_user.lastname, apollo_user.password, admin) 
         user_info = json.loads(p)
         user_id = user_info.get('userId')
         if not user_id:
             self.logger.debug("Cannot create new user: %s; The user may already exist", apollo_user.user_email)
+            subtools.verify_user_login(apollo_user.user_email, apollo_user.password)
             user_id = subtools.arrow_get_users(apollo_user.user_email)
-        self.logger.debug("Got user_id for new or existing user: user_id = %s", str(user_id))
+            self.logger.debug("Got user_id for new or existing user: user_id = %s", str(user_id))
         return user_id   
 
     def grantPermission(self, user_id, organism_id, **user_permissions):
@@ -59,13 +67,16 @@
 
     def addOrganism(self, organism_name, organism_dir):
         p = subtools.arrow_add_organism(organism_name, 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("Added new organism to Apollo instance, %s", p)
         return organism_id
 
     def loadHubToApollo(self, apollo_user, organism_name, organism_dir, admin_user=False, **user_permissions):
-        user_id = self.createApolloUser(apollo_user, admin_user)
+        #user_id = self.createApolloUser(apollo_user, admin_user)
         organism_id = self.addOrganism(organism_name, organism_dir)
-        self.grantPermission(user_id, organism_id, **user_permissions)
+        #self.grantPermission(user_id, organism_id, **user_permissions)
         self.logger.debug("Successfully load the hub to Apollo")
\ No newline at end of file