Module: Uricp::Strategy::Common

Constant Summary collapse

VOL_REGEX =
/^(srv|vol)-.....$/
PIPE_URI =
URI('pipe:/')
DRY_SNAP =
'uricp_snap'

Constants included from CurlPrimitives

CurlPrimitives::ORBIT_HOSTS

Instance Attribute Summary collapse

Attributes included from CurlPrimitives

#options

Instance Method Summary collapse

Methods included from CurlPrimitives

#authentication, #curl_command, #curl_download_to_pipe, #curl_manifest, #curl_upload_from, #from, #from=, #http_authentication?, #orbit?, #temp_url?, #to, #to=

Instance Attribute Details

#proposed_optionsObject (readonly)

Returns the value of attribute proposed_options.



16
17
18
# File 'lib/uricp/strategy/common.rb', line 16

def proposed_options
  @proposed_options
end

Instance Method Details

#all_local_files?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/uricp/strategy/common.rb', line 25

def all_local_files?
  !sequence_complete? && file_source? && to.scheme == 'file'
end

#always_write_sparse?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/uricp/strategy/common.rb', line 41

def always_write_sparse?
  options['force']
end

#compression_required?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/uricp/strategy/common.rb', line 29

def compression_required?
  options['compress']
end

#conversion_required?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/uricp/strategy/common.rb', line 37

def conversion_required?
  options['target-format']
end

#dry_run?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/uricp/strategy/common.rb', line 87

def dry_run?
  options['dry-run']
end

#encoding(io) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/uricp/strategy/common.rb', line 57

def encoding(io)
  magic = io.read(4).to_s
  if lz4?(magic)
    :lz4
  elsif qcow2?(magic)
    version = io.read(4)
    case version.unpack('N')
    when [2]
      :qcow2
    when [3]
      :qcow3
    else
      :qcow2un
    end
  else
    magic += io.read(2).to_s
    if xz?(magic)
      :xz
    else
      :raw
    end
  end
end

#file_source?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/uricp/strategy/common.rb', line 103

def file_source?
  from.scheme == 'file'
end

#format_change?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/uricp/strategy/common.rb', line 81

def format_change?
  conversion_required? && (
    options['source-format'] != options['target-format']
  )
end

#get_temp_filename(base_dir) ⇒ Object



122
123
124
125
# File 'lib/uricp/strategy/common.rb', line 122

def get_temp_filename(base_dir)
  t = Time.now.strftime('%Y%m%d')
  File.join(base_dir, "uricp-#{t}-#{$PROCESS_ID}-#{rand(0x100000000).to_s(36)}")
end

#in_rbd_cache(target) ⇒ Object



208
209
210
211
212
213
214
215
216
# File 'lib/uricp/strategy/common.rb', line 208

def in_rbd_cache(target)
  result =
    if dry_run?
      options['dry-cache'] == :rbd && options['cache_name'] !~ VOL_REGEX
    else
      snap_check(target, rbd_base_name)
    end
  result && rbd_clone_snapshot(target)
end

#in_rbd_cache?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/uricp/strategy/common.rb', line 167

def in_rbd_cache?
  options['in_rbd_cache']
end

#initialize(options) ⇒ Object



11
12
13
14
# File 'lib/uricp/strategy/common.rb', line 11

def initialize(options)
  @options = options
  debug "#{self.class.name}: options are #{options.inspect}"
end

#lz4?(magic) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/uricp/strategy/common.rb', line 45

def lz4?(magic)
  magic.unpack('V') == [0x184D2204]
end

#lz4_source?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/uricp/strategy/common.rb', line 91

def lz4_source?
  options['source-format'] == :lz4
end

#not_in_rbd_cache?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/uricp/strategy/common.rb', line 171

def not_in_rbd_cache?
  options['in_rbd_cache'] == false
end

#proposed_pathObject



127
128
129
# File 'lib/uricp/strategy/common.rb', line 127

def proposed_path
  proposed_options['from_uri'].path
end

#qcow2?(magic) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/uricp/strategy/common.rb', line 49

def qcow2?(magic)
  magic.unpack('a3C') == ['QFI', 0xfb]
end

#raw_target?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/uricp/strategy/common.rb', line 99

def raw_target?
  options['target-format'] == :raw
end

#rbd_base_nameObject



114
115
116
# File 'lib/uricp/strategy/common.rb', line 114

def rbd_base_name
  'base'
end

#rbd_cache_image_exists?(target) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/uricp/strategy/common.rb', line 183

def rbd_cache_image_exists?(target)
  command = "rbd status --id #{rbd_id} --format json #{target} 2>/dev/null"
  if dry_run?
    if options['dry-cache'] == :partial_rbd && options['cache_name'] !~ VOL_REGEX
      command = 'exit 0'
    else
      command = 'exit 2'
    end
  end
  sh!(command)
  true
rescue Methadone::FailedCommandError
  false
end

#rbd_cache_image_spec(uri) ⇒ Object



143
144
145
# File 'lib/uricp/strategy/common.rb', line 143

def rbd_cache_image_spec(uri)
  File.join(File.dirname(uri.path)[1..-1], options['cache_name'])
end

#rbd_cache_nameObject



147
148
149
# File 'lib/uricp/strategy/common.rb', line 147

def rbd_cache_name
  options['rbd_cache_name']
end

#rbd_cache_upload_available?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/uricp/strategy/common.rb', line 179

def rbd_cache_upload_available?
  rbd_cache_name && !rbd_cache_image_exists?(rbd_cache_name)
end

#rbd_clone_snapshot(cache = rbd_cache_name) ⇒ Object



151
152
153
# File 'lib/uricp/strategy/common.rb', line 151

def rbd_clone_snapshot(cache = rbd_cache_name)
  "#{cache}@#{rbd_base_name}"
end

#rbd_idObject



163
164
165
# File 'lib/uricp/strategy/common.rb', line 163

def rbd_id
  'libvirt'
end

#rbd_image_spec(uri) ⇒ Object



139
140
141
# File 'lib/uricp/strategy/common.rb', line 139

def rbd_image_spec(uri)
  uri.path[1..-1]
end

#rbd_sequence_complete?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/uricp/strategy/common.rb', line 159

def rbd_sequence_complete?
  from.path.include?(to.path)
end

#rbd_snapshot_nameObject



118
119
120
# File 'lib/uricp/strategy/common.rb', line 118

def rbd_snapshot_name
  @rbd_snapshot_name ||= dry_run? ? DRY_SNAP : SecureRandom.uuid
end

#rbd_snapshot_spec?(uri) ⇒ Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/uricp/strategy/common.rb', line 175

def rbd_snapshot_spec?(uri)
  uri.scheme == 'rbd' && uri.path.include?('@')
end

#rbd_uri(image_spec) ⇒ Object



155
156
157
# File 'lib/uricp/strategy/common.rb', line 155

def rbd_uri(image_spec)
  URI.join('rbd:///', image_spec)
end

#segmented?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/uricp/strategy/common.rb', line 33

def segmented?
  options['segment-size']
end

#sequence_complete?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/uricp/strategy/common.rb', line 107

def sequence_complete?
  from == to
end

#snap_check(target, name) ⇒ Object



198
199
200
201
202
203
204
205
206
# File 'lib/uricp/strategy/common.rb', line 198

def snap_check(target, name)
  result = false
  sh "rbd snap ls --id #{rbd_id} --format json #{target} 2>/dev/null" do |stdout|
    result = JSON.parse(stdout).any? { |x| x['name'] == name }
  end
  result
rescue Methadone::FailedCommandError
  false
end

#supported_source?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/uricp/strategy/common.rb', line 135

def supported_source?
  options['source-format'] && !lz4_source? && !xz_source?
end

#temp_uriObject



131
132
133
# File 'lib/uricp/strategy/common.rb', line 131

def temp_uri
  URI.join('file:///', get_temp_filename(options['temp']))
end

#unsupported_transferObject Also known as: command



18
19
20
21
# File 'lib/uricp/strategy/common.rb', line 18

def unsupported_transfer
  raise Uricp::UnsupportedURLtype,
        "Unsupported transfer from #{from} to #{to}"
end

#xz?(magic) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/uricp/strategy/common.rb', line 53

def xz?(magic)
  magic.unpack('H12') == %w[fd377a585a00]
end

#xz_source?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/uricp/strategy/common.rb', line 95

def xz_source?
  options['source-format'] == :xz
end