wezterm: add scoped build domain
b972dc429a2026b83e533272a37fe1b699918f42
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: 1604373fdca3
bugabingas dorkfiles
b972dc429a2026b83e533272a37fe1b699918f42
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+201 / −4 text lines
new file mode 100644+47 / −0 text lines
local wez = require 'wezterm'local function append_all(dst, src) for _, value in ipairs(src) do table.insert(dst, value) endendlocal function scoped_domain(name, label, properties) return wez.exec_domain(name, function(cmd) local args = cmd.args or { 'nu', '--login' } local env = cmd.set_environment_variables or {} env.WEZTERM_EXEC_DOMAIN = name cmd.set_environment_variables = env local unit = 'wezterm-' .. name:gsub('[^%w_.-]', '-') .. '-' .. tostring(env.WEZTERM_PANE or os.time()) local wrapped = { 'systemd-run', '--user', '--scope', '--same-dir', '--collect', '--quiet', '--unit=' .. unit, '--description=WezTerm ' .. name, } for _, property in ipairs(properties) do table.insert(wrapped, '-p') table.insert(wrapped, property) end append_all(wrapped, args) cmd.args = wrapped return cmd end, label)endreturn function(cfg) cfg.exec_domains = { scoped_domain('scope:build', 'scope:build CPU 25% MEM max 256M', { 'CPUQuota=25%', 'MemoryMax=256M', 'MemorySwapMax=0', 'OOMPolicy=stop', }), }end+10 / −0 text lines
+6 / −0 text lines
+17 / −4 text lines
local domain_hud = function ( domain ) if not domain or domain == 'local' or domain == 'Local' then return {} end local color = palette.ui.important_local if domain:find( '^scope:', 1, false ) then color = palette.warning end return { { Foreground = { Color = color } }, { Attribute = { Intensity = 'Bold' } }, { Text = '@' .. domain .. ' ' }, { Attribute = { Intensity = 'Normal' } }, }endnew file mode 100755+79 / −0 text lines
#!/usr/bin/env nudef cgroup-path [] { open --raw /proc/self/cgroup | lines | parse --regex '^(?P<id>[^:]*):(?P<controllers>[^:]*):(?P<path>.*)$' | where id == '0' | get path.0 | str trim --left --char '/' | $'/sys/fs/cgroup/($in)'}def read-cgroup-file [cg: string, name: string] { let path = ($cg | path join $name) try { open --raw $path | str trim } catch { $'<missing: ($name)>' }}def scope-info [] { let cg = (cgroup-path) let sensitive_env_names = ( $env | transpose name value | where name =~ '(?i)(KEY|TOKEN|SECRET|PASSWORD)' | get name | sort ) { pid: $nu.pid cwd: (pwd) shell: ($env.SHELL? | default '') wezterm_pane: ($env.WEZTERM_PANE? | default '') wezterm_exec_domain: ($env.WEZTERM_EXEC_DOMAIN? | default '') cgroup: $cg memory_current: (read-cgroup-file $cg memory.current) memory_high: (read-cgroup-file $cg memory.high) memory_max: (read-cgroup-file $cg memory.max) memory_swap_max: (read-cgroup-file $cg memory.swap.max) cpu_max: (read-cgroup-file $cg cpu.max) env_count: ($env | transpose name value | length) sensitive_env_names: $sensitive_env_names }}let chunk_mib = ($env.LEAK_CHUNK_MIB? | default '16' | into int)let sleep_ms = (($env.LEAK_SLEEP_MS? | default '50' | into int) * 1ms)let force = (($env.LEAK_FORCE? | default '0') == '1')let info = (scope-info)let memory_max = $info.memory_maxlet limited = if $memory_max == 'max' { false} else { try { ($memory_max | into int) <= 1073741824 } catch { false }}print ($info | table --expand)if (not $limited) and (not $force) { print '' print 'REFUSING: memory.max is not <= 1GiB. You are not in the tiny scope.' print 'Open scope first: LEADER b, then run this script again.' print 'Override only if intentional: LEAK_FORCE=1 nu scripts/leak-memory.nu' exit 2}print ''print $'leaking ($chunk_mib) MiB every ($sleep_ms); expect cgroup OOM kill inside scope:build'mut chunks = []mut allocated = 0let chunk_size = ($chunk_mib * 1024 * 1024)loop { let chunk = (random binary $chunk_size) $chunks = ($chunks | append $chunk) $allocated = ($allocated + $chunk_mib) print $'allocated=($allocated) MiB' sleep $sleep_ms}new file mode 100755+41 / −0 text lines
#!/usr/bin/env nudef cgroup-path [] { open --raw /proc/self/cgroup | lines | parse --regex '^(?P<id>[^:]*):(?P<controllers>[^:]*):(?P<path>.*)$' | where id == '0' | get path.0 | str trim --left --char '/' | $'/sys/fs/cgroup/($in)'}def read-cgroup-file [cg: string, name: string] { let path = ($cg | path join $name) try { open --raw $path | str trim } catch { $'<missing: ($name)>' }}let cg = (cgroup-path)let sensitive_env_names = ( $env | transpose name value | where name =~ '(?i)(KEY|TOKEN|SECRET|PASSWORD)' | get name | sort){ pid: $nu.pid cwd: (pwd) shell: ($env.SHELL? | default '') wezterm_pane: ($env.WEZTERM_PANE? | default '') wezterm_exec_domain: ($env.WEZTERM_EXEC_DOMAIN? | default '') cgroup: $cg memory_current: (read-cgroup-file $cg memory.current) memory_high: (read-cgroup-file $cg memory.high) memory_max: (read-cgroup-file $cg memory.max) memory_swap_max: (read-cgroup-file $cg memory.swap.max) cpu_max: (read-cgroup-file $cg cpu.max) env_count: ($env | transpose name value | length) sensitive_env_names: $sensitive_env_names}+1 / −0 text lines