def unit_show(unit, op, user=False, follow=False, # follow journal shell=None, # virtual terminal command in form like `alacritty -e` tmux_session=None # tmux session name, will not be created it does not exist ): if op not in ["status", "journal", "show"]: raise ValueError("[unit_show] invalid operation: {op}") cmd = None if op in ["status", "show"]: cmd = f"systemctl {'--user ' if user else ''}{op} {unit.split()[0]}; read" elif op == "journal": cmd = f"journalctl {'--user ' if user else ''}{'--follow ' if follow else ''}-u {unit.split()[0]}{'; read' if not follow else ''}" title = f"{op} :: {unit}" if shell: if tmux_session: tmux_create_window(f"sh -c '{cmd}'", session_name=tmux_session, window_title=title, create_if_not=False, attach=True) else: term_create_window(f"sh -c '{cmd}'", term_cmd=shell) else: show_text_dialog(cmd=cmd, title=title)
if host: host_meta = extra_hosts_data[host] host_vpn = host_meta.get("vpn", None) if host_vpn: shell_cmd(f"vpnctl --start {host_vpn}") ssh_user = host_meta.get("user", None) ssh_port = host_meta.get("port", None) cmd = f"ssh{' -l ' + ssh_user if ssh_user else ''}{' -p ' + str(ssh_port) if ssh_port else ''} {host_meta['ips'][0]}" if args.show_choices: command_choices = json.loads(r.get("net/command_choices")) choice = get_selection(command_choices, "execute", case_insensitive=True, lines=5, font="@wmFontDmenu@") if choice: cmd += f" -t '{choice}'" else: sys.exit(1) if args.ignore_tmux: term_create_window(cmd, term_cmd=["@defaultTerminal@", "-e"]) else: result = tmux_create_window(cmd, session_name=host_meta.get( "tmux", "@tmuxDefaultSession@"), window_title="ssh :: {host}") if not result: term_create_window(cmd, term_cmd=["@defaultTerminal@", "-e"])
dbms_entry = get_selection(dbms_meta.keys(), "", lines=5, font="@wmFontDmenu@") if dbms_entry: dbms_pass = None if dbms_meta[dbms_entry].get("passwordPassPath"): # using pass dbms_pass = shell_cmd( f'pass {dbms_meta[dbms_entry]["passwordPassPath"]}', split_output="\n")[0] elif dbms_meta[dbms_entry].get("password"): # password in plaintext dbms_pass = dbms_meta[dbms_entry].get("password") else: notify("[dbms]", f"No password provided for '{dbms_entry}'", urgency=URGENCY_CRITICAL, timeout=5000) sys.exit(1) host = dbms_meta[dbms_entry]["host"] dbms_vpn = extra_hosts_data[host].get("vpn", None) if dbms_vpn: shell_cmd(f"vpnctl --start {dbms_vpn}") cmd = dbms_meta[dbms_entry]["command"].replace("@passwordPlaceholder@", dbms_pass) tmux_session_name = extra_hosts_data.get(host, dict()).get( "tmux", "@tmuxDefaultSession@") tmux_create_window(cmd, tmux_session_name, window_title=dbms_entry, create_if_not=True, attach=True)
host_meta = extra_hosts_data.get(hostname, None) if not host_meta: notify("[docker]", f"Host '{hostname}' not found", urgency=URGENCY_CRITICAL, timeout=5000) sys.exit(1) if hostname == "localhost": os.environ["DOCKER_HOST"] = "unix:///var/run/docker.sock" else: os.environ["DOCKER_HOST"] = f"ssh://{hostname}" host_vpn = host_meta.get("vpn", None) if host_vpn: shell_cmd(f"vpnctl --start {host_vpn}") container_names = shell_cmd("docker ps --format '{{.Names}}'", split_output="\n") selected_container = get_selection(container_names, "container", case_insensitive=True, lines=10, font="@wmFontDmenu@") if not selected_container: sys.exit(1) get_shell_cmd = f"export DOCKER_HOST={os.environ['DOCKER_HOST']} && docker exec -it {selected_container} @defaultContainerShell@" tmux_create_window(get_shell_cmd, session_name=host_meta.get("tmux", "@tmuxDefaultSession@"), window_title=f"{selected_container} shell")
font="@wmFontDmenu@") service_status = shell_cmd(f"docker service ps {selected_service_name}", split_output="\n") if selected_mode == "status": show_text_dialog(text=service_status) elif selected_mode == "logs": service_running_tasks_items = [ task.split() for task in service_status if "Running" in task ] task_mappings = dict([(task_meta[1], task_meta[0]) for task_meta in service_running_tasks_items]) selected_task = get_selection(list(task_mappings.keys()) + [selected_service_name], "task", case_insensitive=True, lines=10, font="@wmFontDmenu@") if not selected_task: sys.exit(1) task_or_service = task_mappings.get( selected_task ) if selected_task in task_mappings else selected_service_name show_log_cmd = f"DOCKER_HOST={os.environ['DOCKER_HOST']} docker service logs --follow {task_or_service}" tmux_create_window(show_log_cmd, session_name=host_meta.get("tmux", "@tmuxDefaultSession@"), window_title=f"{selected_task} logs")
notify("[docker]", f"Host '{swarm_host}' not found", urgency=URGENCY_CRITICAL, timeout=5000) sys.exit(1) host_vpn = host_meta.get("vpn", None) if host_vpn: shell_cmd(f"vpn --start {host_vpn}") services_meta = shell_cmd("docker service ls --format '{{.Name}} | {{.Mode}} | {{.Replicas}} | {{.Image}}'", split_output="\n") selected_service_meta = get_selection_rofi(services_meta, "service") selected_service_name = selected_service_meta.split("|")[0].strip() selected_mode = get_selection_rofi(service_modes, "show") service_status = shell_cmd(f"docker service ps {selected_service_name}", split_output="\n") if selected_mode == "status": show_text_dialog(text=service_status) elif selected_mode == "logs": service_running_tasks_items = [task.split() for task in service_status if "Running" in task] task_mappings = dict([(task_meta[1], task_meta[0]) for task_meta in service_running_tasks_items]) selected_task = get_selection_rofi(list(task_mappings.keys()) + [selected_service_name], "task") if not selected_task: sys.exit(1) task_or_service = task_mappings.get(selected_task) if selected_task in task_mappings else selected_service_name show_log_cmd = f"DOCKER_HOST={os.environ['DOCKER_HOST']} docker service logs --follow {task_or_service}" tmux_create_window(show_log_cmd, session_name=host_meta.get("tmux", args.tmux_session), window_title=f"{selected_task} logs")
host_meta = extra_hosts_data[host] host_vpn = host_meta.get("vpn") if host_vpn: shell_cmd(f"vpn --start {host_vpn}") jump_hosts = list_jump_hosts(host_meta, extra_hosts_data) cmd = f"ssh {' '.join(['-J ' + format_host_meta(host_meta['host'], host_meta, terse=True) for host_meta in jump_hosts])} {format_host_meta(host, host_meta)}" if args.show_choices: command_choices = json.loads(r.get("net/command_choices")) choice = get_selection_rofi(command_choices, "execute") if choice: cmd += f" -t '{choice}'" else: sys.exit(1) if args.ignore_tmux: term_create_window(cmd, term_cmd=args.term_command) else: target_session = host_meta.get("tmux", args.tmux_session) result = tmux_create_window(cmd, session_name=target_session, window_title=f"ssh :: {host}", attach=False) if not result: notify("[sshmenu]", "error creating tmux window", urgency=URGENCY_CRITICAL) sys.exit(1) else: result = term_create_window(f"tmux attach -t {target_session}", term_cmd=args.term_command)
import redis from pystdlib import shell_cmd from pystdlib.shell import tmux_create_window from pystdlib.uishim import get_selection, notify, URGENCY_CRITICAL from pystdlib.xlib import switch_named_desktop r = redis.Redis(host='localhost', port=6379, db=0) bookmarks = json.loads(r.get("nav/bookmarks")) if not len(bookmarks): notify("[bookmarks]", "No entries", urgency=URGENCY_CRITICAL, timeout=5000) sys.exit(1) bookmark = get_selection(bookmarks.keys(), "", lines=15, font="@wmFontDmenu@") if bookmark: meta = bookmarks[bookmark] path = meta["path"] shell = meta.get("shell", None) if shell: tmux_session = meta.get("tmux", "@tmuxDefaultSession@") tmux_create_window(None, tmux_session, window_title=path.split("/")[-1], attach=True, start_directory=path) elisp_cmd = f'(dired "{path}")' emacs_cmd = f'emacsclient -c -s /run/user/1000/emacs/server -e \'{elisp_cmd}\' &' # TODO: make SPOT for socket path shell_cmd(emacs_cmd, oneshot=True)
hostnames.extend(host_list[:-1]) hostnames = sorted(list(set(hostnames))) hostname = get_selection_rofi(hostnames, "host") host_meta = extra_hosts_data.get(hostname, None) if not host_meta: notify("[docker]", f"Host '{hostname}' not found", urgency=URGENCY_CRITICAL, timeout=5000) sys.exit(1) if hostname == "localhost": os.environ["DOCKER_HOST"] = "unix:///var/run/docker.sock" else: os.environ["DOCKER_HOST"] = f"ssh://{hostname}" host_vpn = host_meta.get("vpn", None) if host_vpn: shell_cmd(f"vpn --start {host_vpn}") container_names = shell_cmd("docker ps --format '{{.Names}}'", split_output="\n") selected_container = get_selection_rofi(container_names, "container") if not selected_container: sys.exit(1) get_shell_cmd = f"export DOCKER_HOST={os.environ['DOCKER_HOST']} && docker exec -it {selected_container} {args.shell}" tmux_create_window(get_shell_cmd, session_name=host_meta.get("tmux", args.tmux_session), window_title=f"{selected_container} shell")
import json import redis r = redis.Redis(host='localhost', port=6379, db=0) bookmarks = json.loads(r.get("nav/bookmarks")) from pystdlib import shell_cmd from pystdlib.shell import tmux_create_window from pystdlib.uishim import get_selection from pystdlib.xlib import switch_named_desktop left_pane_path = get_selection(bookmarks.keys(), "left >", lines=15, font="@wmFontDmenu@") or " " right_pane_path = get_selection(bookmarks.keys(), "right >", lines=15, font="@wmFontDmenu@") or " " cmd = f"@mcCmd@ {left_pane_path} {right_pane_path}".strip(" ") tmux_create_window(cmd, "main", window_title="copier", create_if_not=True, attach=True) # shell_cmd(cmd, oneshot=True)