Skip to content

Instantly share code, notes, and snippets.

@daveneeley
Created September 1, 2017 19:04
Show Gist options
  • Save daveneeley/4ae247cf006dcf9fc16a347f9242a7e8 to your computer and use it in GitHub Desktop.
Save daveneeley/4ae247cf006dcf9fc16a347f9242a7e8 to your computer and use it in GitHub Desktop.
Manage Salt Package Manager (spm) packages with Artifactory (POC)
##First download all existing packages to downloaded_spms.
##I'm doing this in a TeamCity build step.
##The packages themselves are in Artifactory.
if [ -d /srv/spm_build ]; then
rm -rf /srv/spm_build;
fi
spm create_repo downloaded_spms
spm build <formula name>
mv /srv/spm_build/*.spm downloaded_spms
#I believe this perl script can be excluded in newer salt versions because of https://github.com/saltstack/salt/pull/39434
./prepareLocalSPMPackages.pl
spm create_repo downloaded_spms
#now upload the contents of downloaded_spms to Artifactory
#!/usr/local/bin/perl
use File::Basename;
use Data::Dumper;
use List::MoreUtils qw(uniq);
sub mapPackagesForSort {
my $path = shift;
chomp($path);
my $spm = basename($path);
/(.+)-(\d+)-(\d+).spm/;
return [$path, $1, $2, $3];
}
#0: original path
#1: package name
#2: major version
#3: build number
#then sort on the substring prior to that index
#when it matches, sort on the numeric components
my @groups;
my @packages = map { push @groups, $_->[1]; $_->[0] }
sort { $a->[1] cmp $b->[1]
||
$a->[2] <=> $b->[2]
||
$a->[3] <=> $b->[3]
||
$a->[0] cmp $b->[0]
} map { mapPackagesForSort($_)
} `ls downloaded_spms/*.spm`;
#remove everything except the latest of each package
foreach my $group (uniq @groups) {
my $fileToKeep = (grep(/$group-\d/, @packages))[-1];
print "keeping $fileToKeep\n";
my @losers = grep(/$group-\d/ && !/$fileToKeep/, @packages);
foreach my $loser (@losers) {
unlink $loser or warn "Could not unlink $loser: $!";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment