github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cloudconfig/cloudinit/helpers.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Copyright 2015 Cloudbase Solutions SRL
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  package cloudinit
     6  
     7  import (
     8  	"strings"
     9  
    10  	"github.com/juju/utils/packaging/config"
    11  	"github.com/juju/utils/proxy"
    12  )
    13  
    14  // addPackageCommandsCommon is a helper function which applies the given
    15  // packaging-related options to the given CloudConfig.
    16  func addPackageCommandsCommon(
    17  	cfg CloudConfig,
    18  	packageProxySettings proxy.Settings,
    19  	packageMirror string,
    20  	addUpdateScripts bool,
    21  	addUpgradeScripts bool,
    22  	series string,
    23  ) {
    24  	// Set the package mirror.
    25  	cfg.SetPackageMirror(packageMirror)
    26  
    27  	// For LTS series which need support for the cloud-tools archive,
    28  	// we need to enable package-list update regardless of the environ
    29  	// setting, otherwise bootstrap or provisioning will fail.
    30  	if config.SeriesRequiresCloudArchiveTools(series) && !addUpdateScripts {
    31  		addUpdateScripts = true
    32  	}
    33  
    34  	// Bring packages up-to-date.
    35  	cfg.SetSystemUpdate(addUpdateScripts)
    36  	cfg.SetSystemUpgrade(addUpgradeScripts)
    37  
    38  	// Always run this step - this is where we install packages that juju
    39  	// requires.
    40  	cfg.addRequiredPackages()
    41  
    42  	// TODO(bogdanteleaga): Deal with proxy settings on CentOS
    43  	cfg.updateProxySettings(packageProxySettings)
    44  }
    45  
    46  // renderScriptCommon is a helper function which generates a bash script that
    47  // applies all the settings given by the provided CloudConfig when run.
    48  func renderScriptCommon(cfg CloudConfig) (string, error) {
    49  	// TODO(axw): 2013-08-23 bug 1215777
    50  	// Carry out configuration for ssh-keys-per-user,
    51  	// machine-updates-authkeys, using cloud-init config.
    52  	//
    53  	// We should work with smoser to get a supported
    54  	// command in (or next to) cloud-init for manually
    55  	// invoking cloud-config. This would address the
    56  	// above comment by removing the need to generate a
    57  	// script "by hand".
    58  
    59  	// Bootcmds must be run before anything else,
    60  	// as they may affect package installation.
    61  	bootcmds := cfg.BootCmds()
    62  
    63  	// Depending on cfg, potentially add package sources and packages.
    64  	pkgcmds, err := cfg.getCommandsForAddingPackages()
    65  	if err != nil {
    66  		return "", err
    67  	}
    68  
    69  	// Runcmds come last.
    70  	runcmds := cfg.RunCmds()
    71  
    72  	// We prepend "set -xe". This is already in runcmds,
    73  	// but added here to avoid relying on that to be
    74  	// invariant.
    75  	script := []string{"#!/bin/bash", "set -e"}
    76  	// We must initialise progress reporting before entering
    77  	// the subshell and redirecting stderr.
    78  	script = append(script, InitProgressCmd())
    79  	stdout, stderr := cfg.Output(OutAll)
    80  	script = append(script, "(")
    81  	if stderr != "" {
    82  		script = append(script, "(")
    83  	}
    84  	script = append(script, bootcmds...)
    85  	script = append(script, pkgcmds...)
    86  	script = append(script, runcmds...)
    87  	if stderr != "" {
    88  		script = append(script, ") "+stdout)
    89  		script = append(script, ") "+stderr)
    90  	} else {
    91  		script = append(script, ") "+stdout+" 2>&1")
    92  	}
    93  	return strings.Join(script, "\n"), nil
    94  }
    95  
    96  func copyStringSlice(s []string) []string {
    97  	if s == nil {
    98  		return nil
    99  	}
   100  	res := make([]string, len(s))
   101  	for i, item := range s {
   102  		res[i] = item
   103  	}
   104  	return res
   105  }