Class: TrustedSandbox::Config

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

Overview

Allows chaining so that specific invocations can override configurations. Usage:

general_config = Defaults.new.override(pool_size: 10, memory_limit: 100)
specific_invocation = general_config.override(memory_limit: 200)

Direct Known Subclasses

Defaults

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fallback_config, params = {}) ⇒ Config

Returns a new instance of Config.



132
133
134
135
136
137
138
# File 'lib/trusted_sandbox/config.rb', line 132

def initialize(fallback_config, params={})
  @docker_options_for_cert = {}
  @fallback_config = fallback_config
  params.each do |key, value|
    send "#{key}=", value
  end
end

Instance Attribute Details

#docker_auth_emailObject (readonly)

Returns the value of attribute docker_auth_email.



57
58
59
# File 'lib/trusted_sandbox/config.rb', line 57

def docker_auth_email
  @docker_auth_email
end

#docker_auth_neededObject (readonly)

Returns the value of attribute docker_auth_needed.



57
58
59
# File 'lib/trusted_sandbox/config.rb', line 57

def docker_auth_needed
  @docker_auth_needed
end

#docker_auth_passwordObject (readonly)

Returns the value of attribute docker_auth_password.



57
58
59
# File 'lib/trusted_sandbox/config.rb', line 57

def docker_auth_password
  @docker_auth_password
end

#docker_auth_userObject (readonly)

Returns the value of attribute docker_auth_user.



57
58
59
# File 'lib/trusted_sandbox/config.rb', line 57

def docker_auth_user
  @docker_auth_user
end

#docker_cert_pathObject

Returns the value of attribute docker_cert_path.



57
58
59
# File 'lib/trusted_sandbox/config.rb', line 57

def docker_cert_path
  @docker_cert_path
end

#docker_urlObject

Returns the value of attribute docker_url.



57
58
59
# File 'lib/trusted_sandbox/config.rb', line 57

def docker_url
  @docker_url
end

#fallback_configObject (readonly)

Returns the value of attribute fallback_config.



9
10
11
# File 'lib/trusted_sandbox/config.rb', line 9

def fallback_config
  @fallback_config
end

Class Method Details

.attr_accessor_with_fallback(*names) ⇒ Object

Usage:

attr_accessor_with_fallback :my_attribute

Equivalent to:

attr_reader_with_fallback :my_attribute
attr_writer :my_attribute


41
42
43
44
45
46
# File 'lib/trusted_sandbox/config.rb', line 41

def self.attr_accessor_with_fallback(*names)
  names.each do |name|
    attr_reader_with_fallback(name)
    attr_writer(name)
  end
end

.attr_reader_with_fallback(*names) ⇒ Object

Usage:

attr_reader_with_fallback :my_attribute

Equivalent to:

def my_attribute
  return @my_attribute if @my_attribute
  return fallback_config.my_attribute if @my_attribute.nil? and fallback_config.respond_to?(:my_attribute)
  nil
end


23
24
25
26
27
28
29
30
31
32
# File 'lib/trusted_sandbox/config.rb', line 23

def self.attr_reader_with_fallback(*names)
  names.each do |name|
    define_method name do
      value = instance_variable_get("@#{name}")
      return value unless value.nil?
      return fallback_config.send(name) if fallback_config.respond_to?(name)
      nil
    end
  end
end

Instance Method Details

#docker_login=(options = {}) ⇒ Object

Set hash used to authenticate with Docker All keys are mandatory

Parameters:

  • :user (Hash)

    a customizable set of options

  • :password (Hash)

    a customizable set of options

  • :email (Hash)

    a customizable set of options



107
108
109
110
111
112
# File 'lib/trusted_sandbox/config.rb', line 107

def docker_login=(options={})
  @docker_auth_needed = true
  @docker_auth_user = options[:user] || options['user']
  @docker_auth_password = options[:password] || options['password']
  @docker_auth_email = options[:email] || options['email']
end

#finished_configuringConfig

Called to do any necessary setup to allow staged configuration. These involve:

  • Setting Docker.options based on the cert path

  • Calling Docker.authenticate! with the login parameters, if these were entered

Returns:

  • (Config)

    self for chaining



118
119
120
121
122
123
124
125
# File 'lib/trusted_sandbox/config.rb', line 118

def finished_configuring
  Docker.options = @docker_options_for_cert.merge(docker_options)

  return self unless @docker_auth_needed
  Docker.authenticate! username: @docker_auth_user, password: @docker_auth_password, email: @docker_auth_email
  @docker_auth_needed = false
  self
end

#host_code_root_path=(path) ⇒ String

Returns the full path that was set. E.g.: ‘/home/user/tmp’.

Parameters:

  • path (String)

    shorthand version of the path. E.g.: ‘~/tmp’

Returns:

  • (String)

    the full path that was set. E.g.: ‘/home/user/tmp’



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

def host_code_root_path=(path)
  @host_code_root_path = File.expand_path(path)
end

#host_uid_pool_lock_path=(path) ⇒ String

Returns the full path that was set.

Parameters:

  • path (String)

    shorthand version of the path

Returns:

  • (String)

    the full path that was set



98
99
100
# File 'lib/trusted_sandbox/config.rb', line 98

def host_uid_pool_lock_path=(path)
  @host_uid_pool_lock_path = File.expand_path(path)
end

#override(params = {}) ⇒ Config

Returns a new object with the fallback object set to self.

Parameters:

  • params (Hash) (defaults to: {})

    hash of parameters used to override the existing config object’s attributes

Returns:

  • (Config)

    a new object with the fallback object set to self



62
63
64
# File 'lib/trusted_sandbox/config.rb', line 62

def override(params={})
  Config.send :new, self, params
end

#pool_max_uidInteger

Returns the upper boundary of the uid pool based on pool_min_uid and pool_size.

Returns:

  • (Integer)

    the upper boundary of the uid pool based on pool_min_uid and pool_size



67
68
69
# File 'lib/trusted_sandbox/config.rb', line 67

def pool_max_uid
  pool_min_uid + pool_size - 1
end