Build Status Code Climate

Tomahawk

Tomahawk helps generating and parsing Apache 2 configuration files. You can for example parse VirtualHost configs, CRUD some directives in your Ruby code and generate a new config file out of it afterwards.

This gem is still in an early alpha stage and may change it's API faster than you blink. Still any contributions are highly appreciated.

Installation

Add this line to your application's Gemfile:

gem 'tomahawk'

And then execute:

$ bundle

Or install it yourself as:

$ gem install tomahawk

Usage

The following builds a new Apache Config

require 'tomahawk'

# initalize
config = Tomahawk::Config.new do |directives, groups|
  directives[:timeout] = '300'
  directives[:max_keep_alive_requests] = '100'
end

# ...

httpd_config.directives[:keep_alive] = 'On' # change a directive

config.to_s # => 
    # Timeout 300
    # MaxKeepAliveRequests 100
    # KeepAlive On

Nested directive groups can be added as well

require 'tomahawk'

config = Tomahawk::Config.new do |directives, groups|
    directives[:timeout] = '300'
    directives[:max_keep_alive_requests] = '100'

    groups << Tomahawk::DirectiveGroups::VirtualHost.new('*:80') do |directives, groups|
        directives[:server_name] = 'example.com'
        directives[:document_root] = '/var/www/example.com'
    end
end

# ...

config.groups.first.groups << Tomahawk::DirectiveGroups::Directory.new('/var/www/example.com') do |directives, groups|
    directives[:options] = '-Indexes FollowSymLinks MultiViews'
end

config.to_s # => 
    # Timeout 300
    # MaxKeepAliveRequests 100
    #
    # <VirtualHost *:80> 
    #  ServerName example.com
    #  DocumentRoot /var/www/example.com
    #
    # <Directory /var/www/example.com> 
    #  Options -Indexes FollowSymLinks MultiViews
    # </Directory>
    # 
    # </VirtualHost>

Only two types of directive groups are currently implemented: VirtualHost and Directory. More groups are possible of course, just open a new issue :)

Parsing

Tomahawk can also parse existing configs

require 'tomahawk'

config = Tomahawk.parse File.read('/etc/apache2/apache.conf')

config.directives # lists parsed directives such as ServerName and LogLevel

config.groups # lists parsed directive groups such as <VirtualHost> and <Directory>

config.directives['server_name'] = 'google.com' # change a directive

config.directives['some_new_directive'] = 'foo' # create a new directive

File.write('/etc/apache2/apache.conf.new', httpd_config.to_s) # write your new configuration back to disk

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request