Class: ArtifactTools::Client

Inherits:
Object
  • Object
show all
Includes:
Hasher
Defined in:
lib/artifact_tools/client.rb

Overview

Use an object of this class to put/fetch files from storage specified with ConfigFile

Instance Method Summary collapse

Methods included from Hasher

#file_hash

Constructor Details

#initialize(config:, user: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • config (Hash)

    Configuration

  • user (String) (defaults to: nil)

    User name to connect to server with, overrides ARTIFACT_STORAGE_USER and the on stored in config



23
24
25
26
27
# File 'lib/artifact_tools/client.rb', line 23

def initialize(config:, user: nil)
  @config = config
  user ||= ENV['ARTIFACT_STORAGE_USER'] || @config['user']
  @ssh = Net::SSH.start(@config['server'], user, non_interactive: true)
end

Instance Method Details

#fetch(file: nil, dest: nil, match: nil, verify: false, force: false) ⇒ Object

Fetch a file from store

Parameters:

  • file (String) (defaults to: nil)

    Path to file to fetch. Fetches all files from config if omitted.

  • dest (String) (defaults to: nil)

    Optional prefix to add to local path of the file being fetched. Uses cwd if omitted.

  • match (Regexp) (defaults to: nil)

    Optionally fetch only files matching this pattern.

  • verify (Boolean) (defaults to: false)

    Whether to verify the checksum after the file is received. Slows the fetch.

Raises:

  • (HashMismatchError)

    In case checksum doesn’t match the one stored in the config file.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/artifact_tools/client.rb', line 37

def fetch(file: nil, dest: nil, match: nil, verify: false, force: false)
  files = @config['files'].keys
  files = [file] if file
  files.each do |entry|
    next if match && !entry.match?(match)

    entry_hash = @config['files'][entry]['hash']
    remote = compose_remote(entry, entry_hash)
    local = compose_local(dest, entry)
    next if !force && local_file_matches(local, entry_hash)

    @ssh.scp.download!(remote, local)
    verify(entry_hash, local) if verify
  end
end

#put(file:) ⇒ Object

Put a file to storage

Parameters:

  • file (String)

    Path to the file to store.



56
57
58
59
60
61
# File 'lib/artifact_tools/client.rb', line 56

def put(file:)
  hash = file_hash(file)
  remote = compose_remote(file, hash)
  ensure_remote_path_exists(remote)
  @ssh.scp.upload!(file, remote)
end