1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package utils
import (
"bytes"
"fmt"
"os"
"os/exec"
"regexp"
"launchpad.net/loggo"
)
var (
aptLogger = loggo.GetLogger("juju.utils.apt")
aptProxyRE = regexp.MustCompile(`(?im)^\s*Acquire::[a-z]+::Proxy\s+"[^"]+";\s*$`)
)
// Some helpful functions for running apt in a sane way
// commandOutput calls cmd.Output, this is used as an overloading point so we
// can test what *would* be run without actually executing another program
var commandOutput = (*exec.Cmd).CombinedOutput
// This is the default apt-get command used in cloud-init, the various settings
// mean that apt won't actually block waiting for a prompt from the user.
var aptGetCommand = []string{
"apt-get", "--option=Dpkg::Options::=--force-confold",
"--option=Dpkg::options::=--force-unsafe-io", "--assume-yes", "--quiet",
}
// aptEnvOptions are options we need to pass to apt-get to not have it prompt
// the user
var aptGetEnvOptions = []string{"DEBIAN_FRONTEND=noninteractive"}
// AptGetInstall runs 'apt-get install packages' for the packages listed here
func AptGetInstall(packages ...string) error {
cmdArgs := append([]string(nil), aptGetCommand...)
cmdArgs = append(cmdArgs, "install")
cmdArgs = append(cmdArgs, packages...)
aptLogger.Infof("Running: %s", cmdArgs)
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
cmd.Env = append(os.Environ(), aptGetEnvOptions...)
out, err := commandOutput(cmd)
if err != nil {
aptLogger.Errorf("apt-get command failed: %v\nargs: %#v\n%s",
err, cmdArgs, string(out))
return fmt.Errorf("apt-get failed: %v", err)
}
return nil
}
// AptConfigProxy will consult apt-config about the configured proxy
// settings. If there are no proxy settings configured, an empty string is
// returned.
func AptConfigProxy() (string, error) {
cmdArgs := []string{
"apt-config",
"dump",
"Acquire::http::Proxy",
"Acquire::https::Proxy",
"Acquire::ftp::Proxy",
}
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
out, err := commandOutput(cmd)
if err != nil {
aptLogger.Errorf("apt-config command failed: %v\nargs: %#v\n%s",
err, cmdArgs, string(out))
return "", fmt.Errorf("apt-config failed: %v", err)
}
return string(bytes.Join(aptProxyRE.FindAll(out, -1), []byte("\n"))), nil
}
|