Exemplo n.º 1
0
def systemctl_start(service):
    if not el.scb("systemctl is-active {service} || true") == "active\n":
        el.bash(lf("systemctl start {service}"))
        return True
    else:
        print(lf("{service}: OK"))
        return False
Exemplo n.º 2
0
def systemctl_enable(service):
    if service in systemctl_enabled_services():
        print(lf("{service}: OK"))
        return False
    else:
        el.bash(lf("systemctl enable {service}"))
        return True
Exemplo n.º 3
0
def patch(fname, patches):
    """Patch FNAME applying PATCHES.
    Each PATCH in PATCHES is in diff -u format.
    Each line in PATCH starts with [+- ].
    [ ] lines are the optional start and end context.
    [-] lines are expected to exist in the file and will be removed.
    [+] lines will be added to the file after [-] lines are removed.

    PATCH lines should be in contiguous order: optional start context,
    [-] lines, [+] lines, optional end context.

    If PATCH was already applied for FNAME, it will be ignored.
    PATCHES can also be a file name of a "diff -u" output.
    """
    (host, name) = parse_fname(fname)
    patches = parse_patches(patches)
    if el.file_exists_p(fname):
        txt = el.slurp(fname)
    else:
        assert not any([
            re.search("^\\-", patch, flags=re.MULTILINE) for patch in patches
        ])
        el.sc("touch {name}")
        txt = ""
    no_change = True
    for ptch in patches:
        patch_lines = el.delete("", ptch.splitlines())
        chunk_before = render_patch(patch_lines, True)
        chunk_after = render_patch(patch_lines, False)
        if chunk_before == "":
            if not re.search("^" + re.escape(chunk_after), txt, re.MULTILINE):
                no_change = False
                if not (txt == "" or txt[-1] == "\n"):
                    txt += "\n"
                txt += chunk_after + "\n"
        elif (re.search("^" + re.escape(chunk_before), txt, re.MULTILINE) and
              not re.search("^" + re.escape(chunk_after), txt, re.MULTILINE)):
            no_change = False
            txt = re.sub("^" + re.escape(chunk_before),
                         chunk_after,
                         txt,
                         flags=re.MULTILINE)
        else:
            # either already patched or fail
            assert chunk_after in txt
    if no_change:
        print(fname + ": OK")
        return False
    else:
        print(fname + ": Patch")
        with el.hostname(None):
            el.barf("/tmp/insta.txt", txt)
        if host is not None:
            cmd = lf("scp /tmp/insta.txt {host}:{name}")
        else:
            cmd = sudo(lf("cp /tmp/insta.txt {name}"), name)
        with el.hostname(None):
            bash(cmd, desc=(host, "edit " + name))
        return True
Exemplo n.º 4
0
def chown(fname, owner):
    current = sc("stat -c %U:%G {fname}")
    if current == owner:
        print(lf("{fname}: OK"))
        return False
    else:
        if el.HOST is not None:
            bash(lf("chown -R {owner} {fname}"))
        else:
            bash(lf("sudo chown -R {owner} {fname}"))
        return True
Exemplo n.º 5
0
def wget(url, download_dir="/tmp/"):
    if download_dir[:-1] == "/":
        fname = url.split("/")[-1]
        full_name = el.expand_file_name(fname, download_dir)
    else:
        full_name = download_dir
    if el.file_exists_p(full_name):
        print(lf("{full_name}: OK"))
    else:
        bash(lf("wget '{url}' -O {full_name}"))
        return full_name
Exemplo n.º 6
0
def chmod(fname, permissions):
    current = sc("stat -c %a {fname}")
    if current == permissions:
        print(lf("{fname}: OK"))
        return False
    else:
        if el.HOST is not None:
            scb("chmod {permissions} {fname}")
        else:
            cmd = sudo(lf("chmod {permissions} {fname}"), fname)
            bash(cmd)
        return True
Exemplo n.º 7
0
def cp(fr, to):
    if el.file_exists_p(to) and file_equal(fr, to):
        print(lf("{to}: OK"))
        return False
    else:
        el.sc(sudo("cp '{fr}' '{to}'", to))
        return True
Exemplo n.º 8
0
def log_file_name(base_dir, book, recipe):
    sub_dir = "_".join(el.delete("",
                                 os.path.normpath(book).split(os.sep)[:-1]))
    full_dir = el.expand_file_name(sub_dir, base_dir)
    el.make_directory(full_dir)
    ts = el.replace_regexp_in_string(" ", "_", el.timestamp())
    name = el.lf("{ts}-{recipe}.txt")
    return el.expand_file_name(name, full_dir)
Exemplo n.º 9
0
def cp(fr, to):
    fr = el.expand_file_name(fr)
    to = el.expand_file_name(to)
    if el.file_exists_p(to) and file_equal(fr, to):
        print(lf("{to}: OK"))
        return False
    else:
        el.sc(sudo("cp '{fr}' '{to}'", to))
        return True
Exemplo n.º 10
0
def echo(fr_text, to):
    if el.file_exists_p(to) and fr_text == el.slurp(to):
        print(lf("{to}: OK"))
        return False
    else:
        host = el.HOST
        with el.hostname(None):
            el.sc("echo '{fr_text}' | ssh '{host}' -T 'cat > {to}'",
                  desc=(host, "write " + to))
            return True
Exemplo n.º 11
0
def ln(fr, to):
    fr_full = el.expand_file_name(fr)
    if not el.file_exists_p(fr_full):
        raise RuntimeError("File doesn't exist", fr_full)
    to_full = el.expand_file_name(to)
    if el.file_exists_p(to_full):
        if symlink_p(to_full):
            print(lf("{to_full}: OK"))
        else:
            if file_equal(fr_full, to_full):
                print(lf("{to_full} exists, contents equal"))
            else:
                print(lf("{to_full} exists, contents NOT equal"))
    else:
        if el.HOST:
            cmd = lf("ln -s {fr} {to}")
        else:
            fr_abbr = os.path.relpath(fr_full, os.path.dirname(to))
            cmd = sudo(lf("ln -s {fr_abbr} {to_full}"), to_full)
        bash(cmd)
Exemplo n.º 12
0
def install_package(package, url=None):
    if isinstance(package, list):
        res = False
        for p in package:
            res |= install_package(p, url)
        return res
    user = sc("whoami")
    su = "" if user == "root" else "sudo "
    if el.file_exists_p("/usr/bin/dpkg"):
        if package_installed_p_dpkg(package):
            print(lf("{package}: OK"))
            return False
        else:
            if url is None:
                bash(
                    lf("{su}apt-get update && DEBIAN_FRONTEND=noninteractive {su}apt-get install -y {package}"
                       ))
            else:
                fname = wget(url)
                bash(lf("{su}dpkg -i {fname}"))
            return True
    else:
        if package_installed_p_yum(package):
            print(lf("{package}: OK"))
            return False
        else:
            if url is None:
                bash(
                    lf("{su}yum update -y && {su}yum upgrade -y && {su}yum install -y '{package}'"
                       ))
            else:
                fname = wget(url)
                bash(lf("{su}yum localinstall -y {fname}"))
            return True
Exemplo n.º 13
0
def install_root_cert(certfile, certname=None):
    """Install root certificate to certificate trust store of applications using NSS"""
    install_package("libnss3-tools")
    name_crt = el.file_name_nondirectory(certfile) if certname is None else certname
    # Firefox and Chromium
    if not el.file_exists_p("/tmp/cert9.db.done"):
        dbs = el.sc_l("find ~/ -name cert9.db 2>/dev/null || true")
        for db in dbs:
            certdir = el.file_name_directory(db)
            el.scb("certutil -A -n '{name_crt}' -t 'TCu,Cu,Tu' -i {certfile} -d sql:{certdir}")
        el.barf("/tmp/cert9.db.done", "")
    # curl
    cp_cmd = cp_host if el.HOST else cp
    fname_crt = lf("/usr/local/share/ca-certificates/{name_crt}")
    cp_cmd(certfile, fname_crt)
    chmod(fname_crt, "644")
    name_pem = el.file_name_sans_extension(name_crt) + ".pem"
    fname_pem = lf("/etc/ssl/certs/{name_pem}")
    make(fname_pem, sudo("update-ca-certificates"))
    # wget
    install_package("ca-certificates")
    patch("/etc/wgetrc", ["+ca_directory=/etc/ssl/certs"])
Exemplo n.º 14
0
def git_clone(url, target_dir, commit=None):
    target_dir = el.expand_file_name(target_dir)
    if el.file_exists_p(target_dir):
        print(lf("{target_dir}: OK"))
    else:
        gdir = el.expand_file_name(target_dir)
        pdir = el.file_name_directory(gdir)
        if not el.file_exists_p(pdir):
            el.make_directory(pdir)
        (_, gdir) = el.parse_fname(gdir)
        sc("git clone --recursive {url} {gdir}")
        if commit:
            sc("cd {gdir} && git reset --hard {commit}")
Exemplo n.º 15
0
def make(target, cmds, deps=()):
    if (el.file_exists_p(target) and \
        all([get_change_time(target) > get_change_time(dep) for dep in deps])):
        print(lf("{target}: OK"))
        return False
    else:
        if isinstance(cmds, str):
            cmds = [cmds]
        elif callable(cmds):
            cmds(target)
            return True
        fcmds = []
        for cmd in cmds:
            cmd1 = re.sub("\\$@", target, cmd)
            cmd2 = re.sub("\\$\\^", " ".join([shlex.quote(dep) for dep in deps]), cmd1)
            cmd3 = re.sub("\\$<", shlex.quote(deps[0]), cmd2) if deps else cmd2
            if el.sc_hookfn:
                el.sc_hookfn(cmd3)
            fcmds.append(cmd3)
        bash(fcmds)
        return True
Exemplo n.º 16
0
def exe_to_package(recipe, exe):
    if type(recipe) is int:
        return el.lf("dpkg -S $(which {exe})")
    elif recipe[0] == "complete":
        return el.sc("compgen -c -A file " + recipe[1])
Exemplo n.º 17
0
def debconf_select(package, var, v_type, v_val):
    bash(
        lf("echo '{package} {var} {v_type} {v_val}' | debconf-set-selections"))
Exemplo n.º 18
0
def apt_key_add(email, url):
    if grep("apt-key list", email):
        print(email + " key: OK")
    else:
        bash(lf("wget -qO - {url} | apt-key add -"))
Exemplo n.º 19
0
def barf(fname, text):
    if el.file_exists_p(fname) and text == el.slurp(fname):
        print(lf("{fname}: OK"))
    else:
        quoted_text = shlex.quote(text)
        bash(lf("echo {quoted_text} | sudo tee {fname} > /dev/null"))
Exemplo n.º 20
0
def apt_key_add(email, url):
    if grep("apt-key list", email):
        print(email + " key: OK")
    else:
        install_package("gnupg2")
        bash(lf("wget -qO - {url} | sudo apt-key add -"))
Exemplo n.º 21
0
Arquivo: self.py Projeto: zed/cook
def update(recipe):
    cook_el = el.emacs_cook_script("cook.el")
    user_p = " --user " if "/.local/" in cook_el else " "
    el.bash(el.lf("pip3 install{user_p}--upgrade pycook"))
    if "INSIDE_EMACS" in os.environ:
        el.sc(el.emacsclient_eval(el.lf('(load-file "{cook_el}")')))
Exemplo n.º 22
0
Arquivo: apt.py Projeto: zed/cook
def remove(recipe, package=None):
    if recipe == 42:
        return [el.lf("sudo apt-get remove {package}")]
    elif recipe[0] == "complete":
        return el.shell_command_to_string("dpkg --get-selections " +
                                          recipe[1] + "* | awk '{print $1}'")