Module: OsCtld::Utils::Container

Included in:
Commands::Container::Create, Commands::Container::Reinstall
Defined in:
lib/osctld/utils/container.rb

Instance Method Summary collapse

Instance Method Details

#from_remote_template(builder, tpl) ⇒ Object



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
60
61
62
63
64
65
66
67
68
# File 'lib/osctld/utils/container.rb', line 31

def from_remote_template(builder, tpl)
  # TODO: this check is done too late -- the dataset has already been created
  #       and the repo may not exist
  if opts[:repository]
    repo = DB::Repositories.find(opts[:repository], builder.pool)
    error!('repository not found') unless repo
    repos = [repo]

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

  # Rootfs (private/) has to be set up both before and after
  # template application. Before, to prepare the directory for tar -x,
  # after to ensure correct permission.
  builder.setup_rootfs

  repo = repos.detect do |repo|
    begin
      builder.from_repo_template(repo, tpl[:template])

    rescue TemplateNotFound
      next

    rescue TemplateRepositoryUnavailable
      progress("Repository #{repo.name} is unreachable")
      next
    end

    true
  end

  error!('template not found') unless repo

  builder.setup_rootfs
end

#from_stream(builder) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/osctld/utils/container.rb', line 5

def from_stream(builder)
  if opts[:template][:path]
    File.open(opts[:template][:path]) do |f|
      gz = Zlib::GzipReader.new(f)
      recv_stream(builder, gz)
      gz.close
    end

    builder.shift_dataset
    distribution, version, arch = builder.get_distribution_info(opts[:template][:path])

    builder.configure(
      opts[:distribution] || distribution,
      opts[:version] || version,
      opts[:arch] || arch
    )

  else
    client.send({status: true, response: 'continue'}.to_json + "\n", 0)
    recv_stream(builder, client.recv_io)

    builder.shift_dataset
    builder.configure(opts[:distribution], opts[:version], opts[:arch])
  end
end

#recv_stream(builder, io) ⇒ Object



70
71
72
73
74
# File 'lib/osctld/utils/container.rb', line 70

def recv_stream(builder, io)
  builder.from_stream do |recv|
    recv.write(io.read(16*1024)) until io.eof?
  end
end