Class: Bosh::Director::Config

Inherits:
Object
  • Object
show all
Extended by:
DnsHelper
Defined in:
lib/bosh/director/config.rb

Overview

We are in the slow painful process of extracting all of this class-level behavior into instance behavior, much of it on the App class. When this process is complete, the Config will be responsible only for maintaining configuration information - not holding the state of the world.

Constant Summary collapse

CONFIG_OPTIONS =
[
  :base_dir,
  :cloud_options,
  :db,
  :dns,
  :dns_db,
  :dns_domain_name,
  :event_log,
  :logger,
  :max_tasks,
  :max_threads,
  :name,
  :process_uuid,
  :result,
  :revision,
  :task_checkpoint_interval,
  :uuid,
  :current_job,
  :encryption,
  :fix_stateful_nodes,
  :enable_snapshots
]

Constants included from DnsHelper

DnsHelper::SOA, DnsHelper::TTL_4H, DnsHelper::TTL_5M

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Methods included from DnsHelper

add_default_dns_server, canonical, default_dns_server, delete_dns_records, delete_empty_domain, dns_domain_name, dns_ns_record, dns_servers, invalid_dns, reverse_domain, reverse_host, update_dns_a_record, update_dns_ptr_record

Class Attribute Details

.db_configObject (readonly)

Returns the value of attribute db_config.



43
44
45
# File 'lib/bosh/director/config.rb', line 43

def db_config
  @db_config
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



361
362
363
# File 'lib/bosh/director/config.rb', line 361

def hash
  @hash
end

Class Method Details

.clearObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bosh/director/config.rb', line 45

def clear
  CONFIG_OPTIONS.each do |option|
    self.instance_variable_set("@#{option}".to_sym, nil)
  end

  Thread.list.each do |thr|
    thr[:bosh] = nil
  end

  @blobstore = nil
  @compiled_package_cache = nil
  @nats = nil
  @nats_rpc = nil
  @cloud = nil
end

.cloudObject



185
186
187
188
189
190
191
192
193
194
# File 'lib/bosh/director/config.rb', line 185

def cloud
  @lock.synchronize do
    if @cloud.nil?
      plugin = @cloud_options["plugin"]
      properties = @cloud_options["properties"]
      @cloud = Bosh::Clouds::Provider.create(plugin, properties)
    end
  end
  @cloud
end

.cloud_options=(options) ⇒ Object



218
219
220
221
222
223
# File 'lib/bosh/director/config.rb', line 218

def cloud_options=(options)
  @lock.synchronize do
    @cloud_options = options
    @cloud = nil
  end
end

.cloud_typeObject



179
180
181
182
183
# File 'lib/bosh/director/config.rb', line 179

def cloud_type
  if @cloud_options
    @cloud_options["plugin"]
  end
end

.compiled_package_cache_blobstoreObject



164
165
166
167
168
169
170
171
172
173
# File 'lib/bosh/director/config.rb', line 164

def compiled_package_cache_blobstore
  @lock.synchronize do
    if @compiled_package_cache_blobstore.nil? && use_compiled_package_cache?
      provider = @compiled_package_cache_options["provider"]
      options = @compiled_package_cache_options["options"]
      @compiled_package_cache_blobstore = Bosh::Blobstore::Client.safe_create(provider, options)
    end
  end
  @compiled_package_cache_blobstore
end

.compiled_package_cache_providerObject



175
176
177
# File 'lib/bosh/director/config.rb', line 175

def compiled_package_cache_provider
  use_compiled_package_cache? ? @compiled_package_cache_options["provider"] : nil
end

.configure(config) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
130
# File 'lib/bosh/director/config.rb', line 61

def configure(config)

  @base_dir = config["dir"]
  FileUtils.mkdir_p(@base_dir)

  # checkpoint task progress every 30 secs
  @task_checkpoint_interval = 30

  logging = config.fetch('logging', {})
  @log_device = Logger::LogDevice.new(logging.fetch('file', STDOUT))
  @logger = Logger.new(@log_device)
  @logger.level = Logger.const_get(logging.fetch('level', 'debug').upcase)
  @logger.formatter = ThreadFormatter.new

  # use a separate logger for redis to make it stfu
  redis_logger = Logger.new(@log_device)
  logging = config.fetch('redis', {}).fetch('logging', {})
  redis_logger_level = logging.fetch('level', 'info').upcase
  redis_logger.level = Logger.const_get(redis_logger_level)

  # Event logger supposed to be overridden per task,
  # the default one does nothing
  @event_log = EventLog::Log.new

  # by default keep only last 500 tasks in disk
  @max_tasks = config.fetch("max_tasks", 500).to_i

  @max_threads = config.fetch("max_threads", 32).to_i

  self.redis_options= {
    :host     => config["redis"]["host"],
    :port     => config["redis"]["port"],
    :password => config["redis"]["password"],
    :logger   => redis_logger
  }

  @revision = get_revision

  @logger.info("Starting BOSH Director: #{VERSION} (#{@revision})")

  @process_uuid = SecureRandom.uuid
  @nats_uri = config["mbus"]

  @cloud_options = config["cloud"]
  @compiled_package_cache_options = config["compiled_package_cache"]
  @name = config["name"] || ""

  @compiled_package_cache = nil

  @db_config = config['db']
  @db = configure_db(config["db"])
  @dns = config["dns"]
  @dns_domain_name = "bosh"
  if @dns
    @dns_db = configure_db(@dns["db"]) if @dns["db"]
    @dns_domain_name = canonical(@dns["domain_name"]) if @dns["domain_name"]
  end

  @uuid = override_uuid || retrieve_uuid
  @logger.info("Director UUID: #{@uuid}")

  @encryption = config["encryption"]
  @fix_stateful_nodes = config.fetch("scan_and_fix", {})
    .fetch("auto_fix_stateful_nodes", false)
  @enable_snapshots = config.fetch('snapshots', {}).fetch('enabled', false)

  Bosh::Clouds::Config.configure(self)

  @lock = Monitor.new
end

.configure_db(db_config) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/bosh/director/config.rb', line 148

def configure_db(db_config)
  patch_sqlite if db_config["adapter"] == "sqlite"

  connection_options = db_config.delete('connection_options') {{}}
  db_config.delete_if { |_, v| v.to_s.empty? }
  db_config = db_config.merge(connection_options)

  db = Sequel.connect(db_config)
  if logger
    db.logger = logger
    db.sql_log_level = :debug
  end

  db
end

.dns_enabled?Boolean

Returns:

  • (Boolean)


251
252
253
# File 'lib/bosh/director/config.rb', line 251

def dns_enabled?
  !@dns_db.nil?
end

.encryption?Boolean

Returns:

  • (Boolean)


255
256
257
# File 'lib/bosh/director/config.rb', line 255

def encryption?
  @encryption
end

.gen_uuidObject



346
347
348
# File 'lib/bosh/director/config.rb', line 346

def gen_uuid
  SecureRandom.uuid
end

.get_revisionObject



140
141
142
143
144
145
146
# File 'lib/bosh/director/config.rb', line 140

def get_revision
  Dir.chdir(File.expand_path("../../../../../..", __FILE__))
  revision_command = "(cat REVISION 2> /dev/null || " +
      "git show-ref --head --hash=8 2> /dev/null || " +
      "echo 00000000) | head -n1"
  `#{revision_command}`.strip
end

.job_cancelled?Boolean Also known as: task_checkpoint

Returns:

  • (Boolean)


204
205
206
# File 'lib/bosh/director/config.rb', line 204

def job_cancelled?
  @current_job.task_checkpoint if @current_job
end

.load_file(path) ⇒ Object



353
354
355
# File 'lib/bosh/director/config.rb', line 353

def load_file(path)
  Config.new(Psych.load_file(path))
end

.load_hash(hash) ⇒ Object



356
357
358
# File 'lib/bosh/director/config.rb', line 356

def load_hash(hash)
  Config.new(hash)
end

.log_dirObject



132
133
134
# File 'lib/bosh/director/config.rb', line 132

def log_dir
  File.dirname(@log_device.filename) if @log_device.filename
end

.logger=(logger) ⇒ Object



196
197
198
199
200
201
202
# File 'lib/bosh/director/config.rb', line 196

def logger=(logger)
  @logger = logger
  redis_options[:logger] = @logger
  if redis?
    redis.client.logger = @logger
  end
end

.natsObject



225
226
227
228
229
230
231
232
# File 'lib/bosh/director/config.rb', line 225

def nats
  @lock.synchronize do
    if @nats.nil?
      @nats = NATS.connect(:uri => @nats_uri, :autostart => false)
    end
  end
  @nats
end

.nats_rpcObject



234
235
236
237
238
239
240
241
# File 'lib/bosh/director/config.rb', line 234

def nats_rpc
  @lock.synchronize do
    if @nats_rpc.nil?
      @nats_rpc = NatsRpc.new
    end
  end
  @nats_rpc
end

.override_uuidObject



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/bosh/director/config.rb', line 311

def override_uuid
  new_uuid = nil

  if File.exists?(state_file)
    open(state_file, 'r+') do |file|

      # lock before read to avoid director/worker race condition
      file.flock(File::LOCK_EX)
      state = Yajl::Parser.parse(file) || {}
      # empty state file to prevent blocked processes from attempting to set UUID
      file.truncate(0)

      if state["uuid"]
        Bosh::Director::Models::DirectorAttribute.delete
        director = Bosh::Director::Models::DirectorAttribute.new
        director.uuid = state["uuid"]
        director.save
        @logger.info("Using director UUID #{state["uuid"]} from #{state_file}")
        new_uuid = state["uuid"]
      end

      # unlock after storing UUID
      file.flock(File::LOCK_UN)
    end

    FileUtils.rm_f(state_file)
  end

  new_uuid
end

.patch_sqliteObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/bosh/director/config.rb', line 263

def patch_sqlite
  return if @patched_sqlite
  @patched_sqlite = true

  require "sequel"
  require "sequel/adapters/sqlite"

  Sequel::SQLite::Database.class_eval do
    def connect(server)
      opts = server_opts(server)
      opts[:database] = ':memory:' if blank_object?(opts[:database])
      db = ::SQLite3::Database.new(opts[:database])
      db.busy_handler do |retries|
        Bosh::Director::Config.logger.debug "SQLITE BUSY, retry ##{retries}"
        sleep(0.1)
        retries < 20
      end

      connection_pragmas.each { |s| log_yield(s) { db.execute_batch(s) } }

      class << db
        attr_reader :prepared_statements
      end
      db.instance_variable_set(:@prepared_statements, {})

      db
    end
  end
end

.redisObject



243
244
245
# File 'lib/bosh/director/config.rb', line 243

def redis
  threaded[:redis] ||= Redis.new(redis_options)
end

.redis?Boolean

Returns:

  • (Boolean)


247
248
249
# File 'lib/bosh/director/config.rb', line 247

def redis?
  !threaded[:redis].nil?
end

.redis_optionsObject



210
211
212
# File 'lib/bosh/director/config.rb', line 210

def redis_options
  @redis_options ||= {}
end

.redis_options=(options) ⇒ Object



214
215
216
# File 'lib/bosh/director/config.rb', line 214

def redis_options=(options)
  @redis_options = options
end

.retrieve_uuidObject



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/bosh/director/config.rb', line 293

def retrieve_uuid
  directors = Bosh::Director::Models::DirectorAttribute.all
  director = directors.first

  if directors.size > 1
    @logger.warn("More than one UUID stored in director table, using #{director.uuid}")
  end

  unless director
    director = Bosh::Director::Models::DirectorAttribute.new
    director.uuid = gen_uuid
    director.save
    @logger.info("Generated director UUID #{director.uuid}")
  end

  director.uuid
end

.state_fileObject



342
343
344
# File 'lib/bosh/director/config.rb', line 342

def state_file
  File.join(base_dir, "state.json")
end

.threadedObject



259
260
261
# File 'lib/bosh/director/config.rb', line 259

def threaded
  Thread.current[:bosh] ||= {}
end

.use_compiled_package_cache?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/bosh/director/config.rb', line 136

def use_compiled_package_cache?
  !@compiled_package_cache_options.nil?
end