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.

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

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, #status, #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>)



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/locd/agent/site.rb', line 122

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.



78
79
80
# File 'lib/locd/agent/site.rb', line 78

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


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

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

Instance Method Details

#open_pathObject



170
171
172
# File 'lib/locd/agent/site.rb', line 170

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

#portFixnum

Returns Port service runs on.

Returns:

  • (Fixnum)

    Port service runs on.



165
166
167
# File 'lib/locd/agent/site.rb', line 165

def port
  config['port']
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.



177
178
179
# File 'lib/locd/agent/site.rb', line 177

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