Class: Textus::Step::MachineIntakeFetch

Inherits:
Fetch
  • Object
show all
Defined in:
lib/textus/init/templates/machine_intake.rb

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Fetch

required_kwargs

Methods inherited from Base

kind, required_kwargs, step_name

Instance Method Details

#call(config:, args:, caps:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/textus/init/templates/machine_intake.rb', line 12

def call(config:, args:, caps:, **)
  machine = args[:leaf_segments].first or
    raise "machines intake needs a host leaf, e.g. the 'local' in feeds.machines.local"
  spec = (config["machines"] || {}).fetch(machine) { raise "unknown machine: #{machine}" }
  unless (spec["via"] || "local").to_s == "local"
    raise "machine #{machine}: only `via: local` is scaffolded — see " \
          "docs/cookbook/environment-scan.md for the SSH (remote) fan-out"
  end

  sh    = ->(cmd) { `#{cmd}`.strip }                      # local shell-out, no network
  ver   = ->(cmd) { o = `#{cmd} 2>/dev/null`.strip; o.empty? ? nil : o } # nil if tool absent
  count = ->(cmd) { n = `#{cmd} 2>/dev/null`.strip.lines.size; n.zero? ? nil : n }
  { content: {
    # git_* describe THIS repo on the control host — only meaningful for `local`.
    "git_head"       => sh.call("git rev-parse --short HEAD 2>/dev/null"),
    "git_branch"     => sh.call("git rev-parse --abbrev-ref HEAD 2>/dev/null"),
    "git_dirty"      => !sh.call("git status --porcelain 2>/dev/null").empty?,
    "repo_root"      => sh.call("git rev-parse --show-toplevel 2>/dev/null"),
    "captured_at"    => Time.now.utc.iso8601,
    "os"             => RbConfig::CONFIG["host_os"],
    "arch"           => RbConfig::CONFIG["host_cpu"],
    "ruby_version"   => RUBY_VERSION,
    "runtimes"       => {                                 # versions only; nil when not installed
      "node"   => ver.call("node --version"),
      "python" => ver.call("python3 --version"),
      "go"     => ver.call("go version"),
    },
    "packages"       => {                                 # COUNTS only — never the list (size/secrets)
      "brew" => count.call("brew list --formula"),        # ~1-3s on macOS; runs only on fetch, amortized by the ttl rule
      "apt"  => count.call("dpkg-query -f '.\n' -W"),
    },
    "textus_version" => Textus::VERSION,
    "protocol"       => Textus::PROTOCOL,
  } }
end