registry.py 7.5 KB

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