Sha256: 705922c7cb72cb315bcb11d597bb0db3b70b5245574b9f61f90255d1efc71f91

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require 'net/scp'
require 'byebug'

module SimpleProvision
  class SCP
    FILENAME = "simpro.tar.gz"

    def initialize(username, host, opts)
      @username, @host, @opts = username, host, opts
    end

    def copy_files
      create_local_archive
      scp_files_to_server
      remove_local_archive
    end

    private

    def create_local_archive
      files = @opts[:files] || []
      scripts = @opts[:scripts] || []
      includes = files + scripts

      if includes.empty?
        raise "Both files and scripts are empty. You should provide some"
      end

      system("mkdir tmp")
      system("mkdir tmp/files")
      system("mkdir tmp/scripts")
      files.each do |f|
        system("cp #{f} tmp/files/")
      end
      scripts.each do |f|
        system("cp #{f} tmp/scripts/")
      end

      system("cd tmp && tar -czf #{FILENAME} files/ scripts/")
    end

    def scp_files_to_server
      Net::SCP.start(@host, @username) do |scp|
        scp.upload!("tmp/#{FILENAME}", ".")
      end
    rescue Net::SSH::HostKeyMismatch
      puts "Please run ssh #{@username}@#{@host} to verify the fingerprint first"
    end

    def remove_local_archive
      `rm -rf tmp`
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_provision-0.99.7 lib/simple_provision/scp.rb
simple_provision-0.99.6 lib/simple_provision/scp.rb