class OpenShift::FrontendHttpServer

Frontend Http Server

Represents the front-end HTTP server on the system.

Note: This is the Apache VirtualHost implementation; other implementations may vary.

Attributes

container_name[R]
container_uuid[R]
namespace[R]

Public Class Methods

new(container_uuid, container_name, namespace) click to toggle source
# File lib/openshift-origin-node/model/frontend_httpd.rb, line 89
def initialize(container_uuid, container_name, namespace)
  Syslog.open('openshift-origin-node', Syslog::LOG_PID, Syslog::LOG_LOCAL0) unless Syslog.opened?

  @config = OpenShift::Config.new
  @container_uuid = container_uuid
  @container_name = container_name
  @namespace = namespace
  @cloud_domain = @config.get("CLOUD_DOMAIN")
end

Public Instance Methods

add_alias(name) click to toggle source

Public: Add an alias to this namespace

Examples

add_alias("foo.example.com")
# => nil

Returns nil on Success or raises on Failure

# File lib/openshift-origin-node/model/frontend_httpd.rb, line 172
def add_alias(name)
  dname = clean_server_name(name)
  path = server_alias_path(dname)

  # Aliases must be globally unique across all gears.
  existing = server_alias_search(dname, true)
  if not existing.empty?
    raise FrontendHttpServerAliasException.new("Already exists", @container_uuid,                                                     @container_name, @namespace, dname )
  end

  File.open(path, "w") do |f|
    f.write("ServerAlias #{dname}")
    f.flush
  end

  create_routes_alias(dname)

  reload_all
end
clean_server_name(name) click to toggle source

Private: Validate the server name

The name is validated against DNS host name requirements from RFC 1123 and RFC 952. Additionally, OpenShift does not allow names/aliases to be an IP address.

# File lib/openshift-origin-node/model/frontend_httpd.rb, line 219
def clean_server_name(name)
  dname = name.downcase

  if not dname.index(%r[^0-9a-z\-.]/).nil?
    raise FrontendHttpServerNameException.new("Invalid characters", @container_uuid,                                                     @container_name, @namespace, dname )
  end

  if dname.length > 255
    raise FrontendHttpServerNameException.new("Too long", @container_uuid,                                                    @container_name, @namespace, dname )
  end

  if dname.length == 0
    raise FrontendHttpServerNameException.new("Name was blank", @container_uuid,                                                    @container_name, @namespace, dname )
  end

  if dname =~ %r^\d+\.\d+\.\d+\.\d+$/
    raise FrontendHttpServerNameException.new("IP addresses are not allowed", @container_uuid,                                                    @container_name, @namespace, dname )
  end

  return dname
end
connect(path, uri, options={}) click to toggle source

Public: Connect a path element to a back-end URI for this namespace.

Examples

connect('/', 'http://127.0.250.1:8080/')
connect('/phpmyadmin, ''http://127.0.250.2:8080/')
connect('/socket, 'http:%r/127.0.250.3:8080/', {:websocket=>1}

# => nil

Returns nil on Success or raises on Failure

# File lib/openshift-origin-node/model/frontend_httpd.rb, line 146
def connect(path, uri, options={})
  raise NotImplementedError
end
create() click to toggle source

Public: Initialize an empty configuration for this gear

Examples

create
# => nil
# directory for httpd configuration.

Returns nil on Success or raises on Failure

# File lib/openshift-origin-node/model/frontend_httpd.rb, line 108
def create
  basedir = @config.get("GEAR_BASE_DIR")

  token = "#{@container_uuid}_#{@namespace}_#{@container_name}"
  path = File.join(basedir, ".httpd.d", token)

  FileUtils.rm_rf(path) if File.exist?(path)
  FileUtils.mkdir_p(path)      
end
create_routes_alias(alias_name) click to toggle source

Create an alias routing file for the node web proxy server

# File lib/openshift-origin-node/model/frontend_httpd.rb, line 271
def create_routes_alias(alias_name)
  route_file = default_routes_path
  alias_file = File.join(File.dirname(route_file), "routes_alias-#{alias_name}.json")

  begin
    File.open(route_file, 'r') do |fin|
      File.open(alias_file, 'w') do |fout|
        fout.write(fin.read.gsub("#{@container_name}-#{@namespace}.#{@cloud_domain}", "#{alias_name}"))
      end
    end
  rescue => e
    Syslog.alert("ERROR: Failure trying to create routes alias json file: #{@uuid} Exception: #{e.inspect}")
  end
end
default_routes_path() click to toggle source

Get path to the default routes.json file created for the node web proxy

# File lib/openshift-origin-node/model/frontend_httpd.rb, line 264
def default_routes_path
  basedir = @config.get("GEAR_BASE_DIR")
  token = "#{@container_uuid}_#{@namespace}_#{@container_name}"
  File.join(basedir, '.httpd.d', token, "routes.json")
end
destroy(async=true) click to toggle source

Public: Remove the frontend httpd configuration for a gear.

Examples

destroy
# => nil

Returns nil on Success or raises on Failure

# File lib/openshift-origin-node/model/frontend_httpd.rb, line 126
def destroy(async=true)
  basedir = @config.get("GEAR_BASE_DIR")

  path = File.join(basedir, ".httpd.d", "#{container_uuid}_*")
  FileUtils.rm_rf(Dir.glob(path))

  reload_all(async)
end
disconnect(path) click to toggle source

Public: Disconnect a path element from this namespace

Examples

disconnect('/')
disconnect('/phpmyadmin)

# => nil

Returns nil on Success or raises on Failure

# File lib/openshift-origin-node/model/frontend_httpd.rb, line 160
def disconnect(path)
  raise NotImplementedError
end
reload_all(async=false) click to toggle source

Reload both Apache and the proxy server and combine output/return

# File lib/openshift-origin-node/model/frontend_httpd.rb, line 287
def reload_all(async=false)
  output = ''
  errout = ''
  retc = 0

  out, err, rc = reload_node_web_proxy
  output << out
  errout << err
  retc = rc if rc != 0

  out, err, rc = reload_httpd(async)
  output << out
  errout << err
  retc = rc if rc != 0

  return output, errout, retc
end
reload_httpd(async=false) click to toggle source

Reload the Apache configuration

# File lib/openshift-origin-node/model/frontend_httpd.rb, line 306
def reload_httpd(async=false)
  async_opt="-b" if async
  out, err, rc = shellCmd("/usr/sbin/oo-httpd-singular #{async_opt} graceful")
  Syslog.alert("ERROR: failure from oo-httpd-singular(#{rc}): #{@uuid} stdout: #{out} stderr:#{err}") unless rc == 0
  return out, err, rc
end
reload_node_web_proxy() click to toggle source

Reload the configuration of the node web proxy server

# File lib/openshift-origin-node/model/frontend_httpd.rb, line 314
def reload_node_web_proxy
  out, err, rc = shellCmd("service openshift-node-web-proxy reload")
  Syslog.alert("ERROR: failure from openshift-node-web-proxy(#{rc}): #{@uuid} stdout: #{out} stderr:#{err}") unless rc == 0
  return out, err, rc
end
remove_alias(name) click to toggle source

Public: Removes an alias from this namespace

Examples

add_alias("foo.example.com")
# => nil

Returns nil on Success or raises on Failure

# File lib/openshift-origin-node/model/frontend_httpd.rb, line 201
def remove_alias(name)
  dname = clean_server_name(name)
  routes_file_path = server_routes_alias_path(dname)

  FileUtils.rm_f(routes_file_path) if File.exist?(routes_file_path)

  server_alias_search(dname, false).each do |path|
    FileUtils.rm_f(path)
  end

  reload_all
end
server_alias_path(name) click to toggle source

Private: Return path to alias file

# File lib/openshift-origin-node/model/frontend_httpd.rb, line 246
def server_alias_path(name)
  dname = clean_server_name(name)

  basedir = @config.get("GEAR_BASE_DIR")
  token = "#{@container_uuid}_#{@namespace}_#{@container_name}"
  path = File.join(basedir, '.httpd.d', token, "server_alias-#{dname}.conf")
end
server_routes_alias_path(name) click to toggle source

Private: Return path to routes alias file name used by ws proxy server

# File lib/openshift-origin-node/model/frontend_httpd.rb, line 255
def server_routes_alias_path(name)
  dname = clean_server_name(name)

  basedir = @config.get("GEAR_BASE_DIR")
  token = "#{@container_uuid}_#{@namespace}_#{@container_name}"
  path = File.join(basedir, '.httpd.d', token, "routes_alias-#{dname}.json")
end