Module: OsCtld::Utils::Container

Instance Method Summary collapse

Instance Method Details

#format_unavailable_repositories(unavailable) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/osctld/utils/container.rb', line 78

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



68
69
70
71
# File 'lib/osctld/utils/container.rb', line 68

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



88
89
90
91
92
# File 'lib/osctld/utils/container.rb', line 88

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

#image_unavailable_message(tpl, unavailable) ⇒ Object



73
74
75
76
# File 'lib/osctld/utils/container.rb', line 73

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/osctld/utils/container.rb', line 95

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