← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~rvb/juju-core/state-info into lp:~maas-maintainers/juju-core/maas-provider-skeleton

 

Raphaël Badin has proposed merging lp:~rvb/juju-core/state-info into lp:~maas-maintainers/juju-core/maas-provider-skeleton.

Commit message:
Implement environ.StateInfo().

Requested reviews:
  MAAS Maintainers (maas-maintainers)

For more details, see:
https://code.launchpad.net/~rvb/juju-core/state-info/+merge/153099

The implementation is cargo-culted from the openstack provider (which, in-turn, is cargo-culted from the ec2 provider).  This really should be in Juju but for now, this ugly duplication will do.
-- 
https://code.launchpad.net/~rvb/juju-core/state-info/+merge/153099
Your team MAAS Maintainers is requested to review the proposed merge of lp:~rvb/juju-core/state-info into lp:~maas-maintainers/juju-core/maas-provider-skeleton.
=== modified file 'environs/maas/environ.go'
--- environs/maas/environ.go	2013-03-12 16:06:05 +0000
+++ environs/maas/environ.go	2013-03-13 10:16:19 +0000
@@ -21,6 +21,14 @@
 	jujuDataDir = "/var/lib/juju"
 )
 
+var mgoPortSuffix = fmt.Sprintf(":%d", mgoPort)
+var apiPortSuffix = fmt.Sprintf(":%d", apiPort)
+
+var longAttempt = trivial.AttemptStrategy{
+	Total: 3 * time.Minute,
+	Delay: 1 * time.Second,
+}
+
 type maasEnviron struct {
 	name string
 
@@ -184,8 +192,51 @@
 }
 
 // StateInfo is specified in the Environ interface.
-func (*maasEnviron) StateInfo() (*state.Info, *api.Info, error) {
-	panic("Not implemented.")
+func (env *maasEnviron) StateInfo() (*state.Info, *api.Info, error) {
+	st, err := env.loadState()
+	if err != nil {
+		return nil, nil, err
+	}
+	cert, hasCert := env.Config().CACert()
+	if !hasCert {
+		return nil, nil, fmt.Errorf("no CA certificate in environment configuration")
+	}
+	var stateAddrs []string
+	var apiAddrs []string
+	// Wait for the DNS names of any of the instances
+	// to become available.
+	log.Printf("environs/maas: waiting for DNS name(s) of state server instances %v", st.StateInstances)
+	for a := longAttempt.Start(); len(stateAddrs) == 0 && a.Next(); {
+		insts, err := env.Instances(st.StateInstances)
+		if err != nil && err != environs.ErrPartialInstances {
+			log.Debugf("error getting state instance: %v", err.Error())
+			return nil, nil, err
+		}
+		log.Debugf("started processing instances: %#v", insts)
+		for _, inst := range insts {
+			if inst == nil {
+				continue
+			}
+			name, err := inst.(*maasInstance).DNSName()
+			if err != nil {
+				continue
+			}
+			if name != "" {
+				stateAddrs = append(stateAddrs, name+mgoPortSuffix)
+				apiAddrs = append(apiAddrs, name+apiPortSuffix)
+			}
+		}
+	}
+	if len(stateAddrs) == 0 {
+		return nil, nil, fmt.Errorf("timed out waiting for mgo address from %v", st.StateInstances)
+	}
+	return &state.Info{
+			Addrs:  stateAddrs,
+			CACert: cert,
+		}, &api.Info{
+			Addrs:  apiAddrs,
+			CACert: cert,
+		}, nil
 }
 
 // ecfg returns the environment's maasEnvironConfig, and protects it with a

=== modified file 'environs/maas/environ_test.go'
--- environs/maas/environ_test.go	2013-03-12 16:06:05 +0000
+++ environs/maas/environ_test.go	2013-03-13 10:16:19 +0000
@@ -228,6 +228,22 @@
 	c.Check(err, Not(IsNil))
 }
 
+func (suite *EnvironSuite) TestStateInfo(c *C) {
+	env := suite.makeEnviron()
+	hostname := "test"
+	input := `{"system_id": "test", "hostname": "` + hostname + `"}`
+	node := suite.testMAASObject.TestServer.NewNode(input)
+	instance := &maasInstance{&node, suite.environ}
+	err := env.saveState(&bootstrapState{StateInstances: []state.InstanceId{instance.Id()}})
+	c.Assert(err, IsNil)
+
+	stateInfo, apiInfo, err := env.StateInfo()
+
+	c.Assert(err, IsNil)
+	c.Assert(stateInfo.Addrs, DeepEquals, []string{hostname + mgoPortSuffix})
+	c.Assert(apiInfo.Addrs, DeepEquals, []string{hostname + apiPortSuffix})
+}
+
 func (suite *EnvironSuite) TestQuiesceStateFileFailsOnBrokenStateFile(c *C) {
 	const content = "@#$(*&Y%!"
 	reader := bytes.NewReader([]byte(content))


Follow ups