Permalink
Browse files

Moved logger and config to class vars

  • Loading branch information...
1 parent 8b158cd commit c2f6a4f0b63d92a8bc11ff078577c906100cd962 @markround committed Jul 27, 2016
View
@@ -35,11 +35,15 @@ module Tiller
puts "tiller v#{VERSION} (https://github.com/markround/tiller) <github@markround.com>"
- config = parse_options(Tiller::Defaults)
- log = Tiller::Logger.new(config)
+ class << self;
+ attr_accessor :config, :log
+ end
- log.debug("Executable: #{__FILE__}")
+ Tiller::config = parse_options(Tiller::Defaults)
+ Tiller::log = Tiller::Logger.new
+ log.debug("Executable: #{__FILE__}")
+ #Tiller::Logger.debug("Executable: #{__FILE__}")
# Add tiller_lib to the LOAD PATH so we can pull in user-defined plugins
$LOAD_PATH.unshift(config[:tiller_lib]) unless $LOAD_PATH.include?(config[:tiller_lib])
@@ -85,19 +89,19 @@ module Tiller
# e.g. environment-specific execs and so on. We do this first so that connection strings
# to datasources (e.g. zookeeper) can be included in the config before we obtain any
# values.
- config.merge!(data_class.new(config).common) do |key, old, new|
+ config.merge!(data_class.new.common) do |key, old, new|
warn_merge(key, old, new, 'common', data_class.to_s)
end
- global_values.merge!(data_class.new(config).global_values) do |key, old, new|
+ global_values.merge!(data_class.new.global_values) do |key, old, new|
warn_merge(key, old, new, 'global', data_class.to_s)
end
end
# Get all Templates for the given environment
templates = {}
template_classes.each do |template_class|
- ts = template_class.new(config)
+ ts = template_class.new
ts.templates.each do |t|
templates[t] = ts.template(t)
end
@@ -120,7 +124,7 @@ module Tiller
# Now we add to the 'tiller' hash with values from each DataSource, warning if we
# get duplicate values.
data_classes.each do |data_class|
- dc = data_class.new(config)
+ dc = data_class.new
if dc.values(template) != nil
tiller.merge!(dc.values(template)) do |key, old, new|
warn_merge(key, old, new, 'data', data_class.to_s)
View
@@ -73,7 +73,7 @@ You can create your own template provider by extending the `Tiller::TemplateSour
If you create a `setup` method, it will get called straight after initialization. This can be useful for connecting to a database, parsing configuration files and so on.
-When the class is created, it gets passed a hash containing various variables you can use to return different templates based on environment etc. Or you can read any values from `common.yaml` yourself, as it's accessible from the instance variable `@config`.
+When the class is created, it gets passed a hash containing various variables you can use to return different templates based on environment etc. Or you can read any values from `common.yaml` yourself, as it's accessible from the class variable `Tiller::config`.
The simplest possible example template source that returns one hard-coded template would be something like :
@@ -106,7 +106,7 @@ You can create your own datasources by inheriting `Tiller::DataSource` and provi
* `perms`: The octal permissions the file should have (e.g. 0644)
* `global_values` : Return a hash of global values.
-As with template sources, if you need to connect to a database or do any other post-initialisation work, create a `setup` method. You also have the `@config` instance variable available, which is a hash of the Tiller configuration (`common.yaml`).
+As with template sources, if you need to connect to a database or do any other post-initialisation work, create a `setup` method. You also have the `Tiller::config` class variable available, which is a hash of the Tiller configuration (`common.yaml`).
The simplest possible example data source that returns one global value ("example") for all templates would look something like :
@@ -135,13 +135,13 @@ template_sources:
If you don't want to use the default directory of `/usr/local/lib/tiller`, you can specify an alternate location by setting the `tiller_lib` environment variable, or by using the `-l`/`--libdir` flag on the command line.
## Logging
-Both `Tiller::DataSource` and `Tiller::TemplateSource` have a log instance object available through `@log`. The verbosity is set to WARN by default but can be set to `INFO` when Tiller is called with the `-v` flag, and `DEBUG` when the `-d` flag is used. EG:
+There is a class logger available via `Tiller::log`. The verbosity is set to WARN by default but can be set to `INFO` when Tiller is called with the `-v` flag, and `DEBUG` when the `-d` flag is used. EG:
```ruby
class ExampleDataSource < Tiller::DataSource
def setup
- @log.info('You will see this if you have run tiller with the -v flag')
- @log.debug('You will only see this if you have run tiller with the -d flag')
+ Tiller::log.info('You will see this if you have run tiller with the -v flag')
+ Tiller::log.debug('You will only see this if you have run tiller with the -d flag')
end
...
... Rest of file
View
@@ -7,27 +7,27 @@ module Tiller::ConsulCommon
def setup
# Set our defaults if not specified
@consul_config = Tiller::Consul.defaults
- raise 'No Consul configuration block' unless @config.has_key?('consul')
- @consul_config.deep_merge!(@config['consul'])
+ raise 'No Consul configuration block' unless Tiller::config.has_key?('consul')
+ @consul_config.deep_merge!(Tiller::config['consul'])
# Sanity check
['url'].each {|c| raise "Consul: Missing Consul configuration #{c}" unless @consul_config.has_key?(c)}
# Now we connect to Consul
Diplomat.configure do |config|
- @log.debug("#{self} : Connecting to Consul at #{@consul_config['url']}")
+ Tiller::log.debug("#{self} : Connecting to Consul at #{@consul_config['url']}")
config.url = @consul_config['url']
if @consul_config['acl_token']
- @log.debug("#{self} : Using Consul ACL token")
+ Tiller::log.debug("#{self} : Using Consul ACL token")
config.acl_token = @consul_config['acl_token']
end
end
end
# Interpolate configuration placeholders with values
def interpolate(path, template_name = nil)
- path.gsub!('%e', @config[:environment])
+ path.gsub!('%e', Tiller::config[:environment])
path.gsub!('%t', template_name) if template_name
path
end
@@ -10,29 +10,29 @@ class ConsulDataSource < Tiller::DataSource
def global_values
# Fetch globals
path = interpolate("#{@consul_config['values']['global']}")
- @log.debug("#{self} : Fetching globals from #{path}")
+ Tiller::log.debug("#{self} : Fetching globals from #{path}")
globals = fetch_all_keys(path)
# Do we have per-env globals ? If so, merge them
path = interpolate("#{@consul_config['values']['per_env']}")
- @log.debug("#{self} : Fetching per-environment globals from #{path}")
+ Tiller::log.debug("#{self} : Fetching per-environment globals from #{path}")
globals.deep_merge!(fetch_all_keys(path))
# Do we want to register services in consul_services hash ?
if @consul_config['register_services']
- @log.debug("#{self} : Registering Consul services")
+ Tiller::log.debug("#{self} : Registering Consul services")
globals['consul_services'] = {}
services = Diplomat::Service.get_all({ :dc => @consul_config['dc'] })
services.marshal_dump.each do |service, _data|
- @log.debug("#{self} : Fetching Consul service information for #{service}")
+ Tiller::log.debug("#{self} : Fetching Consul service information for #{service}")
service_data = Diplomat::Service.get(service, :all, { :dc => @consul_config['dc']})
globals['consul_services'].merge!( { "#{service}" => service_data })
end
end
# Do we want to register nodes in consul_nodes hash ?
if @consul_config['register_nodes']
- @log.debug("#{self} : Registering Consul nodes")
+ Tiller::log.debug("#{self} : Registering Consul nodes")
globals['consul_nodes'] = {}
nodes = Diplomat::Node.get_all
nodes.each do |n|
@@ -44,13 +44,13 @@ def global_values
def values(template_name)
path = interpolate("#{@consul_config['values']['template']}", template_name)
- @log.debug("#{self} : Fetching template values from #{path}")
+ Tiller::log.debug("#{self} : Fetching template values from #{path}")
fetch_all_keys(path)
end
def target_values(template_name)
path = interpolate("#{@consul_config['values']['target']}", template_name)
- @log.debug("#{self} : Fetching template target values from #{path}")
+ Tiller::log.debug("#{self} : Fetching template target values from #{path}")
fetch_all_keys(path)
end
@@ -60,7 +60,7 @@ def fetch_all_keys(path)
all_keys = {}
if keys.is_a? Array
keys.each do |k|
- @log.debug("#{self} : Fetching key #{k}")
+ Tiller::log.debug("#{self} : Fetching key #{k}")
all_keys[File.basename(k)] = Diplomat::Kv.get(k, { nil_values: true, :dc => @consul_config['dc'] })
end
all_keys
@@ -6,14 +6,14 @@
class DefaultsDataSource < Tiller::DataSource
def setup
- defaults_file = File.join(@config[:tiller_base], 'defaults.yaml')
- defaults_dir = File.join(@config[:tiller_base], 'defaults.d')
+ defaults_file = File.join(Tiller::config[:tiller_base], 'defaults.yaml')
+ defaults_dir = File.join(Tiller::config[:tiller_base], 'defaults.d')
@defaults_hash = Hash.new
# First, try and load defaults from v2 config
- if @config.has_key?('defaults')
- @log.debug("#{self} : Using values from v2 format common.yaml")
- @defaults_hash.deep_merge!(@config['defaults'])
+ if Tiller::config.has_key?('defaults')
+ Tiller::log.debug("#{self} : Using values from v2 format common.yaml")
+ @defaults_hash.deep_merge!(Tiller::config['defaults'])
else
# Read defaults in from defaults file if no v2 config
# Handle empty files - if YAML didn't parse, it returns false so we skip them
@@ -28,7 +28,7 @@ def setup
if File.directory? defaults_dir
Dir.glob(File.join(defaults_dir,'*.yaml')).each do |d|
yaml = YAML.load(open(d))
- @log.debug("Loading defaults from #{d}")
+ Tiller::log.debug("Loading defaults from #{d}")
@defaults_hash.deep_merge!(yaml) if yaml != false
end
end
@@ -52,7 +52,7 @@ def values(template_name)
# Previous versions of Tiller didn't throw an error when we had an empty
# template config definition in a defaults file. Tiller 0.7.3 "broke" this, so while it's arguably the
# correct thing to bail out, in the interests of backwards-compatibility, we now instead log a warning and continue.
- @log.warn("Warning, empty config for #{template_name}")
+ Tiller::log.warn("Warning, empty config for #{template_name}")
Hash.new
end
else
@@ -67,7 +67,7 @@ def target_values(template_name)
values.key?('target') ? values : Hash.new
else
# See comments for values function.
- @log.warn("Warning, empty config for #{template_name}")
+ Tiller::log.warn("Warning, empty config for #{template_name}")
Hash.new
end
else
@@ -4,8 +4,8 @@ class EnvironmentDataSource < Tiller::DataSource
def setup
@plugin_config = Tiller::Environment.defaults
- if @config.has_key? 'environment' and @config['environment'].is_a? Hash
- @plugin_config.merge!(@config['environment'])
+ if Tiller::config.has_key? 'environment' and Tiller::config['environment'].is_a? Hash
+ @plugin_config.merge!(Tiller::config['environment'])
end
end
@@ -13,12 +13,12 @@ def setup
@json_structure = parse.is_a?(Hash) ? parse : Hash.new
if @json_structure[VERSION_KEY].is_a? Integer
@json_version = @json_structure[VERSION_KEY]
- @log.debug("Using v#{@json_version} tiller_json format")
+ Tiller::log.debug("Using v#{@json_version} tiller_json format")
else
@json_version = 1
end
rescue JSON::ParserError
- @log.warn('Warning : Error parsing tiller_json environment variable')
+ Tiller::log.warn('Warning : Error parsing tiller_json environment variable')
end
else
@json_structure = Hash.new
@@ -7,8 +7,8 @@ class ExternalFileDataSource < Tiller::DataSource
def setup
@merged_values = Hash.new
- if @config.has_key?('external_files')
- files = @config['external_files']
+ if Tiller::config.has_key?('external_files')
+ files = Tiller::config['external_files']
files.each do |file|
@merged_values.merge!(parse_file(file)) do |key, old, new|
warn_merge(key, old, new, 'external file data', file)
@@ -23,14 +23,14 @@ def global_values
def parse_file(filename)
raise("External file '#{filename}' could not be loaded") unless File.file?(filename)
- @log.debug("#{self} : Loading #{filename}")
+ Tiller::log.debug("#{self} : Loading #{filename}")
parse = nil
# First try to load it as JSON
if ! parse
begin
parse = JSON.parse(File.read(filename))
- @log.debug("#{self} : #{filename} is in JSON format")
+ Tiller::log.debug("#{self} : #{filename} is in JSON format")
rescue JSON::ParserError
end
end
@@ -39,7 +39,7 @@ def parse_file(filename)
if ! parse
begin
parse = YAML.load(File.read(filename))
- @log.debug("#{self} : #{filename} is in YAML format")
+ Tiller::log.debug("#{self} : #{filename} is in YAML format")
rescue Psych::SyntaxError
end
end
@@ -4,26 +4,26 @@ class FileDataSource < Tiller::DataSource
# Open and parse the environment file. Tries from v2 format common.yaml first, if that
# failes, then it looks for separate environment files.
def setup
- if @config.has_key?('environments')
+ if Tiller::config.has_key?('environments')
# Try and load from v2 format common.yaml
- if @config['environments'].has_key?(@config[:environment])
- @log.debug("#{self} : Using values from v2 format common.yaml")
- if @config['environments'][@config[:environment]].is_a? Hash
- @config_hash = @config['environments'][@config[:environment]]
+ if Tiller::config['environments'].has_key?(Tiller::config[:environment])
+ Tiller::log.debug("#{self} : Using values from v2 format common.yaml")
+ if Tiller::config['environments'][Tiller::config[:environment]].is_a? Hash
+ @config_hash = Tiller::config['environments'][Tiller::config[:environment]]
else
# This permits "stub"" environments, where all the config is provided by another module e.g. defaults
# See https://github.com/markround/tiller/issues/29
- @log.info("Using stub environment for #{@config[:environment]}")
+ Tiller::log.info("Using stub environment for #{Tiller::config[:environment]}")
@config_hash = Hash.new
end
else
- abort("Error : Could not load environment #{@config[:environment]} from common.yaml")
+ abort("Error : Could not load environment #{Tiller::config[:environment]} from common.yaml")
end
else
# Try and load from v1 format files
begin
- env_file = File.join(@config[:tiller_base], 'environments',
- "#{@config[:environment]}.yaml")
+ env_file = File.join(Tiller::config[:tiller_base], 'environments',
+ "#{Tiller::config[:environment]}.yaml")
@config_hash = YAML.load(open(env_file))
rescue
abort("Error : Could not load environment file #{env_file}")
@@ -7,26 +7,26 @@
class XmlFileDataSource < Tiller::DataSource
def global_values
- parse_xml(@config)
+ parse_xml(Tiller::config)
end
def values(template)
- parse_xml(@config['environments'][@config[:environment]][template])
+ parse_xml(Tiller::config['environments'][Tiller::config[:environment]][template])
end
def parse_xml(config_hash)
if config_hash.has_key?('xml_file_path') && config_hash.has_key?('xml_file_var')
path = config_hash['xml_file_path']
var = config_hash['xml_file_var']
- @log.info('Opening XML file : ' + path)
+ Tiller::log.info('Opening XML file : ' + path)
begin
xml = Crack::XML.parse(File.open(path))
rescue StandardError => e
abort "Error : Could not parse XML file #{path}\n#{e}"
end
struct = {}
struct[var] = xml
- @log.debug("Created XML structure : #{struct}")
+ Tiller::log.debug("Created XML structure : #{struct}")
struct
else
{}
@@ -10,8 +10,8 @@ def setup
# Set our defaults if not specified
@zk_config = Tiller::Zookeeper::Defaults
- raise 'No zookeeper configuration block' unless @config.has_key?('zookeeper')
- @zk_config.merge!(@config['zookeeper'])
+ raise 'No zookeeper configuration block' unless Tiller::config.has_key?('zookeeper')
+ @zk_config.merge!(Tiller::config['zookeeper'])
# Sanity check
['uri'].each {|c| raise "Missing Zookeeper configuration #{c}" unless @zk_config.has_key?(c)}
@@ -29,21 +29,21 @@ def setup
def values(template_name)
path = @zk_config['values']['template']
- .gsub('%e',@config[:environment])
+ .gsub('%e',Tiller::config[:environment])
.gsub('%t',template_name)
get_values(path)
end
def global_values
- path = @zk_config['values']['global'].gsub('%e',@config[:environment])
- @log.info("Fetching Zookeeper globals from #{path}")
+ path = @zk_config['values']['global'].gsub('%e',Tiller::config[:environment])
+ Tiller::log.info("Fetching Zookeeper globals from #{path}")
get_values(path)
end
def target_values(template_name)
path = @zk_config['values']['target']
- .gsub('%e',@config[:environment])
+ .gsub('%e',Tiller::config[:environment])
.gsub('%t',template_name)
get_values(path)
end
Oops, something went wrong.

0 comments on commit c2f6a4f

Please sign in to comment.