Advertisement
Guest User

Untitled

a guest
May 4th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """ Manage OpenVZ virtual machines
  3. """
  4.  
  5. import re
  6. import logging
  7. import os.path
  8.  
  9. log = logging.getLogger(__name__)
  10.  
  11. def _simpleret(name,result=None,changes=None,msg=None):
  12. """ Prepare a result structure.
  13. """
  14. return {
  15. "name":name,
  16. "result":result,
  17. "changes":changes,
  18. "comment":msg
  19. }
  20.  
  21. def _list():
  22. """ List all containers.
  23. """
  24. output = {}
  25.  
  26. cmd = __salt__["cmd.run"]("vzlist -a -H")
  27. for vm in cmd.split("\n"):
  28. vm_info = re.split(r"\s+", vm.strip())
  29. if len(vm_info) >= 5:
  30. vmid = int(vm_info[0])
  31. status = vm_info[2] == "running"
  32. output[vmid] = {
  33. "vmid": vmid,
  34. "status": status,
  35. "hostname": vm_info[4]
  36. }
  37. return output
  38.  
  39. def _status(vmid):
  40. """ Get the status of a specific virtual machine.
  41. """
  42. vm_list = _list()
  43. try:
  44. return vm_list[vmid]["status"]
  45. except IndexError:
  46. log.exception("Container(s) not found")
  47. return False
  48.  
  49. def _running(vmid):
  50. return _status(vmid)
  51.  
  52. def _stopped(vmid):
  53. return not _status(vmid)
  54.  
  55. def _exists(vmid):
  56. return vmid in _list()
  57.  
  58. def managed(vmid,
  59. hostname,
  60. template,
  61. diskspace,
  62. memory,
  63. swap,
  64. privvmpages,
  65. kmemsize,
  66. interfaces,
  67. features={}):
  68. if _exists(vmid):
  69. return _simpleret(vmid, True, None, "Container exists.")
  70. else:
  71. # create the machine
  72. create = "vzctl create %s --ostemplate %s --diskspace %sG --hostname %s --config ndh"
  73. create %= (
  74. vmid,
  75. template,
  76. diskspace,
  77. hostname,
  78. )
  79. __salt__["cmd.run"](create)
  80. # set everything
  81. cmd = "vzctl set %s --save " % vmid
  82. cmd += "--ram %sm --swap %sm --privvmpages %s --kmemsize %s:%s " % (memory,swap,privvmpages,kmemsize,kmemsize)
  83. cmd += " ".join([
  84. "--netif_add '%s,,,,%s'" % (ifname, bridge)
  85. for ifname, bridge in interfaces.iteritems()
  86. ])
  87. cmd += " "
  88. cmd += " ".join([
  89. "--features '%s:%s'" % (feature, "on" if value else "off")
  90. for feature, value in features.iteritems()
  91. ])
  92. __salt__["cmd.run"](cmd)
  93. return _simpleret(vmid, True, None, "Container created.")
  94.  
  95.  
  96. def running(vmid):
  97. if not _exists(vmid):
  98. log.exception("Container does not exist.")
  99. return _simpleret(vmid, False, None, "Container does not exist.")
  100. if _running(vmid):
  101. return _simpleret(vmid, True, None, "Container running.")
  102. __salt__["cmd.run"]("vzctl start %d" % vmid)
  103. if _running(vmid):
  104. return _simpleret(vmid, True, None, "Container started.")
  105. return _simpleret(vmid, False, None, "Unable to start container.")
  106.  
  107.  
  108. def cmd(vmid, cmd):
  109. if not _exists(vmid):
  110. log.exception("Container does not exist.")
  111. return _simpleret(vmid,False,None,"Container does not exist.")
  112. if _stopped(vmid):
  113. log.exception("Container is not running.")
  114. return _simpleret(vmid,False,None,"Container is not running.")
  115. return _simpleret(vmid,False,None, __salt__["cmd.run"]("vzctl exec %d %s" % (vmid, cmd)))
  116.  
  117.  
  118. def file(vmid, **kwargs):
  119. __salt__['cmd.run']("vzctl mount %d" % vmid)
  120. name = kwargs["name"]
  121. kwargs["name"] = "/var/lib/vz/root/%d%s" % (vmid, name)
  122. return __salt __["state.single"]("file.managed", **kwargs).values()[0]
  123.  
  124. def stopped(vmid):
  125. if not _exists(vmid):
  126. log.exception("Container does not exist.")
  127. return _simpleret(vmid, False, None, "Container does not exist.")
  128. if not _running(vmid):
  129. return _simpleret(vmid, True, None, "Container not running.")
  130. __salt__["cmd.run"]("vzctl stop %d" % vmid)
  131. if not _running(vmid):
  132. return _simpleret(vmid, True, None, "Container stopped.")
  133. return _simpleret(vmid, False, None, "Unable to stop container.")
  134.  
  135. def absent(vmid):
  136. if not _exists(vmid):
  137. return _simpleret(vmid, True, None, "Container does not exist.")
  138. if _running(vmid):
  139. __salt__["cmd.run"]("vzctl stop %d" % vmid)
  140. if _running(vmid):
  141. return _simpleret(vmid, False, None, "Unable to stop container.")
  142. __salt__["cmd.run"]("vzctl destroy %d" % vmid)
  143. if not _exists(vmid):
  144. return _simpleret(vmid, True, None, "Container destroyed.")
  145. return _simpleret(vmid, False, None, "Unable to destroy container.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement