Module: Golem::Config

Defined in:
lib/golem/config.rb

Overview

Configuration management.

Constant Summary collapse

CFG_PATHS =

List of paths config file is searched for.

["/usr/local/etc/golem/golem.conf.rb", "/usr/local/etc/golem.conf.rb", "/etc/golem/golem.conf.rb", "/etc/golem.conf.rb", "~/golem.conf.rb"]
CFG_VARS =

List of available config variable names.

[:db, :user_home, :repository_dir, :cfg_path, :base_dir, :bin_dir, :hooks_dir, :keys_file_use_command, :keys_file_ssh_opts]

Class Method Summary collapse

Class Method Details

.auto_configure(path = nil, &block) ⇒ Object

Auto configure Golem. Tries to find config file, if one can be found executes it, otherwise calls configure.

Parameters:

  • path (String) (defaults to: nil)

    path to config file.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/golem/config.rb', line 10

def self.auto_configure(path = nil, &block)
	path = if ENV.key?('GOLEM_CONFIG') && File.exists?(ENV['GOLEM_CONFIG'])
 ENV['GOLEM_CONFIG']
	elsif ENV.key?('GOLEM_BASE') && File.exists?(ENV['GOLEM_BASE'].to_s + "/golem.conf.rb")
 ENV['GOLEM_BASE'].to_s + "/golem.conf.rb"
	else
 CFG_PATHS.find {|try_path| File.exists?(try_path)}
	end unless File.exists?(path.to_s)
	if File.exists?(path.to_s)
 @auto_configure_path = path.to_s
 @auto_configure_block = block
 require path.to_s
	end
	configure path unless @vars #configure was not called or there was no config file
end

.config_hashHash

Get configuration variables that is set (e.g. not nil).

Returns:

  • (Hash)

    configuration variables.



80
81
82
83
# File 'lib/golem/config.rb', line 80

def self.config_hash
	auto_configure unless @vars
	@vars.reject {|k, v| v.nil?}
end

.configure(path, &block) ⇒ Config .configure(opts, &block) ⇒ Config

Configure Golem with options given as argument, yield self then setting defaults.

Overloads:

  • .configure(path, &block) ⇒ Config

    Parameters:

    • path (String)

      path to config file (interpreted as :cfg_path => path).

  • .configure(opts, &block) ⇒ Config

    Parameters:

    • opts (Hash)

      options or single path .

    Options Hash (opts):

    • :db (String)

      db configuration (postgres url or ‘static’),

    • :user_home (String) — default: ENV['HOME']

      path to user’s home directory (needed to place .ssh/authorized_keys),

    • :repository_dir (String) — default: user_home + '/repositories'

      path to repositories, may be relative to user_home,

    • :cfg_path (String) — default: base_dir + '/golem.conf.rb'

      path config file,

    • :base_dir (String)

      path to base, defaults to in order ENV, basedir of config file (if exists), basedir of library,

    • :bin_dir (String) — default: base_dir + '/bin'

      path to directory containing the executables,

    • :hooks_dir (String) — default: base_dir + '/bin'

      path to directory containing hooks,

    • :keys_file_use_command (Boolean)

      controls (false) the .ssh/authorized_keys file syntax (command=“”_ or _environment=“”), see authorized_keys,

    • :keys_file_ssh_opts (String) — default: nil

      the ssh options to set in .ssh/authorized_keys file, see authorized_keys.

Yields:

  • (_self)

Yield Parameters:

  • _self (Golem::Config)

    the object that the method was called on

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/golem/config.rb', line 41

def self.configure(opts_or_path = nil, &block)
	opts = opts_or_path.is_a?(Hash) ? opts_or_path : {:cfg_path => opts_or_path}
	opts[:cfg_path] = @auto_configure_path if @auto_configure_path
	@vars = opts.reject {|k, v| ! CFG_VARS.include?(k)}
	@auto_configure_block.call(self) if @auto_configure_block
	yield self if block_given?
	self.user_home = ENV['HOME'] if user_home.nil? && ENV.key?('HOME')
	self.repository_dir = user_home + "/repositories" unless repository_dir
	unless base_dir
 self.base_dir = if ENV.key?('GOLEM_BASE')
		ENV['GOLEM_BASE']
 elsif File.exists?(cfg_path.to_s)
		File.dirname(cfg_path.to_s)
 else
		File.expand_path(File.dirname(__FILE__) + '/../..')
 end
	end
	self.cfg_path = base_dir + '/golem.conf.rb' unless cfg_path
	self.bin_dir = base_dir + '/bin' unless bin_dir
	self.hooks_dir = base_dir + '/hooks' unless hooks_dir
	self.keys_file_use_command = false unless keys_file_use_command
	self
end

.hook_path(hook) ⇒ String

Returns path to given hook.

Parameters:

  • hook (String)

    hook name.

Returns:

  • (String)

    path to given hook.



109
110
111
# File 'lib/golem/config.rb', line 109

def self.hook_path(hook)
	hooks_dir + "/" + hook.to_s
end

.keys_file_pathString

Returns path to authorized_keys file.

Returns:

  • (String)

    path to authorized_keys file.



92
93
94
# File 'lib/golem/config.rb', line 92

def self.keys_file_path
	user_home + "/.ssh/authorized_keys"
end

.method_missing(sym, *args, &block) ⇒ Object

Override method_missing to handle .config_var and .config_var= (e.g. Golem::Config.db = ‘static’).



71
72
73
74
75
76
# File 'lib/golem/config.rb', line 71

def self.method_missing(sym, *args, &block)
	auto_configure unless @vars
	return @vars[sym] if CFG_VARS.include?(sym)
	return @vars[sym.to_s[0..-2].to_sym] = args.first if sym.to_s.match(/=\z/) && CFG_VARS.include?(sym.to_s[0..-2].to_sym)
	super
end

.repository_base_pathString

Returns path to directory containing repositories.

Returns:

  • (String)

    path to directory containing repositories.



97
98
99
# File 'lib/golem/config.rb', line 97

def self.repository_base_path
	(repository_dir[0..0] == "/" ? '' : user_home + '/') + repository_dir
end

.repository_path(repo) ⇒ String

Returns path to given repository.

Parameters:

  • repo (String)

    repository name.

Returns:

  • (String)

    path to given repository.



103
104
105
# File 'lib/golem/config.rb', line 103

def self.repository_path(repo)
	repository_base_path + '/' + repo.to_s + '.git'
end

.respond_to?(sym) ⇒ Boolean

Override respond_to? to respond to .config_var and .config_var= (e.g. Golem::Config.db = ‘static’).

Returns:

  • (Boolean)


66
67
68
# File 'lib/golem/config.rb', line 66

def self.respond_to?(sym)
	CFG_VARS.include?(sym) || (sym.to_s.match(/=\z/) && CFG_VARS.include?(sym.to_s[0..-2].to_sym)) || super
end

.save!Object

Write configuration to file.



86
87
88
89
# File 'lib/golem/config.rb', line 86

def self.save!
	abort "No configuration path given!" unless cfg_path
	File.open(cfg_path, 'w') {|f| f.write("Golem.configure do |cfg|\n" + config_hash.collect {|k, v| "\tcfg.#{k.to_s} = \"#{v.to_s}\""}.join("\n") + "\nend\n")}
end