Skip to content

Instantly share code, notes, and snippets.

@TheCry
Last active August 6, 2019 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheCry/69cb5563fec2070ae16a8f70d707abaf to your computer and use it in GitHub Desktop.
Save TheCry/69cb5563fec2070ae16a8f70d707abaf to your computer and use it in GitHub Desktop.
Perl script to create an offline update repository for VMware Virtual Appliances (V 5.50)
#!/usr/bin/perl -w
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
use LWP::UserAgent;
use File::Path qw(make_path remove_tree);
use Config::Simple;
# create a config file "config-va-updates.ini" in the same folder
# with the content:
# [va-updates-550]
# url="https://vapp-updates.vmware.com/vai-catalog/valm/vmw/8e70f769-fd50-4a7a-bee2-2c0d945e23b0/5.5.0.30800.latest"
my $cfgData;
my $cfgFile = 'config-va-updates.ini';
my $manifestDownloadUrl;
my $repoPath = '/var/lib/vin550';
my $manifestFolder = 'manifest';
my $packagePoolFolder = 'package-pool';
my @manifestFiles = ('manifest-latest.xml', 'manifest-latest.xml.sig', 'manifest-repo.xml');
#my @packagePoolFiles = ('rpm-manifest.json', 'patch-metadata-scripts.zip');
my ($manifestXml, $manifestData, $productRID, $version, $manifestDataHash, $downloadUrl, $download);
my $uid = getpwnam 'apache';
my $gid = getgrnam 'apache';
my $apacheSelinuxUser = 'httpd_sys_content_t';
my $DEBUG = 1;
$cfgData = new Config::Simple();
print "Read CFG file if exist: ".$cfgFile."\n\n" if $DEBUG;
$cfgData->read($cfgFile) or die Config::Simple->error();
$manifestDownloadUrl = $cfgData->param("va-updates-550.url");
if($manifestDownloadUrl eq '')
{
print "No Manifest download url definded in CFG file if exist: ".$cfgFile."\n\n" if $DEBUG;
exit;
}
print "Manifest download url: ".$manifestDownloadUrl."\n\n" if $DEBUG;
print "Remove existing repository if exist: ".$repoPath."\n\n" if $DEBUG;
remove_tree($repoPath) if (-d $repoPath);
print "Create manifest folder if not exist: ".$repoPath."/".$manifestFolder."\n" if $DEBUG;
make_path($repoPath.'/'.$manifestFolder, { verbose => 0, mode => 0700, owner => $uid, group => $gid }) if (!-d $repoPath.'/'.$manifestFolder);
print "Create package pool folder if not exist: ".$repoPath."/".$packagePoolFolder."\n\n" if $DEBUG;
make_path($repoPath.'/'.$packagePoolFolder, { verbose => 0, mode => 0700, owner => $uid, group => $gid }) if (!-d $repoPath.'/'.$packagePoolFolder);
$download = LWP::UserAgent->new;
$download->timeout(3600);
$download->env_proxy;
$download->show_progress("1");
print "Downloading Manifest File:\n" if $DEBUG;
foreach my $manifestFile (@manifestFiles)
{
$download->get($manifestDownloadUrl.'/'.$manifestFolder.'/'.$manifestFile, ":content_file" => $repoPath.'/'.$manifestFolder.'/'.$manifestFile);
}
print "\n" if $DEBUG;
if(-f $repoPath.'/'.$manifestFolder.'/manifest-latest.xml')
{
# read XML file
$manifestXml = new XML::Simple;
$manifestData = $manifestXml->XMLin($repoPath.'/'.$manifestFolder.'/manifest-latest.xml');
$productRID = $manifestData->{productRID};
$productRID =~ s/^\s+|\s+$//g;
$version = $manifestData->{version};
$version =~ s/^\s+|\s+$//g;
$downloadUrl = 'https://vapp-updates.vmware.com/vai-catalog/valm/vmw/'.$productRID.'/'.$version.'.latest';
print "Build package pool download url: ".$downloadUrl."\n\n" if $DEBUG;
} else {
print "File: ".$repoPath."/".$manifestFolder."/manifest-latest.xml not found.\n" if $DEBUG;
exit;
}
#print "Downloading Package Pool File:\n" if $DEBUG;
#foreach my $packagePoolFile (@packagePoolFiles)
#{
# $download->get($downloadUrl.'/'.$packagePoolFolder.'/'.$packagePoolFile, ":content_file" => $repoPath.'/'.$packagePoolFolder.'/'.$packagePoolFile);
#}
#print "\n" if $DEBUG;
#print Dumper($manifestData);
$manifestDataHash = $manifestData->{packageGroups}{group}{package};
print "Downloading repository Files:\n" if $DEBUG;
foreach my $packageKey ( keys %$manifestDataHash )
{
if($manifestDataHash->{$packageKey}{location} =~ 'package-pool')
{
$manifestDataHash->{$packageKey}{location} =~ s/\s+//gs;
$download->get($downloadUrl.'/'.$manifestDataHash->{$packageKey}{location}, ":content_file" => $repoPath.'/'.$manifestDataHash->{$packageKey}{location});
}
}
if (-d $repoPath)
{
print "\nSetting new permissions and owner: ".$repoPath."\n" if $DEBUG;
system("/bin/chown -R ".$uid.":".$gid." ".$repoPath);
system("/bin/chcon -R -t ".$apacheSelinuxUser." ".$repoPath);
}
if($manifestDownloadUrl ne $downloadUrl)
{
print "Find new download URL in online repository: ".$downloadUrl."\n" if $DEBUG;
$cfgData->param("va-updates-550.url", $downloadUrl);
print "Saving new url to CFG File: ".$cfgFile."\n" if $DEBUG;
$cfgData->write() or die $cfgData->error();
}
@TheCry
Copy link
Author

TheCry commented Aug 6, 2019

New version with seperate config file to update the download url if changed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment