Exemplo n.º 1
0
func (ctx *ClusterContext) createPlainDockerProxy(node string) (*dcontainer.DockerProxy, error) {
	tlsConfig, err := ctx.mProxy.ConfigNode(node)
	if err != nil {
		return nil, err
	}
	return dcontainer.NewDockerProxy(tlsConfig)
}
Exemplo n.º 2
0
// init the exists machine, container, and seq
func (ctx *ClusterContext) initContext() {
	fmt.Printf("Parsing port binding in cluster description.\n")
	ctx.parsePortBindings()

	fmt.Printf("Create a new machine proxy. cluster by:%s, driver:%s, discovery: %s, master: %s, driver options:%s\n", ctx.clusterDesc.ClusterBy, ctx.clusterDesc.Driver, ctx.clusterDesc.Discovery, ctx.clusterDesc.MasterNode, strings.Join(ctx.clusterDesc.DriverOptions, " "))
	machineProxy := dmachine.NewMachineClusterProxy("dockerf machine", ctx.clusterDesc.ClusterBy, ctx.clusterDesc.Driver, ctx.clusterDesc.Discovery, ctx.clusterDesc.MasterNode, ctx.clusterDesc.DriverOptions)
	ctx.mProxy = machineProxy

	fmt.Println("Loading the cluster machine info...")
	mis, err := ctx.mProxy.List()
	if err != nil {
		panic("Init cluster context error, cannot list machine infos:" + err.Error())
	}
	ctx.machineInfos = mis

	fmt.Printf("Starting machine master\n")
	if err := ctx.startMaster(); err != nil {
		panic("Start cluster master failed: " + err.Error())
	}

	tlsConfig, err := machineProxy.Config()
	if err != nil {
		panic(fmt.Sprintf("Failed to load master machine node tls config. err:%s\n", err.Error()))
	}

	containerProxy, err := dcontainer.NewDockerProxy(tlsConfig)
	if err != nil {
		panic("Failed to create docker proxy:%s\n" + err.Error())
	}
	ctx.cProxy = containerProxy

	fmt.Printf("Init the consule cluster.\n")
	if err := ctx.startConsulCluster(); err != nil {
		panic("Start consul cluster failed: " + err.Error())
	}

	fmt.Println("Init the named machine sequence...")
	ctx.initMachineSequence(mis)

	fmt.Printf("Loading all filtered container infos")
	if err := ctx.loadContainers(); err != nil {
		panic("Init cluster context error, cannot list container infos:" + err.Error())
	}

	fmt.Printf("Init container sequences.\n")
	if err := ctx.initContainerSequences(); err != nil {
		panic("Init cluster context error, cannot init container seqs:" + err.Error())
	}

	fmt.Println("cluster context inited successfully")
}
Exemplo n.º 3
0
func (ctx *ClusterContext) initSlave(node string, md dcluster.MachineDescription) error {
	fmt.Printf("Init the machine infrastructure envment: '%s'\n", node)
	// exec command on this machine
	command := strings.TrimSpace(md.Init)
	if command != "" {
		if err := ctx.mProxy.ExecCmd(node, command); err != nil {
			fmt.Printf("Failed to exec command(%s) on '%s'\n", command, node)
		}
	}

	if md.Consul {
		tlsConfig, err := ctx.mProxy.ConfigNode(node)
		if err != nil {
			return errors.New(fmt.Sprintf("Failed to load config for node '%s'", node))
		}
		ip, err := ctx.mProxy.IP(node)
		if err != nil {
			fmt.Printf("Failed to load agent ip:'%s', error:%s\n", node, err.Error())
			return err
		}
		proxy, err := dcontainer.NewDockerProxy(tlsConfig)
		if err != nil {
			fmt.Printf("Failed to new docker proxy. machine:%s, tlsConfig:%s\n", node, tlsConfig)
		}
		fmt.Printf("Run consul agent on '%s(%s)'\n", node, ip)
		if cid, err := ctx.runConsulAgent(proxy, ctx.clusterDesc.ConsulCluster.Agent, node, ip); err != nil {
			panic(fmt.Sprintf("Run consul agent on '%s' failed. err:%s\n", node, err.Error()))
		} else {
			fmt.Printf("Consul agent running successfully. id:%s\n", cid)
		}

		fmt.Printf("Run consul registrator on '%s(%s)'\n", node, ip)
		if cid, err := ctx.runConsulRegistrator(proxy, ctx.clusterDesc.ConsulCluster.Registrator, node, ip); err != nil {
			panic(fmt.Sprintf("Failed to run consul registor container on '%s'. err:%s", node, err.Error()))
		} else {
			fmt.Printf("Consul registrator running successfully. id:%s\n", cid)
		}
	}

	fmt.Sprintf("Slave machine init successfully. node:%s\n", node)
	return nil
}
Exemplo n.º 4
0
// init the exists machine, container, and seq
func (ctx *ClusterContext) initContext() {
	// log.Info("Parsing port binding in cluster description.")
	// ctx.parsePortBindings()

	log.Info("Init container description")
	err := ctx.initContainerDescription()
	if err != nil {
		panic("Fail to init container description, err: " + err.Error())
	}

	supportedDrivers := strings.Join(ctx.clusterDesc.Machine.Cloud.SurportedDrivers(), ",")
	log.Infof("Create a new machine proxy. cluster by:%s, supported drivers:%s, discovery: %s, master: %s\n", ctx.clusterDesc.ClusterBy, supportedDrivers, ctx.clusterDesc.Discovery, ctx.clusterDesc.Master)
	machineProxy := dmachine.NewMachineClusterProxy("dockerf machine", ctx.clusterDesc.ClusterBy, ctx.clusterDesc.Discovery, ctx.clusterDesc.Master, ctx.clusterDesc.Machine.Cloud)
	ctx.mProxy = machineProxy

	log.Info("Loading the cluster machine info...")
	mis, err := ctx.mProxy.List()
	if err != nil {
		panic("Init cluster context error, cannot list machine infos:" + err.Error())
	}
	ctx.machineInfos = mis

	log.Info("Init the consule cluster.")
	if err := ctx.startConsulCluster(); err != nil {
		panic("Start consul cluster failed: " + err.Error())
	}

	log.Info("Starting machine master")
	if err := ctx.startMaster(); err != nil {
		panic("Start cluster master failed: " + err.Error())
	}

	tlsConfig, err := machineProxy.Config()
	if err != nil {
		panic(fmt.Sprintf("Failed to load master machine node tls config. err:%s\n", err.Error()))
	}

	containerProxy, err := dcontainer.NewDockerProxy(tlsConfig)
	if err != nil {
		panic("Failed to create docker proxy:%s\n" + err.Error())
	}
	ctx.cProxy = containerProxy

	ctx.containerFilterChain = dcontainerfilter.NewFilterChain(ctx.cProxy)

	log.Info("Init the named machine sequence...")
	ctx.initMachineSequence(mis)

	log.Info("Loading all filtered container infos... ")
	if err := ctx.loadContainers(); err != nil {
		panic("Init cluster context error, cannot list container infos:" + err.Error())
	}

	log.Info("Init container sequences.")
	if err := ctx.initContainerSequences(); err != nil {
		panic("Init cluster context error, cannot init container seqs:" + err.Error())
	}

	log.Infof("ensure machine compacity.")
	if err := ctx.ensureMachineCapacity(); err != nil {
		panic("Ensure machine capacity error:%s" + err.Error())
	}

	log.Info("Init service discovery.")
	if err := ctx.initServiceDiscovery(); err != nil {
		panic("Init service discovery failed:" + err.Error())
	}

	log.Info("cluster context inited successfully")
}