📜 ⬆️ ⬇️

Windows Service Auto Update for Poor AWS

One day a guy came to Edison Software . Looking at the bill, he squinted and exclaimed: “Why is it so expensive ?! I'm poor! ” It has become a catch phrase and label for a whole class of projects. So, we needed to implement an automatic update for the Windows service, while meeting the following conditions.




Choosing a service to store packages


Amazon Web Services (AWS) was chosen as the service for storing service packs. The main reasons for his choice were:

The choice of mechanism


The following libraries for autoupdate were reviewed:

But they all did not satisfy our needs.
AutoUpdate.Net required user interaction when updating. And the library itself has not been supported for a long time.
Squirrel.Windows had all the necessary functions, however, it found a bug due to which the application was not installed at the system level on Win7 x64.
ClickOnce cannot install an application at the system level. Since we needed to install the service, this solution also had to be abandoned.

Update mechanism



')
  1. During the installation of the software, the installer registers in the Windows scheduler the launch of the auto-update program on a schedule.
  2. Run the auto-update program scheduler.
  3. Check for a new installation package on AWS
  4. Getting a new installation package.
  5. Stop the service.
  6. Running the new package in silent mode, and completing the updater process.

The new installation package performs the following actions.
  1. Uninstall the previous version of the software from the system.
  2. Install new software version (install). Along with the new software version, the update program is also updated.
  3. Start the updated service.

The MSI installation package is created using WIX (or another utility) and is straightforward. However, the simplicity of the update process has disadvantages:

These drawbacks are not significant. In normal operation, the installation always occurs without error. If the update did not occur, the user can always manually reinstall the software. The size of the installation package is small, updates are also not frequent, so the presence of files in the Windows temporary directory does not cause problems.

Downloading AWS Packages


Installation packages will be stored on AWS in this form.

string BucketKeyName = "general/{0}/Setup.msi"; //  {0} –    

To get the latest version number, which is stored on the AWS service, the following function is implemented (AwsKeyId, AwsSecretKey and AwsRegion are available in your account after registering on the service).

 private static Version GetLatestVersion() { using (var client = new AmazonS3Client(AwsKeyId, AwsSecretKey, AwsRegion)) { var listRequest = new ListObjectsRequest() { BucketName = BucketName, Prefix = "global/", Delimiter = "/", }; var response = client.ListObjects(listRequest); var versions = response.CommonPrefixes.Select(prefix => { try { var versionString = prefix.Replace(listRequest.Prefix, "").Replace("/", ""); return new Version(versionString); } catch { return new Version(); } }); return versions.Max(); } } 

To download the installation package, use the following function.

 private static FileInfo DownloadInstallerFor(Version version) { var request = new GetObjectRequest { BucketName = BucketName, Key = string.Format("general/{0}/Setup.msi", version.ToString()), }; string dest = Path.Combine(Path.GetTempPath(), request.Key); using (var client = new AmazonS3Client(AwsKeyId, AwsSecretKey, AwsRegion)) { using (var response = client.GetObject(request)) { if (!File.Exists(dest)) { response.WriteResponseStreamToFile(dest); } } } return new FileInfo(dest); } 

The main function of the auto-update program.

 static void Main(string[] args) { var currentFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); var serviceFullFileName = Path.Combine(currentFolder, ServiceFileName); Console.WriteLine(serviceFullFileName); // 1.     var serviceFileInfo = FileVersionInfo.GetVersionInfo(serviceFullFileName); var currentVersion = new Version(String.Format("{0}.{1}.{2}", serviceFileInfo.ProductMajorPart, serviceFileInfo.ProductMinorPart, serviceFileInfo.ProductBuildPart) ?? "0.0.0"); Console.WriteLine(currentVersion); // 2.     AWS var latestVersion = GetLatestVersion(); Console.WriteLine(latestVersion); if (latestVersion<=currentVersion) return; // 3.     AWS var fileInfo = DownloadInstallerFor(latestVersion); if (fileInfo.Exists) { // 4.  ,       StopService(); var process = new Process(); process.StartInfo.FileName = fileInfo.FullName; process.StartInfo.Arguments = "/quiet"; process.Start(); } } 

The presented auto-update mechanism is probably not the only one, but it has worked well for several months of excellent work. In this case, the cost of its creation were minimal.



More projects:
How to create software for a microtomograph for 5233 man-hours
SDK for the introduction of support for e-books in FB2 format
Control access to electronic documents. From DefView to Vivaldi
Integrating two video surveillance systems: Axxon Next and SureView
Read more about the development of x-ray tomograph software
Sphere: how to monitor billions of kilowatt-hours
Developing a simple plugin for JIRA to work with a database
To help DevOps: a firmware builder for network devices on Debian for 1008 hours
Windows Service Auto Update for Poor AWS

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


All Articles