Class: AptAddToFreightLibrary

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/apt_add_to_freight_library.rb

Overview

This is a wrapper for github.com/freight-team/freight, which creates APT repos from deb files.

In our case, we maintain a set of debian repos, based on puppet version and a APT component ‘nightly’ or ‘stable’ (we’ve also used ‘archive’ in the past; there’s some remnants of it left).

freight provides signing services, so this script needs to exist on a machine where GPG signing has been set up.

Typical usage would be for a remote process to call this script with a list of .deb URLS:

apt-add-to-freight-library –puppet-version=7 –component=nightly –codename=stretch

https://some-server.net/somepackage-a_0.19.2-1stretch_amd64.deb
https://some-server.net/somepackage-b_0.19.2-1stretch_amd64.deb

Alternately one could scp a bunch of .deb files to the machine this is installed on then call this script with all the deb files as arguments. For example:

apt-add-to-freight-library –puppet-version=7 –component=nightly –codename=buster

/tmp/xxtmpdir/somepackage-a_0.0.0-1buster_amd64.deb
/tmp/xxtmpdir/somepackage-b_1.2.3-5buster_amd64.deb
  (etc.)

Constant Summary collapse

DOCUMENTATION =
<<~DOCOPT
  Adds debian .deb files to a freight library.

  Usage:
      apt-add-to-freight-library [--verbose] --puppet-version=VERSION --component=COMPONENT --codename=DEBIAN_CODENAME <deb_file> ...
      apt-add-to-freight-library --help
      apt-add-to-freight-library --version

  Options:
  -c --component=COMPONENT       APT component (Valid: #{AptStageArtifacts::VALID_APT_COMPONENTS.join(', ')})
  -n --codename=DEBIAN_CODENAME  Debian codename to deliver to (Valid: #{AptStageArtifacts::VALID_CODENAMES.join(', ')})
  -p --puppet-version=VERSION    Puppet version (Valid: #{AptStageArtifacts::VALID_PUPPET_VERSIONS.join(', ')})

  -v --verbose  Be verbose
  -h --help     Show this help
  --version     Show version
DOCOPT

Instance Method Summary collapse

Methods included from Logging

#fatal, #logger, logger

Constructor Details

#initialize(argv) ⇒ AptAddToFreightLibrary

Returns a new instance of AptAddToFreightLibrary.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/apt_add_to_freight_library.rb', line 56

def initialize(argv)
  @log_level = Logger::INFO
  @user_options = parse_options(argv)
  @verbose = ''
  @verbose = '--verbose' if @user_options['--verbose']

  @script_name = File.basename($PROGRAM_NAME)
  @version_string = "#{@script_name} version #{AptStageArtifacts::VERSION}"
  if @user_options['--version'] # rubocop:disable Style/GuardClause
    warn @version_string
    exit 0
  end
end

Instance Method Details

#parse_options(argv = nil) ⇒ Object



70
71
72
73
74
75
# File 'lib/apt_add_to_freight_library.rb', line 70

def parse_options(argv = nil)
  Docopt.docopt(DOCUMENTATION, { argv: argv })
rescue Docopt::Exit => e
  warn e.message
  exit 1
end

#runObject



77
78
79
80
81
82
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
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/apt_add_to_freight_library.rb', line 77

def run
  logger.info @version_string
  @puppet_version = @user_options['--puppet-version']
  fatal "Invalid Puppet version '#{@puppet_version}'" unless
    AptStageArtifacts::VALID_PUPPET_VERSIONS.include? @puppet_version

  @apt_component = @user_options['--component']
  unless AptStageArtifacts::VALID_APT_COMPONENTS.include? @apt_component
    fatal "Invalid APT component '#{@apt_component}'"
  end

  @codename = @user_options['--codename']
  unless AptStageArtifacts::VALID_CODENAMES.include? @codename
    fatal "Invalid codename '#{@codename}'"
  end

  freight_config_file = File.join(
    AptStageArtifacts::FREIGHT_CONFIG_DIRECTORY,
    "puppet#{@puppet_version}.conf"
  )
  fatal "Cannot read '#{freight_config_file} on #{%x(hostname).chomp}." unless
    File.readable?(freight_config_file)

  logger.info "Using '#{freight_config_file}'"
  deb_artifacts = @user_options['<deb_file>']

  Dir.mktmpdir do |temporary_directory|
    deb_artifacts.each do |artifact_path|
      logger.info "Adding #{artifact_path} to puppet#{@puppet_version}/#{@apt_component}"
      deb_artifact_path = artifact_path
      if deb_artifact_path.start_with?('https://', 'http://')
        download_data = URI.parse(deb_artifact_path).open
        download_path = File.join(
          temporary_directory,
          download_data.base_uri.to_s.split('/').last
        )
        IO.copy_stream(download_data, download_path)
        deb_artifact_path = download_path
      end

      freight_add_command = %W[
        sudo --user=jenkins --set-home
        #{AptStageArtifacts::FREIGHT_COMMAND} add #{@verbose} --conf="#{freight_config_file}"
        #{deb_artifact_path} "apt/#{@codename}/#{@apt_component}"
      ].join(' ')

      %x(#{freight_add_command})
      fatal "#{freight_add_command} failed." unless $CHILD_STATUS.success?
    end
  end
end