func (redismqQFactory) get(name, consumerName string) (*redismqQ, error) { host, err := config.GetString("redis-queue:host") if err != nil { host = "localhost" } port, err := config.GetString("redis-queue:port") if err != nil { if nport, err := config.GetInt("redis-queue:port"); err != nil { port = "6379" } else { port = fmt.Sprintf("%d", nport) } } password, _ := config.GetString("redis-queue:password") db, err := config.GetInt("redis-queue:db") if err != nil { db = 3 } maxIdle, _ := config.GetInt("redis-queue:pool-max-idle-conn") if maxIdle == 0 { maxIdle = 20 } idleTimeout, _ := config.GetDuration("redis-queue:pool-idle-timeout") if idleTimeout == 0 { idleTimeout = 300e9 } pool := redis.NewPool(func() (redis.Conn, error) { conn, err := redis.Dial("tcp", host+":"+port) if err != nil { return nil, err } if password != "" { _, err = conn.Do("AUTH", password) if err != nil { return nil, err } } _, err = conn.Do("SELECT", db) return conn, err }, maxIdle) pool.IdleTimeout = idleTimeout return &redismqQ{name: name, pool: pool}, nil }
func (redismqQFactory) get(name, consumerName string) (*redismqQ, error) { host, err := config.GetString("redis-queue:host") if err != nil { host = "localhost" } port, err := config.GetString("redis-queue:port") if err != nil { if nport, err := config.GetInt("redis-queue:port"); err != nil { port = "6379" } else { port = fmt.Sprintf("%d", nport) } } password, _ := config.GetString("redis-queue:password") db, err := config.GetInt("redis-queue:db") if err != nil { db = 3 } queue := redismq.CreateQueue(host, port, password, int64(db), name) consumer, err := queue.AddConsumer(consumerName) if err != nil { return nil, err } return &redismqQ{name: name, queue: queue, consumer: consumer}, nil }
func (factory *redismqQFactory) dial() (redis.Conn, error) { host, err := config.GetString("redis-queue:host") if err != nil { host = "localhost" } port, err := config.GetString("redis-queue:port") if err != nil { if nport, err := config.GetInt("redis-queue:port"); err != nil { port = "6379" } else { port = fmt.Sprintf("%d", nport) } } password, _ := config.GetString("redis-queue:password") db, err := config.GetInt("redis-queue:db") if err != nil { db = 3 } conn, err := redis.Dial("tcp", host+":"+port) if err != nil { return nil, err } if password != "" { _, err = conn.Do("AUTH", password) if err != nil { return nil, err } } _, err = conn.Do("SELECT", db) return conn, err }
func (factory *redisPubSubFactory) getPool() *redis.Pool { factory.Lock() defer factory.Unlock() if factory.pool != nil { return factory.pool } maxIdle, err := config.GetInt("pubsub:pool-max-idle-conn") if err != nil { maxIdle, err = config.GetInt("redis-queue:pool-max-idle-conn") if err != nil { maxIdle = 20 } } idleTimeout, err := config.GetInt("pubsub:pool-idle-timeout") if err != nil { idleTimeout, err = config.GetInt("redis-queue:pool-idle-timeout") if err != nil { idleTimeout = 300 } } factory.pool = &redis.Pool{ MaxIdle: maxIdle, IdleTimeout: time.Duration(idleTimeout) * time.Second, Dial: factory.dial, } return factory.pool }
func Initialize() (*NodeHealer, error) { if HealerInstance != nil { return nil, errors.New("healer alread initialized") } autoHealingNodes, err := config.GetBool("docker:healing:heal-nodes") if err != nil { autoHealingNodes = true } if !autoHealingNodes { return nil, nil } disabledSeconds, _ := config.GetInt("docker:healing:disabled-time") if disabledSeconds <= 0 { disabledSeconds = 30 } maxFailures, _ := config.GetInt("docker:healing:max-failures") if maxFailures <= 0 { maxFailures = 5 } waitSecondsNewMachine, _ := config.GetInt("docker:healing:wait-new-time") if waitSecondsNewMachine <= 0 { waitSecondsNewMachine = 5 * 60 } HealerInstance = newNodeHealer(nodeHealerArgs{ DisabledTime: time.Duration(disabledSeconds) * time.Second, WaitTimeNewMachine: time.Duration(waitSecondsNewMachine) * time.Second, FailuresBeforeHealing: maxFailures, }) shutdown.Register(HealerInstance) return HealerInstance, nil }
func DefaultPlan() (*Plan, error) { conn, err := db.Conn() if err != nil { return nil, err } defer conn.Close() var plans []Plan err = conn.Plans().Find(bson.M{"default": true}).All(&plans) if err != nil { return nil, err } if len(plans) == 0 { // For backard compatibility only, this fallback will be removed. You // should have at least one plan configured. configMemory, _ := config.GetInt("docker:memory") configSwap, _ := config.GetInt("docker:swap") return &Plan{ Name: "autogenerated", Memory: int64(configMemory) * 1024 * 1024, Swap: int64(configSwap-configMemory) * 1024 * 1024, CpuShare: 100, }, nil } if len(plans) > 1 { return nil, ErrPlanDefaultAmbiguous } return &plans[0], nil }
func (factory *redisPubSubFactory) dial() (redis.Conn, error) { host, err := config.GetString("pubsub:redis-host") if err != nil { host, err = config.GetString("redis-queue:host") if err != nil { host = "localhost" } } port, err := config.Get("pubsub:redis-port") if err != nil { port, err = config.Get("redis-queue:port") if err != nil { port = "6379" } } port = fmt.Sprintf("%v", port) password, err := config.GetString("pubsub:redis-password") if err != nil { password, _ = config.GetString("redis-queue:password") } db, err := config.GetInt("pubsub:redis-db") if err != nil { db, err = config.GetInt("redis-queue:db") if err != nil { db = 3 } } secondFloat := float64(time.Second) dialTimeout, err := config.GetFloat("pubsub:redis-dial-timeout") if err != nil { dialTimeout = 0.1 } dialTimeout = dialTimeout * secondFloat readTimeout, err := config.GetFloat("pubsub:redis-read-timeout") if err != nil { readTimeout = 30 * 60 } readTimeout = readTimeout * secondFloat writeTimeout, err := config.GetFloat("pubsub:redis-write-timeout") if err != nil { writeTimeout = 0.5 } writeTimeout = writeTimeout * secondFloat conn, err := redis.DialTimeout("tcp", fmt.Sprintf("%s:%v", host, port), time.Duration(dialTimeout), time.Duration(readTimeout), time.Duration(writeTimeout)) if err != nil { return nil, err } if password != "" { _, err = conn.Do("AUTH", password) if err != nil { return nil, err } } _, err = conn.Do("SELECT", db) return conn, err }
func readConfig(path string) (Config, error) { cfg := Config{} configFile := filepath.Join(path, "config.yaml") err := config.ReadConfigFile(configFile) if err != nil { return cfg, err } cfg.Id, err = config.GetString("id") if err != nil { return cfg, err } cfg.Hostname, err = config.GetString("hostname") if err != nil { return cfg, err } cfg.DiskPath = filepath.Join(path, "disk.qcow") cfg.Disk, err = config.GetInt("disk") if err != nil { return cfg, err } cfg.Cpu, err = config.GetInt("cpu") if err != nil { return cfg, err } cfg.Memory, err = config.GetInt("memory") if err != nil { return cfg, err } cfg.DNS, err = config.GetString("dns") if err != nil { return cfg, err } cfg.Docker, err = config.GetString("docker") if err != nil { return cfg, err } cfg.Extra, err = config.GetString("extra") if err != nil { return cfg, err } cfg.Route, err = config.GetBool("route") return cfg, err }
func (p *dockerProvisioner) initAutoScaleConfig() *autoScaleConfig { enabled, _ := config.GetBool("docker:auto-scale:enabled") waitSecondsNewMachine, _ := config.GetInt("docker:auto-scale:wait-new-time") runInterval, _ := config.GetInt("docker:auto-scale:run-interval") TotalMemoryMetadata, _ := config.GetString("docker:scheduler:total-memory-metadata") return &autoScaleConfig{ TotalMemoryMetadata: TotalMemoryMetadata, WaitTimeNewMachine: time.Duration(waitSecondsNewMachine) * time.Second, RunInterval: time.Duration(runInterval) * time.Second, Enabled: enabled, provisioner: p, done: make(chan bool), } }
func postnetwork(container *global.Container, ip string) { gulpPort, _ := config.GetInt("docker:gulp_port") url := "http://" + container.SwarmNode + ":" + strconv.Itoa(gulpPort) + "/docker/networks" log.Info("URL:> %s", url) bridge, _ := config.GetString("docker:bridge") gateway, _ := config.GetString("docker:gateway") data := &global.DockerNetworksInfo{Bridge: bridge, ContainerId: container.ContainerID, IpAddr: ip, Gateway: gateway} res2B, _ := json.Marshal(data) req, err := http.NewRequest("POST", url, bytes.NewBuffer(res2B)) req.Header.Set("X-Custom-Header", "myvalue") req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { log.Error("gulpd client was failed : %s", err) } defer resp.Body.Close() log.Info("response Status : %s", resp.Status) log.Info("response Headers : %s", resp.Header) body, _ := ioutil.ReadAll(resp.Body) log.Info("response Body : %s", string(body)) }
func imageHistorySize() int { imgHistorySize, _ := config.GetInt("docker:image-history-size") if imgHistorySize == 0 { imgHistorySize = 10 } return imgHistorySize }
func (u *User) Create() error { conn, err := db.Conn() if err != nil { return err } defer conn.Close() if u.Quota.Limit == 0 { u.Quota = quota.Unlimited var limit int if limit, err = config.GetInt("quota:apps-per-user"); err == nil && limit > -1 { u.Quota.Limit = limit } } err = conn.Users().Insert(u) if err != nil { return err } err = u.createOnRepositoryManager() if err != nil { u.Delete() return err } err = u.AddRolesForEvent(permission.RoleEventUserCreate, "") if err != nil { log.Errorf("unable to add default roles during user creation for %q: %s", u.Email, err) } return nil }
func createUser(w http.ResponseWriter, r *http.Request) error { var u auth.User err := json.NewDecoder(r.Body).Decode(&u) if err != nil { return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()} } if !validation.ValidateEmail(u.Email) { return &errors.HTTP{Code: http.StatusBadRequest, Message: emailError} } if !validation.ValidateLength(u.Password, passwordMinLen, passwordMaxLen) { return &errors.HTTP{Code: http.StatusBadRequest, Message: passwordError} } if _, err = auth.GetUserByEmail(u.Email); err == nil { return &errors.HTTP{Code: http.StatusConflict, Message: "This email is already registered"} } gURL := repository.ServerURL() c := gandalf.Client{Endpoint: gURL} if _, err := c.NewUser(u.Email, keyToMap(u.Keys)); err != nil { return fmt.Errorf("Failed to create user in the git server: %s", err) } u.Quota = quota.Unlimited if limit, err := config.GetInt("quota:apps-per-user"); err == nil && limit > -1 { u.Quota.Limit = limit } if err := u.Create(); err == nil { rec.Log(u.Email, "create-user") w.WriteHeader(http.StatusCreated) return nil } return err }
func removeOldTokens(userEmail string) error { conn, err := db.Conn() if err != nil { return err } defer conn.Close() var limit int if limit, err = config.GetInt("auth:max-simultaneous-sessions"); err != nil { return err } count, err := conn.Tokens().Find(bson.M{"useremail": userEmail}).Count() if err != nil { return err } diff := count - limit if diff < 1 { return nil } var tokens []map[string]interface{} err = conn.Tokens().Find(bson.M{"useremail": userEmail}). Select(bson.M{"_id": 1}).Sort("creation").Limit(diff).All(&tokens) if err != nil { return nil } ids := make([]interface{}, 0, len(tokens)) for _, token := range tokens { ids = append(ids, token["_id"]) } _, err = conn.Tokens().RemoveAll(bson.M{"_id": bson.M{"$in": ids}}) return err }
func (t *runBs) waitDocker(client *docker.Client) error { timeout, _ := config.GetInt("docker:api-timeout") if timeout == 0 { timeout = 600 } timeoutChan := time.After(time.Duration(timeout) * time.Second) pong := make(chan error, 1) exit := make(chan struct{}) go func() { for { err := client.Ping() if err == nil { pong <- nil return } if e, ok := err.(*docker.Error); ok && e.Status > 499 { pong <- err return } select { case <-exit: return case <-time.After(time.Second): } } }() select { case err := <-pong: return err case <-timeoutChan: close(exit) return errors.Errorf("Docker API at %q didn't respond after %d seconds", client.Endpoint(), timeout) } }
func (runBs) waitDocker(endpoint string) error { client, err := docker.NewClient(endpoint) if err != nil { return err } timeout, _ := config.GetInt("docker:api-timeout") if timeout == 0 { timeout = 600 } timeoutChan := time.After(time.Duration(timeout) * time.Second) pong := make(chan error, 1) go func() { for { err := client.Ping() if err == nil { pong <- nil return } if e, ok := err.(*docker.Error); ok && e.Status > 499 { pong <- err return } } }() select { case err := <-pong: return err case <-timeoutChan: return fmt.Errorf("Docker API at %q didn't respond after %d seconds", endpoint, timeout) } }
// Run is the function that starts the collector. The dryMode parameter // indicates whether the collector should loop forever or not. // // It assumes the configuration has already been defined (from a config file or // memory). func Run(dryMode bool) { log.Init() connString, err := config.GetString("database:url") if err != nil { connString = db.DefaultDatabaseURL } dbName, err := config.GetString("database:name") if err != nil { dbName = db.DefaultDatabaseName } fmt.Printf("Using the database %q from the server %q.\n\n", dbName, connString) if !dryMode { provisioner, err := config.GetString("provisioner") if err != nil { fmt.Println("Warning: configuration didn't declare a provisioner, using default provisioner.") provisioner = "juju" } app.Provisioner, err = provision.Get(provisioner) if err != nil { fatal(err) } fmt.Printf("Using %q provisioner.\n\n", provisioner) timer, err := config.GetInt("collector:ticker-time") if err != nil { timer = 60 } ticker := time.Tick(time.Duration(timer) * time.Second) fmt.Println("tsuru collector agent started...") collect(ticker) } }
func getBsSysLogPort() int { bsPort, _ := config.GetInt("docker:bs:syslog-port") if bsPort == 0 { bsPort = 1514 } return bsPort }
func sshAgentPort() int { port, _ := config.GetInt("docker:ssh-agent-port") if port == 0 { port = 4545 } return port }
func (r *hipacheRouter) connect() redis.Conn { r.Lock() defer r.Unlock() if r.pool == nil { srv := r.redisServer() r.pool = &redis.Pool{ Dial: func() (redis.Conn, error) { conn, err := redis.Dial("tcp", srv) if err != nil { return nil, err } password, _ := config.GetString(r.prefix + ":redis-password") if password != "" { _, err = conn.Do("AUTH", password) if err != nil { return nil, err } } db, err := config.GetInt(r.prefix + ":redis-db") if err == nil { _, err = conn.Do("SELECT", db) if err != nil { return nil, err } } return conn, nil }, MaxIdle: 10, IdleTimeout: 180e9, } } return r.pool.Get() }
func (p *swarmProvisioner) Initialize() error { var err error swarmConfig.swarmPort, err = config.GetInt("swarm:swarm-port") if err != nil { swarmConfig.swarmPort = 2377 } return nil }
func loadConfig() error { if cost == 0 && tokenExpire == 0 { var err error if days, err := config.GetInt("auth:token-expire-days"); err == nil { tokenExpire = time.Duration(int64(days) * 24 * int64(time.Hour)) } else { tokenExpire = defaultExpiration } if cost, err = config.GetInt("auth:hash-cost"); err != nil { cost = bcrypt.DefaultCost } if cost < bcrypt.MinCost || cost > bcrypt.MaxCost { return fmt.Errorf("Invalid value for setting %q: it must be between %d and %d.", "auth:hash-cost", bcrypt.MinCost, bcrypt.MaxCost) } } return nil }
func InitializeBS() (bool, error) { bsNodeContainer, err := LoadNodeContainer("", BsDefaultName) if err != nil { return false, err } if len(bsNodeContainer.Config.Env) > 0 { return false, nil } tokenData, err := app.AuthScheme.AppLogin(app.InternalAppName) if err != nil { return false, err } token := tokenData.GetValue() conf := configFor(BsDefaultName) isSet, _ := conf.SetFieldAtomic("", "Config.Env", []string{ "TSURU_TOKEN=" + token, }) if !isSet { // Already set by someone else, just bail out. app.AuthScheme.Logout(token) return false, nil } bsNodeContainer, err = LoadNodeContainer("", BsDefaultName) if err != nil { return true, err } tsuruEndpoint, _ := config.GetString("host") if !strings.HasPrefix(tsuruEndpoint, "http://") && !strings.HasPrefix(tsuruEndpoint, "https://") { tsuruEndpoint = "http://" + tsuruEndpoint } tsuruEndpoint = strings.TrimRight(tsuruEndpoint, "/") + "/" socket, _ := config.GetString("docker:bs:socket") image, _ := config.GetString("docker:bs:image") if image == "" { image = bsDefaultImageName } bsPort, _ := config.GetInt("docker:bs:syslog-port") if bsPort == 0 { bsPort = 1514 } bsNodeContainer.Name = BsDefaultName bsNodeContainer.Config.Env = append(bsNodeContainer.Config.Env, []string{ "TSURU_ENDPOINT=" + tsuruEndpoint, "HOST_PROC=" + bsHostProc, "SYSLOG_LISTEN_ADDRESS=" + fmt.Sprintf("udp://0.0.0.0:%d", bsPort), }...) bsNodeContainer.Config.Image = image bsNodeContainer.HostConfig.RestartPolicy = docker.AlwaysRestart() bsNodeContainer.HostConfig.Privileged = true bsNodeContainer.HostConfig.NetworkMode = "host" bsNodeContainer.HostConfig.Binds = []string{fmt.Sprintf("/proc:%s:ro", bsHostProc)} if socket != "" { bsNodeContainer.Config.Env = append(bsNodeContainer.Config.Env, "DOCKER_ENDPOINT=unix:///var/run/docker.sock") bsNodeContainer.HostConfig.Binds = append(bsNodeContainer.HostConfig.Binds, fmt.Sprintf("%s:/var/run/docker.sock:rw", socket)) } return true, conf.Save("", bsNodeContainer) }
func (factory *redisPubSubFactory) dial() (redis.Conn, error) { host, err := config.GetString("pubsub:redis-host") if err != nil { host, err = config.GetString("redis-queue:host") if err != nil { host = "localhost" } } port, err := config.Get("pubsub:redis-port") if err != nil { port, err = config.Get("redis-queue:port") if err != nil { port = "6379" } } port = fmt.Sprintf("%v", port) password, err := config.GetString("pubsub:redis-password") if err != nil { password, _ = config.GetString("redis-queue:password") } db, err := config.GetInt("pubsub:redis-db") if err != nil { db, err = config.GetInt("redis-queue:db") if err != nil { db = 3 } } connectTimeout := 100 * time.Millisecond readTimeout := 30 * time.Minute writeTimeout := 500 * time.Millisecond conn, err := redis.DialTimeout("tcp", fmt.Sprintf("%s:%v", host, port), connectTimeout, readTimeout, writeTimeout) if err != nil { return nil, err } if password != "" { _, err = conn.Do("AUTH", password) if err != nil { return nil, err } } _, err = conn.Do("SELECT", db) return conn, err }
func runInContainers(containers []container.Container, callback callbackFunc, rollback rollbackFunc, parallel bool) error { if len(containers) == 0 { return nil } workers, _ := config.GetInt("docker:max-workers") if workers == 0 { workers = len(containers) } step := len(containers) / workers if len(containers)%workers != 0 { step += 1 } toRollback := make(chan *container.Container, len(containers)) errs := make(chan error, len(containers)) var wg sync.WaitGroup runFunc := func(start, end int) error { defer wg.Done() for i := start; i < end; i++ { err := callback(&containers[i], toRollback) if err != nil { errs <- err return err } } return nil } for i := 0; i < len(containers); i += step { end := i + step if end > len(containers) { end = len(containers) } wg.Add(1) if parallel { go runFunc(i, end) } else { err := runFunc(i, end) if err != nil { break } } } wg.Wait() close(errs) close(toRollback) if err := <-errs; err != nil { if rollback != nil { for c := range toRollback { rollback(c) } } return err } return nil }
func NewHttpServer() *HttpServer { //apiReadTimeout, _ := config.GetString("read-timeout") apiHttpPortString, _ := config.GetInt("admin:port") self := &HttpServer{} self.HttpPort = apiHttpPortString //self.adminAssetsDir = config.AdminAssetsDir self.shutdown = make(chan bool, 2) self.p = pat.New() self.readTimeout = 10 * time.Second return self }
func (m *Machine) FormatNodeAddress() string { protocol, _ := config.GetString("iaas:node-protocol") if protocol == "" { protocol = "http" } port, _ := config.GetInt("iaas:node-port") if port == 0 { port = 2375 } return fmt.Sprintf("%s://%s:%d", protocol, m.Address, port) }
func (m *Machine) FormatNodeAddress() (string, error) { protocol, err := config.GetString("iaas:node-protocol") if err != nil { return "", err } port, err := config.GetInt("iaas:node-port") if err != nil { return "", err } return fmt.Sprintf("%s://%s:%d", protocol, m.Address, port), nil }
func loadConfig() error { if tokenExpire == 0 { var err error var days int if days, err = config.GetInt("auth:token-expire-days"); err == nil { tokenExpire = time.Duration(int64(days) * 24 * int64(time.Hour)) } else { tokenExpire = defaultExpiration } } return nil }
func (factory *redisPubSubFactory) dial() (redis.Conn, error) { host, err := config.GetString("pubsub:redis-host") if err != nil { host, err = config.GetString("redis-queue:host") if err != nil { host = "localhost" } } port, err := config.Get("pubsub:redis-port") if err != nil { port, err = config.Get("redis-queue:port") if err != nil { port = "6379" } } port = fmt.Sprintf("%v", port) password, err := config.GetString("pubsub:redis-password") if err != nil { password, _ = config.GetString("redis-queue:password") } db, err := config.GetInt("pubsub:redis-db") if err != nil { db, err = config.GetInt("redis-queue:db") if err != nil { db = 3 } } conn, err := redis.Dial("tcp", fmt.Sprintf("%s:%v", host, port)) if err != nil { return nil, err } if password != "" { _, err = conn.Do("AUTH", password) if err != nil { return nil, err } } _, err = conn.Do("SELECT", db) return conn, err }