Module: OsCtld::Utils::Container

Instance Method Summary collapse

Instance Method Details

#format_unavailable_repositories(unavailable) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/osctld/utils/container.rb', line 122

def format_unavailable_repositories(unavailable)
  unavailable.map do |name, message|
    if message && !message.empty?
      "#{name} (#{message})"
    else
      name
    end
  end.join(', ')
end

#get_image_path(repos, tpl) ⇒ Array<String, Hash<String, String>>

Parameters:

Options Hash (tpl):

  • :distribution (String)
  • :version (String)
  • :arch (String)
  • :vendor (String)
  • :variant (String)

Returns:

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


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/osctld/utils/container.rb', line 28

def get_image_path(repos, tpl)
  unavailable = {}

  repos.each do |repo|
    osctl_repo = OsCtlRepo.new(repo)

    begin
      %i[zfs tar].each do |format|
        path = osctl_repo.get_image_path(tpl, format)
        return [path, unavailable] if path
      end
    rescue ImageNotFound
      next
    rescue ImageRepositoryUnavailable => e
      unavailable[repo.name] = e.message
      next
    end
  end

  [nil, unavailable]
end

#get_image_path!(repos, tpl) ⇒ String

Parameters:

Returns:

  • (String)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/osctld/utils/container.rb', line 53

def get_image_path!(repos, tpl)
  if repos.empty?
    error!('no enabled repositories are available for container images')
  end

  path, unavailable = get_image_path(repos, tpl)
  return path if path

  if unavailable.any?
    error!(image_unavailable_message(tpl, unavailable))
  end

  error!(image_not_found_message(tpl, repos))
end

#get_repositories(pool) ⇒ Array<Repository>

Parameters:

Returns:



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/osctld/utils/container.rb', line 7

def get_repositories(pool)
  if opts[:repository]
    repo = DB::Repositories.find(opts[:repository], pool)
    error!('repository not found') unless repo
    [repo]

  else
    DB::Repositories.get.select do |repo|
      repo.enabled? && repo.pool == pool
    end
  end
end

#image_not_found_message(tpl, repos) ⇒ Object



112
113
114
115
# File 'lib/osctld/utils/container.rb', line 112

def image_not_found_message(tpl, repos)
  "container image #{image_spec(tpl)} not found in repositories: " \
    "#{repos.map(&:name).join(', ')}"
end

#image_spec(tpl) ⇒ Object



132
133
134
135
136
# File 'lib/osctld/utils/container.rb', line 132

def image_spec(tpl)
  "#{tpl[:distribution]}:#{tpl[:version]} " \
    "(arch=#{tpl[:arch]}, vendor=#{tpl[:vendor]}, " \
    "variant=#{tpl[:variant]})"
end

#image_unavailable_message(tpl, unavailable) ⇒ Object



117
118
119
120
# File 'lib/osctld/utils/container.rb', line 117

def image_unavailable_message(tpl, unavailable)
  "unable to fetch container image #{image_spec(tpl)}; repositories " \
    "unavailable: #{format_unavailable_repositories(unavailable)}"
end

#remove_accounting_cgroups(ct) ⇒ Object

Remove accounting cgroups to reset counters



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

def remove_accounting_cgroups(ct)
  tries = 0

  begin
    %w[cpuacct cpuset memory].each do |subsys|
      CGroup.rmpath(subsys, ct.base_cgroup_path)
    end
  rescue SystemCallError => e
    ct.log(:warn, "Error occurred while pruning cgroups: #{e.message}")

    return if tries >= 5

    tries += 1
    sleep(0.5)
    retry
  end
end

#with_image_path(pool, type:, path:, image:, &block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/osctld/utils/container.rb', line 100

def with_image_path(pool, type:, path:, image:, &block)
  case type
  when 'image'
    block.call(path)
  when 'remote'
    progress('Fetching image')
    with_repository_image_path!(get_repositories(pool), image) { |tpl_path| block.call(tpl_path) }
  else
    error!('invalid type')
  end
end

#with_repository_image_path!(repos, tpl) ⇒ Object



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/osctld/utils/container.rb', line 68

def with_repository_image_path!(repos, tpl)
  if repos.empty?
    error!('no enabled repositories are available for container images')
  end

  unavailable = {}

  repos.each do |repo|
    osctl_repo = OsCtlRepo.new(repo)

    begin
      repo.with_cache_lock do
        %i[zfs tar].each do |format|
          path = osctl_repo.get_image_path(tpl, format)
          return yield(path) if path
        end
      end
    rescue ImageNotFound
      next
    rescue ImageRepositoryUnavailable => e
      unavailable[repo.name] = e.message
      next
    end
  end

  if unavailable.any?
    error!(image_unavailable_message(tpl, unavailable))
  end

  error!(image_not_found_message(tpl, repos))
end