Module: Piedesaint

Defined in:
lib/piedesaint.rb,
lib/piedesaint/cli.rb,
lib/piedesaint/version.rb

Defined Under Namespace

Modules: Rack Classes: CLI, Piedesaint

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.app(options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/piedesaint.rb', line 74

def self.app ( options = {} )
  ::Rack::Builder.app do 
    use ::Rack::CommonLogger
    use ::Rack::ShowExceptions
    if ( !options[:key].nil? and !options[:key].empty? )
      use ::Rack::SslEnforcer, http_port: options[:http_port], https_port: options[:https_port]
    end
    use ::Rack::Deflater

    if options[:username].nil? or options[:username].empty?
      puts "Service without Basic Authentication"
    else
      use ::Rack::Auth::Basic, "Icecreamland" do |username, password|
        ( options[:username] == username ) && ( options[:password] == password )
      end
    end

    use ::Rack::Deflater

    if options[:metastore].nil? or options[:metastore].empty?
      puts "Service without cache"
    else
      use ::Rack::Cache, verbose: true,
          metastore: options[:metastore],
          entitystore: options[:entitystore],
          default_ttl: options[:freshness]
      puts "Service cache at #{options[:metastore]} / #{options[:entitystore]}"
    end

    map "/" do
      run Rack::DirectoriesTraversal.new(options)
    end
    puts "Serving tar'ed folders" if options[:tar]
  end
end

.execute(options = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/piedesaint.rb', line 110

def self.execute ( options = {} )
  event = ::Puma::Events.new STDOUT, STDERR
  puma = ::Puma::Server.new self.app( options ), event

  ## For Puma 2.0.1 (there is a bug regarding SSL and at least Ruby 1.9.3)
  ## Puma server doesn't receive 'event' (that's left to Puma::Binder)
  #binder = ::Puma::Binder.new event
  #puma.binder = binder
  #ctx = ::Puma::MiniSSL::SSLContext.new
  #ctx.key = "./server.key"
  #ctx.cert = "./server.crt"
  #ctx.verify_mode = ::Puma::MiniSSL::VERIFY_NONE

  puma.add_tcp_listener options[:host], options[:http_port]

  if ( !options[:key].nil? and !options[:key].empty? )
    ctx = ::OpenSSL::SSL::SSLContext.new
    ctx.key = OpenSSL::PKey::RSA.new File.read(options[:key])
    ctx.cert = OpenSSL::X509::Certificate.new File.read(options[:cert])
    ctx.verify_mode = ::OpenSSL::SSL::VERIFY_NONE
    puma.add_ssl_listener options[:host], options[:https_port], ctx
  end

  puma.min_threads = 1
  puma.max_threads = 10

  begin
    Signal.trap "SIGUSR2" do
      @restart = true
      puma.begin_restart
    end
  rescue Exception
    p "*** Sorry signal SIGUSR2 not implemented, restart feature disabled!"
  end

  begin
    Signal.trap "SIGTERM" do
      p " - Gracefully stopping, waiting for requests to finish"
      puma.stop false
    end
  rescue Exception
    p "*** Sorry signal SIGTERM not implemented, gracefully stopping feature disabled!"
  end
  
  begin
    puts "Service starting at #{options[:http_port]} -> #{options[:https_port]}"
    puma.run.join
  rescue Interrupt
    graceful_stop puma
  end

  if @restart
    p "* Restarting..."
    @status.stop true if @status
    restart!
  end
end

.graceful_stop(puma) ⇒ Object



169
170
171
172
173
174
# File 'lib/piedesaint.rb', line 169

def self.graceful_stop(puma)
  p " - Gracefully stopping, waiting for requests to finish"
  @status.stop(true) if @status
  puma.stop(true)
  p " - Goodbye!"
end

.tar(path) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/piedesaint.rb', line 176

def self.tar(path)
  tar = StringIO.new
  Gem::Package::TarWriter.new(tar) do |tarwriter|
    Dir[File.join(path, "**/{*,.*}")].each do |file|
      mode = File.stat(file).mode
      relative_file = File.join(File.basename(path), file.sub(/^#{Regexp::escape path}\/?/, ''))

      if File.directory? file
        next if [ ".", ".."].include? File.basename(file)
        tarwriter.mkdir relative_file, mode
      else
        tarwriter.add_file(relative_file, mode)  do |filepart|
          File.open(file, "rb") { |f| filepart.write f.read }
        end
      end
    end
  end
  tar.rewind
  tar
end