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