environ.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #! /usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. # vim:fenc=utf-8:
  4. """The environ module provides a number of environ variables, which will be
  5. used in your definitions and templates to ensure the correct way to do
  6. actions. Usual environments are related with remote host, like installed
  7. operating system, versions and so on.
  8. Also, this module provides a decorator to create new environment
  9. properties."""
  10. import sys
  11. import mico
  12. from mico.util.dicts import AttrLazyDict
  13. def environ(name):
  14. """Decorator to set new environment variables. Using this decoration you
  15. can create environment dynamic properties, for example, you can set new
  16. one environ called 'uptime' which contains the remote system uptime
  17. using the core template function 'uptime'::
  18. @environ('uptime')
  19. def _env_uptime():
  20. return mico.lib.core.uptime()
  21. Then you can use the new custom environ in the form::
  22. print env.custom.uptime
  23. Or in the old and good dictionary style::
  24. print env.custom["uptime"]
  25. """
  26. def _decorator(fn):
  27. setattr(sys.modules[__name__], name, fn)
  28. mico.env.custom[name] = fn
  29. return _decorator
  30. @environ('kernel')
  31. def _env_kernel():
  32. """Get the kernel version running in the remote host.
  33. """
  34. return mico.run("uname -s").lower()
  35. from mico.lib.core import file_exists
  36. from mico.lib.core import file_read
  37. @environ('operating_system')
  38. def _env_operation_system():
  39. """Get the Operating System of the remote host.
  40. """
  41. # TODO: Add other operating systems here...
  42. if env.custom.kernel == "linux":
  43. if file_exists("/etc/lsb-release"):
  44. return "ubuntu"
  45. if file_exists("/etc/debian_version"):
  46. return "debian"
  47. if file_exists("/etc/gentoo-release"):
  48. return "gentoo"
  49. if file_exists("/etc/fedora-release"):
  50. return "fedora"
  51. if file_exists("/etc/mandriva-release"):
  52. return "mandriva"
  53. if file_exists("/etc/mandrake-release"):
  54. return "mandrake"
  55. if file_exists("/etc/meego-release"):
  56. return "meego"
  57. if file_exists("/etc/arch-release"):
  58. return "arch"
  59. if file_exists("/etc/oracle-release"):
  60. return "oracle"
  61. if file_exists("/etc/vmware-release"):
  62. return "vmware"
  63. if file_exists("/etc/redhat-release"):
  64. dist = file_read("/etc/redhat-release").lower()
  65. if "centos" in dist:
  66. return "centos"
  67. # TODO: Add other exotic redhat based distributions
  68. else:
  69. return "redhat"
  70. if file_exists("/etc/SuSe-release"):
  71. dist = file_read("/etc/SuSe-release").lower()
  72. if "suse linux enterprise server" in dist:
  73. return "sles"
  74. if "suse linux enterprise desktop" in dist:
  75. return "sled"
  76. if "opensuse" in dist:
  77. return "opensuse"
  78. else:
  79. return "suse"
  80. if file_exists("/etc/slackware-release"):
  81. return "slackware"
  82. if file_exists("/etc/system-release"):
  83. return "amazon"
  84. else:
  85. return "unknown"
  86. from mico.lib.core import network_address
  87. from mico.lib.core import network_interfaces
  88. @environ('ipaddr')
  89. def _env_ipaddr():
  90. """Returns a dictionary with the IP address of the remote hosts,
  91. indexed by interface. For example:
  92. .. code-block:: python
  93. ips = env.cutom.ip_addr
  94. print ips['eth0']
  95. """
  96. ret = AttrLazyDict()
  97. for iface in network_interfaces():
  98. ret[iface] = network_address(iface)
  99. return ret
  100. from mico.lib.core import network_nameservers
  101. @environ('nameservers')
  102. def _env_nameservers():
  103. """Returns the nameserver configured in the remote host.
  104. """
  105. return network_nameservers()