Esempio n. 1
0
func (e *Engine) Add(obj interface{}) {
	e.Lock()
	defer e.Unlock()
	kubernetes.LogCallback("ADD", obj)

	resources, er := kubernetes.GenResources(e.Cache, obj)
	if er != nil {
		logger.Errorf(er.Error())
	}

	if er := addResources(e, resources); er != nil {
		logger.Errorf(er.Error())
	}
}
Esempio n. 2
0
func (c *Client) CleanTorrents() error {
	logger.Infof("Running torrent cleaner")
	torrents, er := c.GetTorrents()
	if er != nil {
		return er
	}

	torrents.SortByID(false)
	logger.Infof("Found %d torrents to process", len(torrents))
	for _, t := range torrents {
		logger.Debugf("[Torrent %d: %q] Checking status", t.ID, t.Name)
		id := util.Hashf(md5.New(), t.ID, t.Name)
		status := &torrentStatus{Torrent: t, id: id, failures: 0}
		status.setFailures()

		if st, ok := seen[id]; ok {
			status.failures = status.failures + st.failures
			if !updated(st.Torrent, status.Torrent) {
				status.failures++
			}
		}

		seen[id] = status
		logger.Debugf("[Torrent %d: %q] Failures: %d", t.ID, t.Name, status.failures)
	}

	b := backoff.NewExponentialBackOff()
	b.MaxElapsedTime = 15 * time.Second
	remove := make([]*torrentStatus, 0, 1)
	for _, t := range seen {
		if t.failed() {
			b.Reset()
			logger.Infof("[Torrent %d: %q] Removing", t.ID, t.Name)
			er := backoff.RetryNotify(delTorrent(c, t.Torrent), b, func(e error, w time.Duration) {
				logger.Errorf("[Torrent %d: %q] Failed to remove (retry in %v): %v", t.ID, t.Name, w, e)
			})
			if er == nil {
				remove = append(remove, t)
			} else {
				logger.Errorf("[Torrent %d: %q] Failed to remove, will retry next cycle", t.ID, t.Name)
			}
		}
	}

	for i := range remove {
		delete(seen, remove[i].id)
	}
	return nil
}
Esempio n. 3
0
func (v *vulcan) NewMiddlewares(rsc *kubernetes.Resource) ([]loadbalancer.Middleware, error) {
	mids := make([]loadbalancer.Middleware, 0, 1)
	for key, def := range DefaultMiddleware {
		if val, ok := rsc.GetAnnotation(key); ok && len(val) > 0 {
			switch key {
			case RedirectSSLID:
				if b, er := strconv.ParseBool(val); er != nil || !b {
					continue
				}
			case TraceID:
				re := regexp.MustCompile(`\s+`)
				list, er := json.Marshal(strings.Split(re.ReplaceAllString(val, ""), ","))
				if er != nil || string(list) == "" {
					logger.Warnf("Unable to json-ify trace headers: %v", er)
					list = []byte("[]")
				}
				def = fmt.Sprintf(def, string(list), string(list))
			case AuthID:
				bits := strings.SplitN(val, ":", 2)
				switch len(bits) {
				case 1:
					def = fmt.Sprintf(def, bits[0], "")
				case 2:
					def = fmt.Sprintf(def, bits[0], bits[1])
				default:
					logger.Errorf("Failed to parse provided basic auth, using default (admin:admin)")
					def = fmt.Sprintf(def, "admin", "admin")
				}
			case MaintenanceID:
				def = fmt.Sprintf(def, val)
			}

			m, er := engine.MiddlewareFromJSON([]byte(def), v.Registry.GetSpec, key)
			if er != nil {
				logger.Warnf("Failed to parse Middleware %s: %v", key, er)
				logger.Debugf("%q", def)
				continue
			}
			mids = append(mids, newMiddleware(m))
		}
	}

	rg := regexp.MustCompile(CustomMiddlewareKeyPattern)
	matches, _ := rsc.GetAnnotations(CustomMiddlewareKeyPattern)
	for key, val := range matches {
		if match := rg.FindStringSubmatch(key); match != nil {
			id := match[1]
			m, er := engine.MiddlewareFromJSON([]byte(val), v.Registry.GetSpec, id)
			if er != nil {
				logger.Warnf("Failed to parse Middleware %s: %v", id, er)
				continue
			}
			mids = append(mids, newMiddleware(m))
		}
	}

	return mids, nil
}
Esempio n. 4
0
func (e *Engine) Update(old, next interface{}) {
	e.Lock()
	defer e.Unlock()
	kubernetes.LogCallback("UPDATE", next)

	logger.Debugf("Gather resources from previous object")
	oldResources, er := kubernetes.GenResources(e.Cache, old)
	if er != nil {
		logger.Errorf(er.Error())
	}

	logger.Debugf("Gather resources from new object")
	newResources, er := kubernetes.GenResources(e.Cache, next)
	if er != nil {
		logger.Errorf(er.Error())
	}

	if er := updateResources(e, newResources, oldResources); er != nil {
		logger.Errorf(er.Error())
	}
}
Esempio n. 5
0
func restartProcesses(t, v *process.Process, c *config.Config, ctx context.Context) error {
	var (
		notify = func(e error, t time.Duration) {
			logger.Errorf("Failed to restart processes (retry in %v): %v", t, e)
		}
		operation = func() error {
			t.Stop()
			v.Stop()
			return startProcesses(t, v, c, ctx)
		}
		b = backoff.NewExponentialBackOff()
	)
	b.MaxElapsedTime = 30 * time.Minute
	b.MaxInterval = 10 * time.Second
	return backoff.RetryNotify(operation, b, notify)
}
Esempio n. 6
0
func getIP(dev string, timeout time.Duration, c context.Context) (string, error) {
	var address string
	notify := func(e error, w time.Duration) {
		logger.Errorf("Failed to get IP for %q (retry in %v): %v", dev, w, e)
	}
	fn := func() (er error) {
		select {
		default:
			address, er = vpn.FindIP(dev)
			return
		case <-c.Done():
			return
		}
	}

	b := backoff.NewExponentialBackOff()
	b.MaxElapsedTime = timeout
	return address, backoff.RetryNotify(fn, b, notify)
}
Esempio n. 7
0
func cleaner(conf *config.Config, c context.Context) {
	var (
		d     = conf.Cleaner.Interval.Duration
		clean = time.NewTicker(d)
	)
	logger.Infof("Torrent cleaner will run once every %v", d)
	for {
		select {
		case <-c.Done():
			clean.Stop()
			return
		case t := <-clean.C:
			logger.Infof("Half hourly torrent cleaning at %v", t)
			er := transmission.
				NewClient(conf.Transmission.URL.String(), conf.Transmission.User, conf.Transmission.Pass).
				CleanTorrents()
			if er != nil {
				logger.Errorf(er.Error())
			}
		}
	}
}
Esempio n. 8
0
func getPort(ip, user, pass, id string, timeout time.Duration, c context.Context) (int, error) {
	var port int
	notify := func(e error, w time.Duration) {
		logger.Errorf("Failed to get port from PIA (retry in %v): %v", w, e)
	}
	fn := func() error {
		select {
		default:
			p, er := pia.RequestPort(ip, user, pass, id)
			if er != nil {
				return er
			}
			port = p
			return nil
		case <-c.Done():
			return nil
		}
	}

	b := backoff.NewExponentialBackOff()
	b.MaxElapsedTime = timeout
	return port, backoff.RetryNotify(fn, b, notify)
}