Skip to content

Commit

Permalink
Convert hiera-mysql-backend to use postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianlzt committed Jan 2, 2014
1 parent 622a77b commit 9b475dd
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 79 deletions.
18 changes: 0 additions & 18 deletions .gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in hiera-mysql-backend.gemspec
# Specify your gem's dependencies in hiera-postgres-backend.gemspec
gemspec
14 changes: 7 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
PATH
remote: .
specs:
hiera-mysql-backend (0.0.3)
hiera-postgres-backend (0.0.1)
hiera
mysql2
pg

GEM
remote: https://rubygems.org/
specs:
hiera (1.2.1)
hiera (1.3.0)
json_pure
json_pure (1.8.0)
mysql2 (0.3.13)
rake (10.1.0)
json_pure (1.8.1)
pg (0.17.1)
rake (10.1.1)

PLATFORMS
ruby

DEPENDENCIES
hiera-mysql-backend!
hiera-postgres-backend!
rake
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013 Roberto
Copyright (c) 2013 Adrian Lopez

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
23 changes: 7 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
## hiera-mysql-backend
## hiera-postgresql-backend

Alternate MySQL backend for Hiera
Alternate PostgreSQL backend for Hiera

This is a MySQL backend for Hiera inspired by [hiera-mysql](https://github.com/crayfishx/hiera-mysql). Unfortunately no work has been done to that backend for the past 9 months and it was missing a couple of features I needed so I decided to pick up the torch and implement them myself.
This is a PostgreSQL backend for Hiera inspired by [hiera-mysql-backend](https://github.com/Telmo/hiera-mysql-backend).

### What is different from hiera-mysql

In [hiera-mysql](https://github.com/crayfishx/hiera-mysql) you define the queries in the hiera.yaml file. I felt this was too restricting so instead hiera-mysql-backend uses, poorly named, sql files. This sql files follow the Hiera hierarchy.

[hiera-mysql](https://github.com/crayfishx/hiera-mysql) would also return the last matching query not the first one which I felt it was confusing.

[hiera-mysql](https://github.com/crayfishx/hiera-mysql) used the mysql gem, I am partial to [mysql2](https://github.com/brianmario/mysql2)

Exception handling. hiera-mysql would cause a puppet run to fail if one of the queries was incorrect. For example a fact that you are distributing with a module is needed for the query to return its data but that fact is not available outside the module having a `SELECT * from %{custom_fact}` would make puppet runs fail.

### What goes into the sql files.

The poorly named sql files are really yaml files where the key is the lookup key and the value is the SQL statement (it accepts interpolation)

Lets assume your _datadir_ is `/etc/puppet/hieradata/` and your hierarchy for hiera just have a common. hiera-mysql-backend would look for /etc/puppet/hieradata/common.sql the common.sql would look like:
Lets assume your _datadir_ is `/etc/puppet/hieradata/` and your hierarchy for hiera just have a common. hiera-postgresql-backend would look for /etc/puppet/hieradata/common.sql the common.sql would look like:

```yaml
---
Expand All @@ -31,7 +22,7 @@ running `hiera applications` would run the query against the configured database

### Using

`gem install hiera-mysql-backend`
`gem install hiera-postgresql-backend`


### Configuring Hiera
Expand All @@ -42,12 +33,12 @@ Hiera configuration is pretty simple
---
:backends:
- yaml
- mysql2
- postgres
:yaml:
:datadir: /etc/puppet/hieradata
:mysql2:
:postgres:
:datadir: /etc/puppet/hieradata
:host: hostname
:user: username
Expand Down
19 changes: 0 additions & 19 deletions hiera-mysql-backend.gemspec

This file was deleted.

19 changes: 19 additions & 0 deletions hiera-postgres-backend.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- encoding: utf-8 -*-

Gem::Specification.new do |gem|
gem.name = "hiera-postgres-backend"
gem.version = "0.0.1"
gem.authors = ["Adrian"]
gem.email = ["[email protected]"]
gem.description = %q{Alternative PostgreSQL backend for hiera}
gem.summary = %q{Alternative PostgreSQL backend for hiera}
gem.homepage = "https://github.com/adrianlzt/hiera-postgres-backend"

gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]
gem.add_dependency('pg')
gem.add_dependency('hiera')
gem.add_development_dependency('rake')
end
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
class Hiera
module Backend
class Mysql2_backend
class Postgres_backend

def initialize(cache=nil)
begin
require 'mysql2'
require 'pg'
rescue LoadError
require 'rubygems'
require 'mysql2'
require 'pg'
end

@cache = cache || Filecache.new

Hiera.debug("Hiera MySQL2 initialized")
Hiera.debug("Hiera PostgreSQL initialized")
end

def lookup(key, scope, order_override, resolution_type)
Expand All @@ -22,12 +22,12 @@ def lookup(key, scope, order_override, resolution_type)
# so hiera('myvalue', 'test1') returns [nil,nil,nil,nil]
results = nil

Hiera.debug("looking up #{key} in MySQL2 Backend")
Hiera.debug("looking up #{key} in PostgreSQL Backend")
Hiera.debug("resolution type is #{resolution_type}")

Backend.datasources(scope, order_override) do |source|
Hiera.debug("Looking for data source #{source}")
sqlfile = Backend.datafile(:mysql2, scope, source, "sql") || next
sqlfile = Backend.datafile(:postgres, scope, source, "sql") || next

next unless File.exist?(sqlfile)
data = @cache.read(sqlfile, Hash, {}) do |datafile|
Expand All @@ -52,18 +52,17 @@ def query(query)
Hiera.debug("Executing SQL Query: #{query}")

data=nil
mysql_host = Config[:mysql2][:host]
mysql_user = Config[:mysql2][:user]
mysql_pass = Config[:mysql2][:pass]
mysql_database = Config[:mysql2][:database]
client = Mysql2::Client.new(:host => mysql_host,
:username => mysql_user,
:password => mysql_pass,
:database => mysql_database,
:reconnect => true)
pg_host = Config[:postgres][:host]
pg_user = Config[:postgres][:user]
pg_pass = Config[:postgres][:pass]
pg_database = Config[:postgres][:database]
client = PG::Connection.new(:host => pg_host,
:user => pg_user,
:password => pg_pass,
:dbname => pg_database)
begin
data = client.query(query).to_a
Hiera.debug("Mysql Query returned #{data.size} rows")
data = client.exec(query).to_a
Hiera.debug("PostgreSQL Query returned #{data.size} rows")
rescue => e
Hiera.debug e.message
data = nil
Expand Down

0 comments on commit 9b475dd

Please sign in to comment.