github.com/lymingtonprecision/terraform@v0.9.9-0.20170613092852-62acef9611a9/builtin/provisioners/chef/windows_provisioner.go (about)

     1  package chef
     2  
     3  import (
     4  	"fmt"
     5  	"path"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/communicator"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  const installScript = `
    13  $winver = [System.Environment]::OSVersion.Version | %% {"{0}.{1}" -f $_.Major,$_.Minor}
    14  
    15  switch ($winver)
    16  {
    17    "6.0" {$machine_os = "2008"}
    18    "6.1" {$machine_os = "2008r2"}
    19    "6.2" {$machine_os = "2012"}
    20    "6.3" {$machine_os = "2012"}
    21    default {$machine_os = "2008r2"}
    22  }
    23  
    24  if ([System.IntPtr]::Size -eq 4) {$machine_arch = "i686"} else {$machine_arch = "x86_64"}
    25  
    26  $url = "http://www.chef.io/chef/download?p=windows&pv=$machine_os&m=$machine_arch&v=%s"
    27  $dest = [System.IO.Path]::GetTempFileName()
    28  $dest = [System.IO.Path]::ChangeExtension($dest, ".msi")
    29  $downloader = New-Object System.Net.WebClient
    30  
    31  $http_proxy = '%s'
    32  if ($http_proxy -ne '') {
    33  	$no_proxy = '%s'
    34    if ($no_proxy -eq ''){
    35      $no_proxy = "127.0.0.1"
    36    }
    37  
    38    $proxy = New-Object System.Net.WebProxy($http_proxy, $true, ,$no_proxy.Split(','))
    39    $downloader.proxy = $proxy
    40  }
    41  
    42  Write-Host 'Downloading Chef Client...'
    43  $downloader.DownloadFile($url, $dest)
    44  
    45  Write-Host 'Installing Chef Client...'
    46  Start-Process -FilePath msiexec -ArgumentList /qn, /i, $dest -Wait
    47  `
    48  
    49  func (p *provisioner) windowsInstallChefClient(o terraform.UIOutput, comm communicator.Communicator) error {
    50  	script := path.Join(path.Dir(comm.ScriptPath()), "ChefClient.ps1")
    51  	content := fmt.Sprintf(installScript, p.Version, p.HTTPProxy, strings.Join(p.NOProxy, ","))
    52  
    53  	// Copy the script to the new instance
    54  	if err := comm.UploadScript(script, strings.NewReader(content)); err != nil {
    55  		return fmt.Errorf("Uploading client.rb failed: %v", err)
    56  	}
    57  
    58  	// Execute the script to install Chef Client
    59  	installCmd := fmt.Sprintf("powershell -NoProfile -ExecutionPolicy Bypass -File %s", script)
    60  	return p.runCommand(o, comm, installCmd)
    61  }
    62  
    63  func (p *provisioner) windowsCreateConfigFiles(o terraform.UIOutput, comm communicator.Communicator) error {
    64  	// Make sure the config directory exists
    65  	cmd := fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir)
    66  	if err := p.runCommand(o, comm, cmd); err != nil {
    67  		return err
    68  	}
    69  
    70  	if len(p.OhaiHints) > 0 {
    71  		// Make sure the hits directory exists
    72  		hintsDir := path.Join(windowsConfDir, "ohai/hints")
    73  		cmd := fmt.Sprintf("cmd /c if not exist %q mkdir %q", hintsDir, hintsDir)
    74  		if err := p.runCommand(o, comm, cmd); err != nil {
    75  			return err
    76  		}
    77  
    78  		if err := p.deployOhaiHints(o, comm, hintsDir); err != nil {
    79  			return err
    80  		}
    81  	}
    82  
    83  	return p.deployConfigFiles(o, comm, windowsConfDir)
    84  }