Installing Software and Updates

16 min read

In this article by Fuat Ulugay, author of the book Learning Puppet for Windows Server, we will learn how to install, update, and uninstall a software using Chocolatey. We will also automate these processes as much as possible. You will learn the following topics:

  • What is Chocolatey
  • How to install and update a software with Chocolatey
  • How to use Chocolatey and Puppet together to install/update the mostly used softwares
  • How to update Puppet agents
  • How to uninstall a software using Chocolatey

(For more resources related to this topic, see here.)

What is Chocolatey?

Chocolatey is a package manager for Windows. There are commands for Linux such as apt-get and yum for package management. They are very easy to use. Whenever you need to install something, you just write apt-get install packagename or yum install packagename. Here, the idea is to have a similar functionality in Windows. You can see more details about Chocolatey at https://chocolatey.org.

After learning what Chocolatey is, we will install it manually and install some software using this. Later, we will see how to use Chocolatey with Puppet.

Installing Chocolatey

The installation of Chocolatey is very simple.

Open Command Prompt with administrator rights and copy and paste the following command:

C:> @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%chocolateybin

The output of this installation command is shown in the following screenshot. As you can see, the installation is very easy. After installing Chocolatey, just close Command Prompt and open a new one.

Now, write the following command to see if Chocolatey is installed and it is giving the output:3

C:> choco --version

Installing software with Chocolatey

Now, let’s try to install a software with Chocolatey. To do this, we need to know the package name. For example, assume that we want to install Notepad++. Let’s check this out at https://chocolatey.org/. Searching for notepad brings the details as shown in the following screenshot:

As you can see, there are two results for the same version. The package ending with .install is for portable installations. We will use the regular one. We can see how to install Notepad++ in the following screenshot. The command is as follows:

C:> choco install notepadplusplus

Uninstalling a software with Chocolatey

Chocolatey, also, has the option to uninstall the installed packages. However, there are some exceptions such as:

  • The software to be uninstalled must be installed using Chocolatey
  • The software choco package must have its uninstall script

As you can see, the uninstall part may not work properly. Let’s check the details for Notepad++. On the Web, check the details for the package. In the Files section, there must be an uninstall script, otherwise, the uninstall will not work correctly. It means that when you want to uninstall it, you need to do it manually:

Now, let’s check out another software that we can install and uninstall. This time we will check out 7-Zip. Here are the details of 7-Zip:

As you can see in the previous screenshot, this has the uninstall file. Now, let’s try to install and uninstall.

Normally, all the packages should also have an uninstall package. However, never assume this and check whether it can be uninstalled correctly. When checked, you will see that most of the packages do not have an uninstall option. So the uninstall functionality of Chocolatey is not dependable at the moment. Another important point is that choco uninstall will not give errors, even if it does not uninstall the package.

Using Chocolatey to install a software

After learning Chocolatey and its limits, we will continue with using Chocolatey with Puppet. Using both of them together will be a great plus for us and all the installation process will be much easier. When we manage installations with Chocolatey in Puppet, we will not need to find the installation package, its version, and how to run it silently. The installation will be completed with a very little effort.

To use Chocolatey with Puppet, there is a module from Puppet Forge that we need to install. Go to Puppet Forge website and search for Chocolatey. You can see the module in the following screenshot. We will install the chocolatey/chocolatey module:

To install the module, go to Puppet Master, open a terminal window, and run the following command:

# sudo puppet module install chocolatey-chocolatey

Now, it is time to install a software using Chocolatey. For this purpose, we need to write a module. A sample manifest is as follows:

package { 'notepadplusplus':
  ensure            => installed|latest|'1.0.0'|absent,
  provider          => 'chocolatey',
  install_options   => ['-pre','-params','"','param1','param2','"'],
  uninstall_options => ['-r'],
  source            => 'https://myfeed.example.com/api/v2',
}

Now, let’s check the details step by step:

  • package { ‘notepadplusplus’: Here, we define the package name that is listed in https://chocolatey.org/.
  • ensure: Here, you have different options. installed makes sure that it is installed. latest updates the software whenever there is a new version at https://chocolatey.org/. If you give the version number, such as ‘1.0.0’, it will install this version. absent uninstalls the package. Do not trust the uninstall functionality as we have mentioned previously!
  • provider => ‘chocolatey’: Here, we change the package provider so that the installation is handled by Chocolatey.
  • install_options: Normally, the Chocolatey packages are installed silently. However, you also have the option to use different installation options.
  • uninstall_options: Here you can put different options, for the uninstallation.
  • source: We will not use this one. However, if you have different sources or your own Chocolatey server, you can reference it here.

The following is a more simple form that we will normally use. As you can see, it is very simple to install software using Chocolatey:

package { 'notepadplusplus':
  ensure   => installed,
  provider => 'chocolatey',
}

As you will remember, we used Workrave as an example before. Now, let’s write a new module that uses Chocolatey, and then compare it with our default first module to see the differences. We will create a new module named chocoworkrave

Our old module structure, installworkrave, is shown in the following screenshot:

Here, we can already see that there is less effort required. We do not need to find and upload the installation file.

As you can see, more effort is required with the default provider. You need to find the exact name of the installation. You need to download the installation and upload it to server. You need to send the installation to the host. You need to give the installation options. When you have Chocolatey as provider, you just need to know the package name.

The workrave package for Chocolatey has problems, and is not working properly. So, if you see that your installation is not working properly, do not spend time with it.

Installing Firefox as an example

First, search Chocolatey for Firefox and find the relevant package.

In our example, as you can see in the previous screenshot, the name of the package is firefox. Now, let’s write our module. We can use chocofirefox as a name for our module.

Installing Chocolatey using Puppet

It may occur to you that installation of Chocolatey to each host may be a burden. To install Chocolatey automatically on each host, you can use a module named ceritsc/chocolatey_sw in Puppet Forge. After the installation of this module, if you assign this module to any of your hosts or host groups, Chocolatey will be installed on them.

Using Chocolatey to update a software

One of the challenges for IT is to keep the client software up to date. It is easy for Windows updates that can be handled automatically. However, when it comes to third-party softwares, the updates may become a burden. Next, we will see how Puppet and Chocolatey deal with updates.

As an example, we will use the Java Runtime installation. We will first install an older version and see whether it is updated correctly. Here is the package that we will use:

When we scroll down, we will also see some older versions. We will first install the older version, 7.0.75. We will do it manually from Command Prompt:

Clicking on the older version will give us the details about installing it via Chocolatey.

To update Java, we will create a new module named chocojre.

Now, let’s test this and see whether the update works as expected. First, let’s be sure that the correct version was installed from Control PanelProgramsPrograms and Features in our Windows host.

Re-checking the programs in Windows, we see that the new Java version is installed. However, there is a little problem; the old version is also there. For most software, the old version will be no more; however, this is not the case for Java.

Using Puppet and Chocolatey to update mostly used software

We learned how to use Chocolatey to install and update a software. Now, the next step is to remove some of the burden from our shoulders. I know that there are always problems with some of the updates for certain softwares. There are always new versions and new workload to fulfill.

Here is a list of softwares that we mostly need and are frequently updated. You can, of course, create your own list:

  • The Java Runtime environment
  • Adobe Reader
  • The Flash Player plugin
  • The Flash Player plugin activex
  • Firefox
  • Chrome
  • iTunes
  • 7-Zip

After creating the list of softwares, our next step is to check https://chocolatey.org/ and find their package names:

  • The Java Runtime environment: javaruntime
  • Adobe Reader: adobereader
  • The Flash Player plugin: flashplayerplugin
  • The Flash Player plugin activex: flashplayeractivex
  • Firefox: firefox
  • Chrome: google-chrome-x64
  • iTunes: itunes
  • 7-Zip: 7zip

After learning each of the package’s name, you can use one module and put all of them in it, or you can create one module for each of them. It will be better if we stick to the second option. Sometimes, there are cases where you should not update a software. For example, your document management software may be using an older version of Java. Upgrading it to the newer version may just cause problems for the users. In this case, you may have to use different update policies for different softwares. Keeping the modules separate will help you to easily differentiate.

The example manifest for 7-Zip is as follows:

# install 7zip using chocolatey
class choco7zip {
  package { '7zip':
    ensure   => latest,
    provider => 'chocolatey',
  }
}

Here, by just changing the class name and package name, you can create many different modules to update different kinds of software. When you are done with these modules, you will never have to deal with the Java, Adobe, or Flash updates. This will increase your end user satisfaction, as they will not see the popups of the software updates, which they cannot complete because of missing admin rights. Also, it will help your security and you will have your updates implemented sooner. The on-time updates will patch the security problems and vulnerabilities.

If you want to keep the updates in one class, you can use the following sample class. In this class, you only need to add additional package names:

# update software using chocolatey
class choco7zip {

$packages = [ "javaruntime", "adobereader", " flashplayerplugin" ]

package { $packages:
    ensure   => latest,
    provider => 'chocolatey',
  }
}

One more detail you need to know is that you can also use the latest option for installation. So instead of writing ensure => installed, ensure => latest will help you to install the latest version and keep it updated.

Updating the Puppet agents

One of the challenging tasks we may have is to update of the Puppet agents. Before updating the agents, ensure that the agent version is never higher than the server version. Thus, we should first start updating our server.

Updating the server

Before updating your server, ensure that you have a backup. The easiest method to update is to write the following commands. These two commands will update your Linux server and if there are any updates related to Foreman and Puppet, they will also be implemented:

$ sudo apt-get update
$ sudo apt-get upgrade -y

The following screenshot shows that there are many updates for the server:

After some major updates, the server may require a restart. In this case, write the following command to restart your server:

$ sudo reboot

To check the Puppet Server version, write the following command:

$ puppet --version

Updating the agents with Chocolatey

Trying to find online computers and sending the updates again and again may become a burden for you. It will be much easier if Puppet also handles its agent updates.

Let’s check the version in one of our hosts.

Now, we will upgrade it to version 3.8.1. To do this, search for puppet at https://chocolatey.org/.

This is already the version we require. While testing this package, the default installation causes a restart. We do not want to disturb the users with a restart. So, we will overwrite its parameters. To do this, we need to first check the installation parameters of the puppet installation file. You can download the installation from https://downloads.puppetlabs.com/windows/. After the download, write the package name followed by /? in Command Prompt. This will show you the installation option similar to the one shown in the following screenshot:

In the previous screenshot, you can see that we need two options, /norestart and /quiet, to install the agent silently and prevent a reboot. After learning these details, we are ready to continue with the manifest details.

We will create a module named puppetagent.

In these details, we can see two more installation options:

  • -override: This option is used to override any options that were defined
  • -installArgs: This is used to indicate that there are new installation arguments

Always use the version number to prevent problems. This will ensure that you are not having a version newer than your server and that you have full control over the Puppet agent versions.

After completing all the details, it is test time again. Let’s see what happens when we do a test run:

The previous screenshot shows that it first gives an error. Although, we have put the NORESTART option, it tries to execute the shutdown /a command, which causes an error code with 1116. However, when we check the version, we can see that the update is successful. Finally, the next run gives no error as the installation is successful.

Installing Puppet 3.7.5 gives no error, However, the 3.8.1 version gives an error, which is not important. This is again the case when we may see in new open-source technologies. Putting everything together, it will be best to test any module with Chocolatey before going live.

Another problem is that sometimes, the update may not correctly run and you may need to correct it manually on the host. If your Puppet agent does not run correctly anymore, use the following command in Command Prompt to fix the agent: choco install puppet -version 3.8.1 –force.

Uninstalling a software

After learning the different ways of installing a software, now we will learn how to uninstall a software. At times, you may need to remove some softwares from each client. Instead of dealing with them one by one, you can use Puppet and automate the removal process. For this purpose, it is fine to use the package resource of Puppet.

As an example, we will uninstall the older versions of Java.

Here, we will uninstall two packages: Java 7 Update 75 and Java 7 Update 75 (64-bit). We have already created a module for Java Update: chocojre. Now, let’s modify it so that it does not only install the latest version, but also uninstalls the older one. We will, also, require the latest version before uninstalling the previous version. There is no easy way to remove all older versions. So, we need to specify each of them manually. For uninstalling, the only change we need to add is ensure => absent.

At times, there are leftovers from the upgraded software; it is a good idea to remove the older versions. It will be much easier to use one module to update the same software and uninstall its older versions.

Uninstalling an older version of a software that cannot be differentiated by its name

Assume that we have a case where there are two versions of a software installed. We want to install the older version. However, we cannot differentiate them by their names because they are identical. We had this situation after upgrading Puppet. As you can see in the following screenshot, there are two different Puppet agent versions installed with the same name:

Here, if we use ensure => absent, both the packages will be removed. In this case, the Puppet connection will be lost. Here, we will need a slightly advanced approach. Now, we know that the Puppet agent installation is an MSI package. Checking the registry details, we can find its uninstall string. We will run regedit.exe and go to the HKLMSoftwareMicrosoftWindowsCurrentVersionUninstall folder. Here, we need to find Puppet version 3.7.4.

Here is the full uninstall string:

MsiExec.exe /X{9241D505-58E0-47CF-97A1-5E195F02FA94}

We will also add /Q so it uninstalls quietly. The new command becomes:

MsiExec.exe /Q /X{9241D505-58E0-47CF-97A1-5E195F02FA94}

When you run the uninstallation of older Puppet agents, it also breaks the newer ones. So, this is just an example to show you how to uninstall older packages. Normally, installation packages automatically remove the older version. However, it is not always the case as we see in Puppet agent and Java. As Puppet older uninstallation file breaks the newer one, never use this code in production.

We will add our uninstallation code to our puppetagent module. Also, we are inserting a condition that the uninstallation will only work when the Puppet agent version is 3.8.1. This will prevent the uninstallation from working, when the running version is an older one.

Summary

In this article, we learned how to install a software using Chocolatey. Later, we used both Puppet and Chocolatey in tandem to make our installations and updates much easier. We also checked out some of the softwares that are most used and how to always keep them updated. Finally, we learned how to update Puppet agents and uninstall a software.

If you want to learn more about Puppet, there are many more books about it. The Puppet documentation is, also, one of the places you may check out from time to time. If you have problems and need to ask questions, there are different options available such as:

  • For Enterprise users, Puppet has commercial support at https://tickets.puppetlabs.com/secure/Dashboard.jspa
  • Google groups such as puppet-users and puppet-bugs
  • The #puppet IRC channel on freenode

Resources for Article:


Further resources on this subject:


Packt

Share
Published by
Packt

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago