Last active
August 6, 2019 08:05
-
-
Save TheCry/9c6f849a70092807cff80f51fc81fea5 to your computer and use it in GitHub Desktop.
Perl script to create an offline update repository for VMware Virtual Appliances (V 6.50)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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] | |
# url="https://vapp-updates.vmware.com/vai-catalog/valm/vmw/8d167796-34d5-4899-be0a-6daade4005a3/6.5.0.30000.latest" | |
my $cfgData; | |
my $cfgFile = 'config-va-updates.ini'; | |
my $manifestDownloadUrl; | |
my $repoPath = '/var/lib/vin'; | |
my $manifestFolder = 'manifest'; | |
my $packagePoolFolder = 'package-pool'; | |
my @manifestFiles = ('manifest-latest.xml', 'manifest-latest.xml.sign', '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.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}; | |
$version = $manifestData->{version}; | |
$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.url", $downloadUrl); | |
print "Saving new url to CFG File: ".$cfgFile."\n" if $DEBUG; | |
$cfgData->write() or die $cfgData->error(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
New version with seperate config file to update the download url if changed