Module: Dorothy::Util
Instance Method Summary collapse
- #check_pid_file(file) ⇒ Object
- #create_pid_file(file, pid) ⇒ Object
- #exists?(file) ⇒ Boolean
- #get_time(local = Time.new) ⇒ Object
- #init_db(ddl = DoroSettings.dorothive[:ddl], force = false) ⇒ Object
- #load_profile(p_name) ⇒ Object
- #stop_process(doro_module) ⇒ Object
- #write(file, string) ⇒ Object
Instance Method Details
#check_pid_file(file) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/dorothy2/do-utils.rb', line 46 def check_pid_file(file) if File.exist? file # If we get Errno::ESRCH then process does not exist and # we can safely cleanup the pid file. pid = File.read(file).to_i begin Process.kill(0, pid) rescue Errno::ESRCH stale_pid = true end unless stale_pid puts "[" + "+".red + "] " + "[Dorothy]".yellow + " Dorothy is already running (pid=#{pid})" false end true end end |
#create_pid_file(file, pid) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/dorothy2/do-utils.rb', line 65 def create_pid_file(file, pid) File.open(file, "w") { |f| f.puts pid } ## Sends SIGTERM to process in pidfile. Server should trap this # and shutdown cleanly. at_exit do if File.exist? file File.unlink file end end end |
#exists?(file) ⇒ Boolean
29 30 31 |
# File 'lib/dorothy2/do-utils.rb', line 29 def exists?(file) File.exist?(file) end |
#get_time(local = Time.new) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/dorothy2/do-utils.rb', line 17 def get_time(local=Time.new) time = local case local.class.to_s when 'Time' time.utc.strftime("%Y-%m-%d %H:%M:%S") when 'DateTime' time.strftime("%Y-%m-%d %H:%M:%S") else time end end |
#init_db(ddl = DoroSettings.dorothive[:ddl], force = false) ⇒ Object
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 |
# File 'lib/dorothy2/do-utils.rb', line 94 def init_db(ddl=DoroSettings.dorothive[:ddl], force=false) LOGGER.warn "DB", "The database is going to be initialized with the file #{ddl}. If the Dorothive is already present, " + "all its data will be lost".red + ". Continue?(write yes)" answ = "yes" answ = gets.chop unless force if answ == "yes" begin #ugly, I know, but couldn't find a better and easier way.. LOGGER.info "DB", "Creating DB #{DoroSettings.dorothive[:dbname]}" if system "sh -c 'createdb -h #{DoroSettings.dorothive[:dbhost]} -U #{DoroSettings.dorothive[:dbuser]} -e #{DoroSettings.dorothive[:dbname]} 1> /dev/null'" LOGGER.info "DB", "Importing the dorothive DDL from #{ddl}" system "sh -c 'psql -d #{DoroSettings.dorothive[:dbname]} -h #{DoroSettings.dorothive[:dbhost]} -U #{DoroSettings.dorothive[:dbuser]} -f #{ddl} 1> /dev/null'" else raise 'An error occurred' end LOGGER.info "DB", "Database correctly initialized. Now you can restart Dorothy!" rescue => e LOGGER.error "DB", $! LOGGER.debug "DB", e.inspect end else LOGGER.error "DB", "Database untouched, quitting." end end |
#load_profile(p_name) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dorothy2/do-utils.rb', line 34 def load_profile(p_name) p = YAML.load_file(DoroSettings.env[:home] + '/etc/profiles.yml').select {|k| k == p_name}.first if p.nil? LOGGER.warn "PROFILE", "Warning, the profile specified (#{p_name}) doesn't exist in profiles.yml. Skipping" false else p end end |
#stop_process(doro_module) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/dorothy2/do-utils.rb', line 77 def stop_process(doro_module) pid_file = DoroSettings.env[:pidfiles] + '/' + doro_module + '.pid' doro_module.upcase! puts "[" + "+".red + "]" + " The #{doro_module} module is shutting now.." if pid_file and File.exist? pid_file pid = Integer(File.read(pid_file)) Process.kill(-2,-pid) puts "[" + "+".red + "]" + " The #{doro_module} module (PID #{pid}) was terminated" else puts "[" + "+".red + "]" + "Can't find PID file, is #{doro_module} really running?" end end |
#write(file, string) ⇒ Object
13 14 15 |
# File 'lib/dorothy2/do-utils.rb', line 13 def write(file, string) File.open(file , 'w') {|f| f.write(string) } end |