Module: PcaprLocal::Config::Validate

Defined in:
lib/pcapr_local/config.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.app_host(host, config) ⇒ Object



265
266
267
268
269
270
271
272
273
# File 'lib/pcapr_local/config.rb', line 265

def self.app_host host, config
    begin 
        server = TCPServer.new host, 0
        server.close
    rescue => e
        raise Error, "Got error '#{e.message}' when trying to create server for this host. Please pick a different host."
    end
    return host
end

.app_port(port, config) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/pcapr_local/config.rb', line 285

def self.app_port port, config
    begin
        port = Integer(port)
    rescue
        raise Error, "'#{port}' is not a valid port."
    end
    begin 
        server = TCPServer.new config["app_host"], port
        server.close
    rescue => e
        raise Error, "Got error '#{e.message}' when trying to listen on #{config['app_host']}:#{port}."
    end
    return port
end

.couch_uri(uri, config) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/pcapr_local/config.rb', line 307

def self.couch_uri uri, config
    # Check couch install is reachable.
    begin
        server = CouchRest::Server.new uri
        server.info
    rescue => e
        err = "Could not connect to couchdb at #{uri}. Got error '#{e.message}'\n"
        if system('which apt-get > /dev/null')
            err << "If CouchDB is not installed you may be able to install it with:\n"
            err << "  'sudo apt-get install couchdb'"
        else
            err << "Is CouchDB installed?"
        end
        raise Error, err
    end

    # Check that credentials are sufficient by actually creating the database.
    db_name = config['couch']['database']
    begin
        db = DB.get_db "uri" => uri, "database" => db_name, "host" => 'foo', "port" => 3
    rescue RestClient::Exception => e
        err = "Got '#{e.message}' while creating database.\n"
        err << "If you have authentication enabled in CouchDB, please include username and\n"
        err << "password in the URI like:\n"
        err << "  http://user:[email protected]:5984\n"
        raise Error, err
    end
    return uri 
end

.db_name(name, config) ⇒ Object



300
301
302
303
304
305
# File 'lib/pcapr_local/config.rb', line 300

def self.db_name name, config
    unless name =~ /\A[a-zA-Z0-9_]+\Z/
        raise Error, "Database name can include only letters numbers and underscores."
    end
    return name
end

.dir(dir, config) ⇒ Object



275
276
277
278
279
280
281
282
283
# File 'lib/pcapr_local/config.rb', line 275

def self.dir dir, config
    begin
        dir = File.expand_path dir
        FileUtils.mkdir_p dir
    rescue
        raise Error, "Directory (#{dir}) could not be created. Please choose a different directory."
    end
    return dir
end