Mercurial > repos > yating-l > gonramp_apollo_tools
comparison apollo/ApolloInstance.py @ 0:ce4f91831680 draft default tip
planemo upload for repository https://github.com/Yating-L/suite_gonramp_apollo.git commit 5367a00befb467f162d1870edb91f9face72e894
| author | yating-l |
|---|---|
| date | Fri, 16 Feb 2018 10:57:13 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:ce4f91831680 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 import os | |
| 3 import json | |
| 4 import shutil | |
| 5 import tempfile | |
| 6 import logging | |
| 7 import random | |
| 8 import string | |
| 9 from util import subtools | |
| 10 from mako.lookup import TemplateLookup | |
| 11 | |
| 12 from ApolloOrganism import ApolloOrganism | |
| 13 from ApolloUser import ApolloUser | |
| 14 | |
| 15 class ApolloInstance(object): | |
| 16 def __init__(self, apollo_host, apollo_admin, tool_directory): | |
| 17 self.apollo_host = apollo_host | |
| 18 self.tool_directory = tool_directory | |
| 19 self.logger = logging.getLogger(__name__) | |
| 20 self.apollo_admin = apollo_admin | |
| 21 self.apolloTemplate = self._getApolloTemplate() | |
| 22 self._arrow_init() | |
| 23 | |
| 24 | |
| 25 def _arrow_init(self): | |
| 26 subtools.verify_user_login(self.apollo_admin['user_email'], self.apollo_admin['password'], self.apollo_host) | |
| 27 arrow_config = tempfile.NamedTemporaryFile(bufsize=0) | |
| 28 with open(arrow_config.name, 'w') as conf: | |
| 29 htmlMakoRendered = self.apolloTemplate.render( | |
| 30 apollo_host = self.apollo_host, | |
| 31 admin_user = self.apollo_admin['user_email'], | |
| 32 admin_pw = self.apollo_admin['password'] | |
| 33 ) | |
| 34 conf.write(htmlMakoRendered) | |
| 35 | |
| 36 home_dir = os.path.expanduser('~') | |
| 37 arrow_config_dir = os.path.join(home_dir, '.apollo-arrow.yml') | |
| 38 shutil.copyfile(arrow_config.name, arrow_config_dir) | |
| 39 self.logger.debug("Initated arrow: apollo-arrow.yml= %s", arrow_config_dir) | |
| 40 | |
| 41 #TODO: Encode admin password | |
| 42 ''' | |
| 43 def _generatePassword(self, length=8): | |
| 44 chars = string.digits + string.letters | |
| 45 pw = ''.join([random.choice(chars) for _ in range(length)]) | |
| 46 return pw | |
| 47 ''' | |
| 48 | |
| 49 def _getApolloTemplate(self): | |
| 50 mylookup = TemplateLookup(directories=[os.path.join(self.tool_directory, 'templates')], | |
| 51 output_encoding='utf-8', encoding_errors='replace') | |
| 52 apolloTemplate = mylookup.get_template("apollo-arrow.yml") | |
| 53 return apolloTemplate | |
| 54 | |
| 55 | |
| 56 def manageApolloOrganism(self, organism_name, organism_dir, action): | |
| 57 organism = ApolloOrganism(organism_name, organism_dir) | |
| 58 if action == "add": | |
| 59 organism.addOrganism() | |
| 60 self.logger.info("Successfully add a new organism (%s) to Apollo", organism_name) | |
| 61 elif action == "overwrite": | |
| 62 organism.overwriteOrganism() | |
| 63 self.logger.info("Successfully overwrite the organism %s", organism_name) | |
| 64 else: | |
| 65 self.logger.error("Invalid operation %s", action) | |
| 66 exit(-1) | |
| 67 | |
| 68 def manageApolloUser(self, operations_dictionary = dict()): | |
| 69 for operation, users_list in operations_dictionary.items(): | |
| 70 apollo_user = ApolloUser(users_list) | |
| 71 if operation == "create": | |
| 72 apollo_user.createApolloUser() | |
| 73 elif operation == "delete": | |
| 74 apollo_user.deleteApolloUser() | |
| 75 elif operation == "add": | |
| 76 apollo_user.addApolloUserToGroup() | |
| 77 elif operation == "remove": | |
| 78 apollo_user.removeApolloUserFromeGroup() | |
| 79 | |
| 80 |
