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

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.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/osctld/repository.rb', line 46

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:



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

def attrs
  @attrs
end

#enabledBoolean (readonly) Also known as: enabled?

Returns:

  • (Boolean)


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

def enabled
  @enabled
end

#nameString (readonly)

Returns:

  • (String)


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

def name
  @name
end

#poolPool (readonly)

Returns:



22
23
24
# File 'lib/osctld/repository.rb', line 22

def pool
  @pool
end

#prune_enabledBoolean (readonly)

Returns:

  • (Boolean)


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

def prune_enabled
  @prune_enabled
end

#prune_intervalInteger (readonly)

Returns:

  • (Integer)


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

def prune_interval
  @prune_interval
end

#prune_older_than_daysInteger (readonly)

Returns:

  • (Integer)


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

def prune_older_than_days
  @prune_older_than_days
end

#stateObject (readonly, protected)

Returns the value of attribute state.



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

def state
  @state
end

#urlString (readonly)

Returns:

  • (String)


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

def url
  @url
end

Instance Method Details

#assetsObject



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

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_pathObject



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

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

#config_pathObject



185
186
187
# File 'lib/osctld/repository.rb', line 185

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



66
67
68
69
70
71
72
# File 'lib/osctld/repository.rb', line 66

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



102
103
104
105
# File 'lib/osctld/repository.rb', line 102

def disable
  @enabled = false
  save_config
end

#disabled?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/osctld/repository.rb', line 93

def disabled?
  !enabled?
end

#enableObject



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

def enable
  @enabled = true
  save_config
end

#exportObject



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/osctld/repository.rb', line 173

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



58
59
60
# File 'lib/osctld/repository.rb', line 58

def id
  name
end

#identObject



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

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

#imagesObject



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

def images
  # TODO
end

#load_configObject (protected)



205
206
207
208
209
210
211
212
213
214
# File 'lib/osctld/repository.rb', line 205

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



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

def log_type
  "repository #{ident}"
end

#manipulation_resourceObject



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

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



157
158
159
# File 'lib/osctld/repository.rb', line 157

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

#save_configObject (protected)



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

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)


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

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



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

def start
  start_prune if @prune_enabled
end

#start_pruneObject (protected)



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

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



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

def stop
  stop_prune
end

#stop_pruneObject (protected)



245
246
247
248
249
250
251
# File 'lib/osctld/repository.rb', line 245

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>)


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

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