__init__.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #! /usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. # vim:fenc=utf-8:
  4. import os
  5. import sys
  6. # Set the configuration PATH
  7. config_path = [
  8. os.curdir,
  9. os.path.join(os.environ.get("HOME","/"), ".config/mico"),
  10. "/etc/mico",
  11. os.path.join(os.path.dirname(__file__), "templates"),
  12. os.path.join(os.curdir, "files")
  13. ]
  14. if os.environ.get("MICO_CONFIG_PATH", None):
  15. config_path.insert(0, os.environ.get("MICO_CONFIG_PATH"))
  16. # Set the cache PATH
  17. cache_path = os.environ.get("MICO_CACHE_PATH", None) or \
  18. os.path.join(os.environ.get("HOME","/"), ".cache/mico")
  19. # Set the lib PATH
  20. lib_path = [
  21. os.path.join(os.environ.get("HOME","/"), ".local/share/mico"),
  22. "/usr/lib/mico",
  23. "/usr/local/lib/mico",
  24. "/usr/share/mico"
  25. "/usr/local/share/mico"
  26. ]
  27. if os.environ.get("MICO_LIBRARY_PATH", None):
  28. lib_path.insert(0, os.environ.get("MICO_LIBRARY_PATH"))
  29. sys.path.extend(lib_path)
  30. sys.path.extend(config_path)
  31. import __builtin__
  32. from mico.util.dicts import AutoCreatedLazyDict
  33. from fabric.api import env
  34. __builtin__.env = env
  35. __builtin__.env.custom = AutoCreatedLazyDict(env)
  36. import mico.environ
  37. import mico.hook
  38. from fabric.tasks import execute as fabric_execute
  39. def execute(action, *args, **kwargs):
  40. """Exec allows you to execute an action using fabric API, adding pre-hook
  41. and post-hook into the pipeline if required."""
  42. return fabric_execute(
  43. mico.hook.task_add_pre_run_hook( mico.hook.run_pre_hook )
  44. ( mico.hook.task_add_post_run_hook( mico.hook.run_post_hook )
  45. ( action )), *args, **kwargs )
  46. import fabric.api
  47. from mico.lib.core.sudo import is_sudo
  48. from mico.lib.core.local import is_local, run_local
  49. def run(*args, **kwargs):
  50. """A wrapper to Fabric's run/sudo commands that takes into account
  51. the mode_local.
  52. """
  53. if "force" in kwargs:
  54. force = kwargs["force"]
  55. del kwargs["force"]
  56. else:
  57. force = False
  58. if is_local():
  59. if is_sudo():
  60. kwargs.setdefault("sudo", True)
  61. _exe = run_local(*args, **kwargs)
  62. else:
  63. mico.output.debug(" ".join(args))
  64. if is_sudo():
  65. _exe = fabric.api.sudo(*args, **kwargs)
  66. else:
  67. _exe = fabric.api.run(*args, **kwargs)
  68. if _exe.return_code != 0 and not force:
  69. raise ExecutionError("%s: failed to run '%s' (retcode:%d): '%s'" % (
  70. env.host_string,
  71. " ".join(args),
  72. _exe.return_code,
  73. str(_exe)
  74. ))
  75. else:
  76. return _exe
  77. class ExecutionError(Exception):
  78. """Models an execution error"""
  79. __builtin__.run_local = run_local
  80. __builtin__.run = run
  81. from mico.decorators import async, serial, parallel, sync
  82. __builtin__.async = async
  83. __builtin__.serial = serial
  84. __builtin__.parallel = parallel
  85. __builtin__.sync = sync
  86. from mico.util.storage import FileStorage
  87. __builtin__.env.storage = FileStorage(cache_path)
  88. def save(key, data):
  89. "Save a data in the global cache catalog."
  90. env.storage[key] = data
  91. def load(key):
  92. "Get a data saved in global cache catalog."
  93. return env.storage[key]