54
56
// Prepare implements environs.EnvironProvider.Prepare.
55
57
func (p environProvider) Prepare(cfg *config.Config) (environs.Environ, error) {
56
// TODO prepare environment
58
err := checkLocalPort(cfg.StatePort(), "state port")
62
err = checkLocalPort(cfg.APIPort(), "API port")
69
// checkLocalPort checks that the passed port is not used so far.
70
func checkLocalPort(port int, description string) error {
71
logger.Infof("checking %s", description)
72
// Try to connect the port on localhost.
73
address := fmt.Sprintf("localhost:%d", port)
74
// TODO(mue) Add a timeout?
75
conn, err := net.Dial("tcp", address)
77
if nerr, ok := err.(*net.OpError); ok {
78
if nerr.Err == syscall.ECONNREFUSED {
79
// No connection, so everything is fine.
85
// Connected, so port is in use.
90
return fmt.Errorf("cannot use %d as %s, already in use", port, description)
60
93
// Validate implements environs.EnvironProvider.Validate.
61
94
func (provider environProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) {
62
95
// Check for valid changes for the base config values.