Class: ChefFixie::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/chef_fixie/config.rb

Overview

ChefFixie::Config

configuration for the fixie command.

Example Config File:

Fixie.configure do |mapper|
  mapper.authz_uri = 'http://authz.example.com:5959'
end

Constant Summary collapse

KEYS =
[:authz_uri, :sql_database, :superuser_id, :pivotal_key]

Instance Method Summary collapse

Instance Method Details

#dig(hash, list) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/chef_fixie/config.rb', line 157

def dig(hash, list)
  if hash.respond_to?(:get)
    hash.get(*list)
  elsif hash.nil?
    nil
  elsif list.empty?
    hash
  else
    element = list.shift
    if hash.has_key?(element)
      dig(hash[element], list)
    else
      nil
    end
  end
end

#example_configObject



94
95
96
97
98
99
100
101
# File 'lib/chef_fixie/config.rb', line 94

def example_config
  txt = ["Fixie.configure do |mapper|"]
  KEYS.each do |key|
    txt << "  mapper.%s = %s" % [key.to_s, '"something"']
  end
  txt << "end"
  txt.join("\n")
end

#load_from_pc(dir = "/etc/opscode") ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/chef_fixie/config.rb', line 103

def load_from_pc(dir = "/etc/opscode")
  configdir = Pathname.new(dir)

  config_files = %w{chef-server-running.json}
  config = load_json_from_path([configdir], config_files)

  secrets = load_secrets_from_path([configdir], %w{private-chef-secrets.json} )

  authz_config = config["private_chef"]["oc_bifrost"]
  authz_vip = authz_config["vip"]
  authz_port = authz_config["port"]
  @authz_uri = "http://#{authz_vip}:#{authz_port}"

  @superuser_id = dig(secrets, %w{oc_bifrost superuser_id}) || authz_config["superuser_id"]

  sql_config = config["private_chef"]["postgresql"]
  erchef_config = config["private_chef"]["opscode-erchef"]

  sql_user = sql_config["sql_user"] || erchef_config["sql_user"]
  sql_pw = dig(secrets, %w{opscode_erchef sql_password}) || sql_config["sql_password"] || erchef_config["sql_password"]
  sql_vip = sql_config["vip"]
  sql_port = sql_config["port"]

  @sql_database = "postgres://#{sql_user}:#{sql_pw}@#{sql_vip}/opscode_chef"

  @pivotal_key = configdir + "pivotal.pem"
end

#load_json_from_path(pathlist, filelist) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/chef_fixie/config.rb', line 131

def load_json_from_path(pathlist, filelist)
  parser = FFI_Yajl::Parser.new
  pathlist.each do |path|
    filelist.each do |file|
      configfile = path + file
      if configfile.file?
        data = File.read(configfile)
        return parser.parse(data)
      end
    end
  end
end

#load_secrets_from_path(pathlist, filelist) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/chef_fixie/config.rb', line 144

def load_secrets_from_path(pathlist, filelist)
  pathlist.each do |path|
    filelist.each do |file|
      configfile = path + file
      if configfile.file?
        data = Veil::CredentialCollection::ChefSecretsFile.from_file(configfile)
        return data
      end
    end
  end
  nil
end

#merge_opts(opts = {}) ⇒ Object



70
71
72
73
74
# File 'lib/chef_fixie/config.rb', line 70

def merge_opts(opts = {})
  opts.each do |key, value|
    send("#{key}=".to_sym, value)
  end
end

#to_aryObject

this is waaay tightly coupled to ::Backend’s initialize method



77
78
79
# File 'lib/chef_fixie/config.rb', line 77

def to_ary
  [couchdb_uri, database, auth_uri, authz_couch, sql_database, superuser_id].compact
end

#to_textObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chef_fixie/config.rb', line 81

def to_text
  txt = ["### ChefFixie::Config"]
  max_key_len = KEYS.inject(0) do |max, k|
    key_len = k.to_s.length
    key_len > max ? key_len : max
  end
  KEYS.each do |key|
    value = send(key) || "default"
    txt << "# %#{max_key_len}s: %s" % [key.to_s, value]
  end
  txt.join("\n")
end