Class: OsCtld::Repository

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::File, OsCtl::Lib::Utils::Log, Assets::Definition, Lockable, Manipulable
Defined in:
lib/osctld/repository.rb

Defined Under Namespace

Classes: Image

Constant Summary collapse

USER =
'repository'.freeze
UID =
Etc.getpwnam(USER).uid
DEFAULT_PRUNE_INTERVAL =
24 * 60 * 60
DEFAULT_PRUNE_OLDER_THAN_DAYS =
90
CACHE_LOCK_TIMEOUT =
60 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Assets::Definition

#define_assets

Methods included from Manipulable

#acquire_manipulation_lock, #init_manipulable, #is_being_manipulated?, #manipulate, #manipulated_by, #release_manipulation_lock

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

#initialize(pool, name, load: true) ⇒ Repository

Returns a new instance of Repository.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/osctld/repository.rb', line 49

def initialize(pool, name, load: true)
  init_lock
  init_manipulable
  @pool = pool
  @name = name
  @enabled = true
  @attrs = Attributes.new
  @prune_thread = nil
  @prune_queue = OsCtl::Lib::Queue.new
  load_config if load
end

Instance Attribute Details

#attrsAttributes (readonly)

Returns:



47
48
49
# File 'lib/osctld/repository.rb', line 47

def attrs
  @attrs
end

#enabledBoolean (readonly) Also known as: enabled?

Returns:

  • (Boolean)


34
35
36
# File 'lib/osctld/repository.rb', line 34

def enabled
  @enabled
end

#nameString (readonly)

Returns:

  • (String)


28
29
30
# File 'lib/osctld/repository.rb', line 28

def name
  @name
end

#poolPool (readonly)

Returns:



25
26
27
# File 'lib/osctld/repository.rb', line 25

def pool
  @pool
end

#prune_enabledBoolean (readonly)

Returns:

  • (Boolean)


38
39
40
# File 'lib/osctld/repository.rb', line 38

def prune_enabled
  @prune_enabled
end

#prune_intervalInteger (readonly)

Returns:

  • (Integer)


41
42
43
# File 'lib/osctld/repository.rb', line 41

def prune_interval
  @prune_interval
end

#prune_older_than_daysInteger (readonly)

Returns:

  • (Integer)


44
45
46
# File 'lib/osctld/repository.rb', line 44

def prune_older_than_days
  @prune_older_than_days
end

#stateObject (readonly, protected)

Returns the value of attribute state.



217
218
219
# File 'lib/osctld/repository.rb', line 217

def state
  @state
end

#urlString (readonly)

Returns:

  • (String)


31
32
33
# File 'lib/osctld/repository.rb', line 31

def url
  @url
end

Instance Method Details

#assetsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/osctld/repository.rb', line 77

def assets
  define_assets do |add|
    add.file(
      config_path,
      desc: 'Configuration file',
      user: 0,
      group: 0,
      mode: 0o400
    )
    add.directory(
      cache_path,
      desc: 'Local cache',
      user: UID,
      group: 0,
      mode: 0o700
    )
  end
end

#cache_lock_pathObject



198
199
200
# File 'lib/osctld/repository.rb', line 198

def cache_lock_path
  File.join(cache_path, '.osctld-cache.lock')
end

#cache_pathObject



194
195
196
# File 'lib/osctld/repository.rb', line 194

def cache_path
  File.join(pool.repo_path, name)
end

#config_pathObject



190
191
192
# File 'lib/osctld/repository.rb', line 190

def config_path
  File.join(pool.conf_path, 'repository', "#{name}.yml")
end

#configure(url, prune_enabled: true, prune_interval: DEFAULT_PRUNE_INTERVAL, prune_older_than_days: DEFAULT_PRUNE_OLDER_THAN_DAYS) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/osctld/repository.rb', line 69

def configure(url, prune_enabled: true, prune_interval: DEFAULT_PRUNE_INTERVAL, prune_older_than_days: DEFAULT_PRUNE_OLDER_THAN_DAYS)
  @url = url
  @prune_enabled = prune_enabled
  @prune_interval = prune_interval
  @prune_older_than_days = prune_older_than_days
  save_config
end

#disableObject



105
106
107
108
# File 'lib/osctld/repository.rb', line 105

def disable
  @enabled = false
  save_config
end

#disabled?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/osctld/repository.rb', line 96

def disabled?
  !enabled?
end

#enableObject



100
101
102
103
# File 'lib/osctld/repository.rb', line 100

def enable
  @enabled = true
  save_config
end

#exportObject



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/osctld/repository.rb', line 178

def export
  {
    pool: pool.name,
    name: name,
    url: url,
    enabled: enabled,
    prune_enabled: prune_enabled,
    prune_interval: prune_interval,
    prune_older_than_days: prune_older_than_days
  }
end

#idObject



61
62
63
# File 'lib/osctld/repository.rb', line 61

def id
  name
end

#identObject



65
66
67
# File 'lib/osctld/repository.rb', line 65

def ident
  "#{pool.name}:#{name}"
end

#imagesObject



166
167
168
# File 'lib/osctld/repository.rb', line 166

def images
  # TODO
end

#load_configObject (protected)



219
220
221
222
223
224
225
226
227
228
# File 'lib/osctld/repository.rb', line 219

def load_config
  cfg = OsCtl::Lib::ConfigFile.load_yaml_file(config_path)

  @url = cfg['url']
  @enabled = cfg['enabled']
  @prune_enabled = cfg.fetch('prune_enabled', true)
  @prune_interval = cfg.fetch('prune_interval', DEFAULT_PRUNE_INTERVAL)
  @prune_older_than_days = cfg.fetch('prune_older_than_days', DEFAULT_PRUNE_OLDER_THAN_DAYS)
  @attrs = Attributes.load(cfg['attrs'] || {})
end

#log_typeObject



211
212
213
# File 'lib/osctld/repository.rb', line 211

def log_type
  "repository #{ident}"
end

#manipulation_resourceObject



207
208
209
# File 'lib/osctld/repository.rb', line 207

def manipulation_resource
  ['repository', "#{pool.name}:#{name}"]
end

#prune_images(older_than_days: nil) ⇒ Array(Boolean, Array<String>)

Returns status and a list of deleted image files, if any.

Returns:

  • (Array(Boolean, Array<String>))

    status and a list of deleted image files, if any



160
161
162
163
164
# File 'lib/osctld/repository.rb', line 160

def prune_images(older_than_days: nil)
  with_cache_lock do
    OsCtlRepo.new(self).prune_images(older_than_days:)
  end
end

#save_configObject (protected)



230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/osctld/repository.rb', line 230

def save_config
  regenerate_file(config_path, 0o400) do |f|
    f.write(OsCtl::Lib::ConfigFile.dump_yaml({
      'url' => url,
      'enabled' => enabled?,
      'prune_enabled' => prune_enabled,
      'prune_interval' => prune_interval,
      'prune_older_than_days' => prune_older_than_days,
      'attrs' => attrs.dump
    }))
  end

  File.chown(0, 0, config_path)
end

#set(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :attrs (Hash)


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
# File 'lib/osctld/repository.rb', line 112

def set(opts)
  opts.each do |k, v|
    case k
    when :url
      @url = v

    when :prune_enabled
      @prune_enabled = true
      start_prune

    when :prune_interval
      @prune_interval = v

    when :prune_older_than_days
      @prune_older_than_days = v

    when :attrs
      attrs.update(v)

    else
      raise "unsupported option '#{k}'"
    end
  end

  save_config
end

#startObject



170
171
172
# File 'lib/osctld/repository.rb', line 170

def start
  start_prune if @prune_enabled
end

#start_pruneObject (protected)



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/osctld/repository.rb', line 245

def start_prune
  return if @prune_thread

  @prune_thread = Thread.new do
    loop do
      v = @prune_queue.pop(timeout: @prune_interval)
      break if v == :stop || !@prune_enabled

      log(:info, 'Starting periodic prune')
      prune_images(older_than_days: @prune_older_than_days)
    end
  end
end

#stopObject



174
175
176
# File 'lib/osctld/repository.rb', line 174

def stop
  stop_prune
end

#stop_pruneObject (protected)



259
260
261
262
263
264
265
# File 'lib/osctld/repository.rb', line 259

def stop_prune
  return if @prune_thread.nil?

  @prune_queue << :stop
  @prune_thread.join
  @prune_thread = nil
end

#unset(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :attrs (Array<String>)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/osctld/repository.rb', line 141

def unset(opts)
  opts.each do |k, v|
    case k
    when :prune_enabled
      @prune_enabled = false
      stop_prune

    when :attrs
      v.each { |attr| attrs.unset(attr) }

    else
      raise "unsupported option '#{k}'"
    end
  end

  save_config
end

#with_cache_lockObject



202
203
204
205
# File 'lib/osctld/repository.rb', line 202

def with_cache_lock(&)
  FileUtils.mkdir_p(cache_path)
  Filelock(cache_lock_path, timeout: CACHE_LOCK_TIMEOUT, &)
end