Class: OsCtl::Cli::Send

Inherits:
Command
  • Object
show all
Defined in:
lib/osctl/cli/send.rb

Instance Method Summary collapse

Methods inherited from Command

#cli_opt, #format_output, #osctld_call, #osctld_fmt, #osctld_open, #osctld_resp, run

Instance Method Details

#cancelObject



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/osctl/cli/send.rb', line 108

def cancel
  require_args!('id')

  with_progress(
    :ct_send_cancel,
    pool: gopts[:pool],
    id: args[0],
    force: opts[:force],
    local: opts[:local]
  )
end

#cleanupObject



98
99
100
101
102
103
104
105
106
# File 'lib/osctl/cli/send.rb', line 98

def cleanup
  require_args!('id')

  with_progress(
    :ct_send_cleanup,
    pool: gopts[:pool],
    id: args[0]
  )
end

#configObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/osctl/cli/send.rb', line 43

def config
  require_args!('id', 'dst')

  with_progress(
    :ct_send_config,
    pool: gopts[:pool],
    id: args[0],
    dst: args[1],
    port: opts[:port],
    passphrase: opts[:passphrase],
    as_id: opts['as-id'],
    as_user: opts['as-user'],
    as_group: opts['as-group'],
    to_pool: opts['to-pool'],
    network_interfaces: opts['network-interfaces'],
    snapshots: opts[:snapshots],
    from_snapshot: opts['from-snapshot'],
    preexisting_datasets: opts['preexisting-datasets']
  )
end

#format_str(maxsize) ⇒ Object (protected)



206
207
208
# File 'lib/osctl/cli/send.rb', line 206

def format_str(maxsize)
  "%E %t #{(maxsize / 1024.0).round(2)} GB: [%B] %p%% %r MB/s"
end

#json_progress(msg) ⇒ Object (protected)



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/osctl/cli/send.rb', line 210

def json_progress(msg)
  if msg.is_a?(String)
    puts({ type: :update, text: msg }.to_json)
    return
  end

  case msg[:type].to_sym
  when :step
    puts({ type: :step, text: msg[:title] }.to_json)

  when :progress
    puts({ type: :progress, data: msg[:data] }.to_json)
  end
end

#key_genObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/osctl/cli/send.rb', line 7

def key_gen
  c = osctld_open

  unless opts[:force]
    ret = c.cmd_data!(:send_key_path, pool: gopts[:pool])

    %i[public_key private_key].each do |v|
      if File.exist?(ret[v])
        raise "File #{ret[v]} already exists, use -f, --force to overwrite"
      end
    end
  end

  c.cmd_data!(
    :send_key_gen,
    pool: gopts[:pool],
    type: opts[:type],
    bits: opts[:bits]
  )
end

#key_pathObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/osctl/cli/send.rb', line 28

def key_path
  if args[0] && !%w[public private].include?(args[0])
    raise GLI::BadCommandLine, "expected public/private, got '#{args[0]}'"
  end

  ret = osctld_call(:send_key_path, pool: gopts[:pool])

  if !args[0] || args[0] == 'public'
    puts ret[:public_key]

  else
    puts ret[:private_key]
  end
end

#nowObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/osctl/cli/send.rb', line 120

def now
  require_args!('id', 'dst')

  with_progress(
    :ct_send_now,
    pool: gopts[:pool],
    id: args[0],
    dst: args[1],
    port: opts[:port],
    passphrase: opts[:passphrase],
    as_id: opts['as-id'],
    as_user: opts['as-user'],
    as_group: opts['as-group'],
    to_pool: opts['to-pool'],
    clone: opts[:clone],
    consistent: opts[:consistent],
    restart: opts[:restart],
    start: opts[:start],
    network_interfaces: opts['network-interfaces'],
    snapshots: opts[:snapshots],
    from_snapshot: opts['from-snapshot'],
    preexisting_datasets: opts['preexisting-datasets']
  )
end

#rootfsObject



64
65
66
67
68
69
70
71
72
# File 'lib/osctl/cli/send.rb', line 64

def rootfs
  require_args!('id')

  with_progress(
    :ct_send_rootfs,
    pool: gopts[:pool],
    id: args[0]
  )
end

#stateObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/osctl/cli/send.rb', line 84

def state
  require_args!('id')

  with_progress(
    :ct_send_state,
    pool: gopts[:pool],
    id: args[0],
    clone: opts[:clone],
    consistent: opts[:consistent],
    restart: opts[:restart],
    start: opts[:start]
  )
end

#syncObject



74
75
76
77
78
79
80
81
82
# File 'lib/osctl/cli/send.rb', line 74

def sync
  require_args!('id')

  with_progress(
    :ct_send_sync,
    pool: gopts[:pool],
    id: args[0]
  )
end

#terminal_progress(msg) ⇒ Object (protected)



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/osctl/cli/send.rb', line 163

def terminal_progress(msg)
  return if gopts[:quiet]

  if msg.is_a?(String)
    if @pb
      @pb.finish
      @pb = nil
    end

    puts "> #{msg}"
    return
  end

  case msg[:type].to_sym
  when :step
    if @pb
      @pb.finish
      @pb = nil
    end

    puts "* #{msg[:title]}"

  when :progress
    data = msg[:data]
    @pb ||= ProgressBar.create(
      title: 'Sending',
      total: data[:size],
      format: format_str(data[:size]),
      throttle_rate: 0.2,
      starting_at: 0,
      autofinish: false,
      output: $stdout
    )

    if data[:transfered] > @pb.total
      @pb.total = data[:transfered]
      @pb.format = format_str(@pb.total)
    end

    @pb.progress = data[:transfered]
  end
end

#with_progress(cmd, **opts) ⇒ Object (protected)



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/osctl/cli/send.rb', line 147

def with_progress(cmd, **opts)
  osctld_call(cmd, **opts) do |msg|
    if gopts[:json]
      json_progress(msg)

    else
      terminal_progress(msg)
    end
  end

  @pb.finish if @pb
rescue OsCtl::Client::Error
  @pb.cancel if @pb
  raise
end