Class: Shellac::ConfigClass

Inherits:
Object
  • Object
show all
Defined in:
lib/shellac/config.rb,
lib/shellac/config/task.rb,
lib/shellac/config/tasklist.rb

Defined Under Namespace

Classes: Task, TaskList

Instance Method Summary collapse

Constructor Details

#initializeConfigClass

Returns a new instance of ConfigClass.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/shellac/config.rb', line 8

def initialize
  @configuration = {}
  @configuration[:bind] = []
  @configuration[:minimum_threads] = 1
  @configuration[:maximum_threads] = 10
  @configuration[:worker_count] = 1
  @configuration[:routes] = Hash.new { |h, k| h[k] = [] }

  @meta_configuration = {}
  @meta_configuration[:helptext] = ''
end

Instance Method Details

#[](val) ⇒ Object



20
21
22
# File 'lib/shellac/config.rb', line 20

def [](val)
  @configuration.has_key?(val) ? @configuration[val] : @meta_configuration[val]
end

#classname(klass) ⇒ Object



225
226
227
228
# File 'lib/shellac/config.rb', line 225

def classname(klass)
  parts = Array === klass ? klass : klass.split(/::/)
  parts.inject(::Object) {|o,n| o.const_get n}
end

#configObject



24
25
26
# File 'lib/shellac/config.rb', line 24

def config
  @configuration
end

#merge_task_lists(old_list, new_list) ⇒ Object



58
59
60
# File 'lib/shellac/config.rb', line 58

def merge_task_lists(old_list, new_list)
  ( old_list + new_list ).sort
end

#metaObject



28
29
30
# File 'lib/shellac/config.rb', line 28

def meta
  @meta_configuration
end

#parse(parse_cl = true, additional_config = {}, additional_meta_config = {}, additional_tasks = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/shellac/config.rb', line 32

def parse(parse_cl = true, additional_config = {}, additional_meta_config = {}, additional_tasks = nil)
  @configuration.merge! additional_config
  @meta_configuration.merge! additional_meta_config

  tasklist = parse_command_line if parse_cl

  tasklist = merge_task_lists(tasklist, additional_tasks) if additional_tasks

  run_task_list tasklist
end

#parse_command_lineObject



62
63
64
65
66
67
68
69
70
71
72
73
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
109
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
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
216
217
218
219
220
221
222
223
# File 'lib/shellac/config.rb', line 62

def parse_command_line
  call_list = TaskList.new

  options = OptionParser.new do |opts|
    opts.on( '-h', '--help' ) do
      exe = File.basename( $PROGRAM_NAME )
      @meta_configuration[:helptext] << "\#{exe} [OPTIONS]\n\n\#{exe} is a simple caching proxy server.\n\n-h, --help:\n  Show this help.\n\n-b HOSTNAME[:PORT], --bind HOSTNAME[:PORT]:\n  The hostname/IP and optionally the port to bind to. This defaults to 127.0.0.1:80 if it is not provided.\n\n-c FILENAME, --config FILENAME:\n  The configuration file to load.\n\n-r ROUTESPEC, --route ROUTESPEC:\n  Provides a routing specification for the proxy. A route spec is one or more\n  host names or IPs, comma seperated, to match requests from, a regular\n  expression to match against, and a target to proxy to:\n\n  -r 'foo.bar.com::\\?(\\w+)$::https://github.com/\\\#{$1}'\n\n  This can be specified multiple times. For complex route specs, it is better\n  to use a configuration file.\n\n-s ENGINE, --storageengine ENGINE:\n  The storage engine to use for storing cached content.\n\n-t MIN:MAX, --threads MIN:MAX:\n  The minimum and maximum number of threads to run. Defaults to 0:10\n\n-w COUNT, --workers COUNT:\n  The number of worker processes to start.\n\n-v, --version:\n  Show the version of \#{exe}.\n"
      call_list << Task.new(9999) { puts @meta_configuration[:helptext]; exit 0 }
    end

    opts.on( '-v', '--version') do
      exe = File.basename( $PROGRAM_NAME )
      @meta_configuration[:version] = "#{exe} v. #{Shellac::VERSION}"
      call_list << Task.new(9999) { puts @meta_configuration[:version]; exit 0 }
    end

    opts.on( '-c', '--config FILENAME' ) do |configfile|
      require 'yaml'
      call_list << Task.new(0) do
        parsed_config = YAML.load( File.read( File.expand_path( configfile ) ) )
        @configuration = parsed_config.merge( @configuration ) if Hash === parsed_config
      end
    end

    opts.on( '-s', '--storageengine ENGINE' ) do |storageengine|
      @configuration[:storageengine] = storageengine
      call_list << Task.new(1) do
        libname = "shellac/storage_engine/#{@configuration[:storageengine]}"
        setup_engine(:storageengine, libname)
      end
    end

    opts.on( '-t', '--threads THREADSPEC' ) do |threadspec|
      call_list << Task.new(9000) do
        min = 1
        max = 10
        if threadspec =~ /\s*(\d+)\s*:\s*(\d+)/
          min,max = [ $1.to_i > 0 ? $1.to_i : 1, $2.to_i > 0 ? $2.to_i : 10 ]
        else
          n = Integer( threadspec.to_i )
          max = n > 0 ? n : 10
        end
        @configuration[:minimum_threads] = min
        @configuration[:maximum_threads] = max
      end
    end

    opts.on( '-w', '--workers COUNT' ) do |worker_count|
      call_list << Task.new(9000) do
        count = Integer( worker_count.to_i )
        count = count > 0 ? count : 1
        @configuration[:worker_count] = count
      end
    end

    opts.on( '-r', '--route ROUTESPEC' ) do |routespec|
      # -r 'foo.bar.com::?(\w+)::https://github.com/#{$1}'
      #
      hosts,regexp,matchfunc = routespec.split(/::/,3)

      hosts = hosts.split(/,/).collect {|h| h.strip}

      regexp = Regexp.new( regexp )

      if matchfunc =~ /^lambda:(.*)$/m
        code = "#{$1}"
      else
        code = "\"#{matchfunc}\""
      end

      matchfunc = "  lambda {|s,r|\nif s =~ r\n  \#{code}\nelse\n  nil\nend\n  }\n  ECODE\n      matchfunc = Object.new.instance_eval(matchfunc)\n\n      hosts.each do |h|\n        @configuration[:routes][h] << {\n          regexp: regexp,\n          func: matchfunc\n        }\n      end\n    end\n\n   opts.on( '-b', '--bind HOST') do |host_and_port|\n      if host_and_port =~ /^(\\w+:\\/\\/)/ \n        protocol = $1\n        host_and_port.gsub!(/^\\w+:\\/\\//,'')\n      else\n        protocol = 'tcp://'\n      end\n      h,p = host_and_port.split(/:/,2)\n      h = '127.0.0.1' if h.empty?\n      p = '80' if p.empty?\n      call_list << Task.new(9000) { @configuration[:bind] << \"\#{protocol}\#{h}:\#{p}\" }\n    end\n  end\n\n  leftover_argv = []\n\n  begin\n    options.parse!(ARGV)\n  rescue OptionParser::InvalidOption => e\n    e.recover ARGV\n    leftover_argv << ARGV.shift\n    leftover_argv << ARGV.shift if ARGV.any? && ( ARGV.first[0..0] != '-' )\n    retry\n  ensure\n    puts \"adding default storage engine\"\n    unless @configuration[:storageengine]\n      @configuration[:storageengine] = 'hash'\n      call_list << Task.new(100) do\n        libname = \"shellac/storage_engine/\#{@configuration[:storageengine]}\"\n        setup_engine(:storageengine, libname)\n      end\n    end\n  end\n\n  ARGV.replace( leftover_argv ) if leftover_argv.any?\n\n  call_list\nend\n"

#run_task_list(tasks) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/shellac/config.rb', line 43

def run_task_list( tasks )
  tasks = tasks.sort

  result = nil
  while tasks.any? do
    new_task = tasks.shift
    result = new_task.call # If any task returns a task list, fall out of execution
    break if TaskList === result
  end

  tasks = merge_task_lists(tasks, result) if TaskList === result # merge any new tasks into the remaining tasks

  run_task_list( tasks ) if tasks.any? # run any remaining tasks
end

#setup_engine(key, libname) ⇒ Object



230
231
232
233
234
235
# File 'lib/shellac/config.rb', line 230

def setup_engine(key, libname)
  require libname
  klass = classname( libname.split(/\//).collect {|s| s.capitalize} )
  @configuration[key] = klass.new
  @configuration[key].class.parse_command_line(@configuration, @meta_configuration) if @configuration[key].class.respond_to? :parse_command_line
end