~rogpeppe/juju-core/themue-058-debug-log-api

« back to all changes in this revision

Viewing changes to environs/manual/provisioner.go

  • Committer: Frank Mueller
  • Date: 2014-01-07 13:59:00 UTC
  • mfrom: (2152.1.32 juju-core)
  • Revision ID: frank.mueller@canonical.com-20140107135900-t3akd2tp3s5fsujf
merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
81
81
                client.Close()
82
82
        }()
83
83
 
 
84
        // Create the "ubuntu" user and initialise passwordless sudo. We populate
 
85
        // the ubuntu user's authorized_keys file with the public keys in the current
 
86
        // user's ~/.ssh directory. The authenticationworker will later update the
 
87
        // ubuntu user's authorized_keys.
 
88
        user, host := splitUserHost(args.Host)
 
89
        authorizedKeys, err := config.ReadAuthorizedKeys("")
 
90
        if err := InitUbuntuUser(host, user, authorizedKeys, args.Stdin, args.Stdout); err != nil {
 
91
                return "", err
 
92
        }
 
93
 
84
94
        // Generate a unique nonce for the machine.
85
95
        uuid, err := utils.NewUUID()
86
96
        if err != nil {
87
97
                return "", err
88
98
        }
89
 
        instanceId := instance.Id(manualInstancePrefix + hostWithoutUser(args.Host))
 
99
        instanceId := instance.Id(manualInstancePrefix + host)
90
100
        nonce := fmt.Sprintf("%s:%s", instanceId, uuid.String())
91
101
 
92
102
        // Inform Juju that the machine exists.
93
 
        machineId, series, arch, err := recordMachineInState(client, args.Host, nonce, instanceId)
 
103
        machineId, series, arch, err := recordMachineInState(client, host, nonce, instanceId)
94
104
        if err != nil {
95
105
                return "", err
96
106
        }
102
112
        }
103
113
 
104
114
        // Finally, provision the machine agent.
105
 
        err = provisionMachineAgent(args.Host, mcfg, args.Stdin, args.Stdout, args.Stderr)
 
115
        err = provisionMachineAgent(host, mcfg, args.Stderr)
106
116
        if err != nil {
107
117
                return machineId, err
108
118
        }
111
121
        return machineId, nil
112
122
}
113
123
 
114
 
func hostWithoutUser(host string) string {
115
 
        hostWithoutUser := host
116
 
        if at := strings.Index(hostWithoutUser, "@"); at != -1 {
117
 
                hostWithoutUser = hostWithoutUser[at+1:]
 
124
func splitUserHost(host string) (string, string) {
 
125
        if at := strings.Index(host, "@"); at != -1 {
 
126
                return host[:at], host[at+1:]
118
127
        }
119
 
        return hostWithoutUser
 
128
        return "", host
120
129
}
121
130
 
122
131
func recordMachineInState(
123
132
        client *api.Client, host, nonce string, instanceId instance.Id) (machineId, series, arch string, err error) {
124
133
 
125
134
        // First, gather the parameters needed to inject the existing host into state.
126
 
        sshHostWithoutUser := hostWithoutUser(host)
127
 
        if ip := net.ParseIP(sshHostWithoutUser); ip != nil {
 
135
        if ip := net.ParseIP(host); ip != nil {
128
136
                // Do a reverse-lookup on the IP. The IP may not have
129
137
                // a DNS entry, so just log a warning if this fails.
130
138
                names, err := net.LookupAddr(ip.String())
132
140
                        logger.Infof("failed to resolve %v: %v", ip, err)
133
141
                } else {
134
142
                        logger.Infof("resolved %v to %v", ip, names)
135
 
                        sshHostWithoutUser = names[0]
 
143
                        host = names[0]
136
144
                }
137
145
        }
138
 
        addrs, err := instance.HostAddresses(sshHostWithoutUser)
 
146
        addrs, err := instance.HostAddresses(host)
139
147
        if err != nil {
140
148
                return "", "", "", err
141
149
        }
142
 
        logger.Infof("addresses for %v: %v", sshHostWithoutUser, addrs)
 
150
        logger.Infof("addresses for %v: %v", host, addrs)
143
151
 
144
152
        provisioned, err := checkProvisioned(host)
145
153
        if err != nil {
171
179
                Addrs:                   addrs,
172
180
                Jobs:                    []params.MachineJob{params.JobHostUnits},
173
181
        }
174
 
        results, err := client.InjectMachines([]params.AddMachineParams{machineParams})
 
182
        results, err := client.AddMachines([]params.AddMachineParams{machineParams})
175
183
        if err != nil {
176
184
                return "", "", "", err
177
185
        }
216
224
        return mcfg, nil
217
225
}
218
226
 
219
 
func provisionMachineAgent(host string, mcfg *cloudinit.MachineConfig, stdin io.Reader, stdout, stderr io.Writer) error {
 
227
func provisionMachineAgent(host string, mcfg *cloudinit.MachineConfig, stderr io.Writer) error {
220
228
        cloudcfg := coreCloudinit.New()
221
229
        if err := cloudinit.ConfigureJuju(mcfg, cloudcfg); err != nil {
222
230
                return err
225
233
        // the target machine's existing configuration.
226
234
        cloudcfg.SetAptUpgrade(false)
227
235
        return sshinit.Configure(sshinit.ConfigureParams{
228
 
                Host:   host,
 
236
                Host:   "ubuntu@" + host,
229
237
                Config: cloudcfg,
230
 
                Stdin:  stdin,
231
 
                Stdout: stdout,
232
238
                Stderr: stderr,
233
239
        })
234
240
}