# Path: revolver/contextmanager.py
# def sudo(username=None, login=False):
# def unpatched_state():
# 
# Path: revolver/file.py
# def temp(mode=None, owner=None, group=None):
# def remove(location, recursive=False, force=True):
# def touch(location, mode=None, owner=None, group=None):
# def copy(source, destination, force=True, mode=None, owner=None, group=None):
# 
# Path: revolver/package.py
# def is_installed(name):
# def install_ppa(name):
# 
# Path: revolver/core.py
# VERSION = '0.0.5'
# def put(*args, **kwargs):
# def run(*args, **kwargs):
# def sudo(*args, **kwargs):


from revolver import contextmanager as ctx
from revolver import file, package, core
lxc.network.flags=up
"""

_NETWORK_TEMPLATE = """\
<network>
  <name>%(name)s</name>
  <bridge name="vbr-%(name)s" />
  <forward/>
  <ip address="192.168.%(subnet)d.1" netmask="255.255.255.0">
    <dhcp>
      <range start="192.168.%(subnet)d.2" end="192.168.%(subnet)d.254" />
    </dhcp>
  </ip>
</network>
"""

_DEFAULT_NAME = "lxc"
_DEFAULT_SUBNET = 42


def install(name=_DEFAULT_NAME, subnet=_DEFAULT_SUBNET, _update=True):
    packages = ["lxc", "debootstrap", "libvirt-bin"]
    if _update:
        package.install(packages)
    else:
        package.ensure(packages)

    networks = _list_networks()
    if name not in networks:
        _create_network(name, subnet)

# Path: revolver/contextmanager.py
# def sudo(username=None, login=False):
# def unpatched_state():
# 
# Path: revolver/file.py
# def temp(mode=None, owner=None, group=None):
# def remove(location, recursive=False, force=True):
# def touch(location, mode=None, owner=None, group=None):
# def copy(source, destination, force=True, mode=None, owner=None, group=None):
# 
# Path: revolver/package.py
# def is_installed(name):
# def install_ppa(name):
# 
# Path: revolver/core.py
# VERSION = '0.0.5'
# def put(*args, **kwargs):
# def run(*args, **kwargs):
# def sudo(*args, **kwargs):


# Path: revolver/tool/lxc.py
from revolver import contextmanager as ctx
from revolver import file, package, core

lxc.network.flags=up
"""

_NETWORK_TEMPLATE = """\
<network>
  <name>%(name)s</name>
  <bridge name="vbr-%(name)s" />
  <forward/>
  <ip address="192.168.%(subnet)d.1" netmask="255.255.255.0">
    <dhcp>
      <range start="192.168.%(subnet)d.2" end="192.168.%(subnet)d.254" />
    </dhcp>
  </ip>
</network>
"""

_DEFAULT_NAME = "lxc"
_DEFAULT_SUBNET = 42


def install(name=_DEFAULT_NAME, subnet=_DEFAULT_SUBNET, _update=True):
    packages = ["lxc", "debootstrap", "libvirt-bin"]
    if _update:
        package.install(packages)
    else:
        package.ensure(packages)

    networks = _list_networks()
    if name not in networks:
        _create_network(name, subnet)

        core.run("virsh net-start %s" % name)