Class: OsCtld::OsCtlRepo

Inherits:
Object
  • Object
show all
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:



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

def initialize(repo)
  @repo = repo
end

Instance Attribute Details

#repoObject (readonly)

Returns the value of attribute repo.



8
9
10
# File 'lib/osctld/osctl_repo.rb', line 8

def repo
  @repo
end

Instance Method Details

#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



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

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:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/osctld/osctl_repo.rb', line 16

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

#osctl_repo(*args) ⇒ Array<Integer, String> (protected)

Returns exit status and data.

Returns:

  • (Array<Integer, String>)

    exit status and data



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/osctld/osctl_repo.rb', line 75

def osctl_repo(*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