Module: Portless::State

Defined in:
lib/portless/state.rb

Overview

The on-disk state directory (~/.rb-portless): the single source of coordination — routes, CA, and pid/port/marker files. No daemon IPC; the proxy watches routes.json and everything else is plain files. Mirrors portless's state-dir model.

Class Method Summary collapse

Class Method Details

.ca_certObject



30
# File 'lib/portless/state.rb', line 30

def ca_cert = path("ca.pem")

.ca_keyObject



31
# File 'lib/portless/state.rb', line 31

def ca_key = path("ca-key.pem")

.ca_trusted_markerObject



32
# File 'lib/portless/state.rb', line 32

def ca_trusted_marker = path("ca.trusted")

.dirObject

Read the env at call time so an override (tests, or the PORTLESS_STATE_DIR we pass to the sudo'd daemon) is always respected.



15
# File 'lib/portless/state.rb', line 15

def dir = File.expand_path(ENV["PORTLESS_STATE_DIR"] || Constants::DEFAULT_STATE_DIR)

.ensure_dir!Object



19
20
21
22
# File 'lib/portless/state.rb', line 19

def ensure_dir!
  FileUtils.mkdir_p(dir, mode: 0o755)
  dir
end

.fix_ownership(target = dir) ⇒ Object

When we wrote files while elevated (root), hand them back to the invoking user so the unprivileged CLI can still read/write routes.json. Keyed on the SUDO_UID/SUDO_GID sudo exposes. No-op when not running as root.



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/portless/state.rb', line 38

def fix_ownership(target = dir)
  return unless Process.respond_to?(:uid) && Process.uid.zero?

  uid = Integer(ENV["SUDO_UID"], exception: false)
  gid = Integer(ENV["SUDO_GID"], exception: false)
  return unless uid

  FileUtils.chown_R(uid, gid, target, force: true)
rescue StandardError
  # Best-effort; never fail a command over ownership fix-ups.
  nil
end

.host_certs_dirObject



33
# File 'lib/portless/state.rb', line 33

def host_certs_dir = path("host-certs")

.path(name) ⇒ Object



17
# File 'lib/portless/state.rb', line 17

def path(name) = File.join(dir, name)

.proxy_logObject



29
# File 'lib/portless/state.rb', line 29

def proxy_log = path("proxy.log")

.proxy_pid_fileObject



27
# File 'lib/portless/state.rb', line 27

def proxy_pid_file = path("proxy.pid")

.proxy_port_fileObject



28
# File 'lib/portless/state.rb', line 28

def proxy_port_file = path("proxy.port")

.routes_fileObject

File locations (1:1 with portless's state files).



25
# File 'lib/portless/state.rb', line 25

def routes_file = path("routes.json")

.routes_lockObject



26
# File 'lib/portless/state.rb', line 26

def routes_lock = path("routes.lock")