Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
davecheney committed Sep 1, 2014
1 parent 8a61d29 commit 1acb3dc
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 22 deletions.
15 changes: 8 additions & 7 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/juju/juju/mongo"
"github.com/juju/juju/network"
"github.com/juju/juju/state/api"
"github.com/juju/juju/state"
"github.com/juju/juju/state/api/params"
"github.com/juju/juju/version"
)
Expand Down Expand Up @@ -113,7 +114,7 @@ type Config interface {
// StateServingInfo returns the details needed to run
// a state server and reports whether those details
// are available
StateServingInfo() (params.StateServingInfo, bool)
StateServingInfo() (state.StateServingInfo, bool)

// APIInfo returns details for connecting to the API server.
APIInfo() *api.Info
Expand Down Expand Up @@ -182,7 +183,7 @@ type ConfigSetterOnly interface {

// SetStateServingInfo sets the information needed
// to run a state server
SetStateServingInfo(info params.StateServingInfo)
SetStateServingInfo(info state.StateServingInfo)
}

type ConfigWriter interface {
Expand Down Expand Up @@ -241,7 +242,7 @@ type configInternal struct {
stateDetails *connectionDetails
apiDetails *connectionDetails
oldPassword string
servingInfo *params.StateServingInfo
servingInfo *state.StateServingInfo
values map[string]string
preferIPv6 bool
}
Expand Down Expand Up @@ -325,7 +326,7 @@ func NewAgentConfig(configParams AgentConfigParams) (ConfigSetterWriter, error)

// NewStateMachineConfig returns a configuration suitable for
// a machine running the state server.
func NewStateMachineConfig(configParams AgentConfigParams, serverInfo params.StateServingInfo) (ConfigSetterWriter, error) {
func NewStateMachineConfig(configParams AgentConfigParams, serverInfo state.StateServingInfo) (ConfigSetterWriter, error) {
if serverInfo.Cert == "" {
return nil, errors.Trace(requiredError("state server cert"))
}
Expand Down Expand Up @@ -551,14 +552,14 @@ func (c *configInternal) PreferIPv6() bool {
return c.preferIPv6
}

func (c *configInternal) StateServingInfo() (params.StateServingInfo, bool) {
func (c *configInternal) StateServingInfo() (state.StateServingInfo, bool) {
if c.servingInfo == nil {
return params.StateServingInfo{}, false
return state.StateServingInfo{}, false
}
return *c.servingInfo, true
}

func (c *configInternal) SetStateServingInfo(info params.StateServingInfo) {
func (c *configInternal) SetStateServingInfo(info state.StateServingInfo) {
c.servingInfo = &info
}

Expand Down
14 changes: 14 additions & 0 deletions agent/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func InitializeState(c ConfigSetter, envCfg *config.Config, machineCfg Bootstrap
if err = initAPIHostPorts(c, st, machineCfg.Addresses, servingInfo.APIPort); err != nil {
return nil, nil, err
}
// si := paramsStateServingInfoToStateStateServingInfo(&servingInfo)
if err := st.SetStateServingInfo(servingInfo); err != nil {
return nil, nil, fmt.Errorf("cannot set state serving info: %v", err)
}
Expand All @@ -115,6 +116,19 @@ func InitializeState(c ConfigSetter, envCfg *config.Config, machineCfg Bootstrap
return st, m, nil
}

// convert params.StateServingInfo to a state.StateServingInfo.
// This avoids state having a dependency on api/params.
func paramsStateServingInfoToStateStateServingInfo(si *params.StateServingInfo) state.StateServingInfo {
return state.StateServingInfo {
APIPort: si.APIPort,
StatePort: si.StatePort,
Cert: si.Cert,
PrivateKey: si.PrivateKey,
SharedSecret: si.SharedSecret,
SystemIdentity: si.SystemIdentity,
}
}

func initUsersAndBootstrapMachine(c ConfigSetter, st *state.State, cfg BootstrapMachineConfig) (*state.Machine, error) {
if err := initBootstrapUser(st, c.OldPassword()); err != nil {
return nil, fmt.Errorf("cannot initialize bootstrap user: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions agent/format-1.16.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/juju/names"
goyaml "gopkg.in/yaml.v1"

"github.com/juju/juju/state/api/params"
"github.com/juju/juju/state"
"github.com/juju/juju/version"
)

Expand Down Expand Up @@ -113,7 +113,7 @@ func (formatter_1_16) unmarshal(data []byte) (*configInternal, error) {
}

if len(stateServerKey) != 0 {
config.servingInfo = &params.StateServingInfo{
config.servingInfo = &state.StateServingInfo{
Cert: string(stateServerCert),
PrivateKey: string(stateServerKey),
APIPort: format.APIPort,
Expand Down
3 changes: 2 additions & 1 deletion agent/format-1.18.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
goyaml "gopkg.in/yaml.v1"

"github.com/juju/juju/state/api/params"
"github.com/juju/juju/state"
"github.com/juju/juju/version"
)

Expand Down Expand Up @@ -109,7 +110,7 @@ func (formatter_1_18) unmarshal(data []byte) (*configInternal, error) {
}
}
if len(format.StateServerKey) != 0 {
config.servingInfo = &params.StateServingInfo{
config.servingInfo = &state.StateServingInfo{
Cert: format.StateServerCert,
PrivateKey: format.StateServerKey,
APIPort: format.APIPort,
Expand Down
3 changes: 2 additions & 1 deletion environs/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/juju/juju/environs/config"
"github.com/juju/juju/juju/paths"
"github.com/juju/juju/mongo"
"github.com/juju/juju/state"
"github.com/juju/juju/state/api"
"github.com/juju/juju/state/api/params"
"github.com/juju/juju/version"
Expand Down Expand Up @@ -177,7 +178,7 @@ func FinishMachineConfig(mcfg *cloudinit.MachineConfig, cfg *config.Config) (err
return errors.Annotate(err, "cannot generate state server certificate")
}

srvInfo := params.StateServingInfo{
srvInfo := state.StateServingInfo{
StatePort: cfg.StatePort(),
APIPort: cfg.APIPort(),
Cert: string(cert),
Expand Down
3 changes: 2 additions & 1 deletion environs/cloudinit/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/juju/juju/environs/config"
"github.com/juju/juju/instance"
"github.com/juju/juju/state/api"
"github.com/juju/juju/state"
"github.com/juju/juju/state/api/params"
coretools "github.com/juju/juju/tools"
"github.com/juju/juju/version"
Expand All @@ -44,7 +45,7 @@ type MachineConfig struct {
// This must only be set if the Bootstrap field is true
// (state servers started subsequently will acquire their serving info
// from another server)
StateServingInfo *params.StateServingInfo
StateServingInfo *state.StateServingInfo

// MongoInfo holds the means for the new instance to communicate with the
// juju state database. Unless the new machine is running a state server
Expand Down
18 changes: 16 additions & 2 deletions state/api/agent/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/juju/names"

"github.com/juju/juju/instance"
"github.com/juju/juju/state/"
"github.com/juju/juju/state/api/base"
"github.com/juju/juju/state/api/params"
)
Expand Down Expand Up @@ -43,10 +44,23 @@ func (st *State) getEntity(tag names.Tag) (*params.AgentGetEntitiesResult, error
return &results.Entities[0], nil
}

func (st *State) StateServingInfo() (params.StateServingInfo, error) {
func (st *State) StateServingInfo() (state.StateServingInfo, error) {
var results params.StateServingInfo
err := st.facade.FacadeCall("StateServingInfo", nil, &results)
return results, err
return paramsStateServingInfoToStateStateServingInfo(&results), err
}

// convert params.StateServingInfo to a state.StateServingInfo.
// This avoids state having a dependency on api/params.
func paramsStateServingInfoToStateStateServingInfo(si *params.StateServingInfo) state.StateServingInfo {
return state.StateServingInfo {
APIPort: si.APIPort,
StatePort: si.StatePort,
Cert: si.Cert,
PrivateKey: si.PrivateKey,
SharedSecret: si.SharedSecret,
SystemIdentity: si.SystemIdentity,
}
}

// IsMaster reports whether the connected machine
Expand Down
2 changes: 1 addition & 1 deletion state/apiserver/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (api *API) getEntity(tag names.Tag) (result params.AgentGetEntitiesResult,
return
}

func (api *API) StateServingInfo() (result params.StateServingInfo, err error) {
func (api *API) StateServingInfo() (result state.StateServingInfo, err error) {
if !api.auth.AuthEnvironManager() {
err = common.ErrPerm
return
Expand Down
3 changes: 1 addition & 2 deletions state/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/juju/juju/environmentserver/authentication"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/mongo"
"github.com/juju/juju/state/api/params"
"github.com/juju/juju/state/presence"
"github.com/juju/juju/state/watcher"
)
Expand Down Expand Up @@ -111,7 +110,7 @@ func Initialize(info *authentication.MongoInfo, cfg *config.Config, opts mongo.D
C: stateServersC,
Id: stateServingInfoKey,
Assert: txn.DocMissing,
Insert: &params.StateServingInfo{},
Insert: &StateServingInfo{},
},
}
if err := st.runTransaction(ops); err == txn.ErrAborted {
Expand Down
22 changes: 17 additions & 5 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/juju/juju/environmentserver/authentication"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/mongo"
"github.com/juju/juju/state/api/params"
"github.com/juju/juju/state/multiwatcher"
"github.com/juju/juju/state/presence"
"github.com/juju/juju/state/watcher"
Expand Down Expand Up @@ -99,6 +98,19 @@ type State struct {
environTag names.EnvironTag
}

// StateServingInfo holds information needed by a state server.
// This type is a copy of the type of the same name from the api/params package.
// It is replicated here to avoid the state pacakge depending on api/params.
type StateServingInfo struct {
APIPort int
StatePort int
Cert string
PrivateKey string
// this will be passed as the KeyFile argument to MongoDB
SharedSecret string
SystemIdentity string
}

// EnvironTag() returns the environment tag for the environment controlled by
// this state instance.
func (st *State) EnvironTag() names.EnvironTag {
Expand Down Expand Up @@ -1678,23 +1690,23 @@ func (st *State) StateServerInfo() (*StateServerInfo, error) {
const stateServingInfoKey = "stateServingInfo"

// StateServingInfo returns information for running a state server machine
func (st *State) StateServingInfo() (params.StateServingInfo, error) {
func (st *State) StateServingInfo() (StateServingInfo, error) {
stateServers, closer := st.getCollection(stateServersC)
defer closer()

var info params.StateServingInfo
var info StateServingInfo
err := stateServers.Find(bson.D{{"_id", stateServingInfoKey}}).One(&info)
if err != nil {
return info, err
}
if info.StatePort == 0 {
return params.StateServingInfo{}, errors.NotFoundf("state serving info")
return StateServingInfo{}, errors.NotFoundf("state serving info")
}
return info, nil
}

// SetStateServingInfo stores information needed for running a state server
func (st *State) SetStateServingInfo(info params.StateServingInfo) error {
func (st *State) SetStateServingInfo(info StateServingInfo) error {
if info.StatePort == 0 || info.APIPort == 0 ||
info.Cert == "" || info.PrivateKey == "" {
return fmt.Errorf("incomplete state serving info set in state")
Expand Down

0 comments on commit 1acb3dc

Please sign in to comment.