
Today I will tell you about magic. And this magic can change the life of the Windows system administrator once and for all.
Nowadays there are fewer and fewer people who have not installed software in the Linux environment at least once. It's incredibly simple: to install midnight commander (mc), in the RH environment (RedHat Enterprise, CentOS, Fedora, etc.) we just need a couple of “magic” commands:
The package manager yum will make sure that the latest version of
mc is installed, as well as the dependencies of the package, if any. But what to do if Windows is at our disposal, and we want something like that? Right, go to Linux or read on!
')
Under my “guardianship” there is a heterogeneous network of Windows and Linux machines (easier to say, a zoo), and for about two years now I have been using Chocolatey to install software under Win *. Chocolatey (
chocolatey.org ,
github.com/chocolatey ) is a package management system, much like apt-get or yum, but only for Windows.
On Habré already touched the theme Chocolatey in the context of the developer, today I want to look at this wonderful tool from the point of view of the system administrator. Chocolatey works on the basis of NuGet technology (it is actively used by software developers under Windows), and the main feature of Chocolatey is that the packages most often
do not contain installation files (setup.msi, setup.exe, etc ...). It works as follows: the package contains a script installer for powershell, which downloads and installs the correct version of the installation file from the right place on the Internet, and you just have to enjoy the ease of installation.
Chocolatey installation
Before being able to use Chocolate Chocolate magic, we need to install its core. To do this, run the command line:
@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin
This command will download and run the main installation script for
chocolatey.org/install.ps1 , as well as set the required environment variables. Now that we have everything we need, let's test the package management system and install Nodepad ++. Just run the following command:
cinst notepadplusplus
What other packages are there and where do they come from?
Like NuGet, Chocolatey has an impressive list of packages, which is located in the repository, also
known as the package library . Here are just some of them:
Top 10 most popular packages on chocolatey.org- Git - 51191 downloads
- Notepad ++ - 37533 downloads
- 7Zip - 37802 downloads
- Google Chrome - 25960 downloads
- Java Runtime - 25699 downloads
- NodeJS - 25542 downloads
- Mozilla Firefox - 20747 downloads
- Adobe Flash Player - 20660 downloads
- VLC Player - 20419 downloads
- Ruby 2.0 - 19587 downloads
Packages are added every day, because anyone can add their pack on chocolatey.org, the main thing is that it
meets the requirements .
Requirements for publishing packages- Do not publish illegal programs . Programs that are illegal in most countries of the world are also banned from being hosted on Chocolatey.org. This also applies to programs that violate copyrights, pirated programs and "crack". Remember that this also applies to programs that are used for piracy.
- Do not pack the program in chocolatey for which you do not have distribution rights . Please specify the software distribution rules and do not violate them.
- Do not publish viruses or any other harmful programs .
- Publish only those programs that will be useful to others. If your package does not fall into this category - do not publish it.
- Do not publish spyware or adware. Programs that come with embedded adware or spyware or any other irrelevant programs are not allowed to be published. Usually, all irrelevant programs can be excluded from installation using installer keys. Examples of such programs are PDFCreator and CCleaner .
- Do not publish programs that are already published. Use the search on Chocolatey.org . If you want to improve an existing package, contact the person who supports the package or send a pull-request to its repository.
- Do not include other programs in your package if they already have their own package . If your package requires certain programs, the existing package should be included by you as a dependency.
- Split dependencies into multiple packages . Try to split the package into as many packages as possible. For example, the program comes with optional modules. Create additional packages for modules, instead of including them in the general package. This idea has long been used in Linux packages for the reason that it allows you to create lightweight packages and minimizes the chance of conflict.
How it works?
I would like to take a closer look at the contents of the Chocolatey packages using the example of
logstash , which I created specifically for deploying the logstash agent on a Windows server:
\logstash \tools chocolateyInstall.ps1 logstash.nuspec
Here you can see that there are only 2 files in the package: logstash.nuspec and chocolateyInstall.ps1.
logstash.nuspec - file that describes package meta-information <?xml version="1.0"?> <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> <metadata> <id>logstash</id> <version>1.2.1.2013101701</version> <title>logstash</title> <authors>kireevco</authors> <owners>http://chocolatey.org/profiles/kireevco</owners> <projectUrl>https://github.com/kireevco/chocolatey-packages</projectUrl> <copyright>http://logstash.net</copyright> <iconUrl>http://logstash.net/images/logstash.png</iconUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>Logstash is a tool for managing events and logs. You can use it to collect logs, parse them, and store them for later use (like, for searching). Speaking of searching, logstash comes with a web interface for searching and drilling into all of your logs. This package installs logstash flat jar as an agent service via nssm. All you need to do - is to configure your logstash.conf and start logstash service. Service is installed with these parameters: "java.exe -Xmx512M -jar logstash.jar agent --config logstash.conf --log logstash.log "</description> <summary>Logstash Agent package</summary> <tags>logstash, logging</tags> <dependencies> <dependency id="javaruntime" version="7.0.0" /> <dependency id="NSSM" version="2.16.0" /> <dependency id="Chocolatey" version="0.9.8.20" /> </dependencies> </metadata> </package>
In this file it will be interesting to parse the
dependencies section, in which we indicate that our package requires 3 other packages of certain versions, namely
javaruntime ,
NSSM (allows you to install our .jar file as a Windows service), as well as Chocolatey of a certain version. If any of the required packages is missing, or its version does not match the required one, the dependency system will resolve the situation and lead everything to the required form. It should be noted that
nuget is used to indicate versions.
Powershell script chocolateyInstall.ps1 # jar $url='https://download.elasticsearch.org/logstash/logstash/logstash-1.2.1-flatjar.jar' # cmd $cmdfile='c:/logstash/logstash.cmd' # $confile = 'c:/logstash/logstash.conf.sample' $dir='c:/logstash' if (!(Test-Path -path $dir)) {New-Item $dir -Type Directory} Get-ChocolateyWebFile 'logstash' 'c:/logstash/logstash.jar' $url $url $cmdcontent = @@ Set-Content $cmdfile $cmdcontent -Encoding ASCII # $confcontent = @@ Set-Content $confile $confcontent -Encoding ASCII if ($serviceinfo = Get-Service -ErrorAction SilentlyContinue) { if ($serviceinfo.status -ne 'Running') { if ($serviceinfo.status -eq 'Stopped') { echo echo sc.exe \\localhost delete nssm install C:\logstash\logstash.cmd } } else { echo sc.exe \\localhost stop echo sc.exe \\localhost delete echo nssm install C:\logstash\logstash.cmd } } else { # nssm echo nssm install C:\logstash\logstash.cmd }
Application:
Many admins probably ran to test the functionality - it’s right, because there’s nothing difficult to use with Chocolatey - this is what Chocolatey’s sweetness is about. Nevertheless, I would like to suggest several scenarios for using this package manager for Windows.
Cmd and Powershell scripts
We all use the simplest scripts in our work, and chocolatey is perfectly integrated into this process. The simplest script for a regular client machine might look like this:
cinst flashplayerplugin cinst flashplayeractivex cinst notepadplusplus cinst sublimetext2 cinst 7zip cinst GoogleChrome cinst javaruntime cinst Firefox cinst flashplayerplugin cinst adobereader cinst ccleaner cinst sysinternals cinst putty cinst filezilla cinst dropbox cinst skype cinst paint.net cinst virtualbox cinst DotNet4.5 cinst Wget cinst ConEmu cinst libreoffice cinst PDFCreator cinst teamviewer cinst wuinstall.run
Imagine how much time it saves you?
Puppet
I use Puppet to manage the configuration of my infrastructure, which saves me a lot of time and nerves. Puppet has a wonderful concept of resources, as well as a declarative style, which in a compartment helps to think abstractly, at the level “Which program should be on a particular server”, and not at the level “Which commands should I run on Windows and which ones on Linux ". For Puppet, there is a
Chocolatey provider that allows us to do the following:
package { "7zip" : ensure => installed, }
or
package { "notepadplusplus" : ensure => 1.0, }
Puppet and Chocolatey will take care of the rest. Believe me, it is much more convenient than installing from the msi file, which you need somewhere else, and also to make sure that when you update the version (which still needs to be done), the old versions are also preserved and nothing will break.
Chocolatey and Desktop
I propose to consider two ways to use Chocolatey for the administration of workstations.
Chocolatey GUIChocolateyGUI is a graphical interface for the Chocolatey package management system. A convenient way to review the current state of the repository, as well as the state of locally installed packages. For some reason, I was very much reminded of the early version of synaptic or even aptitude. It works quite well. By the way, you can install it from the command line:
cinst ChocolateyGUI

Windows Post Install (WPI)You can go even further, use the
WPI interface for convenient selection of packages, in which Chocolatey commands will be executed. With WPI, you can conveniently group programs into categories, as well as create templates and installation sets.
The solution may not always be an absolute alternative to using USB-HDD as a source, but replacing all possible components with similar ones from the Chocolatey repository you will save yourself from the painful copy of the image (folder) with a full set of software (Photoshop, Office, 3D Max with Archicad, which is there more?) and the WPI shell (and all in order to install "light" programs like Google Chrome, Notepad ++, Avast, etc.).
For example, for incoming administrators who maintain a scattered fleet of cars without centralized storage, it is convenient to have something like this list of templates:
- Business
- Accountant
- Development
- Home user
- Media Station



Thus, WPI is just a shell for running Chocolatey commands, which allows you to reduce the total volume of the distribution. Of course, with this approach, the client machine should already have a working Internet connection, which today is not a problem, except in some cases.
Returning to programs that are not in the Chocolatey.org repository, it is worth mentioning that Chocolatey supports any NuGet feeds, not just chocolatey.org offered by default. We upload important files to DropBox and create our package somewhere on
www.myget.org - it's very easy!
If anyone is interested, I can tell in detail (in the form of a separate post) how to create my package and how to download it to the chocolatey.org repository, and how I taught Windows to install all updates without my participation (with reboots and licenses), I update the maxmind.dat database automatically, as I use logstash and much more, and all this is not without the help of chocolatey and puppet!
In conclusion, I would say that in my opinion, the idea of ​​a decentralized package management system for Windows and its implementation is another way to make sure that today opensource and the discovery of technology is no less qualitative and applicable to the realities of system administration. Closed code is becoming less and less the market advantage of a community / company, while implementation and support play a huge role. Imagine that a decade ago, an open project created by one person could create such a resonance in wide circles, and even Windows circles - is unrealistic, but today Chocolatey is another chance to plunge into the opensource community and see the open possibility to contribute to the general idea .
For any errors and inaccuracies, please indicate in the comments, I will gladly correct and supplement the material.
And finally, a short survey: