Class: Bolt::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/target.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, options = nil) ⇒ Target

URI can be passes as nil



164
165
166
167
168
169
170
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
# File 'lib/bolt/target.rb', line 164

def initialize(uri, options = nil)
  # lazy-load expensive gem code
  require 'addressable/uri'

  @uri = uri
  @uri_obj = parse(uri)
  @options = options || {}
  @options.freeze

  if @options['user']
    @user = @options['user']
  end

  if @options['password']
    @password = @options['password']
  end

  if @options['port']
    @port = @options['port']
  end

  if @options['protocol']
    @protocol = @options['protocol']
  end

  if @options['host']
    @host = @options['host']
  end

  # WARNING: name should never be updated
  @name = @options['name'] || @uri
end

Instance Attribute Details

#inventoryObject

CODEREVIEW: this feels wrong. The altertative is threading inventory through the executor to the RemoteTransport



154
155
156
# File 'lib/bolt/target.rb', line 154

def inventory
  @inventory
end

#optionsObject (readonly)

Returns the value of attribute options.



151
152
153
# File 'lib/bolt/target.rb', line 151

def options
  @options
end

#uriObject

CODEREVIEW: this feels wrong. The altertative is threading inventory through the executor to the RemoteTransport



154
155
156
# File 'lib/bolt/target.rb', line 154

def uri
  @uri
end

Class Method Details

.from_asserted_hash(hash) ⇒ Object

Satisfies the Puppet datatypes API



159
160
161
# File 'lib/bolt/target.rb', line 159

def self.from_asserted_hash(hash)
  new(hash['uri'], hash['options'])
end

Instance Method Details

#detailObject



288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/bolt/target.rb', line 288

def detail
  {
    'name' => name,
    'alias' => target_alias,
    'config' => {
      'transport' => transport,
      transport => options
    },
    'vars' => vars,
    'facts' => facts,
    'features' => features.to_a,
    'plugin_hooks' => plugin_hooks
  }
end

#eql?(other) ⇒ Boolean Also known as: ==

TODO: WHAT does equality mean here? should we just compare names? is there something else that is meaninful?

Returns:

  • (Boolean)


258
259
260
261
262
263
264
# File 'lib/bolt/target.rb', line 258

def eql?(other)
  if self.class.equal?(other.class)
    @uri ? @uri == other.uri : @name == other.name
  else
    false
  end
end

#factsObject



248
249
250
# File 'lib/bolt/target.rb', line 248

def facts
  @inventory.facts(self)
end

#featuresObject Also known as: feature_set



227
228
229
230
231
232
233
# File 'lib/bolt/target.rb', line 227

def features
  if @inventory
    @inventory.features(self)
  else
    Set.new
  end
end

#hashObject



267
268
269
# File 'lib/bolt/target.rb', line 267

def hash
  @uri.hash ^ @options.hash
end

#hostObject Also known as: safe_name



303
304
305
# File 'lib/bolt/target.rb', line 303

def host
  @uri_obj&.hostname || @host
end

#nameObject



308
309
310
# File 'lib/bolt/target.rb', line 308

def name
  @name || @uri
end

#passwordObject



333
334
335
# File 'lib/bolt/target.rb', line 333

def password
  Addressable::URI.unencode_component(@uri_obj&.password) || @password
end

#plugin_hooksObject



236
237
238
239
240
241
242
# File 'lib/bolt/target.rb', line 236

def plugin_hooks
  if @inventory
    @inventory.plugin_hooks(self)
  else
    {}
  end
end

#portObject



316
317
318
# File 'lib/bolt/target.rb', line 316

def port
  @uri_obj&.port || @port
end

#protocolObject



325
326
327
# File 'lib/bolt/target.rb', line 325

def protocol
  @uri_obj&.scheme || @protocol
end

#remote?Boolean

Returns:

  • (Boolean)


312
313
314
# File 'lib/bolt/target.rb', line 312

def remote?
  @uri_obj&.scheme == 'remote' || @protocol == 'remote'
end

#target_aliasObject



252
253
254
# File 'lib/bolt/target.rb', line 252

def target_alias
  @inventory.target_alias(self)
end

#to_hObject



276
277
278
279
280
281
282
283
284
285
286
# File 'lib/bolt/target.rb', line 276

def to_h
  options.merge(
    'name' => name,
    'uri' => uri,
    'protocol' => protocol,
    'user' => user,
    'password' => password,
    'host' => host,
    'port' => port
  )
end

#to_sObject



271
272
273
274
# File 'lib/bolt/target.rb', line 271

def to_s
  opts = @options.select { |k, _| PRINT_OPTS.include? k }
  "Target('#{@uri}', #{opts})"
end

#transportObject

transport is separate from protocol for remote targets.



321
322
323
# File 'lib/bolt/target.rb', line 321

def transport
  remote? ? 'remote' : protocol
end

#update_conf(conf) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/bolt/target.rb', line 197

def update_conf(conf)
  @protocol = conf[:transport]

  t_conf = conf[:transports][transport.to_sym] || {}
  # Override url methods
  @user = t_conf['user']
  @password = t_conf['password']
  @port = t_conf['port']
  @host = t_conf['host']

  # Preserve everything in options so we can easily create copies of a Target.
  @options = t_conf.merge(@options)

  self
end

#userObject



329
330
331
# File 'lib/bolt/target.rb', line 329

def user
  Addressable::URI.unencode_component(@uri_obj&.user) || @user
end

#varsObject



244
245
246
# File 'lib/bolt/target.rb', line 244

def vars
  @inventory.vars(self)
end