Class: Bosh::Director::Config

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

Overview

We want to shift from class methods to instance methods here.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.base_dirObject

Returns the value of attribute base_dir.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def base_dir
  @base_dir
end

.cloud_optionsObject

Returns the value of attribute cloud_options.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def cloud_options
  @cloud_options
end

.config_server_urlObject (readonly)

Returns the value of attribute config_server_url.



46
47
48
# File 'lib/bosh/director/config.rb', line 46

def config_server_url
  @config_server_url
end

.current_jobObject

Returns the value of attribute current_job.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def current_job
  @current_job
end

.dbObject

Returns the value of attribute db.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def db
  @db
end

.db_configObject (readonly)

Returns the value of attribute db_config.



46
47
48
# File 'lib/bosh/director/config.rb', line 46

def db_config
  @db_config
end

.default_ssh_optionsObject

Returns the value of attribute default_ssh_options.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def default_ssh_options
  @default_ssh_options
end

.director_ipsObject (readonly)

Returns the value of attribute director_ips.



46
47
48
# File 'lib/bosh/director/config.rb', line 46

def director_ips
  @director_ips
end

.dnsObject

Returns the value of attribute dns.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def dns
  @dns
end

.dns_dbObject

Returns the value of attribute dns_db.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def dns_db
  @dns_db
end

.dns_domain_nameObject

Returns the value of attribute dns_domain_name.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def dns_domain_name
  @dns_domain_name
end

.enable_post_deployObject

Returns the value of attribute enable_post_deploy.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def enable_post_deploy
  @enable_post_deploy
end

.enable_snapshotsObject

Returns the value of attribute enable_snapshots.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def enable_snapshots
  @enable_snapshots
end

.enable_virtual_delete_vmsObject

Returns the value of attribute enable_virtual_delete_vms.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def enable_virtual_delete_vms
  @enable_virtual_delete_vms
end

.encryptionObject

Returns the value of attribute encryption.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def encryption
  @encryption
end

.event_logObject

Returns the value of attribute event_log.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def event_log
  @event_log
end

.fix_stateful_nodesObject

Returns the value of attribute fix_stateful_nodes.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def fix_stateful_nodes
  @fix_stateful_nodes
end

.flush_arpObject

Returns the value of attribute flush_arp.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def flush_arp
  @flush_arp
end

.generate_vm_passwordsObject

Returns the value of attribute generate_vm_passwords.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def generate_vm_passwords
  @generate_vm_passwords
end

.ignore_missing_gatewayObject (readonly)

Returns the value of attribute ignore_missing_gateway.



46
47
48
# File 'lib/bosh/director/config.rb', line 46

def ignore_missing_gateway
  @ignore_missing_gateway
end

.keep_unreachable_vmsObject

Returns the value of attribute keep_unreachable_vms.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def keep_unreachable_vms
  @keep_unreachable_vms
end

.loggerObject

Returns the value of attribute logger.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def logger
  @logger
end

.max_tasksObject

Returns the value of attribute max_tasks.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def max_tasks
  @max_tasks
end

.max_threadsObject

Returns the value of attribute max_threads.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def max_threads
  @max_threads
end

.max_vm_create_triesObject

Returns the value of attribute max_vm_create_tries.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def max_vm_create_tries
  @max_vm_create_tries
end

.nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def name
  @name
end

.nats_uriObject

Returns the value of attribute nats_uri.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def nats_uri
  @nats_uri
end

.parse_config_valuesObject (readonly)

Returns the value of attribute parse_config_values.



46
47
48
# File 'lib/bosh/director/config.rb', line 46

def parse_config_values
  @parse_config_values
end

.process_uuidObject

Returns the value of attribute process_uuid.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def process_uuid
  @process_uuid
end

.record_eventsObject (readonly)

Returns the value of attribute record_events.



46
47
48
# File 'lib/bosh/director/config.rb', line 46

def record_events
  @record_events
end

.remove_dev_toolsObject

Returns the value of attribute remove_dev_tools.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def remove_dev_tools
  @remove_dev_tools
end

.resultObject

Returns the value of attribute result.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def result
  @result
end

.revisionObject

Returns the value of attribute revision.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def revision
  @revision
end

.task_checkpoint_intervalObject

Returns the value of attribute task_checkpoint_interval.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def task_checkpoint_interval
  @task_checkpoint_interval
end

.trusted_certsObject

Returns the value of attribute trusted_certs.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def trusted_certs
  @trusted_certs
end

.uuidObject

Returns the value of attribute uuid.



11
12
13
# File 'lib/bosh/director/config.rb', line 11

def uuid
  @uuid
end

Class Method Details

.clearObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bosh/director/config.rb', line 56

def clear
  self.instance_variables.each do |ivar|
    self.instance_variable_set(ivar, nil)
  end

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

  @blobstore = nil

  @compiled_package_cache = nil
  @compiled_package_blobstore = nil
  @compiled_package_cache_options = nil

  @nats = nil
  @nats_rpc = nil
  @cloud = nil
end

.cloudObject



245
246
247
248
249
250
251
252
# File 'lib/bosh/director/config.rb', line 245

def cloud
  @lock.synchronize do
    if @cloud.nil?
      @cloud = Bosh::Clouds::Provider.create(@cloud_options, @uuid)
    end
  end
  @cloud
end

.cloud_typeObject



239
240
241
242
243
# File 'lib/bosh/director/config.rb', line 239

def cloud_type
  if @cloud_options
    @cloud_options['plugin'] || @cloud_options['provider']['name']
  end
end

.compiled_package_cache_blobstoreObject



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

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



235
236
237
# File 'lib/bosh/director/config.rb', line 235

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

.configure(config) ⇒ Object



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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/bosh/director/config.rb', line 76

def configure(config)
  @max_vm_create_tries = Integer(config.fetch('max_vm_create_tries', 5))
  @flush_arp = config.fetch('flush_arp', false)

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

  # checkpoint task progress every 30 secs
  @task_checkpoint_interval = 30

  logging_config = config.fetch('logging', {})
  if logging_config.has_key?('file')
    @log_file_path = logging_config.fetch('file')
    shared_appender = Logging.appenders.file(
      'DirectorLogFile',
      filename: @log_file_path,
      layout: ThreadFormatter.layout
    )
  else
    shared_appender = Logging.appenders.io(
      'DirectorStdOut',
      STDOUT,
      layout: ThreadFormatter.layout
    )
  end

  @logger = Logging::Logger.new('Director')
  @logger.add_appenders(shared_appender)
  @logger.level = Logging.levelify(logging_config.fetch('level', 'debug'))

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

  # by default keep only last 100 tasks of each type in disk
  @max_tasks = config.fetch('max_tasks', 100).to_i

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

  @revision = get_revision

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

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

  @default_ssh_options = config['default_ssh_options']

  @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']
  if @dns && @dns['db']
    @dns_db = configure_db(@dns['db'])
    if @dns_db
      # Load these constants early.
      # These constants are not 'require'd, they are 'autoload'ed
      # in models.rb. We're seeing that in 1.9.3 that sometimes
      # the constants loaded from one thread are not visible to other threads,
      # causing failures.
      # These constants cannot be required because they are Sequel model classes
      # that refer to database configuration that is only present when the (optional)
      # powerdns job is present and configured and points to a valid DB.
      # This is an attempt to make sure the constants are loaded
      # before forking off to other threads, hopefully eliminating the errors.
      Bosh::Director::Models::Dns::Record.class
      Bosh::Director::Models::Dns::Domain.class
    end
  end

  @dns_manager = DnsManagerProvider.create
  @uuid = override_uuid || Bosh::Director::Models::DirectorAttribute.find_or_create_uuid(@logger)
  @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)

  @trusted_certs = config['trusted_certs'] || ''
  @ignore_missing_gateway = config['ignore_missing_gateway']

  @keep_unreachable_vms = config.fetch('keep_unreachable_vms', false)
  @enable_post_deploy = config.fetch('enable_post_deploy', false)
  @generate_vm_passwords = config.fetch('generate_vm_passwords', false)
  @remove_dev_tools = config['remove_dev_tools']
  @record_events = config.fetch('record_events', false)
  @local_dns = config.fetch('local_dns', false)

  @enable_virtual_delete_vms = config.fetch('enable_virtual_delete_vms', false)

  @director_ips = Socket.ip_address_list.reject { |addr| !addr.ip? || !addr.ipv4? || addr.ipv4_loopback? || addr.ipv6_loopback? }.map { |addr| addr.ip_address }


  @parse_config_values = config.fetch('parse_config_values', false)
  if @parse_config_values
    @config_server_url = config['config_server_url']
  end

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

  @lock = Monitor.new
end

.configure_db(db_config) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/bosh/director/config.rb', line 201

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

  Sequel.default_timezone = :utc
  db = Sequel.connect(connection_config)

  Bosh::Common.retryable(sleep: 0.5, tries: 20, on: [Exception]) do
    db.extension :connection_validator
    true
  end

  db.pool.connection_validation_timeout = -1
  if logger
    db.logger = logger
    db.sql_log_level = :debug
  end

  db
end

.cpi_task_logObject



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

def cpi_task_log
  Config.cloud_options.fetch('properties', {}).fetch('cpi_log')
end

.director_poolObject



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

def director_pool
  @director_pool ||= Socket.gethostname
end

.encryption?Boolean

Returns:

  • (Boolean)


287
288
289
# File 'lib/bosh/director/config.rb', line 287

def encryption?
  @encryption
end

.generate_temp_dirObject



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

def generate_temp_dir
  temp_dir = Dir.mktmpdir
  ENV["TMPDIR"] = temp_dir
  FileUtils.mkdir_p(temp_dir)
  at_exit do
    begin
      if $!
        status = $!.is_a?(::SystemExit) ? $!.status : 1
      else
        status = 0
      end
      FileUtils.rm_rf(temp_dir)
    ensure
      exit status
    end
  end
  temp_dir
end

.get_revisionObject



193
194
195
196
197
198
199
# File 'lib/bosh/director/config.rb', line 193

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)


262
263
264
# File 'lib/bosh/director/config.rb', line 262

def job_cancelled?
  @current_job.task_checkpoint if @current_job
end

.load_file(path) ⇒ Object



349
350
351
# File 'lib/bosh/director/config.rb', line 349

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

.load_hash(hash) ⇒ Object



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

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

.log_dirObject



185
186
187
# File 'lib/bosh/director/config.rb', line 185

def log_dir
  File.dirname(@log_file_path) if @log_file_path
end

.nats_rpcObject



275
276
277
278
279
280
281
282
283
284
285
# File 'lib/bosh/director/config.rb', line 275

def nats_rpc
  # double-check locking to reduce synchronization
  if @nats_rpc.nil?
    @lock.synchronize do
      if @nats_rpc.nil?
        @nats_rpc = NatsRpc.new(@nats_uri)
      end
    end
  end
  @nats_rpc
end

.override_uuidObject



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
341
342
343
344
345
# File 'lib/bosh/director/config.rb', line 314

def override_uuid
  new_uuid = nil
  state_file = File.join(base_dir, 'state.json')

  begin
    open(state_file, 'r+') do |file|

      # Lock before read to avoid director/worker race condition
      file.flock(File::LOCK_EX)
      state = JSON.parse(file.read) || {}

      # Empty state file to prevent blocked processes from attempting to set UUID
      file.truncate(0)

      if state['uuid']
        Bosh::Director::Models::DirectorAttribute.update_or_create_uuid(state['uuid'], @logger)
        @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)

  rescue Errno::ENOENT
    # Catch race condition since another process (director/worker) might migrated the state
  end

  new_uuid
end

.threadedObject



291
292
293
# File 'lib/bosh/director/config.rb', line 291

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

.use_compiled_package_cache?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/bosh/director/config.rb', line 189

def use_compiled_package_cache?
  !@compiled_package_cache_options.nil?
end

Instance Method Details

#backup_blobstore_configObject



415
416
417
# File 'lib/bosh/director/config.rb', line 415

def backup_blobstore_config
  hash['backup_destination']
end

#blobstore_configObject



411
412
413
# File 'lib/bosh/director/config.rb', line 411

def blobstore_config
  hash.fetch('blobstore')
end

#configure_evil_config_singleton!Object



427
428
429
# File 'lib/bosh/director/config.rb', line 427

def configure_evil_config_singleton!
  Config.configure(hash)
end

#dbObject



407
408
409
# File 'lib/bosh/director/config.rb', line 407

def db
  Config.configure_db(hash['db'])
end

#director_poolObject



423
424
425
# File 'lib/bosh/director/config.rb', line 423

def director_pool
  Config.director_pool
end

#get_uuid_providerObject



431
432
433
# File 'lib/bosh/director/config.rb', line 431

def get_uuid_provider
  Bosh::Director::Api::DirectorUUIDProvider.new(Config)
end

#identity_providerObject



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/bosh/director/config.rb', line 370

def identity_provider
  @identity_provider ||= begin
    # no fetching w defaults?
    user_management = hash['user_management']
    user_management ||= {'provider' => 'local'}
    provider_name = user_management['provider']

    providers = {
      'uaa' => Bosh::Director::Api::UAAIdentityProvider,
      'local' => Bosh::Director::Api::LocalIdentityProvider,
    }
    provider_class = providers[provider_name]

    if provider_class.nil?
      raise ArgumentError,
        "Unknown user management provider '#{provider_name}', " +
          "available providers are: #{providers.keys.join(", ")}"
    end

    Config.logger.debug("Director configured with '#{provider_name}' user management provider")
    provider_class.new(user_management[provider_name] || {})
  end
end

#log_access_events_to_syslogObject



419
420
421
# File 'lib/bosh/director/config.rb', line 419

def log_access_events_to_syslog
  hash['log_access_events_to_syslog']
end

#nameObject



358
359
360
# File 'lib/bosh/director/config.rb', line 358

def name
  hash['name']
end

#portObject



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

def port
  hash['port']
end

#record_eventsObject



435
436
437
# File 'lib/bosh/director/config.rb', line 435

def record_events
  hash.fetch('record_events', false)
end

#scheduled_jobsObject



366
367
368
# File 'lib/bosh/director/config.rb', line 366

def scheduled_jobs
  hash['scheduled_jobs'] || []
end

#worker_loggerObject



394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/bosh/director/config.rb', line 394

def worker_logger
  logger = Logging::Logger.new('DirectorWorker')
  logging_config = hash.fetch('logging', {})
  worker_logging = hash.fetch('delayed_job', {}).fetch('logging', {})
  if worker_logging.has_key?('file')
    logger.add_appenders(Logging.appenders.file('DirectorWorkerFile', filename: worker_logging.fetch('file'), layout: ThreadFormatter.layout))
  else
    logger.add_appenders(Logging.appenders.stdout('DirectorWorkerIO', layout: ThreadFormatter.layout))
  end
  logger.level = Logging.levelify(logging_config.fetch('level', 'debug'))
  logger
end