Ejemplo n.º 1
0
def create_instance(name, template="ubuntu", config=None, autostart=False):
    """
    Creates new lxc instance.

    .. note::
        Variables for this function cannot be set using env

    :param name: Lxc instance name
    :type name: str
    :param template: Name of template
    :type template: str
    :param config: Path to additional config file
    :type config: str
    :param autostart: Should this instance be started at startup
    :type autostart: bool
    """
    opts = dict(name=name, template=template, config=config, autostart=autostart)
    if opts["config"]:
        put(opts["config"], "/tmp/lxc_config.conf")
        sudo("lxc-create -n %(name)s -t %(template)s -f /tmp/lxc_config.conf" % opts)
    else:
        sudo("lxc-create -n %(name)s -t %(template)s" % opts)

    # Change root password
    sudo("chroot /var/lib/lxc/%(name)s/rootfs/ passwd" % opts)

    if opts["autostart"]:
        toggle_bootstart(name)

    provide("virtualization.lxc.containers.%(name)s" % opts)
Ejemplo n.º 2
0
def apt_get(pkg_name, repo=None):
    """
    Install package

    :param pkg_name: Name or list of packages
    :type pkg_name: list, str
    :param repo: Optional repository to use
    :type repo: str
    """

    opts = dict(
        pkg_name = pkg_name or err("Pkg_name must be set"),
        repo = repo
    )

    if opts["repo"]:
        sudo("apt-add-repository -y %(repo)s"% opts)

    if repo:
        with settings(state_skip=False):
            sudo("apt-get update")

    if isinstance(opts["pkg_name"], basestring):
        sudo("apt-get -yq install %(pkg_name)s" % opts)
        provide("admin.packages.".join(opts["pkg_name"].split()))
    else:
        sudo("apt-get -yq install", " ".join(opts["pkg_name"]))
        provide("admin.packages.".join(opts["pkg_name"]))
Ejemplo n.º 3
0
def create_account(username, default_password=None, groups=[],
                admin=False, priv= None, pub=None):
    """
    Creates account

    .. note::
         Variables for this function cannot be set using env

    :param username: Username
    :type username: str
    :param default_password: Default password or secret
    :type default_password: str
    :param groups: List or string of comma separated groups
    :type groups: list or str
    :param priv: private key
    :type priv: str
    :param pub: public key
    :type pub: str
    """

    opts = dict(
        username=username,
        default_password=default_password,
        admin=admin,
        groups= (",".join(groups)) if not isinstance(groups,basestring) else groups,
        priv=priv,
        pub=pub
    )

    # create user
    sudo('egrep %(username)s /etc/passwd || adduser %(username)s --disabled-password --gecos ""' % opts)

    if opts["groups"]:
        sudo('usermod -a -G  %(groups)s %(username)' % opts)

    # add public key for SSH access
    if not exists('/home/%(username)s/.ssh' % opts):
        sudo('mkdir /home/%(username)s/.ssh' % opts)

    if opts['pub']:
        sudo("echo '%(pub)s' > /home/%(username)s/.ssh/authorized_keys" % opts)

    if opts['priv']:
        sudo("echo '%(priv)s' > /home/%(username)s/.ssh/id_rsa" % opts)

    if opts['admin']:
        # allow sudo for maintenance user by adding it to 'sudo' group
        sudo('usermod -a -G sudo %(username)s' % opts)

    # set default password for initial login
    sudo('echo "%(username)s:%(default_password)s" | chpasswd' % opts)

    provide("account.%(username)s" % opts)
Ejemplo n.º 4
0
def install_instance(path, name):
    """Installs instance from already created instance archive"""
    if path.endswith("tar.gz"):
        put(path, "/tmp/container.tar.gz")
    else:
        spath = os.path.split(path)
        with cd(spath[0]):
            local("tar -cpvzf /tmp/container.tar.gz %s" % spath[1], use_sudo=True)
        put("/tmp/container.tar.gz", "/tmp/container.tar.gz")

    sudo("mv /tmp/container.tar.gz /var/lib/lxc")
    with cd("/var/lib/lxc"):
        sudo("tar -xvf container.tar.gz")
        sudo("rm container.tar.gz")

    local("rm /tmp/container.tar.gz", use_sudo=True)
    provide("virtualization.lxc.containers." + name)
Ejemplo n.º 5
0
def add_startup(service=None):
    """
    Adds service to startup

    :param service: Name of the service in /etc/init.d/
    :type service: str
    """
    opts = dict(
        service=service or err("Service must be set")
        )

    if isinstance(opts["sevice"], (tuple, list, dict, set)):
        for service in opts["service"]:
            sudo("update-rc.d %s defaults", service)
            provide("startup.%s" % service)
    else:
        sudo("update-rc.d %(service)s defaults" % opts)
        provide("startup.%(service)s" % opts)