Class: Dodebui::Build

Inherits:
Object
  • Object
show all
Defined in:
lib/dodebui/build.rb

Overview

Handles the build process of a package

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(distribution) ⇒ Build

Returns a new instance of Build.



10
11
12
13
# File 'lib/dodebui/build.rb', line 10

def initialize(distribution)
  @distribution = distribution
  @cli = distribution.cli
end

Instance Attribute Details

#distributionObject (readonly)

Returns the value of attribute distribution.



8
9
10
# File 'lib/dodebui/build.rb', line 8

def distribution
  @distribution
end

Instance Method Details

#buildObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dodebui/build.rb', line 80

def build
  build_container_create_start

  build_dependencies

  build_package

  @container.stop

  true
rescue RuntimeError
  false
end

#build_container_create_startObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dodebui/build.rb', line 40

def build_container_create_start
  logger.info("Creating container #{@distribution.codename}")
  @container = Docker::Container.create(
    'Image' => @distribution.image_name,
    'Cmd' => %w(sleep 3600),
    'WorkingDir' => '/_build/source'
  )
  logger.info("Starting container #{@distribution.codename}")
  @container.start('Binds' => [
    "#{File.join(cache_dir, 'archives')}:/var/cache/apt/archives",
    "#{build_dir}:/_build"
  ])
end

#build_dependenciesObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dodebui/build.rb', line 54

def build_dependencies
  logger.info("Installing dependencies #{@distribution.codename}")
  stdout, stderr, ret_val = @container.exec([
    '/usr/lib/pbuilder/pbuilder-satisfydepends-aptitude'
  ])
  write_log('apt_install_deps', stdout, stderr)
  if ret_val != 0
    logger.warn("Failed installing dependencies #{@distribution.codename}")
    fail
  end
  logger.info("Finished installing dependencies #{@distribution.codename}")
end

#build_dirObject



103
104
105
106
107
108
109
110
111
112
# File 'lib/dodebui/build.rb', line 103

def build_dir
  File.expand_path(
    File.join(
      @cli.wd,
      '..',
      '_build',
      "#{distribution.os}_#{distribution.codename}"
    )
  )
end

#build_packageObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dodebui/build.rb', line 67

def build_package
  logger.info("Building package #{@distribution.codename}")
  stdout, stderr, ret_val = @container.exec([
    'dpkg-buildpackage'
  ])
  write_log('build', stdout, stderr)
  if ret_val != 0
    logger.warn("Failed building package #{@distribution.codename}")
    fail
  end
  logger.info("Finished building package #{@distribution.codename}")
end

#cache_dirObject



94
95
96
97
98
99
100
101
# File 'lib/dodebui/build.rb', line 94

def cache_dir
  File.expand_path(
    File.join(
      '/var/lib/dodebui',
      "#{distribution.os}_#{distribution.codename}"
    )
  )
end

#local_expect(desc, *args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dodebui/build.rb', line 15

def local_expect(desc, *args)
  Open3.popen3(*args) do |_i, o, e, t|
    if args[0].is_a? Hash
      cmd = args[1]
    else
      cmd = args[0]
    end
    ret_val = t.value.exitstatus
    if ret_val == 0
      logger.debug("#{desc} (cmd=#{cmd}): succeed")
    else
      output = "Exec failed cmd=#{cmd} ret_val=#{ret_val}"
      output += "stdout=#{o.read} stderr=#{e.read}"
      fail output
    end
  end
end

#loggerObject



120
121
122
# File 'lib/dodebui/build.rb', line 120

def logger
  @cli.logger
end

#sourceObject



114
115
116
117
118
# File 'lib/dodebui/build.rb', line 114

def source
  source_copy
  source_changelog
  source_templates
end

#source_changelogObject



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/dodebui/build.rb', line 177

def source_changelog
  path = File.join(source_dir, 'debian/changelog')
  logger.debug "Modify changelog file #{path}"
  local_expect(
    'append distribution build to changelog',
    {
      'DEBFULLNAME' => 'Jenkins Autobuilder',
      'DEBEMAIL' => '[email protected]'
    },
    source_changelog_dch(path)
  )
end

#source_changelog_dch(path) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/dodebui/build.rb', line 165

def source_changelog_dch(path)
  output = 'dch --changelog %{path} -l "+%{cn_str}%{cn}" -D "%{cn}" '
  output += '--force-distribution '
  output += '"Build a changelog entry for %{cn} %{cn}"'

  output % {
    cn: @distribution.codename,
    cn_str: @distribution.codename_str,
    path: path
  }
end

#source_copyObject



157
158
159
160
161
162
163
# File 'lib/dodebui/build.rb', line 157

def source_copy
  logger.debug "Start copying sources to #{source_dir}"
  FileUtils.mkdir_p build_dir
  FileUtils.rm_rf source_dir
  FileUtils.cp_r @cli.wd, source_dir
  logger.debug "Finished copying sources to #{source_dir}"
end

#source_dirObject



124
125
126
# File 'lib/dodebui/build.rb', line 124

def source_dir
  File.join(build_dir, 'source')
end

#source_template_eval(path) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/dodebui/build.rb', line 136

def source_template_eval(path)
  logger.debug "Evaluate template #{path}"
  erb = ERB.new(
    File.read(path),
    nil,
    '-'
  )
  erb.result(source_template_namespace.priv_binding)
end

#source_template_namespaceObject



128
129
130
131
132
133
134
# File 'lib/dodebui/build.rb', line 128

def source_template_namespace
  TemplateNamespace.new(
    os: @distribution.os,
    codename: @distribution.codename,
    codename_int: @distribution.codename_int
  )
end

#source_templatesObject



146
147
148
149
150
151
152
153
154
155
# File 'lib/dodebui/build.rb', line 146

def source_templates
  @cli.source_templates.each do |template|
    src = File.join(source_dir, template)
    dest = src[0...-4]
    File.open(dest, 'w') do |file|
      file.write(source_template_eval(src))
    end
    sh "chmod +x #{template}" if template == 'debian/rules'
  end
end

#write_log(name, o, e) ⇒ Object



33
34
35
36
37
38
# File 'lib/dodebui/build.rb', line 33

def write_log(name, o, e)
  o_path = File.join(build_dir, "#{name}.stdout.log")
  e_path = File.join(build_dir, "#{name}.stderr.log")
  File.open(o_path, 'w') { |file| file.write(o.join '') }
  File.open(e_path, 'w') { |file| file.write(e.join '') }
end