github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/provider/local/config.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 "fmt" 8 "os" 9 "path/filepath" 10 11 "github.com/juju/schema" 12 13 "github.com/juju/juju/agent" 14 "github.com/juju/juju/environs/config" 15 "github.com/juju/juju/instance" 16 ) 17 18 var checkIfRoot = func() bool { 19 return os.Getuid() == 0 20 } 21 22 var ( 23 configFields = schema.Fields{ 24 "root-dir": schema.String(), 25 "bootstrap-ip": schema.String(), 26 "network-bridge": schema.String(), 27 "container": schema.String(), 28 "storage-port": schema.ForceInt(), 29 "namespace": schema.String(), 30 } 31 // The port defaults below are not entirely arbitrary. Local user web 32 // frameworks often use 8000 or 8080, so I didn't want to use either of 33 // these, but did want the familiarity of using something in the 8000 34 // range. 35 configDefaults = schema.Defaults{ 36 "root-dir": "", 37 "network-bridge": "lxcbr0", 38 "container": string(instance.LXC), 39 "bootstrap-ip": schema.Omit, 40 "storage-port": 8040, 41 "namespace": "", 42 } 43 ) 44 45 type environConfig struct { 46 *config.Config 47 attrs map[string]interface{} 48 } 49 50 func newEnvironConfig(config *config.Config, attrs map[string]interface{}) *environConfig { 51 return &environConfig{ 52 Config: config, 53 attrs: attrs, 54 } 55 } 56 57 // Since it is technically possible for two different users on one machine to 58 // have the same local provider name, we need to have a simple way to 59 // namespace the file locations, but more importantly the containers. 60 func (c *environConfig) namespace() string { 61 return c.attrs["namespace"].(string) 62 } 63 64 func (c *environConfig) rootDir() string { 65 return c.attrs["root-dir"].(string) 66 } 67 68 func (c *environConfig) container() instance.ContainerType { 69 return instance.ContainerType(c.attrs["container"].(string)) 70 } 71 72 func (c *environConfig) networkBridge() string { 73 return c.attrs["network-bridge"].(string) 74 } 75 76 func (c *environConfig) storageDir() string { 77 return filepath.Join(c.rootDir(), "storage") 78 } 79 80 func (c *environConfig) mongoDir() string { 81 return filepath.Join(c.rootDir(), "db") 82 } 83 84 func (c *environConfig) logDir() string { 85 return fmt.Sprintf("%s-%s", agent.DefaultLogDir, c.namespace()) 86 } 87 88 // bootstrapIPAddress returns the IP address of the bootstrap machine. 89 // As of 1.18 this is only set inside the environment, and not in the 90 // .jenv file. 91 func (c *environConfig) bootstrapIPAddress() string { 92 addr, _ := c.attrs["bootstrap-ip"].(string) 93 return addr 94 } 95 96 func (c *environConfig) storagePort() int { 97 return c.attrs["storage-port"].(int) 98 } 99 100 func (c *environConfig) storageAddr() string { 101 return fmt.Sprintf("%s:%d", c.bootstrapIPAddress(), c.storagePort()) 102 } 103 104 func (c *environConfig) configFile(filename string) string { 105 return filepath.Join(c.rootDir(), filename) 106 } 107 108 func (c *environConfig) createDirs() error { 109 for _, dirname := range []string{ 110 c.storageDir(), 111 c.mongoDir(), 112 } { 113 logger.Tracef("creating directory %s", dirname) 114 if err := os.MkdirAll(dirname, 0755); err != nil { 115 return err 116 } 117 } 118 return nil 119 }