github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/provider/local/prereqs.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package local
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  	"os/exec"
    10  	"runtime"
    11  
    12  	"github.com/juju/utils"
    13  	"github.com/juju/utils/apt"
    14  
    15  	"github.com/juju/juju/container/kvm"
    16  	"github.com/juju/juju/instance"
    17  )
    18  
    19  var notLinuxError = errors.New("The local provider is currently only available for Linux")
    20  
    21  const aptAddRepositoryJujuStable = `
    22      sudo apt-add-repository ppa:juju/stable   # required for MongoDB SSL support
    23      sudo apt-get update`
    24  
    25  const installLxcUbuntu = `
    26  Linux Containers (LXC) userspace tools must be
    27  installed to enable the local provider:
    28  
    29      sudo apt-get install lxc`
    30  
    31  const installJujuLocalUbuntu = `
    32  juju-local must be installed to enable the local provider:
    33  
    34      sudo apt-get install juju-local`
    35  
    36  const installLxcGeneric = `
    37  Linux Containers (LXC) userspace tools must be installed to enable the
    38  local provider. Please consult your operating system distribution's
    39  documentation for instructions on installing the LXC userspace tools.`
    40  
    41  const errUnsupportedOS = `Unsupported operating system: %s
    42  The local provider is currently only available for Linux`
    43  
    44  // lxclsPath is the path to "lxc-ls", an LXC userspace tool
    45  // we check the presence of to determine whether the
    46  // tools are installed. This is a variable only to support
    47  // unit testing.
    48  var lxclsPath = "lxc-ls"
    49  
    50  // isPackageInstalled is a variable to support testing.
    51  var isPackageInstalled = apt.IsPackageInstalled
    52  
    53  // The operating system the process is running in.
    54  // This is a variable only to support unit testing.
    55  var goos = runtime.GOOS
    56  
    57  // VerifyPrerequisites verifies the prerequisites of
    58  // the local machine (machine 0) for running the local
    59  // provider.
    60  var VerifyPrerequisites = func(containerType instance.ContainerType) error {
    61  	if goos != "linux" {
    62  		return fmt.Errorf(errUnsupportedOS, goos)
    63  	}
    64  	if err := verifyJujuLocal(); err != nil {
    65  		return err
    66  	}
    67  	switch containerType {
    68  	case instance.LXC:
    69  		return verifyLxc()
    70  	case instance.KVM:
    71  		return kvm.VerifyKVMEnabled()
    72  	}
    73  	return fmt.Errorf("Unknown container type specified in the config.")
    74  }
    75  
    76  func verifyLxc() error {
    77  	_, err := exec.LookPath(lxclsPath)
    78  	if err != nil {
    79  		return wrapLxcNotFound(err)
    80  	}
    81  	return nil
    82  }
    83  
    84  func verifyJujuLocal() error {
    85  	if isPackageInstalled("juju-local") {
    86  		return nil
    87  	}
    88  	return errors.New(installJujuLocalUbuntu)
    89  }
    90  
    91  func wrapLxcNotFound(err error) error {
    92  	if utils.IsUbuntu() {
    93  		return fmt.Errorf("%v\n%s", err, installLxcUbuntu)
    94  	}
    95  	return fmt.Errorf("%v\n%s", err, installLxcGeneric)
    96  }