Class: VagrantLXD::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-lxd/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vagrant-lxd/config.rb', line 42

def initialize
  @name = UNSET_VALUE
  @image = UNSET_VALUE
  @timeout = UNSET_VALUE
  @config = UNSET_VALUE
  @devices = UNSET_VALUE
  @environment = UNSET_VALUE
  @nesting = UNSET_VALUE
  @privileged = UNSET_VALUE
  @ephemeral = UNSET_VALUE
  @profiles = UNSET_VALUE
  @project = UNSET_VALUE
  @api_endpoint = UNSET_VALUE
  @vagrant_uid = UNSET_VALUE
  @vagrant_gid = UNSET_VALUE
  @client_certificate = UNSET_VALUE
  @client_key = UNSET_VALUE
end

Instance Attribute Details

#api_endpointObject

Returns the value of attribute api_endpoint.



25
26
27
# File 'lib/vagrant-lxd/config.rb', line 25

def api_endpoint
  @api_endpoint
end

#client_certificateObject

Returns the value of attribute client_certificate.



39
40
41
# File 'lib/vagrant-lxd/config.rb', line 39

def client_certificate
  @client_certificate
end

#client_keyObject

Returns the value of attribute client_key.



40
41
42
# File 'lib/vagrant-lxd/config.rb', line 40

def client_key
  @client_key
end

#configObject

Returns the value of attribute config.



29
30
31
# File 'lib/vagrant-lxd/config.rb', line 29

def config
  @config
end

#devicesObject

Returns the value of attribute devices.



30
31
32
# File 'lib/vagrant-lxd/config.rb', line 30

def devices
  @devices
end

#environmentObject

Returns the value of attribute environment.



31
32
33
# File 'lib/vagrant-lxd/config.rb', line 31

def environment
  @environment
end

#ephemeralObject

Returns the value of attribute ephemeral.



32
33
34
# File 'lib/vagrant-lxd/config.rb', line 32

def ephemeral
  @ephemeral
end

#imageObject

Returns the value of attribute image.



27
28
29
# File 'lib/vagrant-lxd/config.rb', line 27

def image
  @image
end

#nameObject

Returns the value of attribute name.



26
27
28
# File 'lib/vagrant-lxd/config.rb', line 26

def name
  @name
end

#nestingObject

Returns the value of attribute nesting.



33
34
35
# File 'lib/vagrant-lxd/config.rb', line 33

def nesting
  @nesting
end

#privilegedObject

Returns the value of attribute privileged.



34
35
36
# File 'lib/vagrant-lxd/config.rb', line 34

def privileged
  @privileged
end

#profilesObject

Returns the value of attribute profiles.



35
36
37
# File 'lib/vagrant-lxd/config.rb', line 35

def profiles
  @profiles
end

#projectObject

Returns the value of attribute project.



36
37
38
# File 'lib/vagrant-lxd/config.rb', line 36

def project
  @project
end

#timeoutObject

Returns the value of attribute timeout.



28
29
30
# File 'lib/vagrant-lxd/config.rb', line 28

def timeout
  @timeout
end

#vagrant_gidObject

Returns the value of attribute vagrant_gid.



38
39
40
# File 'lib/vagrant-lxd/config.rb', line 38

def vagrant_gid
  @vagrant_gid
end

#vagrant_uidObject

Returns the value of attribute vagrant_uid.



37
38
39
# File 'lib/vagrant-lxd/config.rb', line 37

def vagrant_uid
  @vagrant_uid
end

Instance Method Details

#finalize!Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/vagrant-lxd/config.rb', line 171

def finalize!
  if name == UNSET_VALUE
    @name = nil
  end

  if image == UNSET_VALUE
    @image = nil
  end

  if config == UNSET_VALUE
    @config = {}
  end

  if devices == UNSET_VALUE
    @devices = {}
  end

  if environment == UNSET_VALUE
    @environment = {}
  end

  if nesting == UNSET_VALUE
    @nesting = nil
  end

  if privileged == UNSET_VALUE
    @privileged = nil
  end

  if ephemeral == UNSET_VALUE
    @ephemeral = false
  end

  if profiles == UNSET_VALUE
    @profiles = ['default']
  end

  if project == UNSET_VALUE
    @project = 'default'
  end

  if timeout == UNSET_VALUE
    @timeout = 10
  end

  if api_endpoint == UNSET_VALUE
    @api_endpoint = URI('https://127.0.0.1:8443')
  elsif String === api_endpoint
    @api_endpoint = URI(api_endpoint)
  end

  if vagrant_uid == UNSET_VALUE
    @vagrant_uid = 1000
  end

  if vagrant_gid == UNSET_VALUE
    @vagrant_gid = vagrant_uid
  end

  if client_certificate == UNSET_VALUE
    @client_certificate = nil
  elsif String === client_certificate
    @client_certificate = File.expand_path(client_certificate)
  end

  if client_key == UNSET_VALUE
    @client_key = nil
  elsif String === client_key
    @client_key = File.expand_path(client_key)
  end
end

#validate(machine) ⇒ 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
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
# File 'lib/vagrant-lxd/config.rb', line 61

def validate(machine)
  errors = _detected_errors

  unless name.nil?
    if not name.is_a? String
      errors << "Invalid `name' (value must be a string): #{name.inspect}"
    elsif name.size >= 64
      errors << "Invalid `name' (value must be less than 64 characters): #{name.inspect}"
    elsif name =~ /[^a-zA-Z0-9-]/
      errors << "Invalid `name' (value must contain only letters, numbers, and hyphens): #{name.inspect}"
    end
  end

  unless image.nil? or image.is_a? String
    errors << "Invalid `image' (value must be a string): #{image.inspect}"
  end

  if not timeout.is_a? Integer
    errors << "Invalid `timeout' (value must be an integer): #{timeout.inspect}"
  elsif timeout < 1
    errors << "Invalid `timeout' (value must be positive): #{timeout.inspect}"
  end

  if not config.is_a? Hash
    errors << "Invalid `config' (value must be a hash): #{config.inspect}"
  elsif not config.keys.all? { |x| x.is_a? Symbol }
    errors << "Invalid `config' (keys must be symbols): #{config.inspect}"
  end

  if not devices.is_a? Hash
    errors << "Invalid `devices' (value must be a hash): #{devices.inspect}"
  elsif not devices.keys.all? { |x| x.is_a? String or x.is_a? Symbol }
    errors << "Invalid `devices' (keys must be strings or symbols): #{devices.inspect}"
  elsif devices.keys.any? { |x| x =~ /^(.*([^a-z0-9.\-_]).*)$/ }
    errors << "Invalid `devices' (device name `#{$1}' contains invalid character '#{$2}'): #{devices.inspect}"
  elsif not devices.values.all? { |x| x.is_a? Hash }
    errors << "Invalid `devices' (values must be hashes): #{devices.inspect}"
  elsif not devices.values.map(&:values).flatten.all? { |x| x.is_a? String }
    errors << "Invalid `devices' (device value must be strings): #{devices.inspect}"
  end

  if not environment.is_a? Hash
    errors << "Invalid `environment' (value must be a hash): #{environment.inspect}"
  elsif not environment.keys.all? { |x| x.is_a? String or x.is_a? Symbol }
    errors << "Invalid `environment' (keys must be strings or symbols): #{environment.inspect}"
  elsif not environment.values.all? { |x| x.is_a? String }
    errors << "Invalid `environment' (values must be strings): #{environment.inspect}"
  end

  begin
    URI(api_endpoint).scheme == 'https' or raise URI::InvalidURIError
  rescue URI::InvalidURIError
    errors << "Invalid `api_endpoint' (value must be a valid HTTPS address): #{api_endpoint.inspect}"
  end

  unless [true, false, nil].include? nesting
    errors << "Invalid `nesting' (value must be true or false): #{nesting.inspect}"
  end

  unless [true, false, nil].include? privileged
    errors << "Invalid `privileged' (value must be true or false): #{privileged.inspect}"
  end

  unless [true, false].include? ephemeral
    errors << "Invalid `ephemeral' (value must be true or false): #{ephemeral.inspect}"
  end

  unless profiles.is_a? Array and profiles == profiles.grep(String)
    errors << "Invalid `profiles' (value must be an array of strings): #{profiles.inspect}"
  end

  unless project.is_a? String
    errors << "Invalid `project' (value must be a string): #{project.inspect}"
  end

  unless vagrant_uid.is_a? Integer and vagrant_uid >= 0
    errors << "Invalid `vagrant_uid' (value must be a non-negative integer): #{vagrant_uid.inspect}"
  end

  unless vagrant_gid.is_a? Integer and vagrant_gid >= 0
    errors << "Invalid `vagrant_gid' (value must be a non-negative integer): #{vagrant_gid.inspect}"
  end

  if client_certificate.is_a? String
    begin
      OpenSSL::X509::Certificate.new(File.read(client_certificate))
    rescue Exception => e
      errors << "Invalid `client_certificate' (unable to read certificate): #{e.message}"
    end
  elsif not client_certificate.nil?
    errors << "Invalid `client_certificate' (value must be a string): #{client_certificate.inspect}"
  elsif not client_key.nil?
    errors << "You must provide both `client_certificate' and `client_key'"
  end

  if client_key.is_a? String
    begin
      OpenSSL::PKey.read(File.read(client_key))
    rescue Exception => e
      errors << "Invalid `client_key' (unable to read key): #{e.message}"
    end
  elsif not client_key.nil?
    errors << "Invalid `client_key' (value must be a string): #{client_key.inspect}"
  elsif not client_certificate.nil?
    errors << "You must provide both `client_certificate' and `client_key'"
  end

  { Version::NAME => errors }
end