Class: TrainPlugins::Habitat::Connection

Inherits:
Train::Plugins::Transport::BaseConnection
  • Object
show all
Includes:
Platform
Defined in:
lib/train-habitat/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Platform

#platform

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



16
17
18
19
20
21
22
23
# File 'lib/train-habitat/connection.rb', line 16

def initialize(options = {})
  @transport_options = options
  validate_transport_options!

  super(transport_options)
  initialize_cli_connection! if cli_options_provided?
  enable_cache :api_call
end

Instance Attribute Details

#cli_connectionObject (readonly)

Returns the value of attribute cli_connection.



14
15
16
# File 'lib/train-habitat/connection.rb', line 14

def cli_connection
  @cli_connection
end

#cli_transportObject (readonly)

Returns the value of attribute cli_transport.



14
15
16
# File 'lib/train-habitat/connection.rb', line 14

def cli_transport
  @cli_transport
end

#cli_transport_nameObject (readonly)

Returns the value of attribute cli_transport_name.



14
15
16
# File 'lib/train-habitat/connection.rb', line 14

def cli_transport_name
  @cli_transport_name
end

#transport_optionsObject (readonly)

Returns the value of attribute transport_options.



14
15
16
# File 'lib/train-habitat/connection.rb', line 14

def transport_options
  @transport_options
end

Instance Method Details

#api_options_provided?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/train-habitat/connection.rb', line 29

def api_options_provided?
  have_transport_options_with_prefix?("api")
end

#cli_options_provided?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/train-habitat/connection.rb', line 33

def cli_options_provided?
  TrainPlugins::Habitat::Transport.cli_transport_prefixes.keys.map(&:to_s).any? do |prefix|
    have_transport_options_with_prefix?(prefix)
  end
end

#hab_pathObject



54
55
56
# File 'lib/train-habitat/connection.rb', line 54

def hab_path
  "/bin/hab"
end

#habitat_api_clientObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/train-habitat/connection.rb', line 58

def habitat_api_client
  cached_client(:api_call, :HTTPGateway) do
    # Send all options beginning with api_ to the HTTPGateway, stripping the prefix
    api_options = {}
    transport_options.each do |option_name, option_value|
      next unless option_name.to_s.start_with? "api_"

      api_options[option_name.to_s.sub(/^api_/, "").to_sym] = option_value
    end
    HTTPGateway.new(api_options)
  end
end

#run_command_via_connection(*args) ⇒ Object

See #run_command in BaseConnection.



48
49
50
51
52
# File 'lib/train-habitat/connection.rb', line 48

def run_command_via_connection(*args)
  raise CliNotAvailableError(cli_tranport_names) unless cli_options_provided?

  cli_connection.run_command(*args)
end

#run_hab_cli(command, _exec_options = {}) ⇒ Object

Use this to execute a ‘hab` command. Do not include the `hab` executable in the invocation.



40
41
42
43
44
45
# File 'lib/train-habitat/connection.rb', line 40

def run_hab_cli(command, _exec_options = {})
  raise CliNotAvailableError(cli_tranport_names) unless cli_options_provided?

  # TODO: - leverage exec_options to add things like JSON parsing, ENV setting, etc.
  cli_connection.run_command(hab_path + " " + command)
end

#uriObject



25
26
27
# File 'lib/train-habitat/connection.rb', line 25

def uri
  "habitat://#{@options[:host]}"
end