Sha256: 2183e718a63ae0dd297e6430e42d98d82104636cffc6657db42436bedb9a20d5

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

require 'net/ssh'

module SimpleProvision
  class CLI
    attr_reader :profile, :username, :host
    def initialize(args)
      @profile = args[:profile]
      @username = args[:username]
      @host = args[:host]
    end

    def bootstrap
      SimpleProvision::SCP.new(username, host, options).copy_files
    end

    def configure
      begin
        Net::SSH.start(host, username) do |ssh|
          ssh.exec! "tar -xzf #{SimpleProvision::SCP::FILENAME}"
          scripts = options.fetch(:scripts).each do |script|
            puts "Execute #{script}"
            ssh.open_channel do |ssh_channel|
              ssh_channel.request_pty
              ssh_channel.exec("#{environment_exports} bash -c '#{script}'") do |channel, success|
                unless success
                  raise "Could not execute command: #{command.inspect}"
                end

                channel.on_data do |ch, data|
                  STDOUT << data
                end

                channel.on_extended_data do |ch, type, data|
                  next unless type == 1
                  STDERR << data
                end
              end
            end
            ssh.loop
          end
        end
      rescue Net::SSH::HostKeyMismatch => exception
        exception.remember_host!
        sleep 0.2
        retry
      end
    end

    private

    def environment_exports
      @environment_exports ||= begin
                                 if options[:env].nil?
                                   ""
                                 else
                                   options[:env].map { |k, v| [k, Shellwords.escape(v)].join("=") }.join(" ")
                                 end
                               end
    end

    def server
      SimpleProvision::Server.new(@connection, options)
    end

    def options
      @options ||= SimpleProvision::Configuration.new(@profile).options
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simple_provision-0.99.9 lib/simple_provision/cli.rb
simple_provision-0.99.8 lib/simple_provision/cli.rb
simple_provision-0.99.7 lib/simple_provision/cli.rb