Sha256: 2cac85ba6fc6bd602211dbbf547e9be96afd4aaaf8b3a0bc38cf6f6f9aaa0352

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

require "log4r"

module VagrantPlugins
  module TiktalikVagrant
    module Action
      # This action reads the SSH info for the machine and puts it into the
      # `:machine_ssh_info` key in the environment.
      class ReadSSHInfo
        def initialize(app, env)
          @app    = app
          @logger = Log4r::Logger.new("vagrant_tiktalik::action::read_ssh_info")
          @config = env[:machine].provider_config
        end

        def call(env)
          env[:machine_ssh_info] = read_ssh_info(env[:machine])

          @app.call(env)
        end

        def read_ssh_info(machine)
          return nil if machine.id.nil?

          t = Tiktalik
          t.api_key = @config.api_key
          t.api_secret_key = @config.api_secret
          t.ca_file = @config.ca_file

          i = Tiktalik::Computing::Instance
          instance = i.find machine.id

          if instance.nil?
            # The machine can't be found
            @logger.info("Machine couldn't be found, assuming it got destroyed.")
            machine.id = nil
            return nil
          end

          ip = nil
          instance.interfaces.each do |iface|
            ip = iface.ip if iface.network.public == true
          end

          # no public interfaces found, give a first private one
          ip = instance.interfaces[0].ip if ip.nil?

          # Read the DNS info
          return {
            :host => ip,
            :port => 22,
            :username => "root"
          }
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vagrant-tiktalik-0.0.4 lib/vagrant-tiktalik/action/read_ssh_info.rb
vagrant-tiktalik-0.0.3 lib/vagrant-tiktalik/action/read_ssh_info.rb