Class: AptUpdateFreightCache

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

Overview

This is a wrapper for github.com/freight-team/freight, which creates APT repos from deb files. This triggers the ‘freight cache’ command which creates a proper APT repo (freight calls it a ‘cache’), including signing, from a tree of debian files (freight calls it a ‘library’).

In our case, we maintain a set of debian repos, based on puppet version and a repo-type ‘nightly’, ‘archive’ and ‘stable’.

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

Example usage:

apt-update-freight-cache --puppet-version=7

Constant Summary collapse

DOCUMENTATION =
<<~DOCOPT
  Generates a freight cache from a freight library.

  Usage:
      apt-update-freight-cache [--verbose] --puppet-version=VERSION
      apt-update-freight-cache --help
      apt-update-freight-cache --version

  Options:
  -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) ⇒ AptUpdateFreightCache

Returns a new instance of AptUpdateFreightCache.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/apt_update_freight_cache.rb', line 45

def initialize(argv)
  @log_level = Logger::INFO
  @user_options = parse_options(argv)
  @verbose = ''
  @verbose = '--verbose' if @user_options['--verbose']
  @builder_data_yaml = 'https://raw.githubusercontent.com/puppetlabs/build-data/release/builder_data.yaml'
  @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

#load_builder_data_yamlObject

Load details from the builder_data.yaml on github Keep only the bare necessities. Right now, that’s just the GPG key



90
91
92
93
94
# File 'lib/apt_update_freight_cache.rb', line 90

def load_builder_data_yaml
  yaml_content = URI.parse(@builder_data_yaml).open(&:read)
  yaml_data = YAML.safe_load(yaml_content)
  @gpg_key = yaml_data['gpg_key']
end

#parse_options(argv = nil) ⇒ Object



59
60
61
62
63
64
# File 'lib/apt_update_freight_cache.rb', line 59

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

#runObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/apt_update_freight_cache.rb', line 66

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

  freight_config_file = "/etc/freight.conf.d/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}'"
  freight_cache_command = %W[
    sudo --user=jenkins --set-home
    /usr/local/bin/freight-cache-wrapper #{@verbose}
    --conf=#{freight_config_file} --gpg-key=#{@gpg_key}
  ].join(' ')
  %x(#{freight_cache_command})
  fatal "#{tarball_extract_command} failed." unless $CHILD_STATUS.success?
end