Using Packer and Vagrant to install your SharePoint environments on Windows and Linux!

Ok, time to update this.
I run all my development environments (SharePoint, SOLR, ElasticSearch, SCALA) in containers, either Docker, VMWare or VirtualBox. Doing it this way has allowed my hosts (Ubuntu on a 24 core Intel Server Platform) to remain stable, and not to interfere with development time. Too much time is wasted debugging platforms!
The steps below are applicable to both a Windows host as well as a Linux host.
1. Download and install Packer: https://www.packer.io/downloads.html
2. Download and install Packer-Windows: https://github.com/joefitzgerald/packer-windows . You can either grab the zip file or do #git clone https://github.com/joefitzgerald/packer-windows.git
3. In the downloaded Packer-Windows folder, you’ll find a number of *.json files. For SharePoint, copy the windows-2012-r2.json file. Name it anything that makes sense to you.
4. This is the fun part. Edit the json file:
     a. The first part is the builders. Here you can choose which builders to use. I use both VMWare and VirtiualBox. For SharePoint, you cannot use Docker, for obvious reasons.
          i. My build adds another CD Drive to the machine, which allows me to run an unattended Windows Update from the CD Drive, as opposed to the tedious process of updating from the web. Note that the second CD has to have the full path to the folder specified. Relative paths are only supported on the first CD drive in Packer.
          ii. These also show the machine screen while building (“headless”:”false”).
          iii. The floppy files entries are pretty standard
  "builders": [
         {
             "type": "vmware-iso",
             "iso_url": "./iso/9200.16384.WIN8_RTM.120725-1247_X64FRE_SERVER_EVAL_EN-US-HRM_SSS_X64FREE_EN-US_DV5_UPDATED.ISO",
             "iso_checksum_type": "md5",
             "iso_checksum": "5ef4590ebc4f9c4ff230e504e1cf236e",
             "headless": false,
             "boot_wait": "2m",
             "ssh_username": "vagrant",
             "ssh_password": "vagrant",
             "ssh_wait_timeout": "6h",
             "shutdown_command": "shutdown /s /t 10 /f /d p:4:1 /c \"Packer Shutdown\"",
             "guest_os_type": "windows8srv-64",
             "tools_upload_flavor": "windows",
             "disk_size": 61440,
             "vnc_port_min": 5900,
             "vnc_port_max": 5980,
             "skip_compaction": "true",
             "floppy_files": [
                     "./answer_files/2012_r2-remcam/Autounattend.xml",
                     "./scripts/microsoft-updates.bat",
                     "./scripts/win-updates.ps1",
                     "./scripts/openssh.ps1"
             ],
             "vmx_data": {
                     "ide1:1.devicetype": "cdrom-image",
                     "ide1:1.filename": "/home/svanschalkwyk/Projects/packer-windows/iso/wsusoffline-w63-x64.iso",
                     "ide1:1.present": "true",               
                     "RemoteDisplay.vnc.enabled": "false",
                     "RemoteDisplay.vnc.port": "5900",
                     "memsize": "8192",
                     "numvcpus": "2",
                     "scsi0.virtualDev": "lsisas1068"
             }
         },
        {
            "type": "virtualbox-iso",
               "iso_url": "./iso/9200.16384.WIN8_RTM.120725-1247_X64FRE_SERVER_EVAL_EN-US-HRM_SSS_X64FREE_EN-US_DV5_UPDATED.ISO",
               "iso_checksum_type": "md5",
               "iso_checksum": "5ef4590ebc4f9c4ff230e504e1cf236e",
               "headless": false,
                "boot_wait": "2m",
                "ssh_username": "vagrant",
                "ssh_password": "vagrant",
                 "ssh_wait_timeout": "6h",
                 "shutdown_command": "shutdown /s /t 10 /f /d p:4:1 /c \"Packer Shutdown\"",
                 "guest_os_type": "Windows2012_64",
                  "disk_size": 61440,
                  "floppy_files": [
                      "./answer_files/2012_r2-remcam/Autounattend.xml",
                      "./scripts/microsoft-updates.bat",
                      "./scripts/win-updates.ps1",
                      "./scripts/openssh.ps1",
                      "./scripts/oracle-cert.cer"
                 ],
       "vboxmanage": [
         [
           "modifyvm",
           "{{.Name}}",
           "--memory",
           "8192"
         ],
         [
           "modifyvm",
           "{{.Name}}",
           "--cpus",
           "2"
         ],
             [
                "storagectl",
                "{{.Name}}",
                "--name",
                "Sata Controller",
                "--add",
                "sata",
                "--controller",
                "IntelAHCI"
             ],
             [
                "storageattach",
                "{{.Name}}",
                "--storagectl",
                "IDE Controller",
                "--port",
                "1",
                "--device",
                "0",
                "--type",
                "dvddrive",
                "--medium",
                "/home/svanschalkwyk/Projects/packer-windows/iso/wsusoffline-w63-x64.iso"
             ]
       ]
     }
5. You can now edit the provisioners section. This section takes care of all the post-windows-installation steps. In addition to the entries below, I also install Chocolatey
https://chocolatey.org/ as well as 7zip, notepad++ and StartMenu8 (for those of us who don’t want to spend time swiping the left hand of the screen for “charms”…)
"provisioners": [
 {
 "type": "shell",
 "remote_path": "/tmp/script.bat",
 "execute_command": "{{.Vars}} cmd /c C:/Windows/Temp/script.bat",
 "scripts": [
 "./scripts/vm-guest-tools.bat",
 "./scripts/chocolatey.bat",
 "./scripts/vagrant-ssh.bat",
 "./scripts/enable-rdp.bat",
 "./scripts/compile-dotnet-assemblies.bat",
 "./scripts/disable-auto-logon.bat",
 "./scripts/compact.bat"
 ]
 },
 {
 "type": "shell",
 "inline": [
 "rm -rf /tmp/*"
 ]
 }
 ],
 "post-processors": [
 {
 "type": "vagrant",
 "keep_input_artifact": true
 }
 ]
6. Find the related Vagrant file. In this case it is vagrantfile-windows_2012_r2.template. If you have anything you want to override, you can do it in this file.
I usually add two more forwarded ports:
    config.vm.network "forwarded_port", guest: 80, host: 8080, id: "iis-defaultsite"
     config.vm.network "private_network", ip: "192.168.10.10"

and allow for a gui:

     v.gui = true
All the other entries are usually the same.
7. Find the appropriate answer file in the answer_file folder. For brevity, I won’t show it here. Two of the important sections are “< !– WITHOUT WINDOWS UPDATES — >” and “< !– WITH WINDOWS UPDATES — >”. Select the one which you prefer to have. You can also enter your Windows license here.
8. I also use NTLite (https://www.ntlite.com/) to edit the installation. I haven’t been able to successfully slipstream my installation disk with NTLight though. Your experience may differ.
9. Select the version of Windows 2012-R2 you want to install:
          Name : Windows Server 2012 R2 SERVERSTANDARDCORE
          Description : Windows Server 2012 R2 SERVERSTANDARDCORE
          
          Name : Windows Server 2012 R2 SERVERSTANDARD
          Description : Windows Server 2012 R2 SERVERSTANDARD
         
          Name : Windows Server 2012 R2 SERVERDATACENTERCORE
          Description : Windows Server 2012 R2 SERVERDATACENTERCORE
          
          Name : Windows Server 2012 R2 SERVERDATACENTER
          Description : Windows Server 2012 R2 SERVERDATACENTER
           

10.  I usually add to additional forwarded ports to the Vagrant file. If you’re not going to use the final Vagrant step (see below), these can be added to the packer configuration:

     config.vm.network "forwarded_port", guest: 80, host: 8080, id: "iis-defaultsite"
      config.vm.network "private_network", ip: "192.168.10.10"
11.  Now for your custom scripts. These are typically in the scripts folder.
I have two for my Chocolatey installs:
     a. chocolatey.bat"

          @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin
and
     b. chocolatey_packs.bat:

          :: Ensure C:\Chocolatey\bin is on the path
           :: Install all the things; for example:
           cmd /c %ALLUSERSPROFILE%\chocolatey\bin\choco install 7zip -y
           cmd /c %ALLUSERSPROFILE%\chocolatey\bin\choco install notepadplusplus -y
           cmd /c %ALLUSERSPROFILE%\chocolatey\bin\choco install startmenu8 -y
Do make sure the network is available when these run as Chocolatey downloads from the web.
12. You can now add any other post-windows installation scripts you need.
13. Right. Are you ready to run Packer on this?
First, validate your json file for correctness.
The command is
c:\your dir\packer validate your_windows_2012_r2.json or $/your dir/packer validate your_windows_2012_r2.json
14. If that succeeds, you can build the boxes.
To select only one type of machine (VMWare or VirtualBox), you can specify the builder to use.
packer build -only vmware-iso your_windows_2012_r2.json 
15. In my build, I reqested the post-processors to keep the virtual machine files after building, so the virtual machines will be in the folders
     output-vmware-iso and 

     output-virtualbox.iso.
In my case it makes better sense to keep the virtual machines and not go through the additional step of having to run Vagrant afterwards to provision them. Your needs may differ.
16. Configuring SharePoint. I use AutoSPInstaller and a number of script files to automatically configure SP. This is for another post.
Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *