Class: OsCtld::Repository

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

Defined Under Namespace

Classes: Image

Constant Summary collapse

USER =
'repository'
UID =
Etc.getpwnam(USER).uid

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



18
19
20
21
22
23
24
25
26
# File 'lib/osctld/repository.rb', line 18

def initialize(pool, name, load: true)
  init_lock
  init_manipulable
  @pool = pool
  @name = name
  @enabled = true
  @attrs = Attributes.new
  load_config if load
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs



16
17
18
# File 'lib/osctld/repository.rb', line 16

def attrs
  @attrs
end

#nameObject (readonly)

Returns the value of attribute name



16
17
18
# File 'lib/osctld/repository.rb', line 16

def name
  @name
end

#poolObject (readonly)

Returns the value of attribute pool



16
17
18
# File 'lib/osctld/repository.rb', line 16

def pool
  @pool
end

#stateObject (readonly, protected)

Returns the value of attribute state



126
127
128
# File 'lib/osctld/repository.rb', line 126

def state
  @state
end

#urlObject (readonly)

Returns the value of attribute url



16
17
18
# File 'lib/osctld/repository.rb', line 16

def url
  @url
end

Instance Method Details

#assetsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/osctld/repository.rb', line 37

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

#cache_pathObject



117
118
119
# File 'lib/osctld/repository.rb', line 117

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

#config_pathObject



113
114
115
# File 'lib/osctld/repository.rb', line 113

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

#configure(url) ⇒ Object



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

def configure(url)
  @url = url
  save_config
end

#disableObject



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

def disable
  @enabled = false
  save_config
end

#disabled?Boolean

Returns:

  • (Boolean)


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

def disabled?
  !enabled?
end

#enableObject



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

def enable
  @enabled = true
  save_config
end

#enabled?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/osctld/repository.rb', line 56

def enabled?
  @enabled
end

#idObject



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

def id
  name
end

#imagesObject



109
110
111
# File 'lib/osctld/repository.rb', line 109

def images
  # TODO
end

#load_configObject (protected)



128
129
130
131
132
133
134
# File 'lib/osctld/repository.rb', line 128

def load_config
  cfg = YAML.load_file(config_path)

  @url = cfg['url']
  @enabled = cfg['enabled']
  @attrs = Attributes.load(cfg['attrs'] || {})
end

#manipulation_resourceObject



121
122
123
# File 'lib/osctld/repository.rb', line 121

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

#save_configObject (protected)



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/osctld/repository.rb', line 136

def save_config
  File.open(config_path, 'w', 0400) do |f|
    f.write(YAML.dump({
      'url' => url,
      'enabled' => enabled?,
      'attrs' => attrs.dump,
    }))
  end

  File.chown(0, 0, config_path)
end

#set(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :attrs (Hash)


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

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

    when :attrs
      attrs.update(v)

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

  save_config
end

#unset(opts) ⇒ Object

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :attrs (Array<String>)


95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/osctld/repository.rb', line 95

def unset(opts)
  opts.each do |k, v|
    case k
    when :attrs
      v.each { |attr| attrs.unset(attr) }

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

  save_config
end