class PublishToWeb::Directory

Attributes

config[R]
host[R]
logger[R]

Public Class Methods

new(host:, config:, logger:) click to toggle source
# File lib/publish_to_web/directory.rb, line 18
def initialize(host:, config:, logger:)
  @host   = host
  @config = config
  @logger = logger
end

Public Instance Methods

directory_public_key_sha1() click to toggle source
# File lib/publish_to_web/directory.rb, line 28
def directory_public_key_sha1
  info["pubkey_sha1"] if info
end
hardware_id() click to toggle source

Returns the stored hardware_id or generates a new one

# File lib/publish_to_web/directory.rb, line 47
def hardware_id
  config.hardware_id ||= "aal-#{SecureRandom.uuid}"
end
license_key() click to toggle source

Returns the stored license_key or requests a new one

# File lib/publish_to_web/directory.rb, line 52
def license_key
  config.license_key ||= create_license_key
end
limits() click to toggle source
# File lib/publish_to_web/directory.rb, line 115
def limits
  logger.info "Retrieving limits from directory"
  response = HTTP.get url_for('limits'), params: { license_key: license_key }
  if (200..299).include? response.status
    JSON.parse(response.body)
  else
    raise HttpResponseError.new("Failed to retrieve limits from directory", response)
  end
end
node_name() click to toggle source
# File lib/publish_to_web/directory.rb, line 32
def node_name
  info["node_name"] if info
end
private_key() click to toggle source
# File lib/publish_to_web/directory.rb, line 36
def private_key
  ensure_valid_identity!
  config.private_key
end
public_key() click to toggle source
# File lib/publish_to_web/directory.rb, line 41
def public_key
  ensure_valid_identity!
  config.public_key
end
remote_port() click to toggle source
# File lib/publish_to_web/directory.rb, line 24
def remote_port
  info["port"] if info
end
report_usage() click to toggle source
# File lib/publish_to_web/directory.rb, line 87
def report_usage
  if active_accounts = config.active_accounts
    logger.info "Reporting usage to directory"
    payload = { 
      license_key: license_key, 
      active_accounts: active_accounts
    }
    response = HTTP.post url_for('usage'), form: payload
    if (200..299).include? response.status
      true
    else
      raise HttpResponseError.new("Failed to submit usage to directory", response)
    end
  else
    logger.info "Not reporting usage as no data is available"
  end
end
set_node_name(node_name) click to toggle source
# File lib/publish_to_web/directory.rb, line 60
def set_node_name(node_name)
  logger.info "Setting node name at directory to #{node_name}"
  response = HTTP.post url_for('set_node_name'), form: { license_key: license_key, node_name: node_name }
  if (200..299).include? response.status
    logger.info "New node name registered successfully"
    info refresh: true
    true
  else
    raise HttpResponseError.new("Failed to set new node name in directory", response)
  end
end
set_version() click to toggle source
# File lib/publish_to_web/directory.rb, line 72
def set_version
  logger.info "Setting version at directory to #{version}"
  payload = { 
    license_key: license_key, 
    version: Shellwords.shellescape(version), 
    support_identifier: config.support_identifier 
  }
  response = HTTP.post url_for('set_version'), form: payload
  if (200..299).include? response.status
    true
  else
    raise HttpResponseError.new("Failed to set version in directory", response)
  end
end
smtp_config() click to toggle source
# File lib/publish_to_web/directory.rb, line 105
def smtp_config
  logger.info "Retrieving SMTP configuration from directory"
  response = HTTP.get url_for('smtp_config'), params: { license_key: license_key }
  if (200..299).include? response.status
    JSON.parse(response.body)
  else
    raise HttpResponseError.new("Failed to retrieve smtp credentials from directory", response)
  end
end
version() click to toggle source
# File lib/publish_to_web/directory.rb, line 56
def version
  config.system_version
end

Private Instance Methods

create_license_key() click to toggle source
# File lib/publish_to_web/directory.rb, line 170
def create_license_key
  logger.info "Creating a new license key in directory"
  response = HTTP.get url_for('create_license'), params: { hardware_id: hardware_id }
  if (200..299).include? response.status
    logger.info "Successfully created new license key"
    JSON.parse(response.body)["license_key"]
  else
    raise HttpResponseError.new("Failed to create license in directory", response)
  end
end
ensure_valid_identity!() click to toggle source
# File lib/publish_to_web/directory.rb, line 134
def ensure_valid_identity!
  logger.info "Checking for useable SSH key pair"
  if public_key_ok?
    logger.info "The existing SSH key pair appears to be valid"
    true
  else
    logger.info "Generating new SSH key pair"
    SSHKey.generate(type: 'rsa', bits: 4096).tap do |new_identity|
      register_identity new_identity.ssh_public_key
      info refresh: true

      config.private_key = new_identity.private_key
      config.public_key  = new_identity.ssh_public_key
    end
    true
  end
end
info(refresh: false) click to toggle source
# File lib/publish_to_web/directory.rb, line 152
def info(refresh: false)
  @info = nil if refresh

  @info ||= begin
    logger.info "Retrieving connection info from directory #{host}"
    response = HTTP.get(url_for('info'), params: { license_key: license_key })
    if response.status == 200
      data = JSON.load(response.body)
      data.each do |key, value|
        logger.info "     #{key} = #{value}"
      end
      data
    else
      raise HttpResponseError.new("Failed to get connection info from directory", response)
    end
  end
end
public_key_ok?() click to toggle source
# File lib/publish_to_web/directory.rb, line 127
def public_key_ok?
  if config.public_key and config.private_key
    SSHKey.new(config.private_key).ssh_public_key == config.public_key &&
      Digest::SHA1.hexdigest(config.public_key) == directory_public_key_sha1
  end
end
register_identity(new_public_key) click to toggle source
# File lib/publish_to_web/directory.rb, line 181
def register_identity(new_public_key)
  logger.info "Registering new public key in directory"
  response = HTTP.post url_for("set_public_key"), form: { license_key: license_key, public_key: new_public_key }
  if (200..299).include? response.status
    logger.info "Successfully registered new public key in directory"
    true
  else
    raise HttpResponseError.new("Failed to register identity with directory", response)
  end
end
url_for(path) click to toggle source
# File lib/publish_to_web/directory.rb, line 192
def url_for(path)
  File.join host, path
end