// fetchMachines returns a map from top level machine id to machines, where machines[0] is the host // machine and machines[1..n] are any containers (including nested ones). // // If machineIds is non-nil, only machines whose IDs are in the set are returned. func fetchMachines(st *state.State, machineIds *set.Strings) (map[string][]*state.Machine, error) { v := make(map[string][]*state.Machine) machines, err := st.AllMachines() if err != nil { return nil, err } // AllMachines gives us machines sorted by id. for _, m := range machines { if machineIds != nil && !machineIds.Contains(m.Id()) { continue } parentId, ok := m.ParentId() if !ok { // Only top level host machines go directly into the machine map. v[m.Id()] = []*state.Machine{m} } else { topParentId := state.TopParentId(m.Id()) machines, ok := v[topParentId] if !ok { panic(fmt.Errorf("unexpected machine id %q", parentId)) } machines = append(machines, m) v[topParentId] = machines } } return v, nil }
// fetchAllServicesAndUnits returns a map from service name to service // and a map from service name to unit name to unit. func fetchAllServicesAndUnits(st *state.State, unitMatcher unitMatcher) (map[string]*state.Service, map[string]map[string]*state.Unit, error) { svcMap := make(map[string]*state.Service) unitMap := make(map[string]map[string]*state.Unit) services, err := st.AllServices() if err != nil { return nil, nil, err } for _, s := range services { units, err := s.AllUnits() if err != nil { return nil, nil, err } svcUnitMap := make(map[string]*state.Unit) for _, u := range units { if !unitMatcher.matchUnit(u) { continue } svcUnitMap[u.Name()] = u } if unitMatcher.matchesAny() || len(svcUnitMap) > 0 { unitMap[s.Name()] = svcUnitMap svcMap[s.Name()] = s } } return svcMap, unitMap, nil }
func waitForUnitStarted(stateConn *state.State, unit *state.Unit, c *C) { timeout := time.After(5 * time.Second) for { select { case <-timeout: c.Fatalf("no activity detected") case <-time.After(testing.ShortWait): err := unit.Refresh() c.Assert(err, IsNil) st, info, err := unit.Status() c.Assert(err, IsNil) switch st { case params.StatusPending, params.StatusInstalled: c.Logf("waiting...") continue case params.StatusStarted: c.Logf("started!") return case params.StatusDown: stateConn.StartSync() c.Logf("unit is still down") default: c.Fatalf("unexpected status %s %s", st, info) } } } }
// SetServiceContstraints sets the constraints for a given service func SetServiceConstraints(st *state.State, args params.SetServiceConstraints) error { svc, err := st.Service(args.ServiceName) if err != nil { return err } return svc.SetConstraints(args.Constraints) }
// ServiceGet returns the configuration for the named service. func ServiceGet(st *state.State, p params.ServiceGet) (params.ServiceGetResults, error) { service, err := st.Service(p.ServiceName) if err != nil { return params.ServiceGetResults{}, err } settings, err := service.ConfigSettings() if err != nil { return params.ServiceGetResults{}, err } charm, _, err := service.Charm() if err != nil { return params.ServiceGetResults{}, err } configInfo := describe(settings, charm.Config()) var constraints constraints.Value if service.IsPrincipal() { constraints, err = service.Constraints() if err != nil { return params.ServiceGetResults{}, err } } return params.ServiceGetResults{ Service: p.ServiceName, Charm: charm.Meta().Name, Config: configInfo, Constraints: constraints, }, nil }
// GetServiceConstraints returns the constraints for a given service func GetServiceConstraints(st *state.State, args params.GetServiceConstraints) (params.GetServiceConstraintsResults, error) { svc, err := st.Service(args.ServiceName) if err != nil { return params.GetServiceConstraintsResults{constraints.Value{}}, err } constraints, err := svc.Constraints() return params.GetServiceConstraintsResults{constraints}, err }
// breakDummyProvider changes the environment config in state in a way // that causes the given environMethod of the dummy provider to return // an error, which is also returned as a message to be checked. func breakDummyProvider(c *C, st *state.State, environMethod string) string { oldCfg, err := st.EnvironConfig() c.Assert(err, IsNil) cfg, err := oldCfg.Apply(map[string]interface{}{"broken": environMethod}) c.Assert(err, IsNil) err = st.SetEnvironConfig(cfg) c.Assert(err, IsNil) return fmt.Sprintf("dummy.%s is broken", environMethod) }
func newDeployer(st *state.State, w *state.UnitsWatcher, dataDir string) *deployer.Deployer { info := &state.Info{ EntityName: w.EntityName(), Addrs: st.Addrs(), CACert: st.CACert(), } mgr := newDeployManager(st, info, dataDir) return deployer.NewDeployer(st, mgr, w) }
func isRemoved(st *state.State, name string) func(*C) bool { return func(c *C) bool { _, err := st.Unit(name) if errors.IsNotFoundError(err) { return true } c.Assert(err, IsNil) return false } }
// SetAgentVersion sets the current agent version in the state's // environment configuration. func SetAgentVersion(st *state.State, vers version.Number) error { cfg, err := st.EnvironConfig() if err != nil { return err } cfg, err = cfg.Apply(map[string]interface{}{"agent-version": vers.String()}) if err != nil { return err } return st.SetEnvironConfig(cfg) }
func opClientServiceExpose(c *C, st *api.State, mst *state.State) (func(), error) { err := st.Client().ServiceExpose("wordpress") if err != nil { return func() {}, err } return func() { svc, err := mst.Service("wordpress") c.Assert(err, IsNil) svc.ClearExposed() }, nil }
// fetchAllMachines returns a map[string]*state.Machine representing // a mapping of machine ids to machines. func fetchAllMachines(st *state.State) (map[string]*state.Machine, error) { v := make(map[string]*state.Machine) machines, err := st.AllMachines() if err != nil { return nil, err } for _, m := range machines { v[m.Id()] = m } return v, nil }
// DestroyRelation removes the relation between the specified endpoints. func DestroyRelation(state *state.State, args params.DestroyRelation) error { eps, err := state.InferEndpoints(args.Endpoints) if err != nil { return err } rel, err := state.EndpointsRelation(eps...) if err != nil { return err } return rel.Destroy() }
// fetchAllServices returns a map representing a mapping of service // names to services. func fetchAllServices(st *state.State) (map[string]*state.Service, error) { v := make(map[string]*state.Service) services, err := st.AllServices() if err != nil { return nil, err } for _, s := range services { v[s.Name()] = s } return v, nil }
// setAgentVersion sets the current agent version in the state's // environment configuration. func setAgentVersion(st *state.State, vers version.Number) error { cfg, err := st.EnvironConfig() if err != nil { return err } attrs := cfg.AllAttrs() attrs["agent-version"] = vers.String() cfg, err = config.New(attrs) if err != nil { panic(fmt.Errorf("config refused agent-version: %v", err)) } return st.SetEnvironConfig(cfg) }
// getAllUnits returns a list of all principal and subordinate units // assigned to the given machine. func getAllUnits(st *state.State, machineTag string) ([]string, error) { machine, err := st.Machine(state.MachineIdFromTag(machineTag)) if err != nil { return nil, err } // Start a watcher on machine's units, read the initial event and stop it. watch := machine.WatchUnits() defer watch.Stop() if units, ok := <-watch.Changes(); ok { return units, nil } return nil, fmt.Errorf("cannot obtain units of machine %q: %v", machineTag, watch.Err()) }
func (a *MachineAgent) Entity(st *state.State) (AgentState, error) { m, err := st.Machine(a.MachineId) if err != nil { return nil, err } // Check the machine nonce as provisioned matches the agent.Conf value. if !m.CheckProvisioned(a.Conf.MachineNonce) { // The agent is running on a different machine to the one it // should be according to state. It must stop immediately. log.Errorf("running machine %v agent on inappropriate instance", m) return nil, worker.ErrTerminateAgent } return m, nil }
// NewConnFromState returns a Conn that uses an Environ // made by reading the environment configuration. // The resulting Conn uses the given State - closing // it will close that State. func NewConnFromState(st *state.State) (*Conn, error) { cfg, err := st.EnvironConfig() if err != nil { return nil, err } environ, err := environs.New(cfg) if err != nil { return nil, err } return &Conn{ Environ: environ, State: st, }, nil }
// opRecvTimeout waits for any of the given kinds of operation to // be received from ops, and times out if not. func opRecvTimeout(c *C, st *state.State, opc <-chan dummy.Operation, kinds ...dummy.Operation) dummy.Operation { st.StartSync() for { select { case op := <-opc: for _, k := range kinds { if reflect.TypeOf(op) == reflect.TypeOf(k) { return op } } c.Logf("discarding unknown event %#v", op) case <-time.After(15 * time.Second): c.Fatalf("time out wating for operation") } } }
// ServiceUnexpose changes the juju-managed firewall to unexpose any ports that // were also explicitly marked by units as open. func ServiceUnexpose(state *state.State, args params.ServiceUnexpose) error { svc, err := state.Service(args.ServiceName) if err != nil { return err } return svc.ClearExposed() }
// ServiceDestroy destroys a given service along with all its units and relations. func ServiceDestroy(state *state.State, args params.ServiceDestroy) error { svc, err := state.Service(args.ServiceName) if err != nil { return err } return svc.Destroy() }
// ConfigureBootstrapMachine adds the initial machine into state. As a part // of this process the environmental constraints are saved as constraints used // when bootstrapping are considered constraints for the entire environment. func ConfigureBootstrapMachine( st *state.State, cons constraints.Value, datadir string, jobs []state.MachineJob, instId instance.Id, characteristics instance.HardwareCharacteristics, ) error { logger.Debugf("setting environment constraints") if err := st.SetEnvironConstraints(cons); err != nil { return err } logger.Debugf("create bootstrap machine in state") m, err := st.InjectMachine(version.Current.Series, cons, instId, characteristics, jobs...) if err != nil { return err } // Read the machine agent's password and change it to // a new password (other agents will change their password // via the API connection). logger.Debugf("create new random password for machine %v", m.Id()) mconf, err := agent.ReadConf(datadir, m.Tag()) if err != nil { return err } newPassword, err := utils.RandomPassword() if err != nil { return err } mconf.StateInfo.Password = newPassword mconf.APIInfo.Password = newPassword mconf.OldPassword = "" if err := mconf.Write(); err != nil { return err } if err := m.SetMongoPassword(newPassword); err != nil { return err } if err := m.SetPassword(newPassword); err != nil { return err } return nil }
// NewFirewaller returns a new Firewaller. func NewFirewaller(st *state.State) *Firewaller { fw := &Firewaller{ st: st, environWatcher: st.WatchEnvironConfig(), machinesWatcher: st.WatchEnvironMachines(), machineds: make(map[string]*machineData), unitsChange: make(chan *unitsChange), unitds: make(map[string]*unitData), portsChange: make(chan *portsChange), serviceds: make(map[string]*serviceData), exposedChange: make(chan *exposedChange), } go func() { defer fw.tomb.Done() fw.tomb.Kill(fw.loop()) }() return fw }
// AddRelation adds a relation between the specified endpoint names, and // returns a map from service names to relation endpoints. func AddRelation(state *state.State, args params.AddRelation) (params.AddRelationResults, error) { inEps, err := state.InferEndpoints(args.Endpoints) if err != nil { return params.AddRelationResults{}, err } rel, err := state.AddRelation(inEps...) if err != nil { return params.AddRelationResults{}, err } outEps := make(map[string]charm.Relation) for _, inEp := range inEps { outEp, err := rel.Endpoint(inEp.ServiceName) if err != nil { return params.AddRelationResults{}, err } outEps[inEp.ServiceName] = outEp.Relation } return params.AddRelationResults{Endpoints: outEps}, nil }
// fetchAllServicesAndUnits returns a map from service name to service // and a map from service name to unit name to unit. func fetchAllServicesAndUnits(st *state.State) (map[string]*state.Service, map[string]map[string]*state.Unit, error) { svcMap := make(map[string]*state.Service) unitMap := make(map[string]map[string]*state.Unit) services, err := st.AllServices() if err != nil { return nil, nil, err } for _, s := range services { svcMap[s.Name()] = s units, err := s.AllUnits() if err != nil { return nil, nil, err } svcUnitMap := make(map[string]*state.Unit) for _, u := range units { svcUnitMap[u.Name()] = u } unitMap[s.Name()] = svcUnitMap } return svcMap, unitMap, nil }
// BootstrapUsers creates the initial admin user for the database, and sets // the initial password. func BootstrapUsers(st *state.State, cfg *config.Config, passwordHash string) error { logger.Debugf("adding admin user") // Set up initial authentication. u, err := st.AddUser("admin", "") if err != nil { return err } // Note that at bootstrap time, the password is set to // the hash of its actual value. The first time a client // connects to mongo, it changes the mongo password // to the original password. logger.Debugf("setting password hash for admin user") if err := u.SetPasswordHash(passwordHash); err != nil { return err } if err := st.SetAdminMongoPassword(passwordHash); err != nil { return err } return nil }
// AddServiceUnits adds a given number of units to a service. func AddServiceUnits(state *state.State, args params.AddServiceUnits) ([]*state.Unit, error) { conn, err := juju.NewConnFromState(state) if err != nil { return nil, err } service, err := state.Service(args.ServiceName) if err != nil { return nil, err } if args.NumUnits < 1 { return nil, errors.New("must add at least one unit") } if args.NumUnits > 1 && args.ToMachineSpec != "" { return nil, errors.New("cannot use --num-units with --to") } return conn.AddUnits(service, args.NumUnits, args.ToMachineSpec) }
func (a *MachineAgent) Entity(st *state.State) (AgentState, error) { return st.Machine(a.MachineId) }
func assertMachineCount(c *C, st *state.State, expect int) { ms, err := st.AllMachines() c.Assert(err, IsNil) c.Assert(ms, HasLen, expect, Commentf("%v", ms)) }
func (a *UnitAgent) Entity(st *state.State) (AgentState, error) { return st.Unit(a.UnitName) }