Class: OsCtl::Cli::Migrate

Inherits:
Command
  • Object
show all
Defined in:
lib/osctl/cli/migrate.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



50
51
52
53
54
55
56
57
58
59
# File 'lib/osctl/cli/migrate.rb', line 50

def cancel
  require_args!('id')

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

#cleanupObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/osctl/cli/migrate.rb', line 39

def cleanup
  require_args!('id')

  with_progress(
    :ct_migrate_cleanup,
    pool: gopts[:pool],
    id: args[0],
    delete: opts[:delete]
  )
end

#json_progress(msg) ⇒ Object (protected)



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/osctl/cli/migrate.rb', line 131

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

#nowObject



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

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

  with_progress(
    :ct_migrate_now,
    pool: gopts[:pool],
    id: args[0],
    dst: args[1],
    port: opts[:port],
    delete: opts[:delete]
  )
end

#stageObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/osctl/cli/migrate.rb', line 7

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

  with_progress(
    :ct_migrate_stage,
    pool: gopts[:pool],
    id: args[0],
    dst: args[1],
    port: opts[:port]
  )
end

#syncObject



19
20
21
22
23
24
25
26
27
# File 'lib/osctl/cli/migrate.rb', line 19

def sync
  require_args!('id')

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

#terminal_progress(msg) ⇒ Object (protected)



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
# File 'lib/osctl/cli/migrate.rb', line 92

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: 'Copying',
      total: nil,
      format: "%E %t #{(data[:size] / 1024).round(2)} GB: [%B] %p%% %r MB/s",
      throttle_rate: 0.2,
      starting_at: 0,
      autofinish: false,
      output: STDOUT,
    )

    @pb.total = @pb.progress > data[:size] ? @pb.progress : data[:size]
    @pb.progress = data[:transfered]
  end
end

#transferObject



29
30
31
32
33
34
35
36
37
# File 'lib/osctl/cli/migrate.rb', line 29

def transfer
  require_args!('id')

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

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



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

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