/mysql-enc

A puppet enc (external node classifier) that uses mysql for the backend

Primary LanguageRuby

mysql-enc

mysql-enc is a puppet enc (external node classifier) that uses mysql for the backend.

Installation

These scripts only depend on one gem: mysql2. So if you already have a setup you can just ‘gem install mysql2’. If not I’ll do an example in ubuntu.

These are the steps I took on a fresh install of ubuntu 12.04:

sudo apt-get install mysql-server rubygems libmysqlclient-dev
sudo gem install mysql2

Now git clone the repo and create the config.yml file with your mysql creds. In my example I have the following:

db_host: localhost
db_user: root
db_password: secretpassword
db_name: enc

The db_name doesn’t really matter, it will be created. Now you can run the createDB.rb to initialize the database.

./createDB.rb

And you’re all setup. Now to add a node do something like this:

./addNode.rb web.test.domain testing web_server users

And test to see if it’s in there:

./classify.rb web.test.domain

You should get something like this:

--- 
classes: 
- web_server
- users
environment: testing

I do have a Gemfile, so bundle install should also install the mysql2 gem, but I couldn’t get it to work. Feel free to give me advice.

Configuration

You will need to create a file called config.yml in the directory with the scripts. It should look like this:

db_host: HOSTNAME_OF_DB
db_user: DB_USERNAME
db_password: DB_PASSWORD
db_name: NAME_OF_THE_DB_YOU_WOULD_LIKE_TO_USE

Documentation

Thus far this is a very simple implimentation of an enc. I’m just storing environment and classes in the db for a givin node.

addNode.rb

Usage: addNode.rb FQDN ENVIRONMENT [CLASSES]
eg: ./addNode.rb test.my.domain test web_server api_lb_server
OPTIONS:
   -h, --help                       help

You can have any number of classes, including zero. But you must have a fqdn(fully qualified domain name) and an environment.

classify.rb

This is the file that puppet will call with the fqdn. It will return yaml of the node’s classes and environment. If the node is not found it returns nothing, but has an exit status of 0, so there is no error. This is so that you can have a node that is defined in site.pp and not in the db.

eg. classify.rb test.my.domain

createDB.rb

This will drop and then create a db called ‘enc’

It will then create the table called nodes with id, hostname, fqdn, environment, classes, time_created