Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """ Manage OpenVZ virtual machines
- """
- import re
- import logging
- import os.path
- log = logging.getLogger(__name__)
- def _simpleret(name,result=None,changes=None,msg=None):
- """ Prepare a result structure.
- """
- return {
- "name":name,
- "result":result,
- "changes":changes,
- "comment":msg
- }
- def _list():
- """ List all containers.
- """
- output = {}
- cmd = __salt__["cmd.run"]("vzlist -a -H")
- for vm in cmd.split("\n"):
- vm_info = re.split(r"\s+", vm.strip())
- if len(vm_info) >= 5:
- vmid = int(vm_info[0])
- status = vm_info[2] == "running"
- output[vmid] = {
- "vmid": vmid,
- "status": status,
- "hostname": vm_info[4]
- }
- return output
- def _status(vmid):
- """ Get the status of a specific virtual machine.
- """
- vm_list = _list()
- try:
- return vm_list[vmid]["status"]
- except IndexError:
- log.exception("Container(s) not found")
- return False
- def _running(vmid):
- return _status(vmid)
- def _stopped(vmid):
- return not _status(vmid)
- def _exists(vmid):
- return vmid in _list()
- def managed(vmid,
- hostname,
- template,
- diskspace,
- memory,
- swap,
- privvmpages,
- kmemsize,
- interfaces,
- features={}):
- if _exists(vmid):
- return _simpleret(vmid, True, None, "Container exists.")
- else:
- # create the machine
- create = "vzctl create %s --ostemplate %s --diskspace %sG --hostname %s --config ndh"
- create %= (
- vmid,
- template,
- diskspace,
- hostname,
- )
- __salt__["cmd.run"](create)
- # set everything
- cmd = "vzctl set %s --save " % vmid
- cmd += "--ram %sm --swap %sm --privvmpages %s --kmemsize %s:%s " % (memory,swap,privvmpages,kmemsize,kmemsize)
- cmd += " ".join([
- "--netif_add '%s,,,,%s'" % (ifname, bridge)
- for ifname, bridge in interfaces.iteritems()
- ])
- cmd += " "
- cmd += " ".join([
- "--features '%s:%s'" % (feature, "on" if value else "off")
- for feature, value in features.iteritems()
- ])
- __salt__["cmd.run"](cmd)
- return _simpleret(vmid, True, None, "Container created.")
- def running(vmid):
- if not _exists(vmid):
- log.exception("Container does not exist.")
- return _simpleret(vmid, False, None, "Container does not exist.")
- if _running(vmid):
- return _simpleret(vmid, True, None, "Container running.")
- __salt__["cmd.run"]("vzctl start %d" % vmid)
- if _running(vmid):
- return _simpleret(vmid, True, None, "Container started.")
- return _simpleret(vmid, False, None, "Unable to start container.")
- def cmd(vmid, cmd):
- if not _exists(vmid):
- log.exception("Container does not exist.")
- return _simpleret(vmid,False,None,"Container does not exist.")
- if _stopped(vmid):
- log.exception("Container is not running.")
- return _simpleret(vmid,False,None,"Container is not running.")
- return _simpleret(vmid,False,None, __salt__["cmd.run"]("vzctl exec %d %s" % (vmid, cmd)))
- def file(vmid, **kwargs):
- __salt__['cmd.run']("vzctl mount %d" % vmid)
- name = kwargs["name"]
- kwargs["name"] = "/var/lib/vz/root/%d%s" % (vmid, name)
- return __salt __["state.single"]("file.managed", **kwargs).values()[0]
- def stopped(vmid):
- if not _exists(vmid):
- log.exception("Container does not exist.")
- return _simpleret(vmid, False, None, "Container does not exist.")
- if not _running(vmid):
- return _simpleret(vmid, True, None, "Container not running.")
- __salt__["cmd.run"]("vzctl stop %d" % vmid)
- if not _running(vmid):
- return _simpleret(vmid, True, None, "Container stopped.")
- return _simpleret(vmid, False, None, "Unable to stop container.")
- def absent(vmid):
- if not _exists(vmid):
- return _simpleret(vmid, True, None, "Container does not exist.")
- if _running(vmid):
- __salt__["cmd.run"]("vzctl stop %d" % vmid)
- if _running(vmid):
- return _simpleret(vmid, False, None, "Unable to stop container.")
- __salt__["cmd.run"]("vzctl destroy %d" % vmid)
- if not _exists(vmid):
- return _simpleret(vmid, True, None, "Container destroyed.")
- return _simpleret(vmid, False, None, "Unable to destroy container.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement