Exemple #1
0
func New(cfg config.Config) *Server {
	m := machine.New(cfg.BootId, cfg.PublicIP, cfg.Metadata())
	m.RefreshState()

	regClient := etcd.NewClient(cfg.EtcdServers)
	regClient.SetConsistency(etcd.STRONG_CONSISTENCY)
	r := registry.New(regClient)

	eb := event.NewEventBus()
	eb.Listen()

	eventClient := etcd.NewClient(cfg.EtcdServers)
	eventClient.SetConsistency(etcd.STRONG_CONSISTENCY)
	es := registry.NewEventStream(eventClient)

	a, err := agent.New(r, eb, m, cfg.AgentTTL, cfg.UnitPrefix)
	if err != nil {
		//TODO: return this as an error object rather than panicking
		panic(err)
	}

	e := engine.New(r, eb, m)

	return &Server{a, e, m, r, eb, es}
}
Exemple #2
0
func newTestMachine(region string) *machine.Machine {
	metadata := map[string]string{
		"region": region,
	}
	state := machine.MachineState{Metadata: metadata}
	return machine.New(state)
}
Exemple #3
0
func New(cfg config.Config) *Server {
	m := machine.New(cfg.BootId, cfg.PublicIP, cfg.Metadata())
	m.RefreshState()

	regClient := etcd.NewClient(cfg.EtcdServers)
	regClient.SetConsistency(etcd.STRONG_CONSISTENCY)
	r := registry.New(regClient)

	eb := event.NewEventBus()
	eb.Listen()

	eventClient := etcd.NewClient(cfg.EtcdServers)
	eventClient.SetConsistency(etcd.STRONG_CONSISTENCY)
	es := registry.NewEventStream(eventClient, r)

	var verifier *sign.SignatureVerifier
	if cfg.VerifyUnits {
		var err error
		verifier, err = sign.NewSignatureVerifierFromAuthorizedKeysFile(cfg.AuthorizedKeysFile)
		if err != nil {
			log.Errorln("Failed to get any key from authorized key file in verify_units mode:", err)
			verifier = sign.NewSignatureVerifier()
		}
	}

	a, err := agent.New(r, eb, m, cfg.AgentTTL, cfg.UnitPrefix, verifier)
	if err != nil {
		//TODO: return this as an error object rather than panicking
		panic(err)
	}

	e := engine.New(r, eb, m)

	return &Server{a, e, m, r, eb, es}
}
Exemple #4
0
func TestEventBusNoDispatch(t *testing.T) {
	evchan := make(chan Event)

	bus := NewEventBus()
	bus.AddListener("test", machine.New("X", "", make(map[string]string, 0)), &TestListener{evchan})
	bus.Listen()
	defer bus.Stop()

	go func() {
		ev := Event{"EventTypeTwo", "payload", machine.New("Y", "", make(map[string]string, 0))}
		bus.Channel <- &ev

		ev = Event{"EventTypeOne", "payload", machine.New("Y", "", make(map[string]string, 0))}
		bus.Channel <- &ev
	}()

	recv := <-evchan
	if recv.Type != "EventTypeOne" {
		t.Fatalf("handler received unexpected event")
	}
}
Exemple #5
0
func TestAbleToRunConditionMachineIDMismatch(t *testing.T) {
	u := unit.NewUnit(`[X-Fleet]
X-ConditionMachineID=XYZ
`)
	job := job.NewJob("example.service", *u)

	mach := machine.New(machine.MachineState{ID: "123"})
	agent := Agent{machine: mach, state: NewState()}
	if agent.AbleToRun(job) {
		t.Fatalf("Agent should not be able to run job")
	}
}
Exemple #6
0
func TestAbleToRunConditionMachineBootIDMismatch(t *testing.T) {
	uf := unit.NewSystemdUnitFile(`[X-Fleet]
X-ConditionMachineBootID=XYZ
`)
	payload := job.NewJobPayload("example.service", *uf)
	job := job.NewJob("example.service", make(map[string][]string, 0), payload, nil)

	mach := machine.New("123", "", make(map[string]string, 0))
	agent := Agent{machine: mach, state: NewState()}
	if agent.AbleToRun(job) {
		t.Fatalf("Agent should not be able to run job")
	}
}
Exemple #7
0
func TestEventBus(t *testing.T) {
	evchan := make(chan Event)

	bus := NewEventBus()
	bus.AddListener("test", machine.New("X", "", make(map[string]string, 0)), &TestListener{evchan})
	bus.Listen()
	defer bus.Stop()

	ev := Event{"EventTypeOne", "payload", machine.New("Y", "", make(map[string]string, 0))}
	bus.Channel <- &ev

	select {
	case <-time.After(time.Second):
		t.Fatalf("Failed to dispatch event within a second")
	case recv := <-evchan:
		if recv.Payload.(string) != "payload" {
			t.Error("event payload is incorrect")
		}
		if recv.Context.(*machine.Machine).State().BootID != "Y" {
			t.Error("event context is incorrect")
		}
	}
}
Exemple #8
0
func newMachineFromConfig(cfg config.Config) (*machine.Machine, error) {
	state := machine.MachineState{
		PublicIP: cfg.PublicIP,
		Metadata: cfg.Metadata(),
		Version:  version.Version,
	}

	mach := machine.New(state)
	mach.RefreshState()

	if mach.State().ID == "" {
		return nil, errors.New("unable to determine local machine ID")
	}

	return mach, nil
}