Class: Locd::Agent::Site

Inherits:
Locd::Agent show all
Defined in:
lib/locd/agent/site.rb

Overview

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.

Defined Under Namespace

Classes: Status

Constant Summary collapse

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.

Returns:

  • (String)
'127.0.0.1'.freeze
TO_H_NAMES =

Attribute / method names that Locd::Agent#to_h uses.

Returns:

  • (Hamster::SortedSet<Symbol>)
Locd::Agent::TO_H_NAMES.union [:port, :url]

Instance Attribute Summary

Attributes inherited from Locd::Agent

#path, #plist

Querying collapse

Creating Servers collapse

Instance Methods: Attribute Readers collapse

`launchctl` Interface Instance Methods collapse

Class Method Summary collapse

Methods inherited from Locd::Agent

#<=>, add, add_or_update, all, class_for, #cmd_template, #config, default_log_path, #default_log_path?, #err_path, exists?, find_all, find_all!, find_only!, from_path, get, #initialize, #label, labels, #last_exit_code, #load, #log_paths, #out_path, #pid, plist_abs_path, plists, #reload, #remove, render_cmd, resolve_log_path, #restart, #running?, #start, #stop, #stopped?, #to_h, #to_json, #to_yaml, #unload, #update, user_plist_abs_dir, #workdir

Constructor Details

This class inherits a constructor from Locd::Agent

Class Method Details

.create_plist_data(cmd_template:, label:, workdir:, log_path: nil, keep_alive: false, run_at_load: false, port: nil, bind: , open_path: '/') ⇒ Hash<String, Object>

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

Parameters:

  • port (nil | Fixnum | String) (defaults to: nil)

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

  • bind (String) (defaults to: )

    Address that the server should bind to.

Returns:

  • (Hash<String, Object>)



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/locd/agent/site.rb', line 138

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) ⇒ Boolean

See if a parsed plist looks like a site.

Parameters:

  • plist (Hash<String, Object>)

    Property list parsed by Plist.parse_xml.

Returns:

  • (Boolean)

    true if we think this plist belongs to a site.



94
95
96
# File 'lib/locd/agent/site.rb', line 94

def self.plist? plist
  !! plist.dig( Locd.config[:agent, :config_key], 'port' )
end

.portsHamster::SortedSet<Fixnum>

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

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

Returns:

  • (Hamster::SortedSet<Fixnum>)


109
110
111
# File 'lib/locd/agent/site.rb', line 109

def self.ports
  Hamster::SortedSet.new all.values.map( &:port )
end

Instance Method Details

#open_pathObject



186
187
188
# File 'lib/locd/agent/site.rb', line 186

def open_path
  config['open_path'] || '/'
end

#portInteger

Returns Port service runs on.

Returns:

  • (Integer)

    Port service runs on.



181
182
183
# File 'lib/locd/agent/site.rb', line 181

def port
  config['port']
end

#status(refresh: false) ⇒ Status

The site agent's status from parsing launchctl list.

Status is read on demand and cached on the instance.

Parameters:

  • refresh: (Boolean) (defaults to: false)

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

Returns:



213
214
215
# File 'lib/locd/agent/site.rb', line 213

def status refresh: false
  Status.new port: port, **super( refresh: refresh )
end

#urlString

Returns The URL the agent can be reached at through the proxy.

Returns:

  • (String)

    The URL the agent can be reached at through the proxy.



193
194
195
# File 'lib/locd/agent/site.rb', line 193

def url
  "http://#{ label }:#{ Locd::Proxy.port }#{ open_path }"
end