func (r elbRouter) AddBackend(name string) error { var err error options := elb.CreateLoadBalancer{ Name: name, Listeners: []elb.Listener{ { InstancePort: 80, InstanceProtocol: "HTTP", LoadBalancerPort: 80, Protocol: "HTTP", }, }, } vpc, _ := config.GetBool("juju:elb-use-vpc") if vpc { options.Subnets, err = config.GetList("juju:elb-vpc-subnets") if err != nil { return err } options.SecurityGroups, err = config.GetList("juju:elb-vpc-secgroups") if err != nil { return err } options.Scheme = "internal" } else { options.AvailZones, err = config.GetList("juju:elb-avail-zones") if err != nil { return err } } _, err = r.elb().CreateLoadBalancer(&options) return router.Store(name, name) }
func FlagResolve(localAddr *string, serverAddr *string, username *string, password *string) { flag.Parse() if *helpFlag != false { //log.Log("info", "", nil) fmt.Println("Usage:...........!!!!!") os.Exit(0) } if *versionFlag != false { err := config.ReadConfigFile("../client/config/version.yml") if err != nil { fmt.Println(err) os.Exit(0) } version, _ := config.GetString("version") update, _ := config.GetList("update") instruction, _ := config.GetString("instruction") fmt.Printf("CaeserClient version: %s\n", version) fmt.Printf("New speciality contrast to old version: \n") for k, v := range update { fmt.Printf("%d-- %s\n", k+1, v) } fmt.Printf(" %s\n", instruction) os.Exit(0) } if *localFlag != "" { *localAddr = *localFlag log.Log("info", "you set a new addres", log.Fields{"address": *localFlag}) //fmt.Println("--Notice: you have set a new address", *localAddr) } else { //fmt.Println("--Didn't set the start port. Caesar will start at default port.") log.Log("info", "Didn't set the start port. Caesar will start at default port.", log.Fields{"default address": *localAddr}) } if *serverFlag != "" { fp, err := regexp.MatchString(ipPattern, *serverFlag) handleError(err) if !fp { //fmt.Printf("\"%s\" is not a valid address, please check it and try again!\n", *serverFlag) warnMsg := *serverFlag + "is not a valid address, please check it and try again!" log.Log("warn", warnMsg, nil) os.Exit(0) } *serverAddr = *serverFlag log.Log("info", "You have set a new server address", log.Fields{"new address": *serverAddr}) //fmt.Println("--Notice: you have set a new server address", *serverAddr) } else { log.Log("info", "Didn't set the server address.Caesar will connect the default address.", log.Fields{"new address": *serverAddr}) //fmt.Println("--Didn't set the server address. Caesar will connect the default address.") } if *userFlag != "" && *passwordFlag != "" { *username = *userFlag *password = *passwordFlag fmt.Println(*username, *password) } else { //fmt.Println("--Anonymous login, can do nothing! Please login with exgist user or register a new user.") log.Log("info", "Anonymous login, can do nothing! Please login with exgist user or register a new user.", nil) } }
func getDockerServers() []cluster.Node { servers, _ := config.GetList("docker:servers") nodes := []cluster.Node{} for _, server := range servers { node := cluster.Node{ Address: server, } nodes = append(nodes, node) } return nodes }
// LoadOptions TODO: doc func LoadOptions(configFile string) (Options, error) { var options Options err := config.ReadConfigFile(configFile) if err != nil { return options, err } brokers, err := config.GetList("kafka:brokers") if err != nil || len(brokers) == 0 { return options, errors.New("You must provide at least one kafka broker") } zookeepers, err := config.GetList("kafka:zookeeper") if err != nil || len(zookeepers) == 0 { return options, errors.New("You must provide at least one zookeeper") } consumerGroup, err := config.GetString("kafka:group") if err != nil { return options, errors.New("You must provide the consumer group") } kafkaTopics, err := config.GetList("kafka:topics") if err != nil || len(kafkaTopics) == 0 { return options, errors.New("You must provide at least one topic") } consumersCmd, err := config.GetList("consumers") if err != nil || len(consumersCmd) == 0 { return options, errors.New("You must provide at least one consumer command") } options = Options{ Brokers: brokers, Zookeepers: zookeepers, ConsumerGroup: consumerGroup, KafkaTopics: kafkaTopics, ConsumersCmd: consumersCmd, } return options, nil }
func (c *container) start(p *dockerProvisioner, app provision.App, isDeploy bool) error { port, err := getPort() if err != nil { return err } sharedBasedir, _ := config.GetString("docker:sharedfs:hostdir") sharedMount, _ := config.GetString("docker:sharedfs:mountpoint") sharedIsolation, _ := config.GetBool("docker:sharedfs:app-isolation") sharedSalt, _ := config.GetString("docker:sharedfs:salt") hostConfig := docker.HostConfig{ Memory: app.GetMemory(), MemorySwap: app.GetMemory() + app.GetSwap(), CPUShares: int64(app.GetCpuShare()), } if !isDeploy { hostConfig.RestartPolicy = docker.AlwaysRestart() hostConfig.PortBindings = map[docker.Port][]docker.PortBinding{ docker.Port(port + "/tcp"): {{HostIP: "", HostPort: ""}}, } hostConfig.LogConfig = docker.LogConfig{ Type: "syslog", Config: map[string]string{ "syslog-address": fmt.Sprintf("udp://localhost:%d", getBsSysLogPort()), }, } } hostConfig.SecurityOpt, _ = config.GetList("docker:security-opts") if sharedBasedir != "" && sharedMount != "" { if sharedIsolation { var appHostDir string if sharedSalt != "" { h := crypto.SHA1.New() io.WriteString(h, sharedSalt+c.AppName) appHostDir = fmt.Sprintf("%x", h.Sum(nil)) } else { appHostDir = c.AppName } hostConfig.Binds = append(hostConfig.Binds, fmt.Sprintf("%s/%s:%s:rw", sharedBasedir, appHostDir, sharedMount)) } else { hostConfig.Binds = append(hostConfig.Binds, fmt.Sprintf("%s:%s:rw", sharedBasedir, sharedMount)) } } err = p.getCluster().StartContainer(c.ID, &hostConfig) if err != nil { return err } initialStatus := provision.StatusStarting.String() if isDeploy { initialStatus = provision.StatusBuilding.String() } return c.setStatus(p, initialStatus, false) }
func (c *Container) Create(args *CreateArgs) error { port, err := getPort() if err != nil { log.Errorf("error on getting port for container %s - %s", c.AppName, port) return err } securityOpts, _ := config.GetList("docker:security-opts") var exposedPorts map[docker.Port]struct{} if !args.Deploy { exposedPorts = map[docker.Port]struct{}{ docker.Port(port + "/tcp"): {}, } } var user string if args.Building { user = c.user() } config := docker.Config{ Image: args.ImageID, Cmd: args.Commands, Entrypoint: []string{}, ExposedPorts: exposedPorts, AttachStdin: false, AttachStdout: false, AttachStderr: false, Memory: args.App.GetMemory(), MemorySwap: args.App.GetMemory() + args.App.GetSwap(), CPUShares: int64(args.App.GetCpuShare()), SecurityOpts: securityOpts, User: user, } c.addEnvsToConfig(args, port, &config) opts := docker.CreateContainerOptions{Name: c.Name, Config: &config} var nodeList []string if len(args.DestinationHosts) > 0 { var nodeName string nodeName, err = c.hostToNodeAddress(args.Provisioner, args.DestinationHosts[0]) if err != nil { return err } nodeList = []string{nodeName} } schedulerOpts := []string{args.App.GetName(), args.ProcessName} addr, cont, err := args.Provisioner.Cluster().CreateContainerSchedulerOpts(opts, schedulerOpts, nodeList...) if err != nil { log.Errorf("error on creating container in docker %s - %s", c.AppName, err) return err } c.ID = cont.ID c.HostAddr = net.URLToHost(addr) return nil }
// creates a new container in Docker. func (c *container) create(args runContainerActionsArgs) error { port, err := getPort() if err != nil { log.Errorf("error on getting port for container %s - %s", c.AppName, port) return err } user, err := config.GetString("docker:user") if err != nil { user, _ = config.GetString("docker:ssh:user") } securityOpts, _ := config.GetList("docker:security-opts") var exposedPorts map[docker.Port]struct{} if !args.isDeploy { exposedPorts = map[docker.Port]struct{}{ docker.Port(port + "/tcp"): {}, } } config := docker.Config{ Image: args.imageID, Cmd: args.commands, User: user, ExposedPorts: exposedPorts, AttachStdin: false, AttachStdout: false, AttachStderr: false, Memory: args.app.GetMemory(), MemorySwap: args.app.GetMemory() + args.app.GetSwap(), CPUShares: int64(args.app.GetCpuShare()), SecurityOpts: securityOpts, } c.addEnvsToConfig(args.app, &config) opts := docker.CreateContainerOptions{Name: c.Name, Config: &config} var nodeList []string if len(args.destinationHosts) > 0 { nodeName, err := args.provisioner.hostToNodeAddress(args.destinationHosts[0]) if err != nil { return err } nodeList = []string{nodeName} } schedulerOpts := []string{args.app.GetName(), args.processName} addr, cont, err := args.provisioner.getCluster().CreateContainerSchedulerOpts(opts, schedulerOpts, nodeList...) if err != nil { log.Errorf("error on creating container in docker %s - %s", c.AppName, err) return err } c.ID = cont.ID c.HostAddr = urlToHost(addr) c.User = user return nil }
func getDockerServers() []cluster.Node { servers, _ := config.GetList("docker:servers") nodes := []cluster.Node{} clusterNodes = make(map[string]string) for index, server := range servers { id := fmt.Sprintf("server%d", index) node := cluster.Node{ ID: id, Address: server, } nodes = append(nodes, node) clusterNodes[id] = server } return nodes }
func (c *Container) hostConfig(app provision.App, isDeploy bool) (*docker.HostConfig, error) { sharedBasedir, _ := config.GetString("docker:sharedfs:hostdir") sharedMount, _ := config.GetString("docker:sharedfs:mountpoint") sharedIsolation, _ := config.GetBool("docker:sharedfs:app-isolation") sharedSalt, _ := config.GetString("docker:sharedfs:salt") hostConfig := docker.HostConfig{ CPUShares: int64(app.GetCpuShare()), } if !isDeploy { hostConfig.Memory = app.GetMemory() hostConfig.MemorySwap = app.GetMemory() + app.GetSwap() hostConfig.RestartPolicy = docker.AlwaysRestart() hostConfig.PortBindings = map[docker.Port][]docker.PortBinding{ docker.Port(c.ExposedPort): {{HostIP: "", HostPort: ""}}, } pool := app.GetPool() driver, opts, logErr := LogOpts(pool) if logErr != nil { return nil, logErr } hostConfig.LogConfig = docker.LogConfig{ Type: driver, Config: opts, } } else { hostConfig.OomScoreAdj = 1000 } hostConfig.SecurityOpt, _ = config.GetList("docker:security-opts") if sharedBasedir != "" && sharedMount != "" { if sharedIsolation { var appHostDir string if sharedSalt != "" { h := crypto.SHA1.New() io.WriteString(h, sharedSalt+c.AppName) appHostDir = fmt.Sprintf("%x", h.Sum(nil)) } else { appHostDir = c.AppName } hostConfig.Binds = append(hostConfig.Binds, fmt.Sprintf("%s/%s:%s:rw", sharedBasedir, appHostDir, sharedMount)) } else { hostConfig.Binds = append(hostConfig.Binds, fmt.Sprintf("%s:%s:rw", sharedBasedir, sharedMount)) } } return &hostConfig, nil }
func (s *S) startMultipleServersCluster() (*cluster.Cluster, []string, error) { otherServer, err := dtesting.NewServer("127.0.0.1:0", nil) if err != nil { return nil, nil, err } cmutex.Lock() defer cmutex.Unlock() oldCluster := dCluster oldServers, err := config.GetList("docker:servers") if err != nil { oldServers = []string{} } config.Set("docker:servers", []string{"http://serverAddr0:8888", "http://serverAddr1:8888"}) dCluster, err = cluster.New(nil, &mapStorage{}, cluster.Node{ID: "server0", Address: s.server.URL()}, cluster.Node{ID: "server1", Address: otherServer.URL()}, ) if err != nil { return nil, nil, err } return oldCluster, oldServers, nil }
func FlagResolve(listenAddr *string) { flag.Parse() if *versionFlag != false { err := config.ReadConfigFile("../config/server.yml") if err != nil { fmt.Println(err) os.Exit(0) } version, _ := config.GetString("version") update, _ := config.GetList("update") instruction, _ := config.GetString("instruction") fmt.Printf("CaeserClient version: %s\n", version) fmt.Printf("New speciality contrast to old version: \n") for k, v := range update { fmt.Printf("%d-- %s\n", k+1, v) } fmt.Printf(" %s\n", instruction) os.Exit(0) } if *runtimeFlag == "min" { log.Log("warn", "Start up with least CPU's resousces", log.Fields{"Occupy CPU Num": 1}) } if *listenFlag != "" { fp, err := regexp.MatchString(ipPattern, *listenFlag) handleError(err) if !fp { //fmt.Printf("\"%s\" is not a valid address, please check it and try again!\n", *listenFlag) log.Log("fatal", "-address is not a valid address, please check it and try again!", map[string]interface{}{"address": *listenFlag}) os.Exit(0) } *listenAddr = *listenFlag fmt.Println("--Notice: you have set a new listen address", *listenAddr) } else { //fmt.Println("--Didn't set the listen address. Server will start at default address.") log.Log("info", "Didn't set the listen address. Server will start at default address.", log.Fields{"Default Address": *listenAddr}) } }
func (c *Container) Start(args *StartArgs) error { port, err := getPort() if err != nil { return err } sharedBasedir, _ := config.GetString("docker:sharedfs:hostdir") sharedMount, _ := config.GetString("docker:sharedfs:mountpoint") sharedIsolation, _ := config.GetBool("docker:sharedfs:app-isolation") sharedSalt, _ := config.GetString("docker:sharedfs:salt") hostConfig := docker.HostConfig{ Memory: args.App.GetMemory(), MemorySwap: args.App.GetMemory() + args.App.GetSwap(), CPUShares: int64(args.App.GetCpuShare()), } if !args.Deploy { hostConfig.RestartPolicy = docker.AlwaysRestart() hostConfig.PortBindings = map[docker.Port][]docker.PortBinding{ docker.Port(port + "/tcp"): {{HostIP: "", HostPort: ""}}, } logConf := DockerLog{} pool := args.App.GetPool() driver, opts, logErr := logConf.LogOpts(pool) if logErr != nil { return err } hostConfig.LogConfig = docker.LogConfig{ Type: driver, Config: opts, } } hostConfig.SecurityOpt, _ = config.GetList("docker:security-opts") if sharedBasedir != "" && sharedMount != "" { if sharedIsolation { var appHostDir string if sharedSalt != "" { h := crypto.SHA1.New() io.WriteString(h, sharedSalt+c.AppName) appHostDir = fmt.Sprintf("%x", h.Sum(nil)) } else { appHostDir = c.AppName } hostConfig.Binds = append(hostConfig.Binds, fmt.Sprintf("%s/%s:%s:rw", sharedBasedir, appHostDir, sharedMount)) } else { hostConfig.Binds = append(hostConfig.Binds, fmt.Sprintf("%s:%s:rw", sharedBasedir, sharedMount)) } } allocator, _ := config.GetString("docker:port-allocator") if allocator == "" { allocator = "docker" } switch allocator { case "tsuru": err = c.startWithPortSearch(args.Provisioner, &hostConfig) case "docker": err = args.Provisioner.Cluster().StartContainer(c.ID, &hostConfig) default: return fmt.Errorf("invalid docker:port-allocator: %s", allocator) } if err != nil { return err } initialStatus := provision.StatusStarting if args.Deploy { initialStatus = provision.StatusBuilding } return c.SetStatus(args.Provisioner, initialStatus, false) }
func (runBs) createBsContainer(dockerEndpoint string) error { client, err := docker.NewClient(dockerEndpoint) if err != nil { return err } bsImage, _ := config.GetString("docker:bs:image") if bsImage == "" { bsImage = "tsuru/bs" } tsuruEndpoint, _ := config.GetString("host") if !strings.HasPrefix(tsuruEndpoint, "http://") && !strings.HasPrefix(tsuruEndpoint, "https://") { tsuruEndpoint = "http://" + tsuruEndpoint } interval, _ := config.GetInt("docker:bs:reporter-interval") tsuruEndpoint = strings.TrimRight(tsuruEndpoint, "/") + "/" token, err := app.AuthScheme.AppLogin(app.InternalAppName) if err != nil { return err } hostConfig := docker.HostConfig{RestartPolicy: docker.AlwaysRestart()} endpoint := dockerEndpoint socket, _ := config.GetString("docker:bs:socket") if socket != "" { hostConfig.Binds = []string{fmt.Sprintf("%s:/var/run/docker.sock:rw", socket)} endpoint = "unix:///var/run/docker.sock" } sysLogExternalPort := getBsSysLogPort() hostConfig.PortBindings = map[docker.Port][]docker.PortBinding{ docker.Port("514/udp"): { docker.PortBinding{ HostIP: "0.0.0.0", HostPort: strconv.Itoa(sysLogExternalPort), }, }, } env := []string{ "DOCKER_ENDPOINT=" + endpoint, "TSURU_ENDPOINT=" + tsuruEndpoint, "TSURU_TOKEN=" + token.GetValue(), "STATUS_INTERVAL=" + strconv.Itoa(interval), "SYSLOG_LISTEN_ADDRESS=udp://0.0.0.0:514", } addresses, _ := config.GetList("docker:bs:syslog-forward-addresses") if len(addresses) > 0 { env = append(env, "SYSLOG_FORWARD_ADDRESSES="+strings.Join(addresses, ",")) } opts := docker.CreateContainerOptions{ Name: "big-sibling", HostConfig: &hostConfig, Config: &docker.Config{ Image: bsImage, Env: env, ExposedPorts: map[docker.Port]struct{}{ docker.Port(strconv.Itoa(sysLogExternalPort) + "/udp"): {}, }, }, } container, err := client.CreateContainer(opts) if err == docker.ErrContainerAlreadyExists { err = client.RemoveContainer(docker.RemoveContainerOptions{ID: opts.Name, Force: true}) if err != nil { return err } container, err = client.CreateContainer(opts) } if err == docker.ErrNoSuchImage { pullOpts := docker.PullImageOptions{Repository: bsImage} err = client.PullImage(pullOpts, getRegistryAuthConfig()) if err != nil { return err } container, err = client.CreateContainer(opts) } if err == docker.ErrContainerAlreadyExists { err = client.RemoveContainer(docker.RemoveContainerOptions{ID: opts.Name, Force: true}) if err != nil { return err } container, err = client.CreateContainer(opts) } if err != nil { return err } return client.StartContainer(container.ID, &hostConfig) }
func (c *Container) Create(args *CreateArgs) error { securityOpts, _ := config.GetList("docker:security-opts") var exposedPorts map[docker.Port]struct{} if !args.Deploy { if c.ExposedPort == "" { c.ExposedPort = dockercommon.WebProcessDefaultPort() + "/tcp" } exposedPorts = map[docker.Port]struct{}{ docker.Port(c.ExposedPort): {}, } } var user string if args.Building { user = c.user() } routerName, err := args.App.GetRouter() if err != nil { return err } routerType, _, err := router.Type(routerName) if err != nil { return err } hostConf, err := c.hostConfig(args.App, args.Deploy) if err != nil { return err } conf := docker.Config{ Image: args.ImageID, Cmd: args.Commands, Entrypoint: []string{}, ExposedPorts: exposedPorts, AttachStdin: false, AttachStdout: false, AttachStderr: false, Memory: hostConf.Memory, MemorySwap: hostConf.MemorySwap, CPUShares: hostConf.CPUShares, SecurityOpts: securityOpts, User: user, Labels: map[string]string{ "tsuru.container": strconv.FormatBool(true), "tsuru.app.name": args.App.GetName(), "tsuru.app.platform": args.App.GetPlatform(), "tsuru.process.name": c.ProcessName, "tsuru.router.name": routerName, "tsuru.router.type": routerType, }, } c.addEnvsToConfig(args, strings.TrimSuffix(c.ExposedPort, "/tcp"), &conf) opts := docker.CreateContainerOptions{Name: c.Name, Config: &conf, HostConfig: hostConf} var nodeList []string if len(args.DestinationHosts) > 0 { var node cluster.Node node, err = args.Provisioner.GetNodeByHost(args.DestinationHosts[0]) if err != nil { return err } nodeList = []string{node.Address} } schedulerOpts := &SchedulerOpts{ AppName: args.App.GetName(), ProcessName: args.ProcessName, ActionLimiter: args.Provisioner.ActionLimiter(), } addr, cont, err := args.Provisioner.Cluster().CreateContainerSchedulerOpts(opts, schedulerOpts, net.StreamInactivityTimeout, nodeList...) hostAddr := net.URLToHost(addr) if schedulerOpts.LimiterDone != nil { schedulerOpts.LimiterDone() } if err != nil { log.Errorf("error on creating container in docker %s - %s", c.AppName, err) return err } c.ID = cont.ID c.HostAddr = hostAddr return nil }