Class: Yast::SnapperClass

Inherits:
Module
  • Object
show all
Defined in:
../../src/modules/Snapper.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) CreateSnapshot(args)

Create new snapshot Return true on success



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File '../../src/modules/Snapper.rb', line 293

def CreateSnapshot(args)
  args = deep_copy(args)
  success = Convert.to_boolean(SCR.Execute(path(".snapper.create"), args))
  if !success
    err_map = LastSnapperErrorMap()
    type = Ops.get_string(err_map, "type", "")
    details = _("Reason not known.")

    if type == "wrong_snapshot_type"
      details = _("Wrong snapshot type given.")
    elsif type == "pre_not_given"
      details = _("'Pre' snapshot was not given.")
    elsif type == "pre_not_found"
      details = _("Given 'Pre' snapshot was not found.")
    end

    Builtins.y2warning("creating failed with '%1'", err_map)
    # error popup
    Report.Error(
      Builtins.sformat(_("Failed to create new snapshot:\n%1"), details)
    )
  end
  success
end

- (Object) DeleteSnapshot(args)

Delete existing snapshot Return true on success



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File '../../src/modules/Snapper.rb', line 254

def DeleteSnapshot(args)
  args = deep_copy(args)
  success = Convert.to_boolean(SCR.Execute(path(".snapper.delete"), args))
  if !success
    err_map = LastSnapperErrorMap()
    type = Ops.get_string(err_map, "type", "")
    details = _("Reason not known.")

    details = _("Snapshot was not found.") if type == "not_found"

    Builtins.y2warning("deleting failed with '%1'", err_map)
    # error popup
    Report.Error(
      Builtins.sformat(_("Failed to delete snapshot:\n%1"), details)
    )
  end
  success
end

- (Object) GetFileFullPath(file)

Return the full path to the given file from currently selected configuration (subvolume) GetFileFullPath ("/testfile.txt") -> /abc/testfile.txt for /abc subvolume

Parameters:

  • file (String)

    path, relatively to current config



99
100
101
102
103
104
105
# File '../../src/modules/Snapper.rb', line 99

def GetFileFullPath(file)
  Ops.get_string(
    @snapshots,
    [@selected_snapshot_index, "files_index", file, "full_path"],
    file
  )
end

- (Object) GetFileMode(file)

Return the given file mode as octal number



357
358
359
360
361
362
363
364
365
366
367
# File '../../src/modules/Snapper.rb', line 357

def GetFileMode(file)
  out = Convert.to_map(
    SCR.Execute(
      path(".target.bash_output"),
      Builtins.sformat("/bin/stat --printf=%%a '%1'", String.Quote(file))
    )
  )
  mode = Ops.get_string(out, "stdout", "")
  return 644 if mode == nil || mode == ""
  Builtins.tointeger(mode)
end

- (Object) GetFileModification(file, old, new)

Describe what was done with given file between given snapshots - when new is 0, meaning is 'current system'



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
# File '../../src/modules/Snapper.rb', line 109

def GetFileModification(file, old, new)
  ret = {}
  file1 = Builtins.sformat("%1%2", GetSnapshotPath(old), file)
  file2 = Builtins.sformat("%1%2", GetSnapshotPath(new), file)
  file2 = GetFileFullPath(file) if new == 0

  Builtins.y2milestone("comparing '%1' and '%2'", file1, file2)

  if FileUtils.Exists(file1) && FileUtils.Exists(file2)
    status = ["no_change"]
    out = Convert.to_map(
      SCR.Execute(
        path(".target.bash_output"),
        Builtins.sformat(
          "/usr/bin/diff -u '%1' '%2'",
          String.Quote(file1),
          String.Quote(file2)
        )
      )
    )
    if Ops.get_string(out, "stderr", "") != ""
      Builtins.y2warning("out: %1", out)
      Ops.set(ret, "diff", Ops.get_string(out, "stderr", ""))
    # the file diff
    elsif Ops.get(out, "stdout") != ""
      status = ["diff"]
      Ops.set(ret, "diff", Ops.get_string(out, "stdout", ""))
    end

    # check mode and ownerships
    out = Convert.to_map(
      SCR.Execute(
        path(".target.bash_output"),
        Builtins.sformat(
          "ls -ld -- '%1' '%2' | cut -f 1,3,4 -d ' '",
          String.Quote(file1),
          String.Quote(file2)
        )
      )
    )
    parts = Builtins.splitstring(Ops.get_string(out, "stdout", ""), " \n")

    if Ops.get(parts, 0, "") != Ops.get(parts, 3, "")
      status = Builtins.add(status, "mode")
      Ops.set(ret, "mode1", Ops.get(parts, 0, ""))
      Ops.set(ret, "mode2", Ops.get(parts, 3, ""))
    end
    if Ops.get(parts, 1, "") != Ops.get(parts, 4, "")
      status = Builtins.add(status, "user")
      Ops.set(ret, "user1", Ops.get(parts, 1, ""))
      Ops.set(ret, "user2", Ops.get(parts, 4, ""))
    end
    if Ops.get(parts, 2, "") != Ops.get(parts, 5, "")
      status = Builtins.add(status, "group")
      Ops.set(ret, "group1", Ops.get(parts, 2, ""))
      Ops.set(ret, "group2", Ops.get(parts, 5, ""))
    end
    Ops.set(ret, "status", status)
  elsif FileUtils.Exists(file1)
    Ops.set(ret, "status", ["removed"])
  elsif FileUtils.Exists(file2)
    Ops.set(ret, "status", ["created"])
  else
    Ops.set(ret, "status", ["none"])
  end
  deep_copy(ret)
end

- (Object) GetSnapshotPath(snapshot_num)

Return the path to given snapshot



82
83
84
85
86
87
88
89
90
91
92
93
94
# File '../../src/modules/Snapper.rb', line 82

def GetSnapshotPath(snapshot_num)
  ret = Convert.to_string(
    SCR.Read(path(".snapper.path"), { "num" => snapshot_num })
  )
  if ret == nil
    ret = ""
    # popup error
    Report.Error(
      Builtins.sformat(_("Snapshot '%1' was not found."), snapshot_num)
    )
  end
  ret
end

- (Object) InitializeSnapper(config)

Initialize snapper agent Return true on success



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File '../../src/modules/Snapper.rb', line 226

def InitializeSnapper(config)
  init = Convert.to_boolean(
    SCR.Execute(path(".snapper"), { "config" => config })
  )
  if !init
    err_map = LastSnapperErrorMap()
    type = Ops.get_string(err_map, "type", "")
    details = _("Reason not known.")
    if type == "config_not_found"
      details = _("Configuration not found.")
    elsif type == "config_invalid"
      details = _("Configuration is not valid.")
    end

    Builtins.y2warning("init failed with '%1'", err_map)
    # error popup
    Report.Error(
      Builtins.sformat(
        _("Failed to initialize snapper library:\n%1"),
        details
      )
    )
  end
  init
end

- (Object) LastSnapperErrorMap



199
200
201
# File '../../src/modules/Snapper.rb', line 199

def LastSnapperErrorMap
  Convert.to_map(SCR.Read(path(".snapper.error")))
end

- (Object) main



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File '../../src/modules/Snapper.rb', line 34

def main
  Yast.import "UI"
  textdomain "snapper"

  Yast.import "FileUtils"
  Yast.import "Label"
  Yast.import "Progress"
  Yast.import "Report"
  Yast.import "String"

  # global list of all snapshot
  @snapshots = []

  @selected_snapshot = {}

  # mapping of snapshot number to index in snapshots list
  @id2index = {}

  # index to snapshots list
  @selected_snapshot_index = 0

  # list of configurations
  @configs = ["root"]

  @current_config = "root"
end

- (Object) ModifySnapshot(args)

Modify existing snapshot Return true on success



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File '../../src/modules/Snapper.rb', line 274

def ModifySnapshot(args)
  args = deep_copy(args)
  success = Convert.to_boolean(SCR.Execute(path(".snapper.modify"), args))
  if !success
    err_map = LastSnapperErrorMap()
    type = Ops.get_string(err_map, "type", "")
    details = _("Reason not known.")

    Builtins.y2warning("modification failed with '%1'", err_map)
    # error popup
    Report.Error(
      Builtins.sformat(_("Failed to modify snapshot:\n%1"), details)
    )
  end
  success
end

- (Object) Read

Read all snapper settings

Returns:

  • true on success



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File '../../src/modules/Snapper.rb', line 320

def Read
  # Snapper read dialog caption
  caption = _("Initializing Snapper")

  steps = 2

  # We do not set help text here, because it was set outside
  Progress.New(
    caption,
    " ",
    steps,
    [
      # Progress stage 1/3
      _("Read the list of snapshots")
    ],
    [
      # Progress step 1/3
      _("Reading the database..."),
      # Progress finished
      _("Finished")
    ],
    ""
  )

  Progress.NextStage

  ReadConfigs()

  return false if !InitializeSnapper(@current_config)

  ReadSnapshots()

  Progress.NextStage
  true
end

- (Object) ReadConfigs



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File '../../src/modules/Snapper.rb', line 204

def ReadConfigs
  @configs = Convert.convert(
    SCR.Read(path(".snapper.configs")),
    :from => "any",
    :to   => "list <string>"
  )
  if @configs == nil
    # error popup
    Report.Error(_("File /etc/sysconfig/snapper is not available."))
    @configs = ["root"]
  end
  if !Builtins.contains(@configs, "root") &&
      Ops.greater_than(Builtins.size(@configs), 0)
    @current_config = Ops.get(@configs, 0, "root")
  end
  deep_copy(@configs)
end

- (Object) ReadModifiedFilesIndex(from, to)

Return map of files modified between given snapshots Return structure has just one level, and maps each modified file to it's modification map



63
64
65
66
67
68
69
# File '../../src/modules/Snapper.rb', line 63

def ReadModifiedFilesIndex(from, to)
  Convert.convert(
    SCR.Read(path(".snapper.diff_index"), { "from" => from, "to" => to }),
    :from => "any",
    :to   => "map <string, map>"
  )
end

- (Object) ReadModifiedFilesMap(from, to)

Return map of files modified between given snapshots Map is recursively describing the filesystem structure; helps to build Tree widget contents



73
74
75
76
77
78
79
# File '../../src/modules/Snapper.rb', line 73

def ReadModifiedFilesMap(from, to)
  Convert.convert(
    SCR.Read(path(".snapper.diff_tree"), { "from" => from, "to" => to }),
    :from => "any",
    :to   => "map <string, map>"
  )
end

- (Object) ReadSnapshots

Read the list of snapshots



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File '../../src/modules/Snapper.rb', line 178

def ReadSnapshots
  @snapshots = []
  snapshot_maps = Convert.convert(
    SCR.Read(path(".snapper.snapshots")),
    :from => "any",
    :to   => "list <map>"
  )
  snapshot_maps = [] if snapshot_maps == nil
  i = 0
  Builtins.foreach(snapshot_maps) do |snapshot|
    id = Ops.get_integer(snapshot, "num", 0)
    next if id == 0 # ignore the 'current system'
    Ops.set(snapshot, "name", Builtins.tostring(id))
    Builtins.y2debug("snapshot data: %1", snapshot)
    @snapshots = Builtins.add(@snapshots, snapshot)
    Ops.set(@id2index, id, i)
    i = Ops.add(i, 1)
  end
  true
end

- (Object) RestoreFiles(snapshot_num, files)

Copy given files from selected snapshot to current filesystem

Parameters:

  • snapshot_num (Fixnum)

    snapshot identifier

  • files (Array<String>)

    list of full paths to files

Returns:

  • success



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File '../../src/modules/Snapper.rb', line 373

def RestoreFiles(snapshot_num, files)
  files = deep_copy(files)
  ret = true
  Builtins.y2milestone("going to restore files %1", files)

  UI.OpenDialog(
    Opt(:decorated),
    HBox(
      HSpacing(1.5),
      VBox(
        HSpacing(60),
        # label for log window
        LogView(Id(:log), _("Restoring Files..."), 8, 0),
        ProgressBar(Id(:progress), "", Builtins.size(files), 0),
        PushButton(Id(:ok), Label.OKButton)
      ),
      HSpacing(1.5)
    )
  )

  UI.ChangeWidget(Id(:ok), :Enabled, false)
  progress = 0
  Builtins.foreach(files) do |file|
    UI.ChangeWidget(Id(:progress), :Value, progress)
    orig = Ops.add(GetSnapshotPath(snapshot_num), file)
    full_path = GetFileFullPath(file)
    dir = Builtins.substring(
      full_path,
      0,
      Builtins.findlastof(full_path, "/")
    )
    if !FileUtils.Exists(orig)
      SCR.Execute(
        path(".target.bash"),
        Builtins.sformat("/bin/rm -rf -- '%1'", String.Quote(full_path))
      )
      Builtins.y2milestone("removing '%1' from system", full_path)
      # log entry (%1 is file name)
      UI.ChangeWidget(
        Id(:log),
        :LastLine,
        Builtins.sformat(_("Deleted %1\n"), full_path)
      )
    elsif FileUtils.CheckAndCreatePath(dir)
      Builtins.y2milestone(
        "copying '%1' to '%2' (dir: %3)",
        orig,
        file,
        dir
      )
      if FileUtils.IsDirectory(orig) == true
        stat = Convert.to_map(SCR.Read(path(".target.stat"), orig))
        if !FileUtils.Exists(full_path)
          SCR.Execute(path(".target.mkdir"), full_path)
        end
        SCR.Execute(
          path(".target.bash"),
          Builtins.sformat(
            "/bin/chown -- %1:%2 '%3'",
            Ops.get_integer(stat, "uid", 0),
            Ops.get_integer(stat, "gid", 0),
            String.Quote(full_path)
          )
        )
        SCR.Execute(
          path(".target.bash"),
          Builtins.sformat(
            "/bin/chmod -- %1 '%2'",
            GetFileMode(orig),
            String.Quote(full_path)
          )
        )
      else
        SCR.Execute(
          path(".target.bash"),
          Builtins.sformat(
            "/bin/cp -a -- '%1' '%2'",
            String.Quote(orig),
            String.Quote(full_path)
          )
        )
      end
      UI.ChangeWidget(Id(:log), :LastLine, Ops.add(full_path, "\n"))
    else
      Builtins.y2milestone(
        "failed to copy file '%1' to '%2' (dir: %3)",
        orig,
        full_path,
        dir
      )
      # log entry (%1 is file name)
      UI.ChangeWidget(
        Id(:log),
        :LastLine,
        Builtins.sformat(_("%1 skipped\n"), full_path)
      )
    end
    Builtins.sleep(100)
    progress = Ops.add(progress, 1)
  end

  UI.ChangeWidget(Id(:progress), :Value, progress)
  UI.ChangeWidget(Id(:ok), :Enabled, true)

  UI.UserInput
  UI.CloseDialog

  ret
end