Class: Yast::TarballServer

Inherits:
Object
  • Object
show all
Defined in:
lib/yast/tarball_server.rb

Overview

a webrick server which provides the source tarballs

the server handles these URL paths:

- "/archive/current.tar.gz" - the generated source code tarball
- "/servers/index.json" - index of the tarball servers running on this machine
- "/" - just a simple index page

to stop the server press Ctrl+C

Constant Summary collapse

DEFAULT_HTTP_PORT =

the default port number

8000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port = nil) ⇒ TarballServer

constructor

Parameters:

  • port (Integer, nil) (defaults to: nil)

    the port number, if nil the port will be found automatically



61
62
63
# File 'lib/yast/tarball_server.rb', line 61

def initialize(port = nil)
  @port = port || find_port
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



41
42
43
# File 'lib/yast/tarball_server.rb', line 41

def port
  @port
end

Instance Method Details

#addressesArray<String>

create all URLs valid for this machine, use all network interfaces (except the loop backs, the server will be used only from outside)

Returns:

  • (Array<String>)

    list of URLs



46
47
48
49
50
51
52
53
54
55
# File 'lib/yast/tarball_server.rb', line 46

def addresses
  # ignore the loopback addresses
  hosts = Socket.ip_address_list.reject { |a| a.ipv4_loopback? || a.ipv6_loopback? }
  # IPv6 addresses need to be closed in square brackets in URLs
  hosts.map! { |a| a.ipv6? ? "[#{a.ip_address}]" : a.ip_address.to_s }
  # include also the hostname to make it easier to write
  hostname = Socket.gethostname
  hosts << hostname if !hostname&.empty?
  hosts.map! { |h| "http://#{h}:#{port}" }
end

#startObject

start the webserver, it can be closed by pressing Ctrl+C or by sending SIGTERM signal



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/yast/tarball_server.rb', line 66

def start
  dir = File.basename(Dir.pwd)
  # change the process title so we can find the running
  # servers and their ports just by simple grepping the running processes
  Process.setproctitle("rake server (#{port},#{dir})")

  # Use "*" to bind also the IPv6 addresses
  server = WEBrick::HTTPServer.new(Port: port, BindAddress: "*")
  server.mount("/archive/current.tar.gz", TarballServlet)
  server.mount("/servers/index.json", ServersServlet)
  server.mount("/", IndexServlet)

  # stop the server when receiving a signal like Ctrl+C
  # (inspired by the "un.rb" from the Ruby stdlib)
  signals = ["TERM", "QUIT"]
  signals.concat(["HUP", "INT"]) if $stdin.tty?
  signals.each do |s|
    trap(s) { server.shutdown }
  end

  server.start
end