Module: PcaprLocal::Config

Defined in:
lib/pcapr_local/config.rb

Defined Under Namespace

Modules: Validate Classes: Opt

Constant Summary collapse

HOME =
ENV["HOME"] || File.expand_path('./')
DEFAULT_CONFIG =
{
    # Shared config.
    "install_dir"     => "#{HOME}/pcapr.Local/",
    "pcap_dir"        => "#{HOME}/pcapr.Local/pcaps",
    "index_dir"       => "#{HOME}/pcapr.Local/indexes",

    # UI (Sinatra)
    "app" => {
        "host"        => '127.0.0.1',
        "port"        => 8080,
    },

    # Pcap scanning
    "scanner" => {
        "interval"    => 60, # scan every n seconds.
        "queue_delay" => 60, # skip file if was modified in last n seconds.
    },

    # Couch
    "couch" => {
        "uri"         => 'http://127.0.0.1:5984/',
        "database"    => 'pcapr_local'
    },

    # Xtractr
    "xtractr" => {
        "path"         => 'xtractr',
        "idle_timeout" => 60  # kill xtractr browser after n seconds of idle time 
    },
    
    # tshark
    "tshark" => {
        "options"      => ''
    },
}
REQUIRED_EXES =

Tuple of required dependencies and ubuntu package name

[
    ["tshark", "tshark"], 
    ["zip",    "zip"],
]

Class Method Summary collapse

Class Method Details

.assert_environmentObject



53
54
55
56
57
58
59
# File 'lib/pcapr_local/config.rb', line 53

def self.assert_environment
    check_platform

    REQUIRED_EXES.each do |exe_package|
        check_exe *exe_package
    end
end

.check_exe(exe, package_name) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pcapr_local/config.rb', line 61

def self.check_exe exe, package_name
    if not system "sh -c 'type #{exe} > /dev/null 2>&1'"
        $stderr.puts <<HERE

pcapr.Local requires the #{exe} executable to function but #{exe} is either not
installed or cannot be found in your PATH. Please install #{exe} and ensure
that it is in your PATH.

HERE

        if system "sh -c 'type apt-get > /dev/null 2>&1'"
            $stderr.puts <<HERE
If #{exe} is not installed you may be able to install it with:
  'sudo apt-get install #{package_name}'

HERE
        end

        exit 1
    end
end

.check_platformObject

Exit unless we are on Linux (the xtractr exe is linux only).



84
85
86
87
88
89
# File 'lib/pcapr_local/config.rb', line 84

def self.check_platform
    if not RUBY_PLATFORM =~ /linux/i
        $stderr.puts "Sorry, pcapr.Local only runs on linux :("
        exit 1
    end
end

.config(config_path = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pcapr_local/config.rb', line 100

def self.config config_path=nil
    config_path ||= user_config_path

    if not File.exist? config_path
        self.create_config_file config_path
    end

    config = DEFAULT_CONFIG.dup
    begin
        user_config = JSON.parse(File.read(config_path))
    rescue JSON::ParserError => e
        raise "Config file is not well formed JSON, please correct or delete #{config_path}"
    end
    config = config_merge(config, user_config)

    # Derived config (not persisted)
    config['pidfile'] = File.join(config.fetch('install_dir'), '.server.pid')
    config['log_dir'] = File.join(config.fetch('install_dir'), 'log')

    return config
end

.user_config_pathObject

Return configuration as a Hash. Optionally takes an external configuration file.



94
95
96
97
98
# File 'lib/pcapr_local/config.rb', line 94

def self.user_config_path
    raise "HOME environment variable is not set" unless ENV['HOME']
    config_dir  = File.join(ENV['HOME'], '.pcapr_local')
    File.join(config_dir, 'config')
end