github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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  
    11  // addPackageCommandsCommon is a helper function which applies the given
    12  // packaging-related options to the given CloudConfig.
    13  func addPackageCommandsCommon(
    14  	cfg CloudConfig,
    15  	proxyCfg PackageManagerProxyConfig,
    16  	addUpdateScripts bool,
    17  	addUpgradeScripts bool,
    18  ) error {
    19  	// Set the package mirror.
    20  	cfg.SetPackageMirror(proxyCfg.AptMirror())
    21  
    22  	// Bring packages up-to-date.
    23  	cfg.SetSystemUpdate(addUpdateScripts)
    24  	cfg.SetSystemUpgrade(addUpgradeScripts)
    25  
    26  	// Always run this step - this is where we install packages that juju
    27  	// requires.
    28  	cfg.addRequiredPackages()
    29  
    30  	// TODO(bogdanteleaga): Deal with proxy settings on CentOS
    31  	return cfg.updateProxySettings(proxyCfg)
    32  }
    33  
    34  // renderScriptCommon is a helper function which generates a bash script that
    35  // applies all the settings given by the provided CloudConfig when run.
    36  func renderScriptCommon(cfg CloudConfig) (string, error) {
    37  	// TODO(axw): 2013-08-23 bug 1215777
    38  	// Carry out configuration for ssh-keys-per-user,
    39  	// machine-updates-authkeys, using cloud-init config.
    40  	//
    41  	// We should work with smoser to get a supported
    42  	// command in (or next to) cloud-init for manually
    43  	// invoking cloud-config. This would address the
    44  	// above comment by removing the need to generate a
    45  	// script "by hand".
    46  
    47  	// Bootcmds must be run before anything else,
    48  	// as they may affect package installation.
    49  	bootcmds := cfg.BootCmds()
    50  
    51  	// Depending on cfg, potentially add package sources and packages.
    52  	pkgcmds, err := cfg.getCommandsForAddingPackages()
    53  	if err != nil {
    54  		return "", err
    55  	}
    56  
    57  	// Runcmds come last.
    58  	runcmds := cfg.RunCmds()
    59  
    60  	// We prepend "set -xe". This is already in runcmds,
    61  	// but added here to avoid relying on that to be
    62  	// invariant.
    63  	script := []string{"#!/bin/bash", "set -e"}
    64  	// We must initialise progress reporting before entering
    65  	// the subshell and redirecting stderr.
    66  	script = append(script, InitProgressCmd())
    67  	stdout, stderr := cfg.Output(OutAll)
    68  	script = append(script, "(")
    69  	if stderr != "" {
    70  		script = append(script, "(")
    71  	}
    72  	script = append(script, bootcmds...)
    73  	script = append(script, pkgcmds...)
    74  	script = append(script, runcmds...)
    75  	if stderr != "" {
    76  		script = append(script, ") "+stdout)
    77  		script = append(script, ") "+stderr)
    78  	} else {
    79  		script = append(script, ") "+stdout+" 2>&1")
    80  	}
    81  	return strings.Join(script, "\n"), nil
    82  }
    83  
    84  func copyStringSlice(s []string) []string {
    85  	if s == nil {
    86  		return nil
    87  	}
    88  	res := make([]string, len(s))
    89  	for i, item := range s {
    90  		res[i] = item
    91  	}
    92  	return res
    93  }