Class: Autumn::Tool::Bin::Cmd

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/autumn/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?

Constructor Details

#initialize(args = nil) ⇒ Cmd

Returns a new instance of Cmd.



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/autumn/tool/bin.rb', line 66

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.



64
65
66
# File 'lib/autumn/tool/bin.rb', line 64

def command
  @command
end

Class Method Details

.run(args = nil) ⇒ Object

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



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

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 "Autumn session has ended."
      $stdout.flush
    else
      puts "Autumn failed to stop (or was not running)"
    end
  when /^(?:--?)?console$/
    require "irb"
    require "irb/completion"
    cmd.include_autumn
    require "lib/genesis"
    IRB.start
    puts "Autumn session has ended."
  when /^(?:--?)?h(elp)?$/
    puts cmd.usage
  when /^(?:--?)?v(ersion)?$/
    cmd.include_autumn
    puts Autumn::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

#al_rootObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/autumn/tool/bin.rb', line 172

def al_root
  require "pathname"
  dir = nil
  if ARGV.size == 1
    dir = Pathname.new(ARGV.shift)
  elsif ARGV.size > 1
    $stderr.puts "Unknown options given #{ARGV.join(" ")}"
    puts usage
    exit 1
  end
  if dir.nil? or not dir.directory?
    dir = Pathname.new(ENV["PWD"]).expand_path
    $stderr.puts "Path to autumn tree not given or invalid, using #{dir}"
  end
  Object.const_set("AL_ROOT", dir.expand_path.to_s)
  Dir.chdir(AL_ROOT)
end

#create(command) ⇒ Object

{{{



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/autumn/tool/bin.rb', line 222

def create(command) # {{{
  create_options(opts = {}).parse!(ARGV)
  unless ARGV.size == 1
    $stderr.puts "Invalid options given: #{ARGV.join(" ")}"
    exit 1
  end
  project_name = ARGV.shift
  if project_name.nil?
    $stderr.puts "Must supply a valid project name, you gave none."
    puts usage
    exit 1
  end
  include_autumn
  require 'autumn/tool/create'
  Autumn::Tool::Create.create(project_name, opts)
end

#create_options(opts = {}) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/autumn/tool/bin.rb', line 214

def create_options(opts = {})
  @create_opts ||= OptionParser.new do |o|
    o.banner = "Create Options"
    o.on("-f", "--force", "Force creation if dir already exists") { |yn| opts[:force] = true }
    o.on("-a", "--amend", "Update a tree") { |yn| opts[:amend] = true }
  end
end

#include_autumnObject

}}}



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/autumn/tool/bin.rb', line 123

def include_autumn # {{{
  begin
    $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '/../lib')
    require 'autumn'
  rescue LoadError
    $LOAD_PATH.shift

    begin
      require 'rubygems'
    rescue LoadError
    end
    require 'autumn'
  end
end

#startObject

Methods for commands {{{



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/autumn/tool/bin.rb', line 191

def start # {{{
  start_options.parse!(ARGV)
  al_root
  
  # Find the name of this app
  puts "starting autumn"
  if @daemonize
    require "daemons"
    Daemons.run_proc(Pathname.new(AL_ROOT).basename, :ARGV => ["start"], :dir_mode => :normal, :dir => "tmp", :multiple => false) do
      start_autumn
    end
    puts "Autumn started in the background"
  else
    start_autumn
  end
end

#start_autumnObject

}}}



208
209
210
211
212
# File 'lib/autumn/tool/bin.rb', line 208

def start_autumn
  require "autumn/genesis"
  puts "Loading Autumn #{Autumn::VERSION}"
  Autumn::Genesis.new.boot!
end

#start_optionsObject

}}}



165
166
167
168
169
170
# File 'lib/autumn/tool/bin.rb', line 165

def start_options
  @start_opts ||= OptionParser.new do |o|
    o.banner = "Start/Restart Options"
    o.on("-D", "--daemonize", "Daemonize the process") { |daem| @daemonize = true }
  end
end

#status(command) ⇒ Object

}}}



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/autumn/tool/bin.rb', line 247

def status(command) # {{{
  al_root
  pn = Pathname.new(AL_ROOT)
  unless pid_file = find_pid(pn.join("tmp", pn.basename.to_s + ".pid"))
    $stderr.puts "No pid_file found! Autumn may not be started."
    exit 1
  end
  puts "Pid file #{pid_file} found, PID is #{pid = File.read(pid_file).chomp}"
  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 "Autumn 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 stat_file = proc_dir.join("stat") and stat_file.file?
        stats = File.read(stat_file).split
        puts "Autumn 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 "Autumn process #{pid} is running!"
    begin
      puts %x{ps l #{pid}}
    rescue
      puts "No further information available"
    end
  end
end

#stop(command) ⇒ Object

}}}



239
240
241
242
243
244
245
# File 'lib/autumn/tool/bin.rb', line 239

def stop(command) # {{{
  al_root
  require "daemons"
  Daemons.run_proc(Pathname.new(AL_ROOT).basename, :ARGV => ["stop"], :dir_mode => :normal, :dir => "tmp", :multiple => false) do
    start_autumn
  end
end

#usageObject

}}}



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/autumn/tool/bin.rb', line 138

def usage # {{{
  txt = [
    "\n  Usage:", 
    "autumn <start|stop|restart|status|create|console> PROJECT [options]\n",
    "Commands:\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 autumn instance\n",
    " create  - Creates a new prototype Autumn application in a directory named PROJECT in",
    "           the current directory.  autumn create foo would make ./foo containing an",
    "           application prototype.\n",
    " console - Starts an irb console with autumn (and irb completion) loaded.",
    "           ARGV is passed on to IRB.\n\n"
  ].join("\n\t")

  txt <<  "* All commands take PROJECT as the directory the autumn bot lives in.\n\n"
  txt << start_options.to_s << "\n"
  txt << create_options.to_s << "\n"
  #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
end