Class: Webroar::Command::WebroarCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/webroar_command.rb

Overview

Basic WebROaR commands

Instance Method Summary collapse

Constructor Details

#initializeWebroarCommand

Returns a new instance of WebroarCommand.



23
24
25
26
27
# File 'lib/webroar_command.rb', line 23

def initialize
  @server_status = :down
  @starling_status = :down
  @analyzer_status = :down
end

Instance Method Details

#add(options, args) ⇒ Object

Add and start the application



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
# File 'lib/webroar_command.rb', line 76

def add(options, args)
  return unless User.permitted?
  return unless server_started?
  rails_app = false
  rack_app = false

  if File.exists?(File.join(Dir.getwd, "config", "environment.rb"))
    rails_app = true
  elsif File.exists?(File.join(Dir.getwd, "config.ru"))
    rack_app = true
  end

  if args.length < 2 and rails_app == false and rack_app == false
    puts "Application name is missing."
    return
  end

  load_models

  args[1] = Dir.pwd.match(/[^\/]*$/).to_s if(args[1].nil? and (rails_app or rack_app ))
  options[:path] = Dir.pwd if(options[:path].nil? and (rails_app or rack_app ))
  options[:resolver] = ("/" << args[1].gsub(/[^a-zA-Z0-9_\-.~]/,'_').downcase) if(options[:resolver].nil? and (rails_app or rack_app ))
  options[:type1] = "Rack" if (options[:type1].nil? and rack_app)
  options[:run_as_user] = Etc.getpwuid(File.stat(File.join(Dir.getwd, "config", "environment.rb")).uid).name if(options[:run_as_user].nil? and rails_app)
  options[:run_as_user] = Etc.getpwuid(File.stat(File.join(Dir.getwd, "config.ru")).uid).name if(options[:run_as_user].nil? and rack_app)

  if(!options[:min_worker] or !options[:max_worker])
    info = YAML::load_file(CONFIG_FILE_PATH)
    if info
      options[:min_worker] = info['Server Specification']['min_worker'].to_i unless options[:min_worker]
      options[:max_worker] = info['Server Specification']['max_worker'].to_i unless options[:max_worker]
    end
  end

  params = {:app_id => nil,
    :name => args[1],
    :host_names => nil,
    :baseuri => nil,
    :resolver => options[:resolver],
    :path => options[:path],
    :run_as_user => options[:run_as_user],
    :type1 => options[:type1] ? options[:type1] : 'Rails',
    :analytics => options[:analytics] ? options[:analytics] : 'Disabled',
    :environment => options[:environment] ? options[:environment] : 'production',
    :min_worker => options[:min_worker] ? options[:min_worker] : MIN_WORKERS,
    :max_worker => options[:max_worker] ? options[:max_worker] : MAX_WORKERS}

  application_specification = ApplicationSpecification.new(params)

  if application_specification.save
    application_specification.write
    application_specification.errors.to_a.each{|msg| puts msg }
    reply, err_log = App.start(params[:name])
    #reply = nil indicate success
    puts reply ? reply : "Application '#{params[:name]}' added successfully."
    puts "\n\e[31m" + err_log + "\e[0m" if err_log
    application_specification.remove if (err_log or reply)
  else
    application_specification.errors.to_a.each{|msg| puts msg }
  end

end

#clearObject

Clear log files



30
31
32
33
34
35
36
37
38
39
# File 'lib/webroar_command.rb', line 30

def clear()
  return unless User.permitted?
  print "Clearing log files ..."
  log_file_pattern = File.join('','var','log','webroar','*.log')
  log_files = Dir.glob(log_file_pattern)
  log_files.each do |file|
    File.truncate(file, 0) if File.exists?(file)
  end
  puts " done."
end

#operation(args, op) ⇒ Object

Start/Stop/Restart Command



42
43
44
45
# File 'lib/webroar_command.rb', line 42

def operation(args, op)
  return unless User.permitted?
  (args.nil? or args.length == 1) ? server_operation(op) : application_operation(args, op)
end

#remove(args) ⇒ Object

Stop and remove the application



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
# File 'lib/webroar_command.rb', line 48

def remove(args)
  return unless User.permitted?
  return unless server_started?
  rails_app = false
  rack_app =false
  if args.length < 2
    if File.exists?(File.join(Dir.getwd, "config", "environment.rb"))
      rails_app = true
    elsif File.exists?(File.join(Dir.getwd, "config.ru"))
      rack_app = true
    else
      puts "Application name is missing."
      return
    end
  end

  load_models

  args[1] = Dir.pwd.match(/[^\/]*$/).to_s if(args[1].nil? and (rails_app or rack_app ))
  reply, err_log = App.stop(args[1])
  ApplicationSpecification::remove(args[1]) if reply.nil?

  puts reply ? reply : "Application '#{args[1]}' removed successfully."
  puts "\n\e[31m" + err_log + "\e[0m" if err_log

end