Class: Baykit::BayServer::Docker::Terminal::TerminalDocker

Inherits:
Base::ClubBase
  • Object
show all
Includes:
Agent, Agent::Multiplexer, Bcf, Rudders, Tours, Util
Defined in:
lib/baykit/bayserver/docker/terminal/terminal_docker.rb

Constant Summary collapse

RACK_TERMINAL_PIPE =
"rack.terminal.pipe"
DEFAULT_POST_CACHE_THRESHOLD =

128 KB

1024 * 128
RACK_ERR =
"Cannot find rack package. If you want to use terminal docker, please install rack package like 'gem install rack'."
RACK_MULTITHREAD =
'rack.multithread'
RACK_MULTIPROCESS =
'rack.multiprocess'
RACK_RUNONCE =
'rack.run_once'
RACK_HIJACK_IO =
'rack.hijack_io'
HTTP_VERSION =
'HTTP_VERSION'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTerminalDocker

Returns a new instance of TerminalDocker.



38
39
40
41
# File 'lib/baykit/bayserver/docker/terminal/terminal_docker.rb', line 38

def initialize
  @post_cache_threshold = DEFAULT_POST_CACHE_THRESHOLD
  @available = false
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



32
33
34
# File 'lib/baykit/bayserver/docker/terminal/terminal_docker.rb', line 32

def app
  @app
end

#availableObject (readonly)

Returns the value of attribute available.



36
37
38
# File 'lib/baykit/bayserver/docker/terminal/terminal_docker.rb', line 36

def available
  @available
end

#configObject (readonly)

Returns the value of attribute config.



33
34
35
# File 'lib/baykit/bayserver/docker/terminal/terminal_docker.rb', line 33

def config
  @config
end

#environmentObject (readonly)

Returns the value of attribute environment.



34
35
36
# File 'lib/baykit/bayserver/docker/terminal/terminal_docker.rb', line 34

def environment
  @environment
end

#post_cache_thresholdObject (readonly)

Returns the value of attribute post_cache_threshold.



35
36
37
# File 'lib/baykit/bayserver/docker/terminal/terminal_docker.rb', line 35

def post_cache_threshold
  @post_cache_threshold
end

Instance Method Details

#arrive(tur) ⇒ Object

Implements Club



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/baykit/bayserver/docker/terminal/terminal_docker.rb', line 105

def arrive(tur)

  if not @available
    tur.res.headers.set_content_type("text/plain")
    tur.res.set_consume_listener(&ContentConsumeListener::DEV_NULL)
    tur.res.send_headers(tur.id)
    tur.res.send_res_content(tur.id, RACK_ERR, 0, RACK_ERR.length)
    tur.res.end_res_content(tur.id)
    return
  end

  if tur.req.uri.include? ".."
    raise HttpException.new HttpStatus::FORBIDDEN, tur.req.uri
  end

  env = create_env tur
  train = TerminalTrain.new(self, tur, @app, env)
  train.start_tour()
end

#create_env(tur) ⇒ Object



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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/baykit/bayserver/docker/terminal/terminal_docker.rb', line 125

def create_env(tur)
  env = {}

  cont_len = tur.req.headers.content_length()
  if cont_len > 0
    env["CONTENT_LENGTH"] = cont_len.to_s
  end
  cont_type = tur.req.headers.content_type()
  if StringUtil.set? cont_type
    env["CONTENT_TYPE"]      = cont_type
  end


  env["GATEWAY_INTERFACE"] = "CGI/1.1"
  #env[Rack::PATH_INFO] = tur.req.path_info == nil ? "" : tur.req.path_info
  pos = tur.req.uri.index('?')
  if pos and pos > 0
    env[Rack::PATH_INFO] = tur.req.uri[0 .. pos-1]
  else
    env[Rack::PATH_INFO] = tur.req.uri
  end
  env[Rack::QUERY_STRING] = tur.req.query_string == nil ? "" : tur.req.query_string
  env["REMOTE_ADDR"] = tur.req.remote_address
  env["REMOTE_HOST"] = tur.req.remote_address  # for performance reason
  env["REMOTE_USER"] = tur.req.remote_user == nil ? "" : tur.req.remote_user
  env[Rack::REQUEST_METHOD] = tur.req.method
  env["REQUEST_URI"]  = tur.req.uri
  #env[Rack::SCRIPT_NAME] = tur.req.script_name == nil ? "" : tur.req.script_name
  env[Rack::SCRIPT_NAME] = ""
  env[Rack::SERVER_NAME] = tur.req.server_name
  env[Rack::SERVER_PORT]  = tur.req.server_port.to_s
  env[Rack::SERVER_PROTOCOL] = tur.req.protocol
  env["SERVER_SOFTWARE"] = Baykit::BayServer::BayServer.get_software_name

  tur.req.headers.names.each do |name|
    tur.req.headers.values(name).each do |val|
      if /^content-type$/i =~ name || /^content-length$/i =~ name
        next
      end
      name = "HTTP_" + name
      name.gsub!(/-/o, "_")
      name.upcase!
      env[name] = val
    end
  end

  env[Rack::RACK_VERSION] = Rack::VERSION
  env[Rack::RACK_ERRORS] = STDERR
  env[Rack::RACK_INPUT] = nil
  env[RACK_MULTITHREAD] = true
  env[RACK_MULTIPROCESS] = BayServer.harbor.multi_core
  env[RACK_RUNONCE] = false
  env[Rack::RACK_URL_SCHEME] = tur.is_secure ? "https" : "http"
  env[Rack::RACK_IS_HIJACK] = true

  env[Rack::RACK_HIJACK] = lambda do
    pip = IO.pipe
    env[RACK_TERMINAL_PIPE] = pip

    w_pipe = pip[1]

    env[RACK_HIJACK_IO] = w_pipe

    agt = GrandAgent.get(tur.ship.agent_id)
    mpx = agt.net_multiplexer
    rd_read = IORudder.new(pip[0])
    sip = FullyHijackersShip.new()
    bufsize = tur.ship.protocol_handler.max_res_packet_data_size()

    tp = PlainTransporter.new(mpx, sip, false, bufsize, false)

    sip.init(tur, rd_read, tp)
    sid = sip.ship_id

    tur.res.set_consume_listener do |len, resume|
      if resume
        sip.resume_read(sid)
      end
    end

    mpx.add_rudder_state(rd_read, RudderState.new(rd_read, tp))
    mpx.req_read(rd_read)

    w_pipe
  end

  env[RACK_HIJACK_IO] = nil

  env[HTTP_VERSION] = tur.req.protocol
  env
end

#init(elm, parent) ⇒ Object

Implements Docker



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/baykit/bayserver/docker/terminal/terminal_docker.rb', line 47

def init(elm, parent)
  super

  begin
    require 'rack'
  rescue LoadError => e
    BayLog.error(RACK_ERR)
    return
  end

  require 'rack/handler/bayserver'

  if Rack::Handler::BayServer.app != nil
    # rackup mode
    @app = Rack::Handler::BayServer.app
  else
    if !StringUtil.set?(@config)
      raise ConfigException.new(elm.file_name, elm.line_no, "Config not specified")
    elsif not ::File.exist?(@config)
      raise ConfigException.new(elm.file_name, elm.line_no, "Config not found: %s", @config)
    end
    if @environment == nil
      @environment = "deployment"
    end
    options = {
      :server => "terminal",
      :config => @config,
      :environment => @environment,
      :docker => self,
    }
    Rack::Server.start options
  end

  @available = true
end

#init_key_val(kv) ⇒ Object

Implements DockerBase



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/baykit/bayserver/docker/terminal/terminal_docker.rb', line 87

def init_key_val(kv)
  case kv.key.downcase
  when "config"
    @config = Baykit::BayServer::BayServer.parse_path(kv.value)
  when "environment"
    @environment = kv.value
  when "postcachethreshold"
    @post_cache_threshold = kv.value.to_i
  else
    return super
  end
  return true
end