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