registry.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. """
  18. Registry for the applications
  19. """
  20. import os
  21. import sys
  22. import glob
  23. import json
  24. import logging
  25. import common
  26. from common import cmp
  27. LOG = logging.getLogger(__name__)
  28. class AppRegistry(object):
  29. """
  30. Represents a registry.
  31. """
  32. def __init__(self):
  33. """Open the existing registry"""
  34. self._reg_path = os.path.join(common.HUE_APP_REG_DIR, 'app.reg')
  35. self._initialized = False
  36. self._apps = {} # Map of name -> HueApp
  37. self._open()
  38. def _open(self):
  39. """Open the registry file. May raise OSError"""
  40. if os.path.exists(self._reg_path):
  41. reg_file = open(self._reg_path)
  42. app_list = json.load(reg_file)
  43. reg_file.close()
  44. for app_json in app_list:
  45. app_json.setdefault('author', 'Unknown') # Added after 0.9
  46. app = HueApp.create(app_json)
  47. self._apps[app.name] = app
  48. self._initialized = True
  49. def _write(self, path):
  50. """Write out the registry to the given path"""
  51. outfile = open(path, 'w')
  52. json.dump(list(self._apps.values()), outfile, cls=AppJsonEncoder, indent=2)
  53. outfile.close()
  54. def contains(self, app):
  55. """Returns whether the app (of the same version) is in the registry"""
  56. try:
  57. existing = self._apps[app.name]
  58. return existing.version == app.version
  59. except KeyError:
  60. return False
  61. def register(self, app):
  62. """register(app) -> True/False"""
  63. assert self._initialized, "Registry not yet initialized"
  64. try:
  65. existing = self._apps[app.name]
  66. version_diff = common.cmp_version(existing.version, app.version)
  67. if version_diff == 0:
  68. LOG.warn('%s is already registered' % (app,))
  69. return False
  70. elif version_diff < 0:
  71. LOG.info('Upgrading %s from version %s' % (app, existing.version))
  72. elif version_diff > 0:
  73. LOG.error('A newer version (%s) of %s is already installed' % (existing.version, app))
  74. return False
  75. except KeyError:
  76. pass
  77. LOG.info('Updating registry with %s' % (app,))
  78. self._apps[app.name] = app
  79. return True
  80. def unregister(self, app_name):
  81. """unregister(app_Name) -> HueApp. May raise KeyError"""
  82. assert self._initialized, "Registry not yet initialized"
  83. app = self._apps[app_name]
  84. del self._apps[app_name]
  85. return app
  86. def get_all_apps(self):
  87. """get_all_apps() -> List of HueApp"""
  88. return list(self._apps.values())
  89. def save(self):
  90. """Save and write out the registry"""
  91. assert self._initialized, "Registry not yet initialized"
  92. self._write(self._reg_path)
  93. LOG.info('=== Saved registry at %s' % (self._reg_path,))
  94. class HueApp(object):
  95. """
  96. Represents an app.
  97. Path provided should be absolute or relative to common.APPS_ROOT
  98. """
  99. @staticmethod
  100. def create(json):
  101. return HueApp(json['name'], json['version'], json['path'], json['desc'], json['author'])
  102. def __init__(self, name, version, path, desc, author):
  103. self.name = name
  104. self.version = version
  105. self.path = path
  106. self.desc = desc
  107. self.author = author
  108. def __str__(self):
  109. return "%s v.%s" % (self.name, self.version)
  110. def __cmp__(self, other):
  111. if not isinstance(other, HueApp):
  112. raise TypeError
  113. return cmp((self.name, self.version), (other.name, other.version))
  114. @property
  115. def rel_path(self):
  116. if os.path.isabs(self.path):
  117. return os.path.relpath(self.path, common.APPS_ROOT)
  118. else:
  119. return self.path
  120. @property
  121. def abs_path(self):
  122. if not os.path.isabs(self.path):
  123. return os.path.abspath(os.path.join(common.APPS_ROOT, self.path))
  124. else:
  125. return self.path
  126. def use_rel_path(self):
  127. self.path = self.rel_path
  128. def use_abs_path(self):
  129. self.path = self.abs_path
  130. def jsonable(self):
  131. return dict(name=self.name, version=self.version, path=self.path,
  132. desc=self.desc, author=self.author)
  133. def find_ext_pys(self):
  134. """find_ext_pys() -> A list of paths for all ext-py packages"""
  135. return glob.glob(os.path.join(self.abs_path, 'ext-py', '*'))
  136. def get_conffiles(self):
  137. """get_conffiles() -> A list of config (.ini) files"""
  138. return glob.glob(os.path.join(self.abs_path, 'conf', '*.ini'))
  139. def install_conf(self):
  140. """
  141. install_conf() -> True/False
  142. Symlink the app's conf/*.ini files into the conf directory.
  143. """
  144. installed = []
  145. for target in self.get_conffiles():
  146. link_name = os.path.join(common.HUE_CONF_DIR, os.path.basename(target))
  147. # Does the link already exists?
  148. if os.path.islink(link_name):
  149. try:
  150. cur = os.readlink(link_name)
  151. if cur == target:
  152. LOG.warn("Symlink for configuration already exists: %s" % (link_name,))
  153. installed.append(link_name)
  154. continue
  155. # Remove broken link
  156. if not os.path.exists(cur):
  157. os.unlink(link_name)
  158. LOG.warn("Removing broken link: %s" % (link_name,))
  159. except OSError as ex:
  160. LOG.warn("Error checking for existing link %s: %s" % (link_name, ex))
  161. # Actually install the link
  162. try:
  163. os.symlink(target, link_name)
  164. LOG.info('Symlink config %s -> %s' % (link_name, target))
  165. installed.append(link_name)
  166. except OSError as ex:
  167. LOG.error("Failed to symlink %s to %s: %s" % (target, link_name, ex))
  168. for lnk in installed:
  169. try:
  170. os.unlink(lnk)
  171. except OSError as ex2:
  172. LOG.error("Failed to cleanup link %s: %s" % (link_name, ex2))
  173. return False
  174. return True
  175. def uninstall_conf(self):
  176. """uninstall_conf() -> True/False"""
  177. app_conf_dir = os.path.abspath(os.path.join(self.abs_path, 'conf'))
  178. if not os.path.isdir(app_conf_dir):
  179. return True
  180. # Check all symlink in the conf dir and remove any that point to this app
  181. for name in os.listdir(common.HUE_CONF_DIR):
  182. path = os.path.join(common.HUE_CONF_DIR, name)
  183. if not os.path.islink(path):
  184. continue
  185. target = os.readlink(path)
  186. target_dir = os.path.abspath(os.path.dirname(target))
  187. if target_dir == app_conf_dir:
  188. try:
  189. os.unlink(path)
  190. LOG.info('Remove config symlink %s -> %s' % (path, target))
  191. except OSError as ex:
  192. LOG.error("Failed to remove configuration link %s: %s" % (path, ex))
  193. return False
  194. return True
  195. class AppJsonEncoder(json.JSONEncoder):
  196. def __init__(self, **kwargs):
  197. json.JSONEncoder.__init__(self, **kwargs)
  198. def default(self, obj):
  199. if isinstance(obj, HueApp):
  200. return obj.jsonable()
  201. return json.JSONEncoder.default(self, obj)