packer-community / packer-windows-plugins

A suite of Packer plugins for provisioning Windows machines
113 stars 20 forks source link

Dedicated Powershell & Windows Shell Provisioners #15

Closed mefellows closed 9 years ago

mefellows commented 9 years ago

This PR addresses the requirements outlined in #12 with the intention of creating specific Powershell and Windows Shell (cmd) Packer Provisioners - the two plugins can now be used as provisioners in packer.json templates using the types powershell and windows-shell respectively.

Additionally, this PR combines a number of improvements over the existing system:

  1. Provisioners now implement the documented behaviour of Packer inline scripts, specifically the "same context" behavior of stock shell-provisioner:

    commands are concatenated by newlines and turned into a single file, so they are all executed within the same context

  2. Environment variables are now supported across both inline and script(s) directives
  3. By separating out the concerns of CMD and PS into separate provisioners, it makes it easier for users to target the various script execution environments without having to muck around with all of the parameters & templates interpolation.
  4. Powershell users can now simply execute the commands without having to wrap them with powershell -command ... type statements

Here are some examples that are now supported with the new provisioners:

  "provisioners": [
    {
      "type":"powershell",
      "inline": [
        "echo $env:PACKER_BUILDER_TYPE > c:\\psinline-envvars.txt"
      ]
    },
    {
      "type":"powershell",
      "environment_vars":"FOOBAR=OHYEAHBABY",
      "script":"scripts/envvars.ps1"
    },
    {
      "type":"windows-shell",
      "inline": [
        "whoami",
        "ipconfig /all > c:\\networksettings.txt",
        "echo %PACKER_BUILDER_TYPE% > c:\\envvars-inline.txt"
      ]
    },
    {
      "type":"windows-shell",
      "environment_vars":"FOOBAR=OHYEAHBABY",
      "script":"scripts/envvars.bat"
    },
    {
      "type": "powershell",
      "scripts": [
        "./scripts/virtualbox-guest-tools.ps1"
      ]
    },
    {
      "type":"powershell",
      "inline": [
        "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))"
      ]
    },
    {
      "type":"powershell",
      "inline": [
        "choco install seek-dsc"
      ]
    }
  ]
dylanmei commented 9 years ago

Oh yeah!

My first attempt was to use the amazon ebs builder. However, it hangs after it writes the temp file.

==> amazon-windows-ebs: Provisioning with windows-shell...
2015/01/13 08:08:21 packer-provisioner-powershell: 2015/01/13 08:08:21 Found command: iex ((new-object net.webclient).DownloadString('https://chocolatey.org/i
nstall.ps1'))
2015/01/13 08:08:21 packer-provisioner-powershell: 2015/01/13 08:08:21 Found command: choco install 7zip
2015/01/13 08:08:21 ui: ==> amazon-windows-ebs: Provisioning with shell script: /tmp/packer-windows-shell-provisioner464613483
==> amazon-windows-ebs: Provisioning with shell script: /tmp/packer-windows-shell-provisioner464613483
2015/01/13 08:08:21 packer-provisioner-powershell: 2015/01/13 08:08:21 Opening /tmp/packer-windows-shell-provisioner464613483 for reading
2015/01/13 08:08:21 packer-provisioner-powershell: 2015/01/13 08:08:21 [INFO] 105 bytes written for 'uploadData'
2015/01/13 08:08:21 [INFO] 105 bytes written for 'uploadData'

Provisioner block:

"provisioners": [{
  "type": "powershell",
  "inline": [
    "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))",
    "choco install 7zip"
  ]
}],

I've tried it twice. Any thoughts?

Also the Provisioning with names and the temp script path that we send to the console can be updated to reflect the new awesome.

mefellows commented 9 years ago

My thoughts are sad. I haven't yet tested with Amazon but let me see about trying that out today. Good point wrt output I'll make the adjustments.

mefellows commented 9 years ago

OK I have some good news and some bad news. Well, really just good news - The amazon-windows-ebs builder works great for me. I have tested by installing chocolatey and then another package as per your above example.

I'll contact you in Slack @dylanmei and we can drill down to find out why things aren't working for you (or what I've done differently / forgot more likely).

dylanmei commented 9 years ago

I went back to the stock winrmcp and it worked great on vbox and aws. Nice work! :beers:

mefellows commented 9 years ago

Great news! I shall merge this in for now and we can get more feedback in the beta.