Module: Pkg::Deb::Repo

Defined in:
lib/packaging/deb/repo.rb

Constant Summary collapse

DEBIAN_PACKAGING_ARCHES =

This is the default set of arches we are using for our reprepro repos. We take this list and combine it with the list of supported arches for each given platform to ensure a complete set of architectures. We use this when we initially create the repos and when we sign the repos.

['i386', 'amd64', 'arm64', 'armel', 'armhf', 'powerpc', 'ppc64el', 'sparc', 'mips', 'mipsel']

Class Method Summary collapse

Class Method Details

.base_urlObject



20
21
22
# File 'lib/packaging/deb/repo.rb', line 20

def base_url
  "http://#{Pkg::Config.builds_server}/#{Pkg::Config.project}/#{Pkg::Config.ref}"
end

.create_repos(directory = 'repos') ⇒ Object

This method is doing too much for its name



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/packaging/deb/repo.rb', line 119

def create_repos(directory = 'repos')
  artifact_directory = File.join(Pkg::Config.jenkins_repo_path, Pkg::Config.project, Pkg::Config.ref)
  artifact_paths = Pkg::Repo.directories_that_contain_packages(File.join(artifact_directory, 'artifacts'), 'deb')
  Pkg::Repo.populate_repo_directory(artifact_directory)
  command = repo_creation_command(File.join(artifact_directory, 'repos'), artifact_paths)

  begin
    Pkg::Util::Net.remote_execute(Pkg::Config.distribution_server, command)
    # Now that we've created our package repositories, we can generate repo
    # configurations for use with downstream jobs, acceptance clients, etc.
    Pkg::Deb::Repo.generate_repo_configs

    # Now that we've created the repo configs, we can ship them
    Pkg::Deb::Repo.ship_repo_configs
  ensure
    # Always remove the lock file, even if we've failed
    Pkg::Util::Net.remote_execute(Pkg::Config.distribution_server, "rm -f #{artifact_directory}/repos/.lock")
  end
end

.deploy_repos(apt_path, destination_staging_path, origin_server, destination_server, dryrun = false) ⇒ Object

Deprecated.

this command will die a painful death when we are able to sit down with Operations and refactor our distribution infra. It’s extremely Debian specific due to how Debian repos are signed, which is why it lives here. Yes, it is basically just a layer of indirection around the task of copying content from one node to another. No, I am not proud of it. - Ryan McKern 11/2015

Parameters:

  • apt_path (String)

    path for Deb repos on local and remote filesystem

  • destination_staging_path (String)

    staging path for Deb repos on remote filesystem

  • origin_server (String)

    remote host to start the rsync from

  • destination_server (String)

    remote host to send rsynced content to

  • dryrun (Boolean) (defaults to: false)

    whether or not to use ‘–dry-run’



249
250
251
252
253
254
255
256
257
258
259
# File 'lib/packaging/deb/repo.rb', line 249

def deploy_repos(apt_path, destination_staging_path, origin_server, destination_server, dryrun = false)
  rsync_command = repo_deployment_command(apt_path, destination_staging_path, destination_server, dryrun)
  cp_command = repo_deployment_command(destination_staging_path, apt_path, nil, dryrun)

  Pkg::Util::Net.remote_execute(origin_server, rsync_command)
  if dryrun
    puts "[DRYRUN] not executing #{cp_command} on #{destination_server}"
  else
    Pkg::Util::Net.remote_execute(destination_server, cp_command)
  end
end

.generate_repo_configs(source = "repos", target = "repo_configs") ⇒ Object

Generate apt configuration files that point to the repositories created on the distribution server with packages created from the current source repo commit. There is one for each dist that is packaged for (e.g. lucid, squeeze, etc). Files are created in pkg/repo_configs/deb and are named pl-$project-$sha.list, and can be placed in /etc/apt/sources.list.d to enable clients to install these packages.



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
69
70
71
# File 'lib/packaging/deb/repo.rb', line 31

def generate_repo_configs(source = "repos", target = "repo_configs")
  # We use wget to obtain a directory listing of what are presumably our deb repos
  #
  wget = Pkg::Util::Tool.check_tool("wget")

  # This is the standard path to all debian build artifact repositories on
  # the distribution server for this commit
  #
  repo_base = "#{base_url}/#{source}/apt/"

  # First test if the directory even exists
  #
  begin
    stdout, = Pkg::Util::Execution.capture3("#{wget} --spider -r -l 1 --no-parent #{repo_base} 2>&1")
  rescue RuntimeError
    warn "No debian repos available for #{Pkg::Config.project} at #{Pkg::Config.ref}."
    return
  end

  # We want to exclude index and robots files and only include the http: prefixed elements
  repo_urls = stdout.split.uniq.reject { |x| x =~ /\?|index|robots/ }.select { |x| x =~ /http:/ }.map { |x| x.chomp('/') }


  # Create apt sources.list files that can be added to hosts for installing
  # these packages. We use the list of distributions to create a config
  # file for every distribution.
  #
  FileUtils.mkdir_p(File.join("pkg", target, "deb"))
  repo_urls.each do |url|
    # We want to skip the base_url, which wget returns as one of the results
    next if "#{url}/" == repo_base
    platform_tag = Pkg::Paths.tag_from_artifact_path(url)
    platform, version, = Pkg::Platforms.parse_platform_tag(platform_tag)
    codename = Pkg::Platforms.codename_for_platform_version(platform, version)
    repoconfig = ["# Packages for #{Pkg::Config.project} built from ref #{Pkg::Config.ref}",
                  "deb #{url} #{codename} #{reprepro_repo_name}"]
    config = File.join("pkg", target, "deb", "pl-#{Pkg::Config.project}-#{Pkg::Config.ref}-#{codename}.list")
    File.open(config, 'w') { |f| f.puts repoconfig }
  end
  puts "Wrote apt repo configs for #{Pkg::Config.project} at #{Pkg::Config.ref} to pkg/#{target}/deb."
end

.repo_creation_command(repo_directory, artifact_paths) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/packaging/deb/repo.rb', line 83

def repo_creation_command(repo_directory, artifact_paths)
  cmd = "[ -d #{repo_directory} ] || exit 1 ; "
  cmd << "pushd #{repo_directory} > /dev/null && "
  cmd << 'echo "Checking for running repo creation. Will wait if detected." && '
  cmd << 'while [ -f .lock ] ; do sleep 1 ; echo -n "." ; done && '
  cmd << 'echo "Setting lock" && '
  cmd << 'touch .lock && '

  # Make the conf directory and write out our configuration file
  cmd << 'rm -rf apt && mkdir -p apt ; pushd apt > /dev/null && '

  artifact_paths.each do |path|
    platform_tag = Pkg::Paths.tag_from_artifact_path(path)
    platform, version, = Pkg::Platforms.parse_platform_tag(platform_tag)
    codename = Pkg::Platforms.codename_for_platform_version(platform, version)
    arches = Pkg::Platforms.arches_for_codename(codename)

    cmd << "mkdir -p #{codename}/conf && "
    cmd << "pushd #{codename} ; "
    cmd << %( [ -e 'conf/distributions' ] || echo "
Origin: Puppet Labs
Label: Puppet Labs
Codename: #{codename}
Architectures: #{(DEBIAN_PACKAGING_ARCHES + arches).uniq.join(' ')}
Components: #{reprepro_repo_name}
Description: Apt repository for acceptance testing" >> conf/distributions ; )

    cmd << 'reprepro=$(which reprepro) && '
    cmd << "$reprepro includedeb #{codename} ../../#{path}/*.deb && "
    cmd << 'popd > /dev/null ; '
  end
  cmd << 'popd > /dev/null ; popd > /dev/null '
  cmd
end

.repo_deployment_command(origin_path, destination_path, destination, dryrun = false) ⇒ String

Deprecated.

this command will die a painful death when we are able to sit down with Operations and refactor our distribution infra. For now, it’s extremely debian specific, which is why it lives here.

  • Ryan McKern 11/2015

Returns an rsync command that can be executed on a remote host to copy local content from that host to a remote node.

Parameters:

  • origin_path (String)

    path for Deb repos on local filesystem

  • destination_path (String)

    path for Deb repos on remote filesystem

  • destination (String)

    remote host to send rsynced content to. If nil will copy locally

  • dryrun (Boolean) (defaults to: false)

    whether or not to use ‘–dry-run’

Returns:

  • (String)

    an rsync command that can be executed on a remote host to copy local content from that host to a remote node.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/packaging/deb/repo.rb', line 198

def repo_deployment_command(origin_path, destination_path, destination, dryrun = false)
  path = Pathname.new(origin_path)
  dest_path = Pathname.new(destination_path)

  # You may think "rsync doesn't actually remove the sticky bit, let's
  # remove the Dugo-s from the chmod". However, that will make your rsyncs
  # fail due to permission errors.
  options = %w[
    rsync
    --itemize-changes
    --hard-links
    --copy-links
    --omit-dir-times
    --progress
    --archive
    --update
    --verbose
    --super
    --delay-updates
    --omit-dir-times
    --no-perms
    --no-owner
    --no-group
    --exclude='dists/*-*'
    --exclude='pool/*-*'
  ]

  options << '--dry-run' if dryrun
  options << path
  if destination.nil?
    options << dest_path.parent.to_s
  else
    options << "#{destination}:#{dest_path.parent}"
  end
  options.join("\s")
end

.reprepro_repo_nameObject



12
13
14
15
16
17
18
# File 'lib/packaging/deb/repo.rb', line 12

def reprepro_repo_name
  if Pkg::Config.apt_repo_name
    Pkg::Config.apt_repo_name
  else
    Pkg::Paths.repo_name.empty? ? 'main' : Pkg::Paths.repo_name
  end
end

.retrieve_repo_configs(target = "repo_configs") ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/packaging/deb/repo.rb', line 73

def retrieve_repo_configs(target = "repo_configs")
  wget = Pkg::Util::Tool.check_tool("wget")
  FileUtils.mkdir_p("pkg/#{target}")
  config_url = "#{base_url}/#{target}/deb/"
  stdout, = Pkg::Util::Execution.capture3("#{wget} -r -np -nH --cut-dirs 3 -P pkg/#{target} --reject 'index*' #{config_url}")
  stdout
rescue StandardError => e
  fail "Couldn't retrieve deb apt repo configs.\n#{e}"
end

.ship_repo_configs(target = "repo_configs") ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/packaging/deb/repo.rb', line 139

def ship_repo_configs(target = "repo_configs")
  if !File.exist?("pkg/#{target}/deb") || Pkg::Util::File.empty_dir?("pkg/#{target}/deb")
    warn "No repo configs have been generated! Try pl:deb_repo_configs."
    return
  end

  Pkg::Util::RakeUtils.invoke_task("pl:fetch")
  repo_dir = "#{Pkg::Config.jenkins_repo_path}/#{Pkg::Config.project}/#{Pkg::Config.ref}/#{target}/deb"
  Pkg::Util::Net.remote_execute(Pkg::Config.distribution_server, "mkdir -p #{repo_dir}")
  Pkg::Util::Execution.retry_on_fail(:times => 3) do
    Pkg::Util::Net.rsync_to("pkg/#{target}/deb/", Pkg::Config.distribution_server, repo_dir)
  end
end

.sign_repos(target = "repos", message = "Signed apt repository") ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/packaging/deb/repo.rb', line 153

def sign_repos(target = "repos", message = "Signed apt repository")
  reprepro = Pkg::Util::Tool.check_tool('reprepro')
  Pkg::Util::Gpg.load_keychain if Pkg::Util::Tool.find_tool('keychain')

  dists = Pkg::Util::File.directories("#{target}/apt")
  supported_codenames = Pkg::Platforms.codenames

  unless dists
    warn "No repos found to sign. Maybe you didn't build any debs, or the repo creation failed?"
    return
  end

  dists.each do |dist|
    next unless supported_codenames.include?(dist)
    arches = Pkg::Platforms.arches_for_codename(dist)
    Dir.chdir("#{target}/apt/#{dist}") do
      File.open("conf/distributions", "w") do |f|
        f.puts "Origin: Puppet Labs
Label: Puppet Labs
Codename: #{dist}
Architectures: #{(DEBIAN_PACKAGING_ARCHES + arches).uniq.join(' ')}
Components: #{reprepro_repo_name}
Description: #{message} for #{dist}
SignWith: #{Pkg::Config.gpg_key}"
      end

      stdout, = Pkg::Util::Execution.capture3("#{reprepro} -vvv --confdir ./conf --dbdir ./db --basedir ./ export")
      stdout
    end
  end
end