Class: Registration::RepoStateStorage

Inherits:
Object
  • Object
show all
Includes:
Singleton, Yast::Logger
Defined in:
src/lib/registration/repo_state.rb

Overview

storage for changed repositories

Constant Summary collapse

REPO_STATE_FILE =

location of the persistent storage (to store the data when restarting YaST)

"/var/lib/YaST2/migration_repo_state.yml".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRepoStateStorage


43
44
45
# File 'src/lib/registration/repo_state.rb', line 43

def initialize
  @repositories = []
end

Instance Attribute Details

#repositoriesObject

array of RepoState objects


38
39
40
# File 'src/lib/registration/repo_state.rb', line 38

def repositories
  @repositories
end

Instance Method Details

#add(repo_id, enabled) ⇒ Object


50
51
52
# File 'src/lib/registration/repo_state.rb', line 50

def add(repo_id, enabled)
  repositories << RepoState.new(repo_id, enabled)
end

#cleanObject

clean the persistent storage


103
104
105
# File 'src/lib/registration/repo_state.rb', line 103

def clean
  File.unlink(REPO_STATE_FILE) if File.exist?(REPO_STATE_FILE)
end

#readObject

read the stored state from the persistent storage


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'src/lib/registration/repo_state.rb', line 81

def read
  # reset the previous list
  self.repositories = []

  return unless File.exist?(REPO_STATE_FILE)

  # inverse alias => repo_id mapping
  repo_mapping = repository_mapping.invert

  data = YAML.load_file(REPO_STATE_FILE)
  log.info "Importing repository state: #{data}"

  data.each do |repo_alias, enabled|
    if repo_mapping[repo_alias]
      add(repo_mapping[repo_alias], enabled)
    else
      log.warn "Repository #{repo_alias.inspect} is missing"
    end
  end
end

#restore_allObject

restore all saved states


55
56
57
58
59
60
61
62
63
# File 'src/lib/registration/repo_state.rb', line 55

def restore_all
  return if repositories.empty?

  # activate the original repository states
  repositories.each(&:restore)

  # save all repositories
  Yast::Pkg.SourceSaveAll
end

#writeObject

write the current state to the persistent storage (to survive YaST restart)


66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'src/lib/registration/repo_state.rb', line 66

def write
  data = {}

  # repo_id => alias mapping
  repo_mapping = repository_mapping

  repositories.each do |repository|
    data[repo_mapping[repository.repo_id]] = repository.enabled
  end

  log.info "Exporting repository state: #{data.inspect}"
  File.write(REPO_STATE_FILE, data.to_yaml)
end