github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/tools/lxdclient/utils.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // +build go1.3
     5  
     6  package lxdclient
     7  
     8  import (
     9  	"bytes"
    10  
    11  	"github.com/juju/errors"
    12  	"github.com/juju/utils/series"
    13  
    14  	"github.com/juju/juju/service"
    15  	"github.com/juju/juju/service/common"
    16  )
    17  
    18  type closingBuffer struct {
    19  	bytes.Buffer
    20  }
    21  
    22  // Close implements io.Closer.
    23  func (closingBuffer) Close() error {
    24  	return nil
    25  }
    26  
    27  // IsInstalledLocally returns true if LXD is installed locally.
    28  func IsInstalledLocally() (bool, error) {
    29  	names, err := service.ListServices()
    30  	if err != nil {
    31  		return false, errors.Trace(err)
    32  	}
    33  	for _, name := range names {
    34  		if name == "lxd" {
    35  			return true, nil
    36  		}
    37  	}
    38  	return false, nil
    39  }
    40  
    41  // IsRunningLocally returns true if LXD is running locally.
    42  func IsRunningLocally() (bool, error) {
    43  	installed, err := IsInstalledLocally()
    44  	if err != nil {
    45  		return installed, errors.Trace(err)
    46  	}
    47  	if !installed {
    48  		return false, nil
    49  	}
    50  
    51  	svc, err := service.NewService("lxd", common.Conf{}, series.HostSeries())
    52  	if err != nil {
    53  		return false, errors.Trace(err)
    54  	}
    55  
    56  	running, err := svc.Running()
    57  	if err != nil {
    58  		return running, errors.Trace(err)
    59  	}
    60  
    61  	return running, nil
    62  }
    63  
    64  // EnableHTTPSListener configures LXD to listen for HTTPS requests,
    65  // rather than only via the Unix socket.
    66  func EnableHTTPSListener(client interface {
    67  	SetConfig(k, v string) error
    68  }) error {
    69  	// Make sure the LXD service is configured to listen to local https
    70  	// requests, rather than only via the Unix socket.
    71  	// TODO: jam 2016-02-25 This tells LXD to listen on all addresses,
    72  	//      which does expose the LXD to outside requests. It would
    73  	//      probably be better to only tell LXD to listen for requests on
    74  	//      the loopback and LXC bridges that we are using.
    75  	if err := client.SetConfig("core.https_address", "[::]"); err != nil {
    76  		return errors.Trace(err)
    77  	}
    78  	return nil
    79  }