📜 ⬆️ ⬇️

Automation of updating OpenVZ templates on Proxmox

Once there was such a need to update versions of OpenVZ templates loaded into Proxmox cache.

The task is simple and trivial. All you need is to download the new template and replace it with the old one. But globally, I saw a broader task, namely, periodically checking for updates and downloading new versions of templates as necessary. That is, constantly keep them up to date.

The desire is quite natural and understandable. And it would be foolish to perform these routine and simple operations every time manually, and according to this, the solution of the question suggested itself: to make a script that will do all this while the admin will rest and only occasionally check whether update.
')
So, there is a desire and it's time to start its implementation. First, let's decide what exactly our script should do.

Formulation of the problem


1. From time to time, at certain intervals, the script should run.
2. Connect to the server on which the official updated OpenVZ templates are located.
3. Compare templates that are on the server with those that are loaded into the cache.
4. Download the new template (if it exists), and replace it with the old one if the version on the server differs from the local one.
5. Maintain a log of what is loaded and what is not, and when it is loaded (updated).

That's all.

Implementation


For writing the script, I chose an old and reliable Perl, because of my old friendship with it, as well as because for this language a huge number of ready-made modules for all occasions have been written to perform system tasks and not only.

We skip the first point of the task, it will be written about it at the very end, when the script is ready to work. Moreover, this is a task not related to the script itself and will be performed by third-party tools, namely, we will place all responsibility for the timely launch of our script on Cron. This is his task and it is his duty to plan and launch everything. We will move on and return to the issue of planning later.

We will use the official openvz.org FTP server as a source of templates, and to connect to it using Perl, we will use the standard Net :: FTP module.

The following is a description of the script itself with explanations.

First we connect the modules.

#!/usr/bin/perl -w use strict; use Fcntl qw(:DEFAULT :flock); use Net::FTP; 

Now we will declare the variables we need, with which the script will work. In essence, these are constants that will not change during the execution of the script.

 #   OpenVZ   Proxmox my $vzdir = "/var/lib/vz/template/cache"; #   FTP ,      my $urldir = "ftp://ftp.openvz.org/template/precreated/"; #      log-     my $logdir ="/var/log/"; #   log-,   my $logfile = "ovzupdate.log"; 

As you can guess from the name of the log file, the script itself is called ovzupdate. The .pl extension is optional; it is enough that the first line of the script specifies an interpreter that will process this file.

So, let's continue ...

 #  log-   open(STDOUT, '>>',$logdir.$logfile) or die "Can't open file '$logfile' $!"; #   :)    ,     . print "\n=========================\n".getdate()."\n=========================\n"; print gettime()." * Starting OpenVZ templates update\n"; 

And finally, we got to the real work and came to what the script itself was created for. Connect to the openvz.org FTP server.

 #   ftp- my $server_name = 'ftp.openvz.org'; my $ftp_username = 'anonymous'; my $ftp_password = ''; my $ftp_source_dir = '/template/precreated'; my $ftp = Net::FTP->new($server_name, Debug => 1); $ftp->login($ftp_username,$ftp_password); $ftp->cwd($ftp_source_dir); $ftp->binary(); 

Now we will declare a variable in which the list (array) of local template names will be stored and open a local directory (Proxmox cache) in which already loaded templates are stored to check the relevance of the files.

 my $file; opendir(DIR, $vzdir) or die "Can't open $vzdir: $!"; 

And now we actually register the file comparison cycle itself. I must say that we will not update and download all files from FTP indiscriminately, but we will only select those that are already in our local cache and compare them in size with the files on the ftp server. If the size of the file in the local cache differs from the size of the file with the same name on the remote server, the file in the cache will be deleted, and a new file in FTP will be uploaded in its place.

 #         while( defined ($file = readdir (DIR)) ) { #          if ($file ne "." and $file ne "..") { #     my $local_file_size = -s $vzdir."/".$file; #       ,    ftp- my $remote_file_size = $ftp->size($file); #      log      #  ,     , #        print gettime()."  : $file -> ",$remote_file_size,"\n"; print gettime()."  : $file -> ",$local_file_size,"\n"; #     if ($local_file_size ne $remote_file_size) { #    ,    unlink $vzdir."/".$file; #       system ("wget -P ".$vzdir." ".$urldir.$file); #   log   ,    print gettime()." +    ".$file."\n"; } else { # ,   log     print gettime()."  ".$file." \n" } } } 

Close the directory, ftp connection and standard output stream.

 closedir(DIR); $ftp->quit; print gettime()." *   \n"; close STDOUT; 

Well, almost everything. The script itself contains three additional functions that perform only a supporting role and are not relevant to the essence of the question. The finished script can be viewed here .

Completion


And now, as promised, let us return to item number one. The script can be put on the Proxmox server at any convenient place. And it remains only to register it in the crontab, so that it will be launched for execution after the required period of time. Choose the frequency yourself, but I think that there is no need to run it more than once a week.

To add an update task, go to the crontab task edit mode.

crontab -e


And last we add a line that will execute our update script at a certain time and day. For example, the line below states that the script will run every Thursday at three in the morning.

 0 3 * * 4 /root/bin/ovzupdate 2>/dev/null 

2> / dev / null - redirects the output of the STDERR stream to the “black hole” without saving anywhere, but this is in person’s discretion, you can also save it to the log.

We save the changes we have made and that's it, the automatic update is configured. Now we only need to check the log /var/log/ovzupdate.log from time to time, to know whether or not the update was. And when this happens, we will see something like this:

 ========================= 12-03-2015 ========================= 03:00:01 *   OpenVZ templates 03:00:03  : debian-7.0-x86_64.tar.gz -> 235043942 03:00:03  : debian-7.0-x86_64.tar.gz -> 235004350 03:01:52 +    debian-7.0-x86_64.tar.gz 03:01:52  : ubuntu-12.04-x86_64.tar.gz -> 131011759 03:01:52  : ubuntu-12.04-x86_64.tar.gz -> 130987444 03:02:48 +    ubuntu-12.04-x86_64.tar.gz 03:02:48  : scientific-6-x86_64.tar.gz -> 221101244 03:02:48  : scientific-6-x86_64.tar.gz -> 219898164 03:03:45 +    scientific-6-x86_64.tar.gz 03:03:45  : centos-7-x86_64.tar.gz -> 211178690 03:03:45  : centos-7-x86_64.tar.gz -> 211139455 ... 03:16:31 *    

Have a nice update!

Source: https://habr.com/ru/post/263269/


All Articles