github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/provisioners/chef/linux_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 (
    13  	chmod      = "find %s -maxdepth 1 -type f -exec /bin/chmod %d {} +"
    14  	installURL = "https://www.chef.io/chef/install.sh"
    15  )
    16  
    17  func (p *Provisioner) linuxInstallChefClient(
    18  	o terraform.UIOutput,
    19  	comm communicator.Communicator) error {
    20  
    21  	// Build up the command prefix
    22  	prefix := ""
    23  	if p.HTTPProxy != "" {
    24  		prefix += fmt.Sprintf("http_proxy='%s' ", p.HTTPProxy)
    25  	}
    26  	if p.HTTPSProxy != "" {
    27  		prefix += fmt.Sprintf("https_proxy='%s' ", p.HTTPSProxy)
    28  	}
    29  	if p.NOProxy != nil {
    30  		prefix += fmt.Sprintf("no_proxy='%s' ", strings.Join(p.NOProxy, ","))
    31  	}
    32  
    33  	// First download the install.sh script from Chef
    34  	err := p.runCommand(o, comm, fmt.Sprintf("%scurl -LO %s", prefix, installURL))
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	// Then execute the install.sh scrip to download and install Chef Client
    40  	err = p.runCommand(o, comm, fmt.Sprintf("%sbash ./install.sh -v %q", prefix, p.Version))
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	// And finally cleanup the install.sh script again
    46  	return p.runCommand(o, comm, fmt.Sprintf("%srm -f install.sh", prefix))
    47  }
    48  
    49  func (p *Provisioner) linuxCreateConfigFiles(
    50  	o terraform.UIOutput,
    51  	comm communicator.Communicator) error {
    52  	// Make sure the config directory exists
    53  	if err := p.runCommand(o, comm, "mkdir -p "+linuxConfDir); err != nil {
    54  		return err
    55  	}
    56  
    57  	// Make sure we have enough rights to upload the files if using sudo
    58  	if p.useSudo {
    59  		if err := p.runCommand(o, comm, "chmod 777 "+linuxConfDir); err != nil {
    60  			return err
    61  		}
    62  		if err := p.runCommand(o, comm, fmt.Sprintf(chmod, linuxConfDir, 666)); err != nil {
    63  			return err
    64  		}
    65  	}
    66  
    67  	if err := p.deployConfigFiles(o, comm, linuxConfDir); err != nil {
    68  		return err
    69  	}
    70  
    71  	if len(p.OhaiHints) > 0 {
    72  		// Make sure the hits directory exists
    73  		hintsDir := path.Join(linuxConfDir, "ohai/hints")
    74  		if err := p.runCommand(o, comm, "mkdir -p "+hintsDir); err != nil {
    75  			return err
    76  		}
    77  
    78  		// Make sure we have enough rights to upload the hints if using sudo
    79  		if p.useSudo {
    80  			if err := p.runCommand(o, comm, "chmod 777 "+hintsDir); err != nil {
    81  				return err
    82  			}
    83  			if err := p.runCommand(o, comm, fmt.Sprintf(chmod, hintsDir, 666)); err != nil {
    84  				return err
    85  			}
    86  		}
    87  
    88  		if err := p.deployOhaiHints(o, comm, hintsDir); err != nil {
    89  			return err
    90  		}
    91  
    92  		// When done copying the hints restore the rights and make sure root is owner
    93  		if p.useSudo {
    94  			if err := p.runCommand(o, comm, "chmod 755 "+hintsDir); err != nil {
    95  				return err
    96  			}
    97  			if err := p.runCommand(o, comm, fmt.Sprintf(chmod, hintsDir, 600)); err != nil {
    98  				return err
    99  			}
   100  			if err := p.runCommand(o, comm, "chown -R root.root "+hintsDir); err != nil {
   101  				return err
   102  			}
   103  		}
   104  	}
   105  
   106  	// When done copying all files restore the rights and make sure root is owner
   107  	if p.useSudo {
   108  		if err := p.runCommand(o, comm, "chmod 755 "+linuxConfDir); err != nil {
   109  			return err
   110  		}
   111  		if err := p.runCommand(o, comm, fmt.Sprintf(chmod, linuxConfDir, 600)); err != nil {
   112  			return err
   113  		}
   114  		if err := p.runCommand(o, comm, "chown -R root.root "+linuxConfDir); err != nil {
   115  			return err
   116  		}
   117  	}
   118  
   119  	return nil
   120  }