Esempio n. 1
0
func ovs_connect() (*libovsdb.OvsdbClient, error) {
	quit = make(chan bool)
	update = make(chan *libovsdb.TableUpdates)
	cache = make(map[string]map[string]libovsdb.Row)

	// By default libovsdb connects to 127.0.0.0:6400.
	var ovs *libovsdb.OvsdbClient
	var err error
	for {
		ovs, err = libovsdb.Connect("", 0)
		if err != nil {
			log.Errorf("Error(%s) connecting to OVS. Retrying...", err.Error())
			time.Sleep(time.Second * 2)
			continue
		}
		break
	}
	var notifier Notifier
	ovs.Register(notifier)

	initial, _ := ovs.MonitorAll("Open_vSwitch", "")
	populateCache(*initial)
	go monitorDockerBridge(ovs)
	for getRootUuid() == "" {
		time.Sleep(time.Second * 1)
	}
	log.Debug("Connected to OVS...")
	return ovs, nil
}
Esempio n. 2
0
func LeaveDatastore() error {
	if err := ecc.Leave(); err != nil {
		log.Error(err)
		return err
	}
	if err := os.RemoveAll(dataDir); err != nil {
		log.Errorf("Error deleting data directory %s", err)
		return err
	}
	return nil
}
Esempio n. 3
0
func clusterJoinRPC(d *Daemon, joinAddress string) error {
	bindInterface, err := GetIfaceForRoute(joinAddress)
	if err != nil {
		return err
	}
	if d.clusterListener != bindInterface {
		log.Debug("Cluster is already bound on another interface. Leaving...")
		LeaveDatastore()
		time.Sleep(time.Second * 10)
		log.Debugf("Setting new cluster listener to %s", bindInterface)
		d.bootstrapNode = false
		d.clusterListener = bindInterface
		InitDatastore(d.clusterListener, d.bootstrapNode)
	}
	if err = JoinDatastore(joinAddress); err != nil {
		log.Errorf("Could not join cluster %s. %s", joinAddress, err.Error())
	}
	return nil
}
Esempio n. 4
0
func ClusterRPCHandler(d *Daemon) {
	for {
		context := <-d.bindChan
		switch context.Action {
		case ClusterBind:
			bindInterface := context.Param
			err := clusterBindRPC(d, bindInterface)
			if err != nil {
				break
			}
		case ClusterJoin:
			joinAddress := context.Param
			err := clusterJoinRPC(d, joinAddress)
			if err != nil {
				break
			}
		case ClusterLeave:
			if err := LeaveDatastore(); err != nil {
				log.Errorf("Error leaving cluster. %s", err.Error())
				break
			}
		}
	}
}
Esempio n. 5
0
func (d *Daemon) Run(ctx *cli.Context) {
	if isDebugEnabled(ctx) {
		log.SetLevel(log.DebugLevel)
	}
	d.bootstrapNode = isBootstrapNode(ctx)

	if err := os.Mkdir("/var/run/netns", 0777); err != nil {
		fmt.Println("mkdir /var/run/netns failed", err)
	}

	go ServeAPI(d)
	go func() {
		var bindInterface string
		if ctx.String("iface") != "auto" {
			bindInterface = ctx.String("iface")
		} else {
			intf := d.identifyInterfaceToBind()
			if intf != nil {
				bindInterface = intf.Name
			}
		}
		if bindInterface != "" {
			log.Printf("Binding to %s", bindInterface)
			d.clusterListener = bindInterface
		} else {
			log.Errorf("Unable to identify any Interface to Bind to. Going with Defaults")
		}
		InitDatastore(bindInterface, d.bootstrapNode)
		Bonjour(bindInterface)
		if !d.bootstrapNode {
			d.serialChan <- true
		}
	}()

	go ClusterRPCHandler(d)

	go func() {
		if !d.bootstrapNode {
			log.Printf("Non-Bootstrap node waiting on peer discovery")
			<-d.serialChan
			log.Printf("Non-Bootstrap node admitted into cluster")
		}
		err := CreateBridge()
		if err != nil {
			log.Error(err.Error)
		}
		d.populateConnections()
		_, err = CreateDefaultNetwork()
		if err != nil {
			log.Error(err.Error)
		}
	}()

	go ConnectionRPCHandler(d)

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		for _ = range c {
			os.Exit(0)
		}
	}()
	select {}
}