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
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
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
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
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
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
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
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)
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
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
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)
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
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"])
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}")
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
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])
def debconf_select(package, var, v_type, v_val): bash( lf("echo '{package} {var} {v_type} {v_val}' | debconf-set-selections"))
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 -"))
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"))
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 -"))
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}")')))