Sha256: 628366c6c0412723e5cb44ff991b8804446210a82f221733e7f4f2fbbb32b029

Contents?: true

Size: 1.78 KB

Versions: 13

Compression:

Stored size: 1.78 KB

Contents

module ZTK
  class SSH

    # SSH Bootstrap Functionality
    module Bootstrap
      require 'tempfile'

      # SSH Bootstrap
      #
      # Renders the *content* string into a file on the remote host and proceeds
      # to execute it via */bin/bash*.  Sudo is prefixed by default, but can be
      # disabled.
      #
      # @example Sample Bootstrap, assuming the @ssh variable is an instance of ZTK::SSH connected to a host.
      #   @ssh.bootstrap(IO.read("bootstrap.sh"))
      #
      # @example
      #   @ssh.bootstrap("apt-get -y upgrade")
      #
      # @param [String] content What to render out to the bootstrap file we will
      #   execute.
      # @param [Hash] options The options hash.  This will also accept options
      #   for #exec in order to better control the bootstrapping execution.
      # @option options [String] :use_sudo True if we should execute the
      #   bootstrap via sudo; False to execute it as the defined user.
      def bootstrap(content, options={})
        options = {
          :silence => true,
          :use_sudo => true
        }.merge(options)

        bootstrap_tempfile = Tempfile.new("bootstrap")
        remote_tempfile = ::File.join("", "tmp", ::File.basename(bootstrap_tempfile.path.dup))
        bootstrap_tempfile.close!

        local_tempfile  = Tempfile.new("tempfile-local")

        local_tempfile.puts(content)
        local_tempfile.respond_to?(:flush) and local_tempfile.flush

        command = Array.new
        command << %(sudo) if (options[:use_sudo] == true)
        command << %(/bin/bash)
        command << remote_tempfile
        command = command.join(' ')

        self.upload(local_tempfile.path, remote_tempfile)

        result = self.exec(command, options)

        local_tempfile.close!

        result
      end

    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ztk-1.9.0 lib/ztk/ssh/bootstrap.rb
ztk-1.8.0 lib/ztk/ssh/bootstrap.rb
ztk-1.7.1 lib/ztk/ssh/bootstrap.rb
ztk-1.7.0 lib/ztk/ssh/bootstrap.rb
ztk-1.6.30 lib/ztk/ssh/bootstrap.rb
ztk-1.6.29 lib/ztk/ssh/bootstrap.rb
ztk-1.6.28 lib/ztk/ssh/bootstrap.rb
ztk-1.6.27 lib/ztk/ssh/bootstrap.rb
ztk-1.6.26 lib/ztk/ssh/bootstrap.rb
ztk-1.6.25 lib/ztk/ssh/bootstrap.rb
ztk-1.6.24 lib/ztk/ssh/bootstrap.rb
ztk-1.6.23 lib/ztk/ssh/bootstrap.rb
ztk-1.6.22 lib/ztk/ssh/bootstrap.rb