Class: Ramaze::Tool::Bin::Cmd

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/ramaze/tool/bin.rb

Overview

End helper methods }}}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#check_running?, #default_pidfile, #find_pid, #is_running?, #is_windows?, #rackup_path

Constructor Details

#initialize(args = nil) ⇒ Cmd

Returns a new instance of Cmd.



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ramaze/tool/bin.rb', line 107

def initialize(args = nil)
  args ||= ARGV
  raise "arguments must be an array!" unless args.respond_to?(:detect)
  @ourargs = args.dup
  @command = args.detect { |arg| arg.match(/^(?:--?)?(?:start|stop|restart|create|h(?:elp)?|v(?:ersion)?|console|status)/) }
  if command.nil?
    @command = ""
  else
    args.delete(@command)
  end
  ARGV.replace(args)
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



105
106
107
# File 'lib/ramaze/tool/bin.rb', line 105

def command
  @command
end

Class Method Details

.run(args = nil) ⇒ Object

{{{ #run is called when we’re interactive ($0 == __FILE__)



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
# File 'lib/ramaze/tool/bin.rb', line 121

def self.run(args = nil)
  cmd = new(args)
  case cmd.command
  when /^(?:--?)?status$/
    cmd.status(cmd.command)
  when /^(?:--?)?restart$/
    cmd.stop(cmd.command)
    cmd.start
  when /^(?:--?)?start$/
    cmd.start
  when /^(?:--?)?create$/
    cmd.create(cmd.command)
  when /^(?:--?)?stop$/
    if cmd.stop(cmd.command)
      puts "Ramazement has ended, go in peace."
      $stdout.flush
    else
      puts "Ramaze failed to stop (or was not running)"
    end
  when /^(?:--?)?console$/
    require "ramaze"
    require "irb"
    require "irb/completion"
    Ramaze.options.started = true
    require "start"
    IRB.start
    puts "Ramazement has ended, go in peace."
  when /^(?:--?)?h(elp)?$/
    puts cmd.usage
  when /^(?:--?)?v(ersion)?$/
    cmd.include_ramaze
    puts Ramaze::VERSION
    exit
  when /^$/
    puts "Must supply a valid command"
    puts cmd.usage
    exit 1
  else
    puts "#{command} not implemented"
    puts cmd.usage
    exit 1
  end
end

Instance Method Details

#create(command) ⇒ Object

}}}



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/ramaze/tool/bin.rb', line 245

def create(command) # {{{
  opts = {}
  if forced = @ourargs.detect { |arg| arg.match(/^(--force)/) }
    puts "Overwriting any existing files as requested."
    opts[:force] = true
    @ourargs.delete(forced)
  end
  if amended = @ourargs.detect { |arg| arg.match(/^(--amend)/) }
    puts "Only amending missing files as requested."
    opts[:amend] = true
    @ourargs.delete(amended)
  end
  project_name = @ourargs[@ourargs.index(command) + 1]
  if project_name.nil?
    $stderr.puts "Must supply a project name" if project_name.nil?
    puts usage
    exit 1
  end
  include_ramaze
  require 'ramaze/tool/create'
  Ramaze::Tool::Create.create(project_name, opts)
end

#include_ramazeObject

}}}



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ramaze/tool/bin.rb', line 165

def include_ramaze # {{{
  begin
    $:.unshift File.join(File.dirname(__FILE__), '/../lib')
    require 'ramaze'
  rescue LoadError
    $:.shift

    begin
      require 'rubygems'
    rescue LoadError
    end
    require 'ramaze'
  end
end

#startObject

Methods for commands {{{



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/ramaze/tool/bin.rb', line 210

def start # {{{
  include_ramaze

  # Find the name of this app
  app_name = default_pidfile.sub(/\.pid$/,'')
  rack_args = []

  if daemonize = @ourargs.detect { |arg| arg.match(/^(-[dD]|--daemonize)$/) }
    if pid_arg = @ourargs.detect { |arg| arg.match(/^(-P|--pid)/) }
      puts "User supplied pid: #{pid_arg}"
      pid_file = @ourargs[@ourargs.index(pid_arg) + 1]
      puts "Starting daemon with user defined pidfile: #{pid_file}"
    else
      puts "Starting daemon with default pidfile: #{pid_file = default_pidfile}"
      rack_args += ["-P", pid_file]
    end
    if check_running?(pid_file)
      $stderr.puts "Ramaze is already running with pidfile: #{pid_file}"
      exit 127
    end
  end

  port = Ramaze.options.adapter.port.to_s
  rack_args += ["-p", port   ] if @ourargs.grep(/^(-p|--port)/).empty?

  handler = Ramaze.options.adapter.handler.to_s
  rack_args += ["-s", handler] if @ourargs.grep(/^(-s|--server)/).empty?

  if is_windows?
    exec("ruby", rackup_path.to_s, "config.ru", *(ARGV + rack_args))
  else
    exec(rackup_path.to_s, "config.ru", *(ARGV + rack_args))
  end
end

#status(command) ⇒ Object

}}}



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/ramaze/tool/bin.rb', line 288

def status(command) # {{{
  unless pid_file = find_pid(@ourargs[@ourargs.index(command) + 1])
    $stderr.puts "No pid_file found! Ramaze may not be started."
    exit 1
  end
  puts "Pid file #{pid_file} found, PID is #{pid = File.read(pid_file)}"
  unless is_running?(pid.to_i)
    $stderr.puts "PID #{pid} is not running"
    exit 1
  end
  if is_windows?
    wmi = WIN32OLE.connect("winmgmts://")
    processes, ours = wmi.ExecQuery("select * from win32_process where ProcessId = #{pid}"), []
    processes.each { |p| ours << [p.Name, p.CommandLine, p.VirtualSize, p.CreationDate, p.ExecutablePath, p.Status ] }
    puts "Ramaze is running!\n\tName: %s\n\tCommand Line: %s\n\tVirtual Size: %s\n\tStarted: %s\n\tExec Path: %s\n\tStatus: %s" % ours.first
  else
    require "pathname"
    # Check for /proc
    if File.directory?(proc_dir = Pathname.new("/proc"))
      proc_dir = proc_dir.join(pid)
      # If we have a "stat" file, we'll assume linux and get as much info
      # as we can
      if File.file?(stat_file = proc_dir.join("stat"))
        stats = File.read(stat_file).split
        puts "Ramaze is running!\n\tCommand Line: %s\n\tVirtual Size: %s\n\tStarted: %s\n\tExec Path: %s\n\tStatus: %s" % [
          File.read(proc_dir.join("cmdline")).split("\000").join(" "),
          "%s k" % (stats[22].to_f / 1024),
          File.mtime(proc_dir),
          File.readlink(proc_dir.join("exe")),
          stats[2]
        ]
        exit
      end
    end
    # Fallthrough status, just print a ps
    puts "Ramaze process #{pid} is running!"
    begin
      puts %x{ps l #{pid}}
    rescue
      puts "No further information available"
    end
  end
end

#stop(command) ⇒ Object

}}}



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/ramaze/tool/bin.rb', line 268

def stop(command) # {{{
  unless pid_file = find_pid(@ourargs[@ourargs.index(command) + 1])
    $stderr.puts "No pid_file found!  Cannot stop ramaze (may not be started)."
    return false
  end
  pid = File.read(pid_file).to_i
  puts "Stopping pid #{pid}"
  Process.kill("INT", pid)
  sleep 2
  if is_running?(pid)
    $stderr.puts "Process #{pid} did not die, forcing it with -9"
    Process.kill(9, pid)
    File.unlink(pid_file) if File.file?(pid_file)
    true
  else
    File.unlink(pid_file) if File.file?(pid_file)
    true
  end
end

#usageObject

}}}



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
# File 'lib/ramaze/tool/bin.rb', line 180

def usage # {{{
  txt = [
    "\n  Usage:",
    "ramaze <start [PIDFILE]|stop [PIDFILE]|restart [PIDFILE]|status [PIDFILE]|create PROJECT|console> [ruby/rack options]\n",
    "Commands:\n",
    "  * All commands which take an optional PIDFILE (defaults to PROJECT.pid otherwise).",
    "  * All commands which start a ramaze instance will default to webrick on port 7000",
    "    unless you supply the rack options -p/--port PORT and/or * -s/--server SERVER.\n",
    " start   - Starts an instance of this application.\n",
    " stop    - Stops a running instance of this application.\n",
    " restart - Stops running instance of this application, then starts it back up.  Pidfile",
    "           (if supplied) is used for both stop and start.\n",
    " status  - Gives status of a running ramaze instance\n",
    " create  - Creates a new prototype Ramaze application in a directory named PROJECT in",
    "           the current directory.  ramaze create foo would make ./foo containing an",
    "           application prototype. Rack options are ignored here.\n",
    " console - Starts an irb console with app.rb (and irb completion) loaded. This command",
    "           ignores rack options, ARGV is passed on to IRB.\n\n\t"
  ].join("\n\t")

  if is_windows?
    txt << %x{ruby #{rackup_path} --help}.split("\n").reject { |line| line.match(/^Usage:/) }.join("\n\t")
  else
    txt << %x{#{rackup_path} --help}.split("\n").reject { |line| line.match(/^Usage:/) }.join("\n\t")
  end

  txt.gsub(/^\t$/, '')
end