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



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/osctl/cli/send.rb', line 102

def cancel
  require_args!('id')

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

#cleanupObject



92
93
94
95
96
97
98
99
100
# File 'lib/osctl/cli/send.rb', line 92

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
# 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],
    as_id: opts['as-id'],
    to_pool: opts['to-pool'],
    network_interfaces: opts['network-interfaces'],
  )
end

#format_str(maxsize) ⇒ Object (protected)



194
195
196
# File 'lib/osctl/cli/send.rb', line 194

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

#json_progress(msg) ⇒ Object (protected)



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/osctl/cli/send.rb', line 198

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/osctl/cli/send.rb', line 114

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

  with_progress(
    :ct_send_now,
    pool: gopts[:pool],
    id: args[0],
    dst: args[1],
    port: opts[:port],
    as_id: opts['as-id'],
    to_pool: opts['to-pool'],
    clone: opts[:clone],
    consistent: opts[:consistent],
    restart: opts[:restart],
    start: opts[:start],
    network_interfaces: opts['network-interfaces'],
  )
end

#rootfsObject



58
59
60
61
62
63
64
65
66
# File 'lib/osctl/cli/send.rb', line 58

def rootfs
  require_args!('id')

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

#stateObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/osctl/cli/send.rb', line 78

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



68
69
70
71
72
73
74
75
76
# File 'lib/osctl/cli/send.rb', line 68

def sync
  require_args!('id')

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

#terminal_progress(msg) ⇒ Object (protected)



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
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/osctl/cli/send.rb', line 151

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)



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/osctl/cli/send.rb', line 134

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