本文整理汇总了Golang中launchpad/net/juju-core/state.MachineTag函数的典型用法代码示例。如果您正苦于以下问题:Golang MachineTag函数的具体用法?Golang MachineTag怎么用?Golang MachineTag使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MachineTag函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: writeBootstrapAgentConfFile
func (env *localEnviron) writeBootstrapAgentConfFile(cert, key []byte) error {
info, apiInfo, err := env.StateInfo()
if err != nil {
logger.Errorf("failed to get state info to write bootstrap agent file: %v", err)
return err
}
tag := state.MachineTag("0")
info.Tag = tag
apiInfo.Tag = tag
conf := &agent.Conf{
DataDir: env.config.rootDir(),
StateInfo: info,
APIInfo: apiInfo,
StateServerCert: cert,
StateServerKey: key,
StatePort: env.config.StatePort(),
APIPort: env.config.APIPort(),
MachineNonce: state.BootstrapNonce,
}
if err := conf.Write(); err != nil {
logger.Errorf("failed to write bootstrap agent file: %v", err)
return err
}
return nil
}
示例2: CanDeploy
// CanDeploy returns if the currently authenticated entity (a machine
// agent) can deploy each passed unit entity.
func (d *DeployerAPI) CanDeploy(args params.Entities) (params.BoolResults, error) {
result := params.BoolResults{
Results: make([]params.BoolResult, len(args.Entities)),
}
for i, entity := range args.Entities {
unitName := state.UnitNameFromTag(entity.Tag)
unit, err := d.st.Unit(unitName)
if errors.IsNotFoundError(err) {
// Unit not found, so no need to continue.
continue
} else if err != nil {
// Any other error get reported back.
result.Results[i].Error = common.ServerError(err)
continue
}
machineId, err := unit.AssignedMachineId()
if err != nil && !state.IsNotAssigned(err) && !errors.IsNotFoundError(err) {
// Any other errors get reported back.
result.Results[i].Error = common.ServerError(err)
continue
} else if err != nil {
// This means the unit wasn't assigned to the machine
// agent or it wasn't found. In both cases we just return
// false so as not to leak information about the existence
// of a unit to a potentially rogue machine agent.
continue
}
// Finally, check if we're allowed to access this unit.
// When assigned machineId == "" it will fail.
result.Results[i].Result = d.authorizer.AuthOwner(state.MachineTag(machineId))
}
return result, nil
}
示例3: FakeAPIInfo
// FakeAPIInfo holds information about no state - it will always
// give an error when connected to. The machine id gives the machine id
// of the machine to be started.
func FakeAPIInfo(machineId string) *api.Info {
return &api.Info{
Addrs: []string{"0.1.2.3:1234"},
Tag: state.MachineTag(machineId),
Password: "unimportant",
CACert: []byte(testing.CACert),
}
}
示例4: startMachine
func (p *Provisioner) startMachine(m *state.Machine) error {
// TODO(dfc) the state.Info passed to environ.StartInstance remains contentious
// however as the PA only knows one state.Info, and that info is used by MAs and
// UAs to locate the state for this environment, it is logical to use the same
// state.Info as the PA.
stateInfo, apiInfo, err := p.setupAuthentication(m)
if err != nil {
return err
}
cons, err := m.Constraints()
if err != nil {
return err
}
// Generate a unique nonce for the new instance.
uuid, err := utils.NewUUID()
if err != nil {
return err
}
// Generated nonce has the format: "machine-#:UUID". The first
// part is a badge, specifying the tag of the machine the provisioner
// is running on, while the second part is a random UUID.
nonce := fmt.Sprintf("%s:%s", state.MachineTag(p.machineId), uuid.String())
inst, err := p.environ.StartInstance(m.Id(), nonce, m.Series(), cons, stateInfo, apiInfo)
if err != nil {
// Set the state to error, so the machine will be skipped next
// time until the error is resolved, but don't return an
// error; just keep going with the other machines.
log.Errorf("worker/provisioner: cannot start instance for machine %q: %v", m, err)
if err1 := m.SetStatus(params.StatusError, err.Error()); err1 != nil {
// Something is wrong with this machine, better report it back.
log.Errorf("worker/provisioner: cannot set error status for machine %q: %v", m, err1)
return err1
}
return nil
}
if err := m.SetProvisioned(inst.Id(), nonce); err != nil {
// The machine is started, but we can't record the mapping in
// state. It'll keep running while we fail out and restart,
// but will then be detected by findUnknownInstances and
// killed again.
//
// TODO(dimitern) Stop the instance right away here.
//
// Multiple instantiations of a given machine (with the same
// machine ID) cannot coexist, because findUnknownInstances is
// called before startMachines. However, if the first machine
// had started to do work before being replaced, we may
// encounter surprising problems.
return err
}
// populate the local cache
p.instances[m.Id()] = inst
p.machines[inst.Id()] = m.Id()
log.Noticef("worker/provisioner: started machine %s as instance %s", m, inst.Id())
return nil
}
示例5: addLogging
func (cfg *MachineConfig) addLogging(c *cloudinit.Config) error {
var configRenderer syslog.SyslogConfigRenderer
if cfg.StateServer {
configRenderer = syslog.NewAccumulateConfig(
state.MachineTag(cfg.MachineId))
} else {
configRenderer = syslog.NewForwardConfig(
state.MachineTag(cfg.MachineId), cfg.stateHostAddrs())
}
content, err := configRenderer.Render()
if err != nil {
return err
}
addScripts(c,
fmt.Sprintf("cat > /etc/rsyslog.d/25-juju.conf << 'EOF'\n%sEOF\n", string(content)),
)
c.AddRunCmd("restart rsyslog")
return nil
}
示例6: TestMachineIdFromTag
func (s *MachineSuite) TestMachineIdFromTag(c *C) {
c.Assert(state.MachineIdFromTag("machine-10"), Equals, "10")
// Check a container id.
c.Assert(state.MachineIdFromTag("machine-10-lxc-1"), Equals, "10/lxc/1")
// Check reversability.
nested := "2/kvm/0/lxc/3"
c.Assert(state.MachineIdFromTag(state.MachineTag(nested)), Equals, nested)
// Try with an invalid tag format.
c.Assert(state.MachineIdFromTag("foo"), Equals, "")
}
示例7: makeMachineConfig
// makeMachineConfig produces a valid cloudinit machine config.
func makeMachineConfig(c *gc.C) *cloudinit.MachineConfig {
dir := c.MkDir()
machineID := "0"
return &cloudinit.MachineConfig{
MachineId: machineID,
MachineNonce: "gxshasqlnng",
DataDir: dir,
Tools: &tools.Tools{URL: "file://" + dir},
StateInfo: &state.Info{
CACert: []byte(testing.CACert),
Addrs: []string{"127.0.0.1:123"},
Tag: state.MachineTag(machineID),
},
APIInfo: &api.Info{
CACert: []byte(testing.CACert),
Addrs: []string{"127.0.0.1:123"},
Tag: state.MachineTag(machineID),
},
ProviderType: "azure",
}
}
示例8: checkStartInstanceCustom
func (s *CommonProvisionerSuite) checkStartInstanceCustom(c *C, m *state.Machine, secret string, cons constraints.Value) (inst instance.Instance) {
s.State.StartSync()
for {
select {
case o := <-s.op:
switch o := o.(type) {
case dummy.OpStartInstance:
inst = o.Instance
s.waitInstanceId(c, m, inst.Id())
// Check the instance was started with the expected params.
c.Assert(o.MachineId, Equals, m.Id())
nonceParts := strings.SplitN(o.MachineNonce, ":", 2)
c.Assert(nonceParts, HasLen, 2)
c.Assert(nonceParts[0], Equals, state.MachineTag("0"))
c.Assert(nonceParts[1], checkers.Satisfies, utils.IsValidUUIDString)
c.Assert(o.Secret, Equals, secret)
c.Assert(o.Constraints, DeepEquals, cons)
// Check we can connect to the state with
// the machine's entity name and password.
info := s.StateInfo(c)
info.Tag = m.Tag()
c.Assert(o.Info.Password, Not(HasLen), 0)
info.Password = o.Info.Password
c.Assert(o.Info, DeepEquals, info)
// Check we can connect to the state with
// the machine's entity name and password.
st, err := state.Open(o.Info, state.DefaultDialOpts())
c.Assert(err, IsNil)
// All provisioned machines in this test suite have their hardware characteristics
// attributes set to the same values as the constraints due to the dummy environment being used.
hc, err := m.HardwareCharacteristics()
c.Assert(err, IsNil)
c.Assert(*hc, DeepEquals, instance.HardwareCharacteristics{
Arch: cons.Arch,
Mem: cons.Mem,
CpuCores: cons.CpuCores,
CpuPower: cons.CpuPower,
})
st.Close()
return
default:
c.Logf("ignoring unexpected operation %#v", o)
}
case <-time.After(2 * time.Second):
c.Fatalf("provisioner did not start an instance")
return
}
}
return
}
示例9: setupLocalMachineAgent
func (env *localEnviron) setupLocalMachineAgent(cons constraints.Value) error {
dataDir := env.config.rootDir()
toolList, err := environs.FindBootstrapTools(env, cons)
if err != nil {
return err
}
// ensure we have at least one valid tools
if len(toolList) == 0 {
return fmt.Errorf("No bootstrap tools found")
}
// unpack the first tools into the agent dir.
agentTools := toolList[0]
logger.Debugf("tools: %#v", agentTools)
// brutally abuse our knowledge of storage to directly open the file
toolsUrl, err := url.Parse(agentTools.URL)
toolsLocation := filepath.Join(env.config.storageDir(), toolsUrl.Path)
logger.Infof("tools location: %v", toolsLocation)
toolsFile, err := os.Open(toolsLocation)
defer toolsFile.Close()
// Again, brutally abuse our knowledge here.
// The tools that FindBootstrapTools has returned us are based on the
// default series in the config. However we are running potentially on a
// different series. When the machine agent is started, it will be
// looking based on the current series, so we need to override the series
// returned in the tools to be the current series.
agentTools.Binary.Series = version.CurrentSeries()
err = tools.UnpackTools(dataDir, agentTools, toolsFile)
machineId := "0" // Always machine 0
tag := state.MachineTag(machineId)
toolsDir := tools.SharedToolsDir(dataDir, agentTools.Binary)
logDir := env.config.logDir()
logConfig := "--debug" // TODO(thumper): specify loggo config
agent := upstart.MachineAgentUpstartService(
env.machineAgentServiceName(),
toolsDir, dataDir, logDir, tag, machineId, logConfig, env.config.Type())
agent.Env["USER"] = env.config.user
agent.Env["HOME"] = os.Getenv("HOME")
agent.Env["JUJU_STORAGE_DIR"] = env.config.storageDir()
agent.Env["JUJU_STORAGE_ADDR"] = env.config.storageAddr()
agent.Env["JUJU_SHARED_STORAGE_DIR"] = env.config.sharedStorageDir()
agent.Env["JUJU_SHARED_STORAGE_ADDR"] = env.config.sharedStorageAddr()
agent.InitDir = upstartScriptLocation
logger.Infof("installing service %s to %s", env.machineAgentServiceName(), agent.InitDir)
if err := agent.Install(); err != nil {
logger.Errorf("could not install machine agent service: %v", err)
return err
}
return nil
}
示例10: startMachine
func (task *provisionerTask) startMachine(machine *state.Machine) error {
stateInfo, apiInfo, err := task.auth.SetupAuthentication(machine)
if err != nil {
logger.Errorf("failed to setup authentication: %v", err)
return err
}
cons, err := machine.Constraints()
if err != nil {
return err
}
// Generate a unique nonce for the new instance.
uuid, err := utils.NewUUID()
if err != nil {
return err
}
// Generated nonce has the format: "machine-#:UUID". The first
// part is a badge, specifying the tag of the machine the provisioner
// is running on, while the second part is a random UUID.
nonce := fmt.Sprintf("%s:%s", state.MachineTag(task.machineId), uuid.String())
inst, metadata, err := task.broker.StartInstance(machine.Id(), nonce, machine.Series(), cons, stateInfo, apiInfo)
if err != nil {
// Set the state to error, so the machine will be skipped next
// time until the error is resolved, but don't return an
// error; just keep going with the other machines.
logger.Errorf("cannot start instance for machine %q: %v", machine, err)
if err1 := machine.SetStatus(params.StatusError, err.Error()); err1 != nil {
// Something is wrong with this machine, better report it back.
logger.Errorf("cannot set error status for machine %q: %v", machine, err1)
return err1
}
return nil
}
if err := machine.SetProvisioned(inst.Id(), nonce, metadata); err != nil {
logger.Errorf("cannot register instance for machine %v: %v", machine, err)
// The machine is started, but we can't record the mapping in
// state. It'll keep running while we fail out and restart,
// but will then be detected by findUnknownInstances and
// killed again.
//
// TODO(dimitern) Stop the instance right away here.
//
// Multiple instantiations of a given machine (with the same
// machine ID) cannot coexist, because findUnknownInstances is
// called before startMachines. However, if the first machine
// had started to do work before being replaced, we may
// encounter surprising problems.
return err
}
logger.Infof("started machine %s as instance %s with hardware %q", machine, inst.Id(), metadata)
return nil
}
示例11: primeAgent
// primeAgent adds a new Machine to run the given jobs, and sets up the
// machine agent's directory. It returns the new machine, the
// agent's configuration and the tools currently running.
func (s *MachineSuite) primeAgent(c *C, jobs ...state.MachineJob) (*state.Machine, *agent.Conf, *state.Tools) {
m, err := s.State.InjectMachine("series", constraints.Value{}, "ardbeg-0", instance.HardwareCharacteristics{}, jobs...)
c.Assert(err, IsNil)
err = m.SetMongoPassword("machine-password")
c.Assert(err, IsNil)
err = m.SetPassword("machine-password")
c.Assert(err, IsNil)
conf, tools := s.agentSuite.primeAgent(c, state.MachineTag(m.Id()), "machine-password")
conf.MachineNonce = state.BootstrapNonce
conf.APIInfo.Nonce = conf.MachineNonce
err = conf.Write()
c.Assert(err, IsNil)
return m, conf, tools
}
示例12: SetUpTest
func (s *commonSuite) SetUpTest(c *C) {
s.JujuConnSuite.SetUpTest(c)
var err error
s.machine0, err = s.State.AddMachine("series", state.JobManageEnviron, state.JobManageState)
c.Assert(err, IsNil)
s.machine1, err = s.State.AddMachine("series", state.JobHostUnits)
c.Assert(err, IsNil)
// Create a FakeAuthorizer so we can check permissions,
// set up assuming machine 1 has logged in.
s.authorizer = apiservertesting.FakeAuthorizer{
Tag: state.MachineTag(s.machine1.Id()),
LoggedIn: true,
Manager: false,
MachineAgent: true,
}
}
示例13: checkStartInstanceCustom
func (s *ProvisionerSuite) checkStartInstanceCustom(c *C, m *state.Machine, secret string, cons constraints.Value) {
s.State.StartSync()
for {
select {
case o := <-s.op:
switch o := o.(type) {
case dummy.OpStartInstance:
s.waitInstanceId(c, m, o.Instance.Id())
// Check the instance was started with the expected params.
c.Assert(o.MachineId, Equals, m.Id())
nonceParts := strings.SplitN(o.MachineNonce, ":", 2)
c.Assert(nonceParts, HasLen, 2)
c.Assert(nonceParts[0], Equals, state.MachineTag("0"))
c.Assert(utils.IsValidUUIDString(nonceParts[1]), Equals, true)
c.Assert(o.Secret, Equals, secret)
c.Assert(o.Constraints, DeepEquals, cons)
// Check we can connect to the state with
// the machine's entity name and password.
info := s.StateInfo(c)
info.Tag = m.Tag()
c.Assert(o.Info.Password, Not(HasLen), 0)
info.Password = o.Info.Password
c.Assert(o.Info, DeepEquals, info)
// Check we can connect to the state with
// the machine's entity name and password.
st, err := state.Open(o.Info, state.DefaultDialOpts())
c.Assert(err, IsNil)
st.Close()
return
default:
c.Logf("ignoring unexpected operation %#v", o)
}
case <-time.After(2 * time.Second):
c.Fatalf("provisioner did not start an instance")
return
}
}
}
示例14: verifyConfig
func verifyConfig(cfg *MachineConfig) (err error) {
defer utils.ErrorContextf(&err, "invalid machine configuration")
if !state.IsMachineId(cfg.MachineId) {
return fmt.Errorf("invalid machine id")
}
if cfg.DataDir == "" {
return fmt.Errorf("missing var directory")
}
if cfg.Tools == nil {
return fmt.Errorf("missing tools")
}
if cfg.Tools.URL == "" {
return fmt.Errorf("missing tools URL")
}
if cfg.StateInfo == nil {
return fmt.Errorf("missing state info")
}
if len(cfg.StateInfo.CACert) == 0 {
return fmt.Errorf("missing CA certificate")
}
if cfg.APIInfo == nil {
return fmt.Errorf("missing API info")
}
if len(cfg.APIInfo.CACert) == 0 {
return fmt.Errorf("missing API CA certificate")
}
if cfg.ProviderType == "" {
return fmt.Errorf("missing provider type")
}
if cfg.StateServer {
if cfg.Config == nil {
return fmt.Errorf("missing environment configuration")
}
if cfg.StateInfo.Tag != "" {
return fmt.Errorf("entity tag must be blank when starting a state server")
}
if cfg.APIInfo.Tag != "" {
return fmt.Errorf("entity tag must be blank when starting a state server")
}
if len(cfg.StateServerCert) == 0 {
return fmt.Errorf("missing state server certificate")
}
if len(cfg.StateServerKey) == 0 {
return fmt.Errorf("missing state server private key")
}
if cfg.StatePort == 0 {
return fmt.Errorf("missing state port")
}
if cfg.APIPort == 0 {
return fmt.Errorf("missing API port")
}
} else {
if len(cfg.StateInfo.Addrs) == 0 {
return fmt.Errorf("missing state hosts")
}
if cfg.StateInfo.Tag != state.MachineTag(cfg.MachineId) {
return fmt.Errorf("entity tag must match started machine")
}
if len(cfg.APIInfo.Addrs) == 0 {
return fmt.Errorf("missing API hosts")
}
if cfg.APIInfo.Tag != state.MachineTag(cfg.MachineId) {
return fmt.Errorf("entity tag must match started machine")
}
}
if cfg.MachineNonce == "" {
return fmt.Errorf("missing machine nonce")
}
return nil
}
示例15: Tag
func (a *MachineAgent) Tag() string {
return state.MachineTag(a.MachineId)
}