class Locd::Agent::Site

An site is a {Locd::Agent} that the proxy can route HTTP requests to.

If you send an HTTP request to the proxy with the site's label as the host, the proxy will start the site if it isn't running and route the request to it's {#port}.

Combined with DNSMasq pointing the site's label to the proxy (and using the port the proxy is running on) this allows you to open the site's {#url} in the browser (or make a request to it from anything that resolves DNS properly through DNSMasq… should work fine for APIs and other HTTP services) and get through to the agent.

Constants

BIND

Address we expect servers to bind to so we can reach them. Provided during command rendering as `{bind}` so it can be used when running service commands.

For the moment at least this seems to need to be `127.0.0.1` 'cause I think ProxyMachine had trouble connecting to `localhost`, so services need to bind to it or be reachable at it.

@return [String]

TO_H_NAMES

Attribute / method names that {#to_h} uses.

@return [Hamster::SortedSet<Symbol>]

Public Class Methods

create_plist_data(cmd_template:, label:, workdir:, log_path: nil, keep_alive: false, run_at_load: false, port: nil, bind: Locd.config[:site, :bind], open_path: '/') click to toggle source

Create the `launchd` property list data for a new {Locd::Agent::Site}, which has an additional `port:` keyword versus {Locd::Agent.create_plist_data}.

@param cmd_template (see Locd::Agent.create_plist_data) @param label (see Locd::Agent.create_plist_data) @param workdir (see Locd::Agent.create_plist_data) @param log_path (see Locd::Agent.create_plist_data) @param keep_alive (see Locd::Agent.create_plist_data) @param run_at_load (see Locd::Agent.create_plist_data)

@param [nil | Fixnum | String] port

Port to run the service on. If you don't provide one, one will be
provided for you (see {Locd::Proxy.allocate_port}).

@param [String] bind

Address that the server should bind to.

@return (see Locd::Agent.create_plist_data)

Calls superclass method
# File lib/locd/agent/site.rb, line 141
def self.create_plist_data  cmd_template:,
                            label:,
                            workdir:,
                            log_path: nil,
                            keep_alive: false,
                            run_at_load: false,
                            port: nil,
                            bind: Locd.config[:site, :bind],
                            open_path: '/'
  # Allocate a port if one was not provided
  port = if port.nil?
    Locd::Proxy.allocate_port
  else
    # or just normalize to {Fixnum}
    port.to_i
  end
  
  open_path = "/#{ path }" unless open_path.start_with? '/'
  
  super cmd_template: cmd_template,
        label: label,
        workdir: workdir,
        log_path: log_path,
        keep_alive: keep_alive,
        run_at_load: run_at_load,
        # Extras specific to {Locd::Agent::Site}:
        port: port,
        bind: bind,
        open_path: open_path
end
plist?(plist) click to toggle source

See if a parsed plist looks like a site.

@param [Hash<String, Object>] plist

Property list parsed by {Plist.parse_xml}.

@return [Boolean]

`true` if we think this `plist` belongs to a site.
# File lib/locd/agent/site.rb, line 94
def self.plist? plist
  !!(
    plist.dig( Locd.config[:agent, :config_key], 'port' ) &&
    plist.dig( Locd.config[:agent, :config_key], 'is_system' ) != true
  )
end
ports() click to toggle source

Sorted set of all ports configured for installed {Locd::Agent::Site}.

Used to determine available ports to allocate when creating new services.

@return [Hamster::SortedSet<Fixnum>]

# File lib/locd/agent/site.rb, line 112
def self.ports
  Hamster::SortedSet.new all.values.map( &:port )
end

Public Instance Methods

open_path() click to toggle source
# File lib/locd/agent/site.rb, line 189
def open_path
  config['open_path'] || '/'
end
port() click to toggle source

@return [Integer]

Port service runs on.
# File lib/locd/agent/site.rb, line 184
def port
  config['port']
end
status(refresh: false) click to toggle source

The site agent's status from parsing `launchctl list`.

Status is read on demand and cached on the instance.

@param [Boolean] refresh:

When `true`, will re-read from `launchd` (and cache results)
before returning.

@return [Status]

Calls superclass method
# File lib/locd/agent/site.rb, line 216
def status refresh: false
  Status.new port: port, **super( refresh: refresh )
end
url() click to toggle source

@return [String]

The URL the agent can be reached at through the proxy.
# File lib/locd/agent/site.rb, line 196
def url
  "http://#{ label }:#{ Locd::Proxy.port }#{ open_path }"
end