Skip to content

Commit

Permalink
Enhance code to create/delete application record at the time of
Browse files Browse the repository at this point in the history
deploying/undeploying the application. Previously it was created
from  Analyzer module.
  • Loading branch information
Nikunj Limbaseeya committed Mar 18, 2011
1 parent da79ed6 commit 73a9f49
Show file tree
Hide file tree
Showing 19 changed files with 583 additions and 617 deletions.
6 changes: 3 additions & 3 deletions bin/webroar
Expand Up @@ -20,14 +20,14 @@

# Ruby script to control WebROaR

WEBROAR_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
WEBROAR_LIB_DIR = File.expand_path(File.join(WEBROAR_ROOT, 'lib'))
require File.join(File.dirname(__FILE__), '..', 'lib', 'constant.rb')

$LOAD_PATH.unshift("#{WEBROAR_LIB_DIR}")
$LOAD_PATH.unshift("#{ADMIN_PANEL_LIB_DIR}")

autoload :YAML, 'yaml'
require 'digest/md5'
require 'constant'
require 'db_connect'
require 'dependencies'
require 'control'
require 'scgi'
Expand Down
18 changes: 16 additions & 2 deletions bin/webroar-analyzer
Expand Up @@ -19,5 +19,19 @@
# along with WebROaR. If not, see <http://www.gnu.org/licenses/>.

require 'rubygems'
WEBROAR_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')).freeze
require File.expand_path(File.join(WEBROAR_ROOT, 'src', 'ruby_lib', 'analyzer', 'webroar_analyzer.rb'))
require File.join(File.dirname(__FILE__), '..', 'lib', 'constant.rb')

$LOAD_PATH.unshift("#{WEBROAR_LIB_DIR}")
$LOAD_PATH.unshift("#{ANALYZER_DIR}")

require 'db_connect'
require 'wlogger'
require 'process_helper'
require 'user_defined_exception'
require 'with_exception_handling'
require 'message_reader'
require 'message_analyzer'
require 'resources_analyzer'
require 'webroar_analyzer'

Webroar::Analyzer::ScriptRunner.run
7 changes: 4 additions & 3 deletions lib/constant.rb
Expand Up @@ -16,22 +16,23 @@
# You should have received a copy of the GNU General Public License
# along with WebROaR. If not, see <http://www.gnu.org/licenses/>.

WEBROAR_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
WEBROAR_LIB_DIR = File.expand_path(File.join(WEBROAR_ROOT, 'lib'))
WEBROAR_TEST_DIR = File.expand_path(File.join(WEBROAR_ROOT, 'test', 'unit'))
WEBROAR_BIN_DIR = File.expand_path(File.join(WEBROAR_ROOT, 'bin'))
ADMIN_PANEL_DIR = File.join(WEBROAR_ROOT, 'src', 'admin_panel')
ADMIN_PANEL_LIB_DIR = File.join(ADMIN_PANEL_DIR, 'lib')
RAILS_ROOT=ADMIN_PANEL_DIR
RAILS_ROOT = ADMIN_PANEL_DIR
GEM_BIN_DIR = File.expand_path(File.join(WEBROAR_ROOT,'..','..','bin'))
USR_BIN_DIR = File.join('','usr','bin')
ANALYZER_DIR = File.join(WEBROAR_ROOT, 'src', 'ruby_lib', 'analyzer')

ENV["PATH"] += ":/usr/bin:/usr/sbin:/sbin:/usr/local/sbin:#{WEBROAR_BIN_DIR}"
WEBROAR_LOG_FILE = File.join('','var','log','webroar','webroar.log')
PIDFILE = "/var/run/webroar.pid"
TESTFILE = "/etc/profile"
MESSAGE_DEPLOYMENT = "This command needs to be run as root. Please try again using 'sudo'.".freeze

$LOAD_PATH.unshift("#{ADMIN_PANEL_LIB_DIR}")

module Webroar
class CheckUser
# Check for root user
Expand Down
77 changes: 39 additions & 38 deletions src/ruby_lib/analyzer/db_connect.rb → lib/db_connect.rb
Expand Up @@ -16,52 +16,53 @@
# You should have received a copy of the GNU General Public License
# along with WebROaR. If not, see <http://www.gnu.org/licenses/>.

ADMIN_PANEL_ROOT = File.join(WEBROAR_ROOT, 'src', 'admin_panel').freeze
$LOAD_PATH.unshift("#{File.join(ADMIN_PANEL_ROOT,'vendor', 'rails', 'activerecord', 'lib')}")
$LOAD_PATH.unshift("#{File.join(ADMIN_PANEL_ROOT,'vendor', 'rails', 'activesupport', 'lib')}")
require File.join(ADMIN_PANEL_ROOT,'vendor', 'rails', 'activerecord', 'lib', 'active_record')
#gem 'activesupport', '>= 2.3.5'
#gem 'activerecord', '>= 2.3.5'
#require 'active_record'
$LOAD_PATH.unshift("#{File.join(ADMIN_PANEL_DIR, 'vendor', 'rails', 'activerecord', 'lib')}")
$LOAD_PATH.unshift("#{File.join(ADMIN_PANEL_DIR, 'vendor', 'rails', 'activesupport', 'lib')}")
require File.join(ADMIN_PANEL_DIR, 'vendor', 'rails', 'activerecord', 'lib', 'active_record')

module Webroar
module Analyzer
module DBConnect
def self.get_db_configuration(environment='production')
config = YAML.load_file(File.join(ADMIN_PANEL_ROOT, 'config', 'database.yml'))
configuration = config[environment]
if configuration["adapter"] == "sqlite3" and !configuration['database'].start_with?('/')
db_file = File.expand_path(File.join(ADMIN_PANEL_ROOT, configuration["database"]))
configuration["database"] = db_file
class DBConnect
class << self
def db_up(environment='production')

begin
ActiveRecord::Base.establish_connection(get_db_configuration(environment))
load_models
rescue NameError => e
puts e
puts e.backtrace.join("\n")
end
configuration

end

def self.load_models
models = Dir.glob(File.join(ADMIN_PANEL_ROOT, 'app', 'models', "{app,url_breakup_time_sample,app_time_sample,resource_usage,url_time_sample,app_exception,exception_detail}.rb"))
unloaded = Array.new

private

def load_models
models = Dir.glob(File.join(ADMIN_PANEL_DIR, 'app', 'models', "{pseudo_model,application_specification,server_specification,app,url_breakup_time_sample,app_time_sample,resource_usage,url_time_sample,app_exception,exception_detail}.rb"))

models.each do |f|
begin
require f
rescue NameError
unloaded << f
next
require f
rescue NameError => e
puts e
puts e.backtrace.join("\n")
end
end
unloaded.each do |f|
require f
end

end

def self.establish_connection(environment='production')
begin
ActiveRecord::Base.establish_connection(get_db_configuration(environment))
rescue Exception => e
Logger.error(e)
Logger.error(e.backtrace.join("\n"))

def get_db_configuration(environment)
config = YAML.load_file(File.join(ADMIN_PANEL_DIR, 'config', 'database.yml'))
configuration = config[environment]

if configuration["adapter"] == "sqlite3" and !configuration['database'].start_with?('/')
db_file = File.expand_path(File.join(ADMIN_PANEL_DIR, configuration["database"]))
configuration["database"] = db_file
end

configuration
end
end # DBConnect
end
end

end # self
end # class DBConnect
end # module Webroar
78 changes: 29 additions & 49 deletions lib/webroar_command.rb
Expand Up @@ -16,8 +16,6 @@
# You should have received a copy of the GNU General Public License
# along with WebROaR. If not, see <http://www.gnu.org/licenses/>.

ADMIN_PANEL_ROOT = File.join(WEBROAR_ROOT, 'src', 'admin_panel').freeze

module Webroar
module Command
# Basic WebROaR commands
Expand Down Expand Up @@ -49,6 +47,7 @@ def operation(args, op)
# Stop and remove the application
def remove(args)
return unless CheckUser.check
return unless server_started?
rails_app = false
rack_app =false
if args.length < 2
Expand All @@ -62,18 +61,7 @@ def remove(args)
end
end

# gem 'activesupport', '>= 2.3.5'
# gem 'activerecord', '>= 2.3.5'
# require 'active_record'
$LOAD_PATH.unshift("#{File.join(ADMIN_PANEL_ROOT,'vendor', 'rails', 'activerecord', 'lib')}")
$LOAD_PATH.unshift("#{File.join(ADMIN_PANEL_ROOT,'vendor', 'rails', 'activesupport', 'lib')}")
require File.join(ADMIN_PANEL_ROOT,'vendor', 'rails', 'activerecord', 'lib', 'active_record')

files = Dir.glob(File.join(ADMIN_PANEL_ROOT, 'app', 'models', "{app,pseudo_model,application_specification,server_specification}.rb"))
files << File.join(ADMIN_PANEL_ROOT, 'config','initializers','application_constants.rb')
files << File.join(ADMIN_PANEL_ROOT, 'lib','yaml_writer.rb')

load_files(files)
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])
Expand All @@ -87,6 +75,7 @@ def remove(args)
# Add and start the application
def add(options, args)
return unless CheckUser.check
return unless server_started?
rails_app = false
rack_app = false

Expand All @@ -101,25 +90,7 @@ def add(options, args)
end
end

sockFile = File.join("","tmp","webroar.sock")

unless File.exist?(sockFile)
puts "Either the server is not started or 'webroar.sock' file is deleted."
return
end

# gem 'activesupport', '>= 2.3.5'
# gem 'activerecord', '>= 2.3.5'
# require 'active_record'

$LOAD_PATH.unshift("#{File.join(ADMIN_PANEL_ROOT,'vendor', 'rails', 'activerecord', 'lib')}")
$LOAD_PATH.unshift("#{File.join(ADMIN_PANEL_ROOT,'vendor', 'rails', 'activesupport', 'lib')}")
require File.join(ADMIN_PANEL_ROOT,'vendor', 'rails', 'activerecord', 'lib', 'active_record')

files = Dir.glob(File.join(ADMIN_PANEL_ROOT, 'app', 'models', "{app,pseudo_model,application_specification,server_specification}.rb"))
files << File.join(ADMIN_PANEL_ROOT, 'config','initializers','application_constants.rb')
files << File.join(ADMIN_PANEL_ROOT, 'lib','yaml_writer.rb')
load_files(files)
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 ))
Expand Down Expand Up @@ -167,6 +138,31 @@ def add(options, args)

private

# Check the server status
def server_started?
sockFile = File.join("","tmp","webroar.sock")
return true if File.exist?(sockFile)
puts "Either the server is not started or 'webroar.sock' file is deleted."
return false
end

#Check server status and load models
def load_models
files = [File.join(ADMIN_PANEL_DIR, 'config','initializers','application_constants.rb'),
File.join(ADMIN_PANEL_DIR, 'lib','yaml_writer.rb')]

files.each do |f|
begin
require f
rescue NameError => e
puts e
puts e.backtrace.join("\n")
end
end

DBConnect.db_up
end

def check_server_status
pid = File.read(PIDFILE).chomp.to_i rescue nil
unless pid
Expand Down Expand Up @@ -359,22 +355,6 @@ def kill_process(pid)
end
end

# Load the list of files
def load_files(files)
unloaded = Array.new
files.each do |f|
begin
require f
rescue NameError
unloaded << f
next
end
end
unloaded.each do |f|
require f
end
end

# Server start/stop/restart
def server_operation(op)
case op
Expand Down
26 changes: 14 additions & 12 deletions src/ruby_lib/analyzer/logger.rb → lib/wlogger.rb
Expand Up @@ -17,40 +17,42 @@
# along with WebROaR. If not, see <http://www.gnu.org/licenses/>.

module Webroar
module Analyzer
class Logger
# Logging messages
class WLogger
class << self
def set_log_file(log_file)
@@log_file = log_file.freeze
@@debug_msg = true
end

def info(str)
log_to_file("Info: " + str.to_s)
end

def error(str)
log_to_file("Error: " + str.to_s)
end

def debug(str)
log_to_file("Debug: " + str.to_s) if @@debug_msg
end


private

def log_to_file(str)
begin
File.open(@@log_file, "a") do |f|
File.open(@@log_file, "a") do |f|
f.puts get_identifier + str
end
rescue Errno::ENOENT
puts get_identifier + str
end
end

def get_identifier
"#{Time.now.strftime('%a %b %d %H:%M:%S %Y')}-#{Process.pid}-"
end
end
end
end
end
end # << self
end # Class WLogger

end # module Webroar
Expand Up @@ -71,12 +71,10 @@ def add_application
# This action is to delete the application specification from the WebROaR config file.
#This method requires the id of the application specification to be deleted.
def delete_application
application_name = params[:id]
application_id = ApplicationSpecification.get_application_id_from_name(application_name)
app_name = ApplicationSpecification.delete(application_id)
reply, err_log = App.stop(app_name)
ApplicationSpecification.remove(params[:id])
reply, err_log = App.stop(params[:id])
# reply = nil indicate success
flash[:server_message] = "Application '#{app_name}' deleted successfully." if reply == nil
flash[:server_message] = "Application '#{params[:id]}' deleted successfully." if reply == nil
set_error(reply, err_log)
render :js => "<script>self.top.location='#{configuration_path}'</script>"
end
Expand Down

0 comments on commit 73a9f49

Please sign in to comment.