Class: OsCtld::OsCtlRepo

Inherits:
Object
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log
Defined in:
lib/osctld/osctl_repo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ OsCtlRepo

Returns a new instance of OsCtlRepo.

Parameters:



15
16
17
# File 'lib/osctld/osctl_repo.rb', line 15

def initialize(repo)
  @repo = repo
end

Instance Attribute Details

#repoRepository (readonly)

Returns:



12
13
14
# File 'lib/osctld/osctl_repo.rb', line 12

def repo
  @repo
end

Instance Method Details

#exec_as_repo_user(*args) ⇒ Array<Integer, String> (protected) Also known as: osctl_repo

Run command as an unprivileged user that has access to the repository data

Returns:

  • (Array<Integer, String>)

    exit status and data



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/osctld/osctl_repo.rb', line 112

def exec_as_repo_user(*args)
  r, w = IO.pipe

  pid = Process.fork do
    SwitchUser.switch_to_system(
      Repository::USER,
      Repository::UID,
      Etc.getgrnam('nogroup').gid,
      RunState::REPOSITORY_DIR
    )

    ENV['GLI_DEBUG'] = 'true'

    $stdout.reopen(w)
    r.close

    Process.exec(*args)
  end

  w.close

  data = r.read
  r.close
  Process.wait(pid)
  [$?.exitstatus, data]
end

#get_image_path(tpl, format) ⇒ String?

Returns path to the image in cache.

Parameters:

  • tpl (Hash)
  • format (:tar, :zfs)

Options Hash (tpl):

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

Returns:

  • (String, nil)

    path to the image in cache



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
101
102
# File 'lib/osctld/osctl_repo.rb', line 76

def get_image_path(tpl, format)
  exit_status, data = osctl_repo(
    File.join(OsCtl::Repo.root, 'bin', 'osctl-repo'),
    'remote', 'get', 'path',
    '--cache', repo.cache_path,
    repo.url,
    tpl[:vendor], tpl[:variant], tpl[:arch], tpl[:distribution], tpl[:version],
    format.to_s
  )

  case exit_status
  when OsCtl::Repo::EXIT_OK
    data.strip

  when OsCtl::Repo::EXIT_FORMAT_NOT_FOUND
    nil

  when OsCtl::Repo::EXIT_IMAGE_NOT_FOUND
    raise ImageNotFound

  when OsCtl::Repo::EXIT_HTTP_ERROR, OsCtl::Repo::EXIT_NETWORK_ERROR
    raise ImageRepositoryUnavailable

  else
    raise "osctl-repo remote get path failed with exit status #{exit_status}"
  end
end

#list_imagesArray<Repository::Image>

Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/osctld/osctl_repo.rb', line 20

def list_images
  exit_status, data = osctl_repo(
    File.join(OsCtl::Repo.root, 'bin', 'osctl-repo'),
    'remote', 'ls',
    '--cache', repo.cache_path,
    repo.url
  )

  case exit_status
  when OsCtl::Repo::EXIT_OK
    JSON.parse(data, symbolize_names: true).map { |v| Repository::Image.new(v) }

  when OsCtl::Repo::EXIT_HTTP_ERROR, OsCtl::Repo::EXIT_NETWORK_ERROR
    raise ImageRepositoryUnavailable

  else
    raise "osctl-repo remote ls failed with exit status #{exit_status}"
  end
end

#log_typeObject



104
105
106
# File 'lib/osctld/osctl_repo.rb', line 104

def log_type
  "repository #{repo.ident}"
end

#prune_images(older_than_days: nil) ⇒ Array(Boolean, Array<String>)

Returns status and a list of deleted image files, if any.

Returns:

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

    status and a list of deleted image files, if any



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/osctld/osctl_repo.rb', line 41

def prune_images(older_than_days: nil)
  cmd = [
    'find',
    repo.cache_path,
    '-name',
    '*.tar'
  ]
  cmd << '-mtime' << "+#{older_than_days}" if older_than_days
  cmd << '-print'
  cmd << '-delete'

  exit_status, output = exec_as_repo_user(*cmd)

  if exit_status != 0
    log(:warn, "Command #{cmd.join(' ').inspect} exited with status #{exit_status}")
    return [false, []]
  end

  files = output.strip.split

  files.each do |file|
    log(:info, "Pruned image file #{file}")
  end

  [true, files]
end