Class: RbRotate::Dispatcher

Inherits:
Object show all
Defined in:
lib/rb.rotate/dispatcher.rb

Overview

Dispatches all operations.

Instance Method Summary collapse

Instance Method Details

#install!Object

Installs the application configuration files.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rb.rotate/dispatcher.rb', line 57

def install!
    require "sys/uname"
    require "fileutils"
    
    basedir = ::File.dirname(__FILE__)
    
    # Loads and creates the configuration dir
    case Sys::Uname.sysname.downcase.to_sym
        when :freebsd
            etc = "/usr/local/etc"
        when :linux
            etc = "/etc"
        else
            raise Exception::new("You are running on an unknown platform. It cannot be problem, but it's necessary define path to configuration file and define paths in configuration file.")
    end
    
    etc << "/rb.rotate"
    FileUtils.mkdir_p(etc)
    
    # Creates other important directories
    FileUtils.mkdir_p("/var/log")
    FileUtils.mkdir_p("/var/lib")
    
    # Puts configuration files to configuration directory
    source = basedir.dup << "/install"
    replacements = { "%%configuration" => etc }
    files = ["rotate.yaml", "defaults.yaml"]
    
    files.each do |file|
        body = ::File.read(source.dup << "/" << file << ".initial")
        replacements.each_pair do |key, value|
            body.gsub! key, value
        end
        ::File.open(etc.dup << "/" << file, "w") do |io|
            io.write(body)
        end
    end
    
    # Puts to library root path path to configuration directory
    ::File.open(basedir.dup << "/../paths.conf", "w") do |io|
        io.write(etc.dup << "/rotate.yaml")
    end
end

#run!Object

Runs the rotate session.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rb.rotate/dispatcher.rb', line 15

def run!
    require "rb.rotate/configuration"
    require "rb.rotate/state"
    require "rb.rotate/storage"
    require "rb.rotate/log"

    # Reads configuration file
    locator = ::File.dirname(::File.dirname(__FILE__)).dup << "/paths.conf"
    if not ::File.exists? locator
        STDERR.write("FATAL: rb.rotate unconfigured. Please, run 'rb.rotate install' or eventually create the " << locator << " file with path to configuration file. Aborted.\n")
        exit
    end
    
    path = ::File.read(locator)
    path.strip!
    
    Configuration::read(path)
    log "Configuration file loaded."
    
    # Process
    log "Start of processing."
    Configuration::each_directory do |directory|
        begin
            directory.rotate!
        rescue Exception => e
            log "Exception: " << e.to_s
        end
    end
    
    # Removes orhpans
    log "Start of orphans removing."
    Storage::remove_orphans!
    
    # Saves state file
    State::save!
    log "New state saved."
end

#sysname!Object

Prints out system name.



105
106
107
108
# File 'lib/rb.rotate/dispatcher.rb', line 105

def sysname!
    require "sys/uname"
    puts Sys::Uname.sysname.downcase
end