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.



118
119
120
# File 'lib/osctl/repo/local/repository.rb', line 118

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

  FileUtils.mkdir_p(t.abs_dir_path)

  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

#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:



62
63
64
# File 'lib/osctl/repo/local/repository.rb', line 62

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

#imagesObject



112
113
114
# File 'lib/osctl/repo/local/repository.rb', line 112

def images
  index.images
end


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

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:



68
69
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
# File 'lib/osctl/repo/local/repository.rb', line 68

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



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

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



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

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