github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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(
    50  	o terraform.UIOutput,
    51  	comm communicator.Communicator) error {
    52  	script := path.Join(path.Dir(comm.ScriptPath()), "ChefClient.ps1")
    53  	content := fmt.Sprintf(installScript, p.Version, p.HTTPProxy, strings.Join(p.NOProxy, ","))
    54  
    55  	// Copy the script to the new instance
    56  	if err := comm.UploadScript(script, strings.NewReader(content)); err != nil {
    57  		return fmt.Errorf("Uploading client.rb failed: %v", err)
    58  	}
    59  
    60  	// Execute the script to install Chef Client
    61  	installCmd := fmt.Sprintf("powershell -NoProfile -ExecutionPolicy Bypass -File %s", script)
    62  	return p.runCommand(o, comm, installCmd)
    63  }
    64  
    65  func (p *Provisioner) windowsCreateConfigFiles(
    66  	o terraform.UIOutput,
    67  	comm communicator.Communicator) error {
    68  	// Make sure the config directory exists
    69  	cmd := fmt.Sprintf("cmd /c if not exist %q mkdir %q", windowsConfDir, windowsConfDir)
    70  	if err := p.runCommand(o, comm, cmd); err != nil {
    71  		return err
    72  	}
    73  
    74  	if len(p.OhaiHints) > 0 {
    75  		// Make sure the hits directory exists
    76  		hintsDir := path.Join(windowsConfDir, "ohai/hints")
    77  		cmd := fmt.Sprintf("cmd /c if not exist %q mkdir %q", hintsDir, hintsDir)
    78  		if err := p.runCommand(o, comm, cmd); err != nil {
    79  			return err
    80  		}
    81  
    82  		if err := p.deployOhaiHints(o, comm, hintsDir); err != nil {
    83  			return err
    84  		}
    85  	}
    86  
    87  	return p.deployConfigFiles(o, comm, windowsConfDir)
    88  }