Module: Kanrisuru::Core::Transfer

Extended by:
OsPackage::Define
Defined in:
lib/kanrisuru/core/transfer.rb,
lib/kanrisuru/core/transfer/constants.rb,
lib/kanrisuru/core/transfer/commands/wget.rb,
lib/kanrisuru/core/transfer/commands/upload.rb,
lib/kanrisuru/core/transfer/commands/download.rb

Constant Summary collapse

WGET_FILENAME_MODES =
%w[unix windows nocontrol ascii lowercase uppercase].freeze
WGET_SSL_PROTO =
%w[auto SSLv2 SSLv3 TLSv1].freeze

Instance Method Summary collapse

Methods included from OsPackage::Define

extended, os_define

Instance Method Details

#download(remote_path, local_path = nil, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kanrisuru/core/transfer/commands/download.rb', line 6

def download(remote_path, local_path = nil, opts = {})
  recursive = opts[:recursive] || false

  remote_path += '/' if !remote_path.end_with?('/') && recursive
  local_path += '/' if !local_path.nil? && !local_path.end_with?('/') && recursive

  if local_path.instance_of?(Hash)
    opts = local_path
    local_path = nil
  end

  tmp_name = "kanrisuru-tmp-#{Time.now.to_i}-#{object_id}"
  tmp_path = "/tmp/#{tmp_name}"

  begin
    result = cp(remote_path, tmp_path, force: true, follow: true, recursive: recursive)
    raise 'Unable to copy remote file to temp path' unless result.success?

    result = chown(tmp_path, owner: @username, group: @username, recursive: recursive)
    raise 'Unable to update owner or group for temp path' unless result.success?

    result = chmod(tmp_path, 'u+r', recursive: true)
    raise 'Unable to update owner permission read access' unless result.success?

    local_path = ::File.expand_path(local_path) if local_path
    result = ssh.scp.download!(tmp_path, local_path, opts)
    return false unless result

    if Kanrisuru::Util.present?(local_path) && recursive
      remote_dirname = remote_path.split('/').last
      FileUtils.mv("#{local_path}/#{tmp_name}", "#{local_path}/#{remote_dirname}")
    else
      result
    end
  ensure
    rm(tmp_path, force: true, recursive: recursive) if inode?(tmp_path)
  end
end

#upload(local_path, remote_path, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kanrisuru/core/transfer/commands/upload.rb', line 6

def upload(local_path, remote_path, opts = {})
  tmp_path = "/tmp/kanrisuru-tmp-#{Time.now.to_i}-#{object_id}"

  begin
    result = ssh.scp.upload!(local_path, tmp_path, opts)
    raise 'Unable to upload file' unless result

    ## Need to copy internal dir contents, not the tmp dir itself
    if opts[:recursive]
      mkdir(remote_path, silent: true) unless dir?(remote_path)

      result = cp("#{tmp_path}/*", remote_path, recursive: true)
    else
      result = mv(tmp_path, remote_path)
    end

    raise "Unable to move file to remote path - #{result.command.raw_result}" unless result.success?

    stat(remote_path)
  ensure
    rm(tmp_path, force: true) if inode?(tmp_path)
  end
end

#wget(url, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/kanrisuru/core/transfer/commands/wget.rb', line 6

def wget(url, opts = {})
  command = Kanrisuru::Command.new('wget')

  ## Logging and input
  command.append_flag('--quiet', opts[:quiet])

  case opts[:verbose]
  when true
    command.append_flag('--verbose')
  when false
    command.append_flag('--no-verbose')
  end

  command.append_arg('--output-file', opts[:log_file])
  command.append_arg('--append-output', opts[:append_log_file])

  ## Download
  command.append_arg('--bind-address', opts[:bind_address])
  command.append_arg('--tries', opts[:retries])
  command.append_arg('--output-document', opts[:output_document])
  command.append_flag('--no-clobber', opts[:no_clobber])
  command.append_flag('--continue', opts[:continue])
  command.append_flag('--server-response', opts[:server_response])
  command.append_flag('--spider', opts[:spider])
  command.append_arg('--timeout', opts[:timeout])
  command.append_arg('--dns-timeout', opts[:dns_timeout])
  command.append_arg('--connect-timeout', opts[:connect_timeout])
  command.append_arg('--read-timeout', opts[:read_timeout])
  command.append_arg('--limit-rate', opts[:limit_rate])
  command.append_arg('--wait', opts[:wait])
  command.append_arg('--waitretry', opts[:waitretry])
  command.append_flag('--random-wait', opts[:random_wait])
  command.append_flag('--no-proxy', opts[:no_proxy])
  command.append_flag('--no-dns-cache', opts[:no_dns_cache])

  command.append_arg('--quota', opts[:quota])

  if Kanrisuru::Util.present?(opts[:restrict_file_names])
    command.append_arg('--restrict-file-names', Kanrisuru::Util.array_join_string(opts[:restrict_file_names]))
  end

  case opts[:family]
  when 'inet'
    command.append_flag('--inet4-only')
  when 'inet6'
    command.append_flag('--inet6-only')
  end

  command.append_flag('--retry-connrefused', opts[:retry_connrefused])
  command.append_arg('--user', opts[:user])
  command.append_arg('--password', opts[:password])
  command.append_flag('--no-iri', opts[:no_iri])
  command.append_arg('--local-encoding', opts[:local_encoding])
  command.append_arg('--remote-encoding', opts[:remote_encoding])

  ## Directories
  command.append_flag('--no-directories', opts[:no_directories])
  command.append_flag('--force-directories', opts[:force_directories])
  command.append_flag('--no-host-directories', opts[:no_host_directories])
  command.append_flag('--protocol-directories', opts[:protocol_directories])
  command.append_arg('--cut-dirs', opts[:cut_dirs])
  command.append_arg('--directory-prefix', opts[:directory_prefix])

  ## HTTP
  command.append_arg('--default-page', opts[:default_page])
  command.append_flag('--adjust-extension', opts[:adjust_extension])
  command.append_arg('--http-user', opts[:http_user])
  command.append_arg('--http-password', opts[:http_password])
  command.append_arg('--load-cookies', opts[:load_cookies])
  command.append_arg('--save-cookies', opts[:save_cookies])
  command.append_flag('--no-http-keep-alive', opts[:no_http_keep_alive])
  command.append_flag('--no-cache', opts[:no_cache])
  command.append_flag('--no-cookies', opts[:no_cookies])
  command.append_flag('--keep-session-cookies', opts[:keep_session_cookies])
  command.append_flag('--ignore-length', opts[:ignore_length])
  command.append_arg('--max-redirect', opts[:max_redirect])
  command.append_arg('--proxy-user', opts[:proxy_user])
  command.append_arg('--proxy-password', opts[:proxy_password])
  command.append_arg('--referer', opts[:referer])
  command.append_flag('--save-headers', opts[:save_headers])
  command.append_arg('--user-agent', opts[:user_agent])

  headers = opts[:headers]
  if Kanrisuru::Util.present?(headers)
    if headers.instance_of?(Hash)
      headers.each do |key, value|
        header = "'#{key}: #{value}'"
        command.append_arg('--header', header)
      end
    elsif headers.instance_of?(String)
      command.append_arg('--header', headers)
    end
  end

  post_data = opts[:post_data]

  if Kanrisuru::Util.present?(post_data)
    post_data = post_data.instance_of?(Hash) ? URI.encode_www_form(post_data) : post_data
    command.append_arg('--post-data', post_data)
  end

  command.append_arg('--post-file', opts[:post_file])
  command.append_arg('--method', opts[:method])

  command.append_flag('--content-disposition', opts[:content_disposition])
  command.append_flag('--content-on-error', opts[:content_on_error])
  command.append_flag('--trust-server-names', opts[:trust_server_names])
  command.append_flag('--retry-on-host-error', opts[:retry_on_host_error])

  ## SSL / TLS
  if Kanrisuru::Util.present?(opts[:secure_protocol])
    raise ArgumentError, 'invalid ssl protocol' unless WGET_SSL_PROTO.include?(opts[:secure_protocol])

    command.append_arg('--secure-protocol', opts[:secure_protocol])
  end

  command.append_flag('--no-check-certificate', opts[:no_check_certificate])
  command.append_arg('--certificate', opts[:certificate])
  command.append_arg('--certificate-type', opts[:certificate_type])
  command.append_arg('--private-key', opts[:private_key])
  command.append_arg('--private-key-type', opts[:private_key_type])
  command.append_arg('--ca-certificate', opts[:ca_certificate])
  command.append_arg('--ca-directory', opts[:ca_directory])
  command.append_arg('--random-file', opts[:random_file])
  command.append_arg('--egd-file', opts[:egd_file])
  command.append_flag('--https-only', opts[:https_only])

  ## FTP / FTPS
  command.append_arg('--ftp-user', opts[:ftp_user])
  command.append_arg('--ftp-password', opts[:ftp_password])
  command.append_flag('--no-remove-listing', opts[:no_remove_listing])
  command.append_flag('--no-glob', opts[:no_glob])
  command.append_flag('--no-passive-ftp', opts[:no_passive_ftp])
  command.append_flag('--retr-symlinks', opts[:retr_symlinks])
  command.append_flag('--preserve-permissions', opts[:preserve_permissions])
  command.append_flag('--ftps-implicit', opts[:ftps_implicit])
  command.append_flag('--no-ftps-resume-ssl', opts[:no_ftps_resume_ssl])
  command.append_flag('--ftps-clear-data-connection', opts[:ftps_clear_data_connection])
  command.append_flag('--ftps-fallback-to-ftp', opts[:ftps_fallback_to_ftp])

  ## Recursive Retrieval
  command.append_flag('--recursive', opts[:recursive])
  command.append_arg('--level', opts[:level])
  command.append_flag('--delete-after', opts[:delete_after])
  command.append_flag('--convert-links', opts[:convert_links])
  command.append_flag('--backup-converted', opts[:backup_converted])
  command.append_flag('--mirror', opts[:mirror])
  command.append_flag('--page-requisites', opts[:page_requisites])
  command.append_flag('--strict-comments', opts[:strict_comments])

  ## Recursive Accept/Reject
  command.append_arg('--accept', Kanrisuru::Util.array_join_string(opts[:accept]))
  command.append_arg('--reject', Kanrisuru::Util.array_join_string(opts[:reject]))
  command.append_arg('--accept-regex', opts[:accept_regex])
  command.append_arg('--reject-regex', opts[:reject_regex])
  command.append_arg('--regex_type', opts[:regex_type])
  command.append_arg('--domains', Kanrisuru::Util.array_join_string(opts[:domains]))
  command.append_arg('--exclude-domains', Kanrisuru::Util.array_join_string(opts[:exclude_domains]))
  command.append_arg('--follow-tags', Kanrisuru::Util.array_join_string(opts[:follow_tags]))
  command.append_arg('--ignore-tags', Kanrisuru::Util.array_join_string(opts[:ignore_tags]))
  command.append_arg('--include-directories', Kanrisuru::Util.array_join_string(opts[:include_directories]))
  command.append_arg('--exclude-directories', Kanrisuru::Util.array_join_string(opts[:exclude_directories]))
  command.append_flag('--follow-ftp', opts[:follow_ftp])
  command.append_flag('--ignore-case', opts[:ignore_case])
  command.append_flag('--span-hosts', opts[:span_hosts])
  command.append_flag('--relative', opts[:relative])
  command.append_flag('--no-parent', opts[:no_parent])

  command << url

  execute_shell(command)

  Kanrisuru::Result.new(command)
end