//ValidEntity validates Host fields func (h *Host) ValidEntity() error { glog.V(4).Info("Validating host") //if err := validation.ValidHostID(entity.ID); err != nil { // return fmt.Errorf("invalid hostid:'%s' for host Name:'%s' IP:%s", entity.ID, entity.Name, entity.IPAddr) //} trimmedID := strings.TrimSpace(h.ID) violations := validation.NewValidationError() violations.Add(validation.NotEmpty("Host.ID", h.ID)) violations.Add(validation.ValidHostID(h.ID)) violations.Add(validation.StringsEqual(h.ID, trimmedID, "leading and trailing spaces not allowed for host id")) violations.Add(validation.ValidPort(h.RPCPort)) violations.Add(validation.NotEmpty("Host.PoolID", h.PoolID)) violations.Add(validation.IsIP(h.IPAddr)) //TODO: what should we be validating here? It doesn't seem to work for glog.V(4).Infof("Validating IPAddr %v for host %s", h.IPAddr, h.ID) ipAddr, err := net.ResolveIPAddr("ip4", h.IPAddr) if err != nil { glog.Errorf("Could not resolve: %s to an ip4 address: %v", h.IPAddr, err) violations.Add(err) } else if ipAddr.IP.IsLoopback() { glog.Errorf("Can not use %s as host address because it is a loopback address", h.IPAddr) violations.Add(errors.New("host ip can not be a loopback address")) } if len(violations.Errors) > 0 { return violations } return nil }
func (d *daemon) startAgent() error { muxListener, err := createMuxListener() if err != nil { return err } mux, err := proxy.NewTCPMux(muxListener) if err != nil { return err } agentIP := options.OutboundIP if agentIP == "" { var err error agentIP, err = utils.GetIPAddress() if err != nil { glog.Fatalf("Failed to acquire ip address: %s", err) } } rpcPort := "0" parts := strings.Split(options.Listen, ":") if len(parts) > 1 { rpcPort = parts[1] } thisHost, err := host.Build(agentIP, rpcPort, "unknown") if err != nil { panic(err) } myHostID, err := utils.HostID() if err != nil { return fmt.Errorf("HostID failed: %v", err) } else if err := validation.ValidHostID(myHostID); err != nil { glog.Errorf("invalid hostid: %s", myHostID) } go func() { var poolID string for { poolID = func() string { glog.Infof("Trying to discover my pool...") var myHost *host.Host masterClient, err := master.NewClient(d.servicedEndpoint) if err != nil { glog.Errorf("master.NewClient failed (endpoint %+v) : %v", d.servicedEndpoint, err) return "" } defer masterClient.Close() myHost, err = masterClient.GetHost(myHostID) if err != nil { glog.Warningf("masterClient.GetHost %v failed: %v (has this host been added?)", myHostID, err) return "" } poolID = myHost.PoolID glog.Infof(" My PoolID: %v", poolID) //send updated host info updatedHost, err := host.UpdateHostInfo(*myHost) if err != nil { glog.Infof("Could not send updated host information: %v", err) return poolID } err = masterClient.UpdateHost(updatedHost) if err != nil { glog.Warningf("Could not update host information: %v", err) return poolID } glog.V(2).Infof("Sent updated host info %#v", updatedHost) return poolID }() if poolID != "" { break } select { case <-d.shutdown: return case <-time.After(5 * time.Second): continue } } thisHost.PoolID = poolID basePoolPath := "/pools/" + poolID dsn := coordzk.NewDSN(options.Zookeepers, time.Second*15).String() glog.Infof("zookeeper dsn: %s", dsn) zClient, err := coordclient.New("zookeeper", dsn, basePoolPath, nil) if err != nil { glog.Errorf("failed create a new coordclient: %v", err) } zzk.InitializeLocalClient(zClient) poolBasedConn, err := zzk.GetLocalConnection(zzk.GeneratePoolPath(poolID)) if err != nil { glog.Errorf("Error in getting a connection based on pool %v: %v", poolID, err) } if options.NFSClient != "0" { nfsClient, err := storage.NewClient(thisHost, path.Join(options.VarPath, "volumes")) if err != nil { glog.Fatalf("could not create an NFS client: %s", err) } go func() { <-d.shutdown glog.Infof("shutting down storage client") nfsClient.Close() }() //loop and log waiting for Storage Leader nfsDone := make(chan struct{}) go func() { defer close(nfsDone) nfsClient.Wait() }() //wait indefinitely(?) for storage to work before starting glog.Info("Waiting for Storage Leader") nfsUp := false for !nfsUp { select { case <-nfsDone: nfsUp = true glog.Info("Found Storage Leader") break case <-time.After(time.Second * 30): glog.Info("Waiting for Storage Leader, will not be available for running services. ") continue } } } else { glog.Info("NFS Client disabled") } agentOptions := node.AgentOptions{ PoolID: thisHost.PoolID, Master: options.Endpoint, UIPort: options.UIPort, RPCPort: options.RPCPort, DockerDNS: options.DockerDNS, VarPath: options.VarPath, Mount: options.Mount, FSType: options.FSType, Zookeepers: options.Zookeepers, Mux: mux, UseTLS: options.TLS, DockerRegistry: dockerRegistry, MaxContainerAge: time.Duration(int(time.Second) * options.MaxContainerAge), VirtualAddressSubnet: options.VirtualAddressSubnet, } // creates a zClient that is not pool based! hostAgent, err := node.NewHostAgent(agentOptions) d.hostAgent = hostAgent d.waitGroup.Add(1) go func() { hostAgent.Start(d.shutdown) glog.Info("Host Agent has shutdown") d.waitGroup.Done() }() // register the API glog.V(0).Infoln("registering ControlPlaneAgent service") if err = d.rpcServer.RegisterName("ControlPlaneAgent", hostAgent); err != nil { glog.Fatalf("could not register ControlPlaneAgent RPC server: %v", err) } if options.ReportStats { statsdest := fmt.Sprintf("http://%s/api/metrics/store", options.HostStats) statsduration := time.Duration(options.StatsPeriod) * time.Second glog.V(1).Infoln("Staring container statistics reporter") statsReporter, err := stats.NewStatsReporter(statsdest, statsduration, poolBasedConn) if err != nil { glog.Errorf("Error kicking off stats reporter %v", err) } else { go func() { defer statsReporter.Close() <-d.shutdown }() } } }() glog.Infof("agent start staticips: %v [%d]", d.staticIPs, len(d.staticIPs)) if err = d.rpcServer.RegisterName("Agent", agent.NewServer(d.staticIPs)); err != nil { glog.Fatalf("could not register Agent RPC server: %v", err) } if err != nil { glog.Fatalf("Could not start ControlPlane agent: %v", err) } // TODO: Integrate this server into the rpc server, or something. // Currently its only use is for command execution. go func() { sio := shell.NewProcessExecutorServer(options.Endpoint, dockerRegistry) http.ListenAndServe(":50000", sio) }() return nil }
func (d *daemon) run() (err error) { if d.hostID, err = utils.HostID(); err != nil { glog.Fatalf("Could not get host ID: %s", err) } else if err := validation.ValidHostID(d.hostID); err != nil { glog.Errorf("invalid hostid: %s", d.hostID) } if currentDockerVersion, err := node.GetDockerVersion(); err != nil { glog.Fatalf("Could not get docker version: %s", err) } else if minDockerVersion.Compare(currentDockerVersion.Client) < 0 { glog.Fatalf("serviced requires docker >= %s", minDockerVersion) } if _, ok := volume.Registered(options.FSType); !ok { glog.Fatalf("no driver registered for %s", options.FSType) } d.startRPC() d.startDockerRegistryProxy() if options.Master { d.startISVCS() if err := d.startMaster(); err != nil { glog.Fatal(err) } } if options.Agent { if err := d.startAgent(); err != nil { glog.Fatal(err) } } signalC := make(chan os.Signal, 10) signal.Notify(signalC, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) sig := <-signalC glog.Info("Shutting down due to interrupt") close(d.shutdown) done := make(chan struct{}) go func() { defer close(done) glog.Info("Stopping sub-processes") d.waitGroup.Wait() glog.Info("Sub-processes have stopped") }() select { case <-done: defer glog.Info("Shutdown") case <-time.After(60 * time.Second): defer glog.Infof("Timeout waiting for shutdown") } zzk.ShutdownConnections() if options.Master { switch sig { case syscall.SIGHUP: glog.Infof("Not shutting down isvcs") command := os.Args glog.Infof("Reloading by calling syscall.exec for command: %+v\n", command) syscall.Exec(command[0], command[0:], os.Environ()) default: d.stopISVCS() } } return nil }