Class: OsCtl::Cli::Send

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

Instance Attribute Summary

Attributes inherited from Command

#args, #gopts, #opts

Instance Method Summary collapse

Methods inherited from Command

#cli_opt, #format_output, #initialize, #osctld_call, #osctld_fmt, #osctld_open, #osctld_resp, #require_args!, run

Methods included from Utils::Humanize

#break_interval, #format_long_duration, #format_percent, #format_short_duration, #humanize_data, #humanize_time_ns, #parse_data

Constructor Details

This class inherits a constructor from OsCtl::Cli::Command

Instance Method Details

#cancelObject



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/osctl/cli/send.rb', line 105

def cancel
  require_args!('id')

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

#cleanupObject



95
96
97
98
99
100
101
102
103
# File 'lib/osctl/cli/send.rb', line 95

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
# 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'],
  )
end

#format_str(maxsize) ⇒ Object (protected)



200
201
202
# File 'lib/osctl/cli/send.rb', line 200

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

#json_progress(msg) ⇒ Object (protected)



204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/osctl/cli/send.rb', line 204

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])
        fail "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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/osctl/cli/send.rb', line 117

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'],
  )
end

#rootfsObject



61
62
63
64
65
66
67
68
69
# File 'lib/osctl/cli/send.rb', line 61

def rootfs
  require_args!('id')

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

#stateObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/osctl/cli/send.rb', line 81

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



71
72
73
74
75
76
77
78
79
# File 'lib/osctl/cli/send.rb', line 71

def sync
  require_args!('id')

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

#terminal_progress(msg) ⇒ Object (protected)



157
158
159
160
161
162
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
# File 'lib/osctl/cli/send.rb', line 157

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)



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/osctl/cli/send.rb', line 140

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