Class: OsCtl::Repo::Local::Repository

Inherits:
Object
  • Object
show all
Includes:
Lib::Utils::File
Defined in:
lib/osctl/repo/local/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Repository

Returns a new instance of Repository.



10
11
12
13
# File 'lib/osctl/repo/local/repository.rb', line 10

def initialize(path)
  @path = File.join(path, "v#{SCHEMA}")
  @index = Local::Index.new(self)
end

Instance Attribute Details

#indexObject (readonly, protected)

Returns the value of attribute index.



120
121
122
# File 'lib/osctl/repo/local/repository.rb', line 120

def index
  @index
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/osctl/repo/local/repository.rb', line 8

def path
  @path
end

Instance Method Details

#add(vendor, variant, arch, dist, ver, opts) ⇒ Object



24
25
26
27
28
29
30
31
32
33
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
60
61
# File 'lib/osctl/repo/local/repository.rb', line 24

def add(vendor, variant, arch, dist, ver, opts)
  t = Base::Image.new(
    self,
    vendor,
    variant,
    arch,
    dist,
    ver,
    tags: opts[:tags],
    image: opts[:image].keys.map(&:to_s)
  )
  existing = index.find(vendor, variant, arch, dist, ver)

  FileUtils.mkdir_p(t.abs_dir_path)
  cleanup_replaced_image(existing, new_image: t) if existing && existing.version == ver

  opts[:image].each do |format, file|
    FileUtils.cp(file, t.abs_image_path(format))
  end

  t.tags.each do |tag|
    path = t.abs_tag_path(tag)

    if File.symlink?(path)
      next if File.readlink(path) == t.version

      File.unlink(path)

    else
      unlink_if_exists(path)
    end

    File.symlink(t.version, path)
  end

  index.add(t)
  index.save
end

#cleanup_replaced_image(image, new_image:) ⇒ Object (protected)



141
142
143
144
145
146
147
148
149
150
# File 'lib/osctl/repo/local/repository.rb', line 141

def cleanup_replaced_image(image, new_image:)
  (image.image - new_image.image).each do |format|
    FileUtils.rm_f(image.abs_image_path(format))
  end

  (image.tags - new_image.tags).each do |tag|
    path = image.abs_tag_path(tag)
    File.unlink(path) if File.symlink?(path)
  end
end

#createObject



19
20
21
22
# File 'lib/osctl/repo/local/repository.rb', line 19

def create
  FileUtils.mkpath(path)
  index.save
end

#exist?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/osctl/repo/local/repository.rb', line 15

def exist?
  index.exist?
end

#find(vendor, variant, arch, distribution, version) ⇒ Base::Image?

Returns:



64
65
66
# File 'lib/osctl/repo/local/repository.rb', line 64

def find(vendor, variant, arch, distribution, version)
  index.find(vendor, variant, arch, distribution, version)
end

#imagesObject



114
115
116
# File 'lib/osctl/repo/local/repository.rb', line 114

def images
  index.images
end


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/osctl/repo/local/repository.rb', line 122

def install_symlink(type, base_path, link_name, target_name)
  path = File.join(base_path, link_name)
  target = File.join(base_path, target_name)

  if !Dir.exist?(target)
    raise GLI::BadCommandLine, "#{type} '#{target_name}' not found"

  elsif File.symlink?(path)
    return if File.readlink(path) == target_name

    File.unlink(path)

  else
    unlink_if_exists(path)
  end

  File.symlink(target_name, path)
end

#remove(image) ⇒ Object

Remove image from the repository

Parameters:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/osctl/repo/local/repository.rb', line 70

def remove(image)
  # Remove image from the index
  index.delete(image)
  index.save

  # Remove image
  image.image.each do |v|
    path = image.abs_image_path(v)

    FileUtils.rm_f(path)
  end

  # Remove tags
  image.tags.each do |v|
    path = image.abs_tag_path(v)

    File.unlink(path) if File.symlink?(path)
  end

  # Remove empty dir from the version dir up to the repository root
  version_dir = image.abs_dir_path

  5.times.map do |i|
    File.absolute_path(File.join(version_dir, *Array.new(i, '..')))
  end.each do |dir|
    # Use Dir.empty?(dir) when we don't care for Ruby < 2.4
    break unless (Dir.entries(dir) - %w[. ..]).empty?

    Dir.rmdir(dir)
  end
end

#set_default_variant(vendor, variant) ⇒ Object



108
109
110
111
112
# File 'lib/osctl/repo/local/repository.rb', line 108

def set_default_variant(vendor, variant)
  install_symlink('variant', File.join(path, vendor), 'default', variant)
  index.set_default_variant(vendor, variant)
  index.save
end

#set_default_vendor(vendor) ⇒ Object



102
103
104
105
106
# File 'lib/osctl/repo/local/repository.rb', line 102

def set_default_vendor(vendor)
  install_symlink('vendor', path, 'default', vendor)
  index.set_default_vendor(vendor)
  index.save
end