~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/lxc/lxd/client.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
163
163
                return nil, fmt.Errorf("unknown remote name: %q", remote)
164
164
        }
165
165
        info := ConnectInfo{
166
 
                Name: remote,
167
 
                Addr: r.Addr,
 
166
                Name:         remote,
 
167
                RemoteConfig: r,
168
168
        }
169
169
 
170
170
        if strings.HasPrefix(r.Addr, "unix:") {
171
171
                // replace "unix://" with the official "unix:/var/lib/lxd/unix.socket"
172
 
                if info.Addr == "unix://" {
173
 
                        info.Addr = fmt.Sprintf("unix:%s", shared.VarPath("unix.socket"))
 
172
                if info.RemoteConfig.Addr == "unix://" {
 
173
                        info.RemoteConfig.Addr = fmt.Sprintf("unix:%s", shared.VarPath("unix.socket"))
174
174
                }
175
175
        } else {
176
176
                certf, keyf, err := ensureMyCert(config.ConfigDir)
202
202
                return nil, err
203
203
        }
204
204
        c.Config = *config
205
 
        c.Remote = &r
206
 
 
207
 
        if c.Remote.Protocol == "simplestreams" {
208
 
                ss, err := shared.SimpleStreamsClient(c.Remote.Addr)
209
 
                if err != nil {
210
 
                        return nil, err
211
 
                }
212
 
 
213
 
                c.simplestreams = ss
214
 
        }
215
205
 
216
206
        return c, nil
217
207
}
222
212
        // the name used to lookup the address and other information in the
223
213
        // config.yml file.
224
214
        Name string
225
 
        // Addr is the host address to connect to. It can be
226
 
        // unix:/path/to/socket to indicate we should connect over a unix
227
 
        // socket, or it can be an IP Address or
228
 
        // Hostname, or an https:// URL.
229
 
        // The standard unix socket is located at $LXD_DIR/unix.socket
230
 
        // See also github.com/lxc/lxd/shared.VarPath("unix.socket")
231
 
        Addr string
 
215
        // RemoteConfig is the information about the Remote that we are
 
216
        // connecting to. This includes information like if the remote is
 
217
        // Public and/or Static.
 
218
        RemoteConfig RemoteConfig
232
219
        // ClientPEMCert is the PEM encoded bytes of the client's certificate.
233
220
        // If Addr indicates a Unix socket, the certificate and key bytes will
234
221
        // not be used.
241
228
        ServerPEMCert string
242
229
}
243
230
 
244
 
func connectViaUnix(c *Client, addr string) error {
 
231
func connectViaUnix(c *Client, remote *RemoteConfig) error {
245
232
        c.BaseURL = "http://unix.socket"
246
233
        c.BaseWSURL = "ws://unix.socket"
247
234
        c.Transport = "unix"
248
 
        r := &RemoteConfig{Addr: addr}
249
235
        uDial := func(network, addr string) (net.Conn, error) {
250
236
                // The arguments 'network' and 'addr' are ignored because
251
237
                // they are the wrong information.
255
241
                //   unix:///path/to/socket
256
242
                //   unix:/path/to/socket
257
243
                //   unix:path/to/socket
258
 
                path := strings.TrimPrefix(r.Addr, "unix:")
 
244
                path := strings.TrimPrefix(remote.Addr, "unix:")
259
245
                if strings.HasPrefix(path, "///") {
260
246
                        // translate unix:///path/to, to just "/path/to"
261
247
                        path = path[2:]
268
254
        }
269
255
        c.Http.Transport = &http.Transport{Dial: uDial}
270
256
        c.websocketDialer.NetDial = uDial
271
 
        c.Remote = r
 
257
        c.Remote = remote
272
258
 
273
259
        st, err := c.ServerStatus()
274
260
        if err != nil {
278
264
        return nil
279
265
}
280
266
 
281
 
func connectViaHttp(c *Client, addr, clientCert, clientKey, serverCert string) error {
 
267
func connectViaHttp(c *Client, remote *RemoteConfig, clientCert, clientKey, serverCert string) error {
282
268
        tlsconfig, err := shared.GetTLSConfigMem(clientCert, clientKey, serverCert)
283
269
        if err != nil {
284
270
                return err
287
273
        tr := &http.Transport{
288
274
                TLSClientConfig: tlsconfig,
289
275
                Dial:            shared.RFC3493Dialer,
290
 
                Proxy:           http.ProxyFromEnvironment,
 
276
                Proxy:           shared.ProxyFromEnvironment,
291
277
        }
292
278
 
293
279
        c.websocketDialer.NetDial = shared.RFC3493Dialer
294
280
        c.websocketDialer.TLSClientConfig = tlsconfig
295
281
 
296
 
        justAddr := strings.TrimPrefix(addr, "https://")
 
282
        justAddr := strings.TrimPrefix(remote.Addr, "https://")
297
283
        c.BaseURL = "https://" + justAddr
298
284
        c.BaseWSURL = "wss://" + justAddr
299
285
        c.Transport = "https"
300
286
        c.Http.Transport = tr
301
 
        c.Remote = &RemoteConfig{Addr: addr}
 
287
        c.Remote = remote
302
288
        c.Certificate = serverCert
303
289
        // We don't actually need to connect yet, defer that until someone
304
290
        // needs something from the server.
318
304
        }
319
305
        c.Name = info.Name
320
306
        var err error
321
 
        if info.Addr[0:5] == "unix:" {
322
 
                err = connectViaUnix(c, info.Addr)
 
307
        if strings.HasPrefix(info.RemoteConfig.Addr, "unix:") {
 
308
                err = connectViaUnix(c, &info.RemoteConfig)
323
309
        } else {
324
 
                err = connectViaHttp(c, info.Addr, info.ClientPEMCert, info.ClientPEMKey, info.ServerPEMCert)
 
310
                err = connectViaHttp(c, &info.RemoteConfig, info.ClientPEMCert, info.ClientPEMKey, info.ServerPEMCert)
325
311
        }
326
312
        if err != nil {
327
313
                return nil, err
328
314
        }
329
315
 
 
316
        if info.RemoteConfig.Protocol == "simplestreams" {
 
317
                ss, err := shared.SimpleStreamsClient(c.Remote.Addr, shared.ProxyFromEnvironment)
 
318
                if err != nil {
 
319
                        return nil, err
 
320
                }
 
321
 
 
322
                c.simplestreams = ss
 
323
        }
 
324
 
330
325
        return c, nil
331
326
}
332
327