Class: VpsAdmin::CLI::Commands::SnapshotDownload

Inherits:
BaseDownload
  • Object
show all
Defined in:
lib/vpsadmin/cli/commands/snapshot_download.rb

Instance Method Summary collapse

Methods inherited from BaseDownload

#initialize

Constructor Details

This class inherits a constructor from VpsAdmin::CLI::Commands::BaseDownload

Instance Method Details

#do_exec(opts) ⇒ Object



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
# File 'lib/vpsadmin/cli/commands/snapshot_download.rb', line 72

def do_exec(opts)
  @opts = opts
  f = action = nil
  pos = 0

  if @opts[:file] == '-'
    f = $stdout

  elsif @opts[:file]
    f, action, pos = open_file(@opts[:file])
  end

  dl, created = find_or_create_dl(@opts, action != :resume)
  f, action, pos = open_file(dl.file_name) unless @opts[:file]

  if created
    if action == :resume
      warn 'Unable to resume the download: the file has been deleted from the server'
      exit(false)
    end

    msg 'The download is being prepared...'
    sleep(5)

  else
    warn "Reusing existing SnapshotDownload (id=#{dl.id})"
  end

  msg "Downloading to #{f.path}"

  begin
    VpsAdmin::CLI::StreamDownloader.download(
      @api,
      dl,
      f,
      progress: !@opts[:quiet] && (f == $stdout ? $stderr : $stdout),
      position: pos,
      max_rate: @opts[:max_rate],
      checksum: @opts[:checksum]
    )
  rescue VpsAdmin::CLI::DownloadError => e
    warn e.message
    exit(false)
  ensure
    f.close
  end

  @api.snapshot_download.delete(dl.id) if @opts[:delete_after]
end

#exec(args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vpsadmin/cli/commands/snapshot_download.rb', line 57

def exec(args)
  if args.empty? && $stdin.tty?
    @opts[:snapshot] = snapshot_chooser

  elsif args.size != 1
    warn 'Provide exactly one SNAPSHOT_ID as an argument'
    exit(false)

  else
    @opts[:snapshot] = args.first.to_i
  end

  do_exec(@opts)
end

#options(opts) ⇒ Object



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
# File 'lib/vpsadmin/cli/commands/snapshot_download.rb', line 7

def options(opts)
  @opts = {
    delete_after: true,
    send_mail: false,
    checksum: true,
    format: 'archive'
  }

  opts.on('-f', '--format FORMAT', 'archive, stream or incremental_stream') do |f|
    @opts[:format] = f
  end

  opts.on('-I', '--from-snapshot SNAPSHOT_ID', Integer, 'Download snapshot incrementally from SNAPSHOT_ID') do |s|
    @opts[:from_snapshot] = s
  end

  opts.on('-d', '--[no-]delete-after', 'Delete the file from the server after successful download') do |d|
    @opts[:delete_after] = d
  end

  opts.on('-F', '--force', 'Overwrite existing files if necessary') do |f|
    @opts[:force] = f
  end

  opts.on('-o', '--output FILE', 'Save the download to FILE') do |f|
    @opts[:file] = f
  end

  opts.on('-q', '--quiet', 'Print only errors') do |q|
    @opts[:quiet] = q
  end

  opts.on('-r', '--resume', 'Resume cancelled download') do |r|
    @opts[:resume] = r
  end

  opts.on('-s', '--[no-]send-mail', 'Send mail after the file for download is completed') do |s|
    @opts[:send_mail] = s
  end

  opts.on('-x', '--max-rate N', Integer, 'Maximum download speed in kB/s') do |r|
    exit_msg('--max-rate must be greater than zero') if r <= 0
    @opts[:max_rate] = r
  end

  opts.on('--[no-]checksum', 'Verify checksum of the downloaded data (enabled)') do |c|
    @opts[:checksum] = c
  end
end