registry.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. from builtins import object
  21. import glob
  22. import logging
  23. import os
  24. import sys
  25. import json
  26. import common
  27. from common import cmp
  28. if sys.version_info[0] > 2:
  29. from builtins import object
  30. LOG = logging.getLogger(__name__)
  31. class AppRegistry(object):
  32. """
  33. Represents a registry.
  34. """
  35. def __init__(self):
  36. """Open the existing registry"""
  37. self._reg_path = os.path.join(common.HUE_APP_REG_DIR, 'app.reg')
  38. self._initialized = False
  39. self._apps = { } # Map of name -> HueApp
  40. self._open()
  41. def _open(self):
  42. """Open the registry file. May raise OSError"""
  43. if os.path.exists(self._reg_path):
  44. if sys.version_info[0] > 2:
  45. reg_file = open(self._reg_path)
  46. else:
  47. reg_file = file(self._reg_path)
  48. app_list = json.load(reg_file)
  49. reg_file.close()
  50. for app_json in app_list:
  51. app_json.setdefault('author', 'Unknown') # Added after 0.9
  52. app = HueApp.create(app_json)
  53. self._apps[app.name] = app
  54. self._initialized = True
  55. def _write(self, path):
  56. """Write out the registry to the given path"""
  57. if sys.version_info[0] > 2:
  58. outfile = open(path, 'w')
  59. else:
  60. outfile = file(path, 'w')
  61. json.dump(list(self._apps.values()), outfile, cls=AppJsonEncoder, indent=2)
  62. outfile.close()
  63. def contains(self, app):
  64. """Returns whether the app (of the same version) is in the registry"""
  65. try:
  66. existing = self._apps[app.name]
  67. return existing.version == app.version
  68. except KeyError:
  69. return False
  70. def register(self, app):
  71. """register(app) -> True/False"""
  72. assert self._initialized, "Registry not yet initialized"
  73. try:
  74. existing = self._apps[app.name]
  75. version_diff = common.cmp_version(existing.version, app.version)
  76. if version_diff == 0:
  77. LOG.warn('%s is already registered' % (app,))
  78. return False
  79. elif version_diff < 0:
  80. LOG.info('Upgrading %s from version %s' % (app, existing.version))
  81. elif version_diff > 0:
  82. LOG.error('A newer version (%s) of %s is already installed' % (existing.version, app))
  83. return False
  84. except KeyError:
  85. pass
  86. LOG.info('Updating registry with %s' % (app,))
  87. self._apps[app.name] = app
  88. return True
  89. def unregister(self, app_name):
  90. """unregister(app_Name) -> HueApp. May raise KeyError"""
  91. assert self._initialized, "Registry not yet initialized"
  92. app = self._apps[app_name]
  93. del self._apps[app_name]
  94. return app
  95. def get_all_apps(self):
  96. """get_all_apps() -> List of HueApp"""
  97. return list(self._apps.values())
  98. def save(self):
  99. """Save and write out the registry"""
  100. assert self._initialized, "Registry not yet initialized"
  101. self._write(self._reg_path)
  102. LOG.info('=== Saved registry at %s' % (self._reg_path,))
  103. class HueApp(object):
  104. """
  105. Represents an app.
  106. Path provided should be absolute or relative to common.APPS_ROOT
  107. """
  108. @staticmethod
  109. def create(json):
  110. return HueApp(json['name'], json['version'], json['path'], json['desc'], json['author'])
  111. def __init__(self, name, version, path, desc, author):
  112. self.name = name
  113. self.version = version
  114. self.path = path
  115. self.desc = desc
  116. self.author = author
  117. def __str__(self):
  118. return "%s v.%s" % (self.name, self.version)
  119. def __cmp__(self, other):
  120. if not isinstance(other, HueApp):
  121. raise TypeError
  122. return cmp((self.name, self.version), (other.name, other.version))
  123. @property
  124. def rel_path(self):
  125. if os.path.isabs(self.path):
  126. return os.path.relpath(self.path, common.APPS_ROOT)
  127. else:
  128. return self.path
  129. @property
  130. def abs_path(self):
  131. if not os.path.isabs(self.path):
  132. return os.path.abspath(os.path.join(common.APPS_ROOT, self.path))
  133. else:
  134. return self.path
  135. def use_rel_path(self):
  136. self.path = self.rel_path
  137. def use_abs_path(self):
  138. self.path = self.abs_path
  139. def jsonable(self):
  140. return dict(name=self.name, version=self.version, path=self.path,
  141. desc=self.desc, author=self.author)
  142. def find_ext_pys(self):
  143. """find_ext_pys() -> A list of paths for all ext-py packages"""
  144. return glob.glob(os.path.join(self.abs_path, 'ext-py', '*'))
  145. def get_conffiles(self):
  146. """get_conffiles() -> A list of config (.ini) files"""
  147. return glob.glob(os.path.join(self.abs_path, 'conf', '*.ini'))
  148. def install_conf(self):
  149. """
  150. install_conf() -> True/False
  151. Symlink the app's conf/*.ini files into the conf directory.
  152. """
  153. installed = [ ]
  154. for target in self.get_conffiles():
  155. link_name = os.path.join(common.HUE_CONF_DIR, os.path.basename(target))
  156. # Does the link already exists?
  157. if os.path.islink(link_name):
  158. try:
  159. cur = os.readlink(link_name)
  160. if cur == target:
  161. LOG.warn("Symlink for configuration already exists: %s" % (link_name,))
  162. installed.append(link_name)
  163. continue
  164. # Remove broken link
  165. if not os.path.exists(cur):
  166. os.unlink(link_name)
  167. LOG.warn("Removing broken link: %s" % (link_name,))
  168. except OSError as ex:
  169. LOG.warn("Error checking for existing link %s: %s" % (link_name, ex))
  170. # Actually install the link
  171. try:
  172. os.symlink(target, link_name)
  173. LOG.info('Symlink config %s -> %s' % (link_name, target))
  174. installed.append(link_name)
  175. except OSError as ex:
  176. LOG.error("Failed to symlink %s to %s: %s" % (target, link_name, ex))
  177. for lnk in installed:
  178. try:
  179. os.unlink(lnk)
  180. except OSError as ex2:
  181. LOG.error("Failed to cleanup link %s: %s" % (link_name, ex2))
  182. return False
  183. return True
  184. def uninstall_conf(self):
  185. """uninstall_conf() -> True/False"""
  186. app_conf_dir = os.path.abspath(os.path.join(self.abs_path, 'conf'))
  187. if not os.path.isdir(app_conf_dir):
  188. return True
  189. # Check all symlink in the conf dir and remove any that point to this app
  190. for name in os.listdir(common.HUE_CONF_DIR):
  191. path = os.path.join(common.HUE_CONF_DIR, name)
  192. if not os.path.islink(path):
  193. continue
  194. target = os.readlink(path)
  195. target_dir = os.path.abspath(os.path.dirname(target))
  196. if target_dir == app_conf_dir:
  197. try:
  198. os.unlink(path)
  199. LOG.info('Remove config symlink %s -> %s' % (path, target))
  200. except OSError as ex:
  201. LOG.error("Failed to remove configuration link %s: %s" % (path, ex))
  202. return False
  203. return True
  204. class AppJsonEncoder(json.JSONEncoder):
  205. def __init__(self, **kwargs):
  206. json.JSONEncoder.__init__(self, **kwargs)
  207. def default(self, obj):
  208. if isinstance(obj, HueApp):
  209. return obj.jsonable()
  210. return json.JSONEncoder.default(self, obj)