Class: OsCtl::Image::Operations::Image::Build

Inherits:
Base
  • Object
show all
Includes:
Lib::Utils::Log, Lib::Utils::System
Defined in:
lib/osctl/image/operations/image/build.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

run

Constructor Details

#initialize(base_dir, image, opts) ⇒ Build

Returns a new instance of Build.

Parameters:

  • base_dir (String)
  • image (Image)
  • opts (Hash)

Options Hash (opts):

  • :build_dataset (String)
  • :output_dir (String)
  • :vendor (String)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/osctl/image/operations/image/build.rb', line 50

def initialize(base_dir, image, opts)
  @base_dir = base_dir

  @image = image
  image.load_config

  @builder = Builder.new(base_dir, image.builder)
  builder.load_config

  @build_id = SecureRandom.hex(4)
  @build_dataset = File.join(opts[:build_dataset], build_id)
  @output_dataset = File.join(build_dataset, 'output')
  @work_dataset = File.join(build_dataset, 'work')
  @output_dir = opts[:output_dir]

  name = [
    image.distribution,
    image.version,
    image.arch,
    opts[:vendor] || image.vendor,
    image.variant,
  ].join('-')

  @output_tar = File.join(output_dir, "#{name}-archive.tar")
  @output_stream = File.join(output_dir, "#{name}-stream.tar")

  @client = OsCtldClient.new
end

Instance Attribute Details

#base_dirString (readonly)

Returns:

  • (String)


12
13
14
# File 'lib/osctl/image/operations/image/build.rb', line 12

def base_dir
  @base_dir
end

#build_datasetObject (readonly, protected)

Returns the value of attribute build_dataset.



107
108
109
# File 'lib/osctl/image/operations/image/build.rb', line 107

def build_dataset
  @build_dataset
end

#build_idString (readonly)

Returns:

  • (String)


36
37
38
# File 'lib/osctl/image/operations/image/build.rb', line 36

def build_id
  @build_id
end

#builderBuilder (readonly)

Returns:



18
19
20
# File 'lib/osctl/image/operations/image/build.rb', line 18

def builder
  @builder
end

#clientObject (readonly, protected)

Returns the value of attribute client.



107
108
109
# File 'lib/osctl/image/operations/image/build.rb', line 107

def client
  @client
end

#config_fileString (readonly)

Returns:

  • (String)


33
34
35
# File 'lib/osctl/image/operations/image/build.rb', line 33

def config_file
  @config_file
end

#imageImage (readonly)

Returns:



15
16
17
# File 'lib/osctl/image/operations/image/build.rb', line 15

def image
  @image
end

#install_dirString (readonly)

Returns:

  • (String)


30
31
32
# File 'lib/osctl/image/operations/image/build.rb', line 30

def install_dir
  @install_dir
end

#output_datasetString (readonly)

Returns:

  • (String)


21
22
23
# File 'lib/osctl/image/operations/image/build.rb', line 21

def output_dataset
  @output_dataset
end

#output_dirString (readonly, protected)

Returns:

  • (String)


27
28
29
# File 'lib/osctl/image/operations/image/build.rb', line 27

def output_dir
  @output_dir
end

#output_streamString (readonly)

Returns:

  • (String)


42
43
44
# File 'lib/osctl/image/operations/image/build.rb', line 42

def output_stream
  @output_stream
end

#output_tarString (readonly)

Returns:

  • (String)


39
40
41
# File 'lib/osctl/image/operations/image/build.rb', line 39

def output_tar
  @output_tar
end

#work_datasetString (readonly)

Returns:

  • (String)


24
25
26
# File 'lib/osctl/image/operations/image/build.rb', line 24

def work_dataset
  @work_dataset
end

#work_dirObject (readonly, protected)

Returns the value of attribute work_dir.



107
108
109
# File 'lib/osctl/image/operations/image/build.rb', line 107

def work_dir
  @work_dir
end

Instance Method Details

#buildObject (protected)



109
110
111
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/osctl/image/operations/image/build.rb', line 109

def build
  Operations::Builder::UseOrCreate.run(builder, base_dir)

  root_uid, root_gid = Operations::Builder::GetRootUgid.run(builder)

  zfs(
    :create,
    "-p -o uidmap=0:#{root_uid}:65536 -o gidmap=0:#{root_gid}:65536",
    work_dataset
  )
  zfs(
    :create,
    "-p -o uidmap=0:#{root_uid}:65536 -o gidmap=0:#{root_gid}:65536",
    output_dataset
  )

  @work_dir = zfs(:get, '-H -o value mountpoint', work_dataset).output.strip
  @output_dir = zfs(:get, '-H -o value mountpoint', output_dataset).output.strip
  @install_dir = File.join(output_dir, 'private')
  @config_file = File.join(install_dir, 'container.yml')

  Dir.mkdir(install_dir)

  client.batch do
    client.bind_mount(builder.ctid, base_dir, builder_base_dir)
    client.bind_mount(builder.ctid, work_dir, builder_work_dir)
    client.bind_mount(builder.ctid, install_dir, builder_install_dir)

    client.activate_mount(builder.ctid, builder_base_dir)
    client.activate_mount(builder.ctid, builder_work_dir)
    client.activate_mount(builder.ctid, builder_install_dir)
  end

  rc = Operations::Builder::ControlledExec.run(
    builder,
    [
      File.join(builder_base_dir, 'bin', 'runner'),
      'image',
      'build',
      build_id,
      builder_work_dir,
      builder_install_dir,
      image.name,
    ],
    id: build_id,
    client: client,
  )

  if rc != 0
    raise OperationError,
          "build of #{image.name} on #{builder.name} failed with "+
          "exit status #{rc}"
  end

  zfs(:unmount, nil, output_dataset)
  zfs(:set, 'uidmap=none gidmap=none', output_dataset)
  zfs(:mount, nil, output_dataset)

  Operations::Image::Export.run(self)
end

#builder_base_dirObject (protected)



200
201
202
# File 'lib/osctl/image/operations/image/build.rb', line 200

def builder_base_dir
  "/build/basedir.#{build_id}"
end

#builder_install_dirObject (protected)



208
209
210
# File 'lib/osctl/image/operations/image/build.rb', line 208

def builder_install_dir
  "/build/installdir.#{build_id}"
end

#builder_work_dirObject (protected)



204
205
206
# File 'lib/osctl/image/operations/image/build.rb', line 204

def builder_work_dir
  "/build/workdir.#{build_id}"
end

#cached?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/osctl/image/operations/image/build.rb', line 98

def cached?
  File.exist?(output_tar) || File.exist?(output_stream)
end

#cleanupObject (protected)



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/osctl/image/operations/image/build.rb', line 170

def cleanup
  client.batch do
    client.ignore_error { client.unmount(builder.ctid, builder_work_dir) }
    client.ignore_error { client.unmount(builder.ctid, builder_install_dir) }
    client.ignore_error { client.unmount(builder.ctid, builder_base_dir) }
  end

  if builder.attrs
    [builder_base_dir, builder_work_dir, builder_install_dir].each do |dir|
      begin
        Dir.rmdir(File.join(builder.attrs[:rootfs], dir))
      rescue Errno::ENOENT
      end
    end
  end

  zfs(:destroy, nil, work_dataset, valid_rcs: :all)

  list = zfs(:list, '-H -o name -t snapshot', output_dataset, valid_rcs: :all)

  if list.success?
    list.output.split("\n").each do |s|
      zfs(:destroy, nil, s.strip)
    end
  end

  zfs(:destroy, nil, output_dataset, valid_rcs: :all)
  zfs(:destroy, nil, build_dataset, valid_rcs: :all)
end

#executeOperations::Image::Build



80
81
82
83
84
85
86
# File 'lib/osctl/image/operations/image/build.rb', line 80

def execute
  log(:info, "Using builder #{builder.name}")
  build
  self
ensure
  cleanup
end

#has_config_file?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/osctl/image/operations/image/build.rb', line 88

def has_config_file?
  File.exist?(config_file)
end

#log_typeObject



102
103
104
# File 'lib/osctl/image/operations/image/build.rb', line 102

def log_type
  "build #{image.name}@#{builder.name}"
end

#read_config_fileObject



92
93
94
95
96
# File 'lib/osctl/image/operations/image/build.rb', line 92

def read_config_file
  ret = YAML.load_file(config_file)
  File.unlink(config_file)
  ret
end