forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userdatacfg.go
232 lines (202 loc) · 7.3 KB
/
userdatacfg.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2012, 2013, 2014, 2015 Canonical Ltd.
// Copyright 2014, 2015 Cloudbase Solutions SRL
// Licensed under the AGPLv3, see LICENCE file for details.
package cloudconfig
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/names/v4"
"github.com/juju/os"
"github.com/juju/os/series"
"github.com/juju/proxy"
"github.com/juju/utils"
"github.com/juju/juju/agent"
"github.com/juju/juju/cloudconfig/cloudinit"
"github.com/juju/juju/cloudconfig/instancecfg"
)
const (
// fileSchemePrefix is the prefix for file:// URLs.
fileSchemePrefix = "file://"
httpSchemePrefix = "http://"
httpsSchemePrefix = "https://"
// NonceFile is written by cloud-init as the last thing it does.
// The file will contain the machine's nonce. The filename is
// relative to the Juju data-dir.
NonceFile = "nonce.txt"
)
// UserdataConfig is the bridge between instancecfg and cloudinit
// It supports different levels of configuration for instances
type UserdataConfig interface {
// Configure is a convenience function that updates the cloudinit.Config
// with appropriate configuration. It will run ConfigureBasic() and
// ConfigureJuju()
Configure() error
// ConfigureBasic updates the provided cloudinit.Config with
// basic configuration to initialise an OS image.
ConfigureBasic() error
// ConfigureJuju updates the provided cloudinit.Config with configuration
// to initialise a Juju machine agent.
ConfigureJuju() error
// ConfigureCustomOverrides updates the provided cloudinit.Config with
// user provided cloudinit data. Data provided will overwrite current
// values with three exceptions: preruncmd was handled in ConfigureBasic()
// and packages and postruncmd were handled in ConfigureJuju().
ConfigureCustomOverrides() error
}
// NewUserdataConfig is supposed to take in an instanceConfig as well as a
// cloudinit.cloudConfig and add attributes in the cloudinit structure based on
// the values inside instanceConfig and on the series
func NewUserdataConfig(icfg *instancecfg.InstanceConfig, conf cloudinit.CloudConfig) (UserdataConfig, error) {
// TODO(ericsnow) bug #1426217
// Protect icfg and conf better.
operatingSystem, err := series.GetOSFromSeries(icfg.Series)
if err != nil {
return nil, err
}
base := baseConfigure{
tag: names.NewMachineTag(icfg.MachineId),
icfg: icfg,
conf: conf,
os: operatingSystem,
}
switch operatingSystem {
case os.Ubuntu:
return &unixConfigure{base}, nil
case os.CentOS:
return &unixConfigure{base}, nil
case os.OpenSUSE:
return &unixConfigure{base}, nil
case os.Windows:
return &windowsConfigure{base}, nil
default:
return nil, errors.NotSupportedf("OS %s", icfg.Series)
}
}
type baseConfigure struct {
tag names.Tag
icfg *instancecfg.InstanceConfig
conf cloudinit.CloudConfig
os os.OSType
}
// addAgentInfo adds agent-required information to the agent's directory
// and returns the agent directory name.
func (c *baseConfigure) addAgentInfo(tag names.Tag) (agent.Config, error) {
acfg, err := c.icfg.AgentConfig(tag, c.icfg.AgentVersion().Number)
if err != nil {
return nil, errors.Trace(err)
}
acfg.SetValue(agent.AgentServiceName, c.icfg.MachineAgentServiceName)
cmds, err := acfg.WriteCommands(c.conf.ShellRenderer())
if err != nil {
return nil, errors.Annotate(err, "failed to write commands")
}
c.conf.AddScripts(cmds...)
return acfg, nil
}
func (c *baseConfigure) addMachineAgentToBoot() error {
svc, err := c.icfg.InitService(c.conf.ShellRenderer())
if err != nil {
return errors.Trace(err)
}
// Make the agent run via a symbolic link to the actual tools
// directory, so it can upgrade itself without needing to change
// the init script.
toolsDir := c.icfg.ToolsDir(c.conf.ShellRenderer())
c.conf.AddScripts(c.toolsSymlinkCommand(toolsDir))
name := c.tag.String()
cmds, err := svc.InstallCommands()
if err != nil {
return errors.Annotatef(err, "cannot make cloud-init init script for the %s agent", name)
}
startCmds, err := svc.StartCommands()
if err != nil {
return errors.Annotatef(err, "cannot make cloud-init init script for the %s agent", name)
}
cmds = append(cmds, startCmds...)
svcName := c.icfg.MachineAgentServiceName
// TODO (gsamfira): This is temporary until we find a cleaner way to fix
// cloudinit.LogProgressCmd to not add >&9 on Windows.
targetOS, err := series.GetOSFromSeries(c.icfg.Series)
if err != nil {
return err
}
if targetOS != os.Windows {
c.conf.AddRunCmd(cloudinit.LogProgressCmd("Starting Juju machine agent (service %s)", svcName))
}
c.conf.AddScripts(cmds...)
return nil
}
// SetUbuntuUser creates an "ubuntu" use for unix systems so the juju client
// can access the machine using ssh with the configuration we expect.
// On precise, the default cloudinit version is too old to support the users
// option, so instead rely on the default user being created and adding keys.
// It may make sense in the future to add a "juju" user instead across
// all distributions.
func SetUbuntuUser(conf cloudinit.CloudConfig, authorizedKeys string) {
targetSeries := conf.GetSeries()
if targetSeries == "precise" {
conf.SetSSHAuthorizedKeys(authorizedKeys)
} else {
var groups []string
targetOS, _ := series.GetOSFromSeries(targetSeries)
switch targetOS {
case os.Ubuntu:
groups = UbuntuGroups
case os.CentOS:
groups = CentOSGroups
case os.OpenSUSE:
groups = OpenSUSEGroups
}
conf.AddUser(&cloudinit.User{
Name: "ubuntu",
Groups: groups,
Shell: "/bin/bash",
Sudo: []string{"ALL=(ALL) NOPASSWD:ALL"},
SSHAuthorizedKeys: authorizedKeys,
})
}
}
// TODO(ericsnow) toolsSymlinkCommand should just be replaced with a
// call to shell.Renderer.Symlink.
func (c *baseConfigure) toolsSymlinkCommand(toolsDir string) string {
switch c.os {
case os.Windows:
return fmt.Sprintf(
`cmd.exe /C mklink /D %s %v`,
c.conf.ShellRenderer().FromSlash(toolsDir),
c.icfg.AgentVersion(),
)
default:
// TODO(dfc) ln -nfs, so it doesn't fail if for some reason that
// the target already exists.
return fmt.Sprintf(
"ln -s %v %s",
c.icfg.AgentVersion(),
shquote(toolsDir),
)
}
}
func shquote(p string) string {
return utils.ShQuote(p)
}
// packageManagerProxySettings implements cloudinit.PackageManagerConfig.
type packageManagerProxySettings struct {
aptProxy proxy.Settings
aptMirror string
snapProxy proxy.Settings
snapStoreAssertions string
snapStoreProxyID string
snapStoreProxyURL string
}
// AptProxy implements cloudinit.PackageManagerConfig.
func (p packageManagerProxySettings) AptProxy() proxy.Settings { return p.aptProxy }
// AptMirror implements cloudinit.PackageManagerConfig.
func (p packageManagerProxySettings) AptMirror() string { return p.aptMirror }
// SnapProxy implements cloudinit.PackageManagerConfig.
func (p packageManagerProxySettings) SnapProxy() proxy.Settings { return p.snapProxy }
// SnapStoreAssertions implements cloudinit.PackageManagerConfig.
func (p packageManagerProxySettings) SnapStoreAssertions() string { return p.snapStoreAssertions }
// SnapStoreProxyID implements cloudinit.PackageManagerConfig.
func (p packageManagerProxySettings) SnapStoreProxyID() string { return p.snapStoreProxyID }
// SnapStoreProxyURL implements cloudinit.PackageManagerConfig.
func (p packageManagerProxySettings) SnapStoreProxyURL() string { return p.snapStoreProxyURL }