class ModeScp

Public Instance Methods

download(local_path, remote_path, mode_opts: nil, dry_run: false, verbose: false) click to toggle source
# File lib/punt/mode/mode_scp.rb, line 46
def download(local_path, remote_path, mode_opts: nil, dry_run: false, verbose: false)
    scp_args = ["scp"]
    scp_common_options(mode_opts, verbose, scp_args)

    if (File.directory?(local_path))
        scp_args << "-r"
    end

    scp_args << scp_remote_file(remote_path, mode_opts)
    scp_args << local_path

    scp_command = scp_args.join(" ")

    puts scp_command if verbose
    `#{scp_command}` unless dry_run
end
download_versionfile(deploystage, mode_opts: nil, verbose: false) click to toggle source
# File lib/punt/mode/mode_scp.rb, line 76
def download_versionfile(deploystage, mode_opts: nil, verbose: false)
    filename = ".punt_#{deploystage}"

    versionfile = Tempfile.new(filename)
    download(versionfile.path, filename, mode_opts: mode_opts, verbose: verbose)
    version = versionfile.read
    versionfile.unlink

    return version
end
mode_opts(environment) click to toggle source
# File lib/punt/mode/mode_scp.rb, line 3
def mode_opts(environment)
    return environment["scp"]
end
mode_start(mode_opts: nil, dry_run: false, verbose: true) click to toggle source
# File lib/punt/mode/mode_scp.rb, line 7
def mode_start(mode_opts: nil, dry_run: false, verbose: true)
    @has_sshpass = feature_detect_sshpass

    if @has_sshpass && !mode_opts['ssh_key']
        print "(using sshpass) enter the ssh password: "
        @ssh_password = gets.chomp
    elsif !mode_opts['ssh_key']
        puts "sshpass is not installed. You will need to enter your password for every scp action that is generated. To avoid entering your password for every scp action, you can switch to key-based authentication by setting ssh_key. If key based authentication is not possible, try installing sshpass (https://gist.github.com/arunoda/7790979)"
        puts ""
    end
end
transfer(local_path, remote_path, mode_opts: nil, dry_run: false, verbose: false) click to toggle source
# File lib/punt/mode/mode_scp.rb, line 25
def transfer(local_path, remote_path, mode_opts: nil, dry_run: false, verbose: false)
    scp_args = ["scp"]
    scp_common_options(mode_opts, verbose, scp_args)

    if (File.directory?(local_path))
        scp_args << "-r"
    end

    if (@has_sshpass && @ssh_password)
        scp_args.unshift(@ssh_password).unshift("-p").unshift("sshpass")
    end

    scp_args << local_path
    scp_args << scp_remote_file(remote_path, mode_opts)

    scp_command = scp_args.join(" ")

    puts scp_command
    `#{scp_command}` unless dry_run
end
transfer_complete(mode_opts: nil, dry_run: false, verbose: true) click to toggle source
# File lib/punt/mode/mode_scp.rb, line 22
def transfer_complete(mode_opts: nil, dry_run: false, verbose: true)
end
transfer_start(mode_opts: nil, dry_run: false, verbose: true) click to toggle source
# File lib/punt/mode/mode_scp.rb, line 19
def transfer_start(mode_opts: nil, dry_run: false, verbose: true)
end
upload_versionfile(version, deploystage, mode_opts: nil, verbose: false) click to toggle source
# File lib/punt/mode/mode_scp.rb, line 63
def upload_versionfile(version, deploystage, mode_opts: nil, verbose: false)

    filename = ".punt_#{deploystage}"

    versionfile = Tempfile.new(filename)
    versionfile.write("#{version}")
    versionfile.close()

    transfer(versionfile.path, filename, mode_opts: mode_opts)

    versionfile.unlink
end

Private Instance Methods

feature_detect_sshpass() click to toggle source
# File lib/punt/mode/mode_scp.rb, line 89
def feature_detect_sshpass
    feature_check = `command -v sshpass >/dev/null 2>&1 || { echo "NOSCP"; }`.chomp

    return feature_check != "NOSCP";
end
scp_common_options(scp_opts, verbose, scp_args) click to toggle source
# File lib/punt/mode/mode_scp.rb, line 95
def scp_common_options(scp_opts, verbose, scp_args)
    if (scp_opts["ssh_key"])
        scp_args << "-i"
        scp_args << scp_opts["ssh_key"]
    end

    if (verbose)
        scp_args << "-v"
    end

    if (scp_opts["port"])
        scp_args << "-P"
        scp_args << scp_opts["port"]
    end
end
scp_host(scp_opts) click to toggle source
# File lib/punt/mode/mode_scp.rb, line 119
def scp_host(scp_opts)
    arg = "#{scp_opts["host"]}:"
    arg = "#{scp_opts["user"]}@#{arg}" if scp_opts["user"]

    return arg
end
scp_remote_file(remote_path, scp_opts) click to toggle source
# File lib/punt/mode/mode_scp.rb, line 111
def scp_remote_file(remote_path, scp_opts)
    if !remote_path.start_with?("/") && !remote_path.start_with?("~")
        remote_path = File.join(scp_opts["remote_base"], remote_path)
    end

    return "#{scp_host(scp_opts)}#{remote_path}"
end