forked from adrianlzt/hiera-postgres-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in hiera-mysql-backend.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
hiera-mysql-backend (0.0.1) | ||
mysql2 | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
mysql2 (0.3.13) | ||
rake (10.1.0) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
hiera-mysql-backend! | ||
rake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "bundler/gem_tasks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# -*- encoding: utf-8 -*- | ||
|
||
Gem::Specification.new do |gem| | ||
gem.name = "hiera-mysql-backend" | ||
gem.version = "0.0.1" | ||
gem.authors = ["Telmo"] | ||
gem.email = ["[email protected]"] | ||
gem.description = %q{Alternative MySQL backend for hiera} | ||
gem.summary = %q{Alternative MySQL backend for hiera} | ||
gem.homepage = "https://github.com/Telmo/hiera-mysql-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('mysql2') | ||
gem.add_development_dependency('rake') | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
class Hiera | ||
module Backend | ||
class Mysql_backend | ||
|
||
def initialize(cache=nil) | ||
begin | ||
require 'mysql2' | ||
rescue LoadError | ||
require 'rubygems' | ||
require 'mysql2' | ||
end | ||
|
||
@cache = cache || Filecache.new | ||
|
||
Hiera.debug("Hiera MySQL initialized") | ||
end | ||
|
||
def lookup(key, scope, order_override, resolution_type) | ||
# default answer just to make it easier on ourselves | ||
answer = nil | ||
|
||
Hiera.debug("looking up %{key} in MySQL 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(:sql, scope, source, "sql") || next | ||
|
||
next unless File.exist?(sqlfile) | ||
data = @cache.read(sqlfile, Hash, {}) do |data| | ||
YAML.load(data) | ||
end | ||
|
||
next if data.empty? | ||
next unless data.include?(key) | ||
|
||
Hiera.debug("Found #{key} in #{source}") | ||
|
||
new_answer = Backend.parse_answer(data[key], scope) | ||
results = query(new_answer) | ||
return results | ||
end | ||
|
||
|
||
def query(query) | ||
Hiera.debug("Executing SQL Query: #{sql}") | ||
|
||
data=[] | ||
mysql_host = Config[:mysql][:host] | ||
mysql_user = Config[:mysql][:user] | ||
mysql_pass = Config[:mysql][:pass] | ||
mysql_database = Config[:mysql][:database] | ||
client = Mysql2::Client.new(:host => mysql_host, | ||
:username => mysql_username, | ||
:password => mysql_pass, | ||
:database => mysql_database, | ||
:reconnect => true) | ||
begin | ||
data = client.query(sql) | ||
Hiera.debug("Mysql Query returned #{res.size} rows") | ||
rescue => e | ||
Hiera.debug e.message | ||
data = nil | ||
end | ||
|
||
return data | ||
|
||
end | ||
end | ||
end | ||
end | ||
end |