launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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"
    10  	"os/exec"
    11  	"runtime"
    12  
    13  	"launchpad.net/juju-core/container/kvm"
    14  	"launchpad.net/juju-core/instance"
    15  	"launchpad.net/juju-core/utils"
    16  	"launchpad.net/juju-core/version"
    17  )
    18  
    19  var notLinuxError = errors.New("The local provider is currently only available for Linux")
    20  
    21  const installMongodUbuntu = "MongoDB server must be installed to enable the local provider:"
    22  const aptAddRepositoryJujuStable = `
    23      sudo apt-add-repository ppa:juju/stable   # required for MongoDB SSL support
    24      sudo apt-get update`
    25  const aptGetInstallMongodbServer = `
    26      sudo apt-get install mongodb-server`
    27  
    28  const installMongodGeneric = `
    29  MongoDB server must be installed to enable the local provider.
    30  Please consult your operating system distribution's documentation
    31  for instructions on installing the MongoDB server. Juju requires
    32  a MongoDB server built with SSL support.
    33  `
    34  
    35  const installLxcUbuntu = `
    36  Linux Containers (LXC) userspace tools must be
    37  installed to enable the local provider:
    38    
    39      sudo apt-get install lxc`
    40  
    41  const installLxcGeneric = `
    42  Linux Containers (LXC) userspace tools must be installed to enable the
    43  local provider. Please consult your operating system distribution's
    44  documentation for instructions on installing the LXC userspace tools.`
    45  
    46  const errUnsupportedOS = `Unsupported operating system: %s
    47  The local provider is currently only available for Linux`
    48  
    49  // mongodPath is the path to "mongod", the MongoDB server.
    50  // This is a variable only to support unit testing.
    51  var mongodPath = "/usr/bin/mongod"
    52  
    53  // lxclsPath is the path to "lxc-ls", an LXC userspace tool
    54  // we check the presence of to determine whether the
    55  // tools are installed. This is a variable only to support
    56  // unit testing.
    57  var lxclsPath = "lxc-ls"
    58  
    59  // The operating system the process is running in.
    60  // This is a variable only to support unit testing.
    61  var goos = runtime.GOOS
    62  
    63  // VerifyPrerequisites verifies the prerequisites of
    64  // the local machine (machine 0) for running the local
    65  // provider.
    66  func VerifyPrerequisites(containerType instance.ContainerType) error {
    67  	if goos != "linux" {
    68  		return fmt.Errorf(errUnsupportedOS, goos)
    69  	}
    70  	if err := verifyMongod(); err != nil {
    71  		return err
    72  	}
    73  	switch containerType {
    74  	case instance.LXC:
    75  		return verifyLxc()
    76  	case instance.KVM:
    77  		return kvm.VerifyKVMEnabled()
    78  	}
    79  	return fmt.Errorf("Unknown container type specified in the config.")
    80  }
    81  
    82  func verifyMongod() error {
    83  	if _, err := os.Stat(mongodPath); err != nil {
    84  		if os.IsNotExist(err) {
    85  			return wrapMongodNotExist(err)
    86  		} else {
    87  			return err
    88  		}
    89  	}
    90  	// TODO(axw) verify version/SSL capabilities
    91  	return nil
    92  }
    93  
    94  func verifyLxc() error {
    95  	_, err := exec.LookPath(lxclsPath)
    96  	if err != nil {
    97  		return wrapLxcNotFound(err)
    98  	}
    99  	return nil
   100  }
   101  
   102  func wrapMongodNotExist(err error) error {
   103  	if utils.IsUbuntu() {
   104  		series := version.Current.Series
   105  		args := []interface{}{err, installMongodUbuntu}
   106  		format := "%v\n%s\n%s"
   107  		if series == "precise" || series == "quantal" {
   108  			format += "%s"
   109  			args = append(args, aptAddRepositoryJujuStable)
   110  		}
   111  		args = append(args, aptGetInstallMongodbServer)
   112  		return fmt.Errorf(format, args...)
   113  	}
   114  	return fmt.Errorf("%v\n%s", err, installMongodGeneric)
   115  }
   116  
   117  func wrapLxcNotFound(err error) error {
   118  	if utils.IsUbuntu() {
   119  		return fmt.Errorf("%v\n%s", err, installLxcUbuntu)
   120  	}
   121  	return fmt.Errorf("%v\n%s", err, installLxcGeneric)
   122  }