Esempio n. 1
0
func (c *Container) Start(ctx context.Context) (err error) {
	c.ctx, c.Cancel = context.WithCancel(ctx)

	if c.watch {
		dCli, err := dockercli.NewClient(c.endpoint)
		if err != nil {
			logx.Error(err)
			return err
		}
		ch := make(chan *dockercli.Stats)
		errChan := make(chan error, 1)

		go func() {
			err := dCli.Stats(dockercli.StatsOptions{
				ID:     c.id,
				Stats:  ch,
				Stream: true,
			})
			if err != nil {
				errChan <- err
				close(errChan)
			}
		}()

		go func() {
			warmCount := 0
			for {
				select {
				case <-c.ctx.Done():
					logx.Infof("stop reporting for %s", c.name)
					return
				case err := <-errChan:
					logx.OnError(err)
					return
				case stats := <-ch:
					if !c.warmedUp {
						if warmCount < 4 {
							warmCount++
						} else {
							logx.Infof("start reporting for %s", c.name)
							c.warmedUp = true
						}
					}
					if stats != nil {
						c.updateStats(*stats)
					}
				}
			}
		}()
		logx.Infof("docker container probe for %s started", c.name)
	} else {
		logx.Infof("docker container probe for %s uses dummy mode", c.name)
	}
	return
}
Esempio n. 2
0
func newContainer(
	c *dockercli.Container,
	endpoint string,
	dropper report.Reporter,
	annotator report.Reporter,
) (res *Container) {
	res = &Container{
		id:        c.ID,
		name:      strings.TrimPrefix(c.Name, "/"),
		endpoint:  endpoint,
		statMutex: &sync.Mutex{},
		annotator: annotator,
	}

	// Check for "WAVES_IGNORE" env
	env := dockercli.Env(c.Config.Env)
	if env.Get("WAVES_WATCH") != "" {
		res.watch = true
		escaped := res.formatName(c.Name)
		res.dropper = report.NewMetaReporter([]string{escaped}, dropper)
	} else {
		logx.Infof("using dummy mode for %s", res.name)
	}
	return
}
Esempio n. 3
0
func (p *Probe) Start(
	ctx context.Context,
) (err error) {
	p.dockerclient, err = dockercli.NewClient(p.endpoint)
	if err != nil {
		return
	}

	var cancel context.CancelFunc
	p.ctx, cancel = context.WithCancel(ctx)

	err = p.prepare()
	if err != nil {
		return
	}

	events := make(chan *dockercli.APIEvents)
	err = p.dockerclient.AddEventListener(events)
	if err != nil {
		return
	}

	go func() {
		defer cancel()
		logx.Infof("start docker probe on %s", p.endpoint)
		for {
			select {
			case <-p.ctx.Done():
				logx.Infof("stop docker probe on %s", p.endpoint)
				return
			case ev := <-events:
				switch ev.Status {
				case "start":
					err = p.addContainer(ev.ID)
					if err != nil {
						logx.Error(err)
					}
					p.emit(ev.ID, "start", time.Unix(ev.Time, 0))
				case "die":
					p.emit(ev.ID, "stop", time.Unix(ev.Time, 0))
					p.removeContainer(ev.ID)
				}
			}
		}
	}()
	return
}
Esempio n. 4
0
func (p *Probe) removeContainer(id string) {
	p.mu.Lock()
	defer p.mu.Unlock()
	cn, ok := p.containers[id]
	if ok {
		logx.Infof("remove %s from watch", id)
		defer cn.Cancel()
		delete(p.containers, id)
	}
}
Esempio n. 5
0
func (r *Reporter) Drop(rep *report.Report) (err error) {
	if !r.noop {
		v := url.Values{
			"title":       {rep.Key()},
			"description": {rep.Value()},
			"start_time":  {fmt.Sprintf("%d", rep.When().Unix())},
			"tag":         rep.Meta(),
		}
		res, err := r.cli.PostForm(r.endpoint, v)
		logx.OnFatal(err)
		bd, _ := ioutil.ReadAll(res.Body)
		bds := string(bd[:len(bd)])
		res.Body.Close()
		if bds != "" {
			logx.Fatal(bds)
		}
		logx.Infof("annotation sent: %+v", rep)
	} else {
		logx.Infof("annotation: %+v", rep)
	}
	return
}
Esempio n. 6
0
// Finish diversion.
func (d *Divert) Commit(spec DivertSpec, message string) (err error) {
	// Add
	if err = d.Git.Add(spec.TargetFiles...); err != nil {
		return
	}
	// Commit

	if err = d.Git.Commit(message); err != nil {
		return
	}
	// force recreate index
	if err = d.Git.BranchRecreate(spec.TargetBranch); err != nil {
		return
	}
	logx.Infof("diversion commited")
	return
}
Esempio n. 7
0
func (m *Model) SquashBlobs(blobs lists.BlobMap) (err error) {
	logx.Tracef("squashing blobs %s", blobs.IDMap())

	var req, res []interface{}
	for _, v := range blobs.ToSlice() {
		req = append(req, v)
	}

	err = m.BatchPool.Do(
		func(ctx context.Context, in interface{}) (out interface{}, err error) {
			r := in.(lists.BlobLink)

			lock, err := m.FdLocks.Take()
			if err != nil {
				return
			}
			defer lock.Release()

			absname := filepath.Join(m.WD, r.Name)
			backName := absname + ".bar-backup"
			os.Rename(absname, absname+".bar-backup")
			os.MkdirAll(filepath.Dir(absname), 0755)

			w, err := os.Create(absname)
			if err != nil {
				return
			}
			err = r.Manifest.Serialize(w)
			if err != nil {
				os.Remove(absname)
				os.Rename(backName, absname)
				return
			}
			defer os.Remove(backName)
			logx.Debugf("squashed %s", r.Name)
			return
		},
		&req, &res, concurrency.DefaultBatchOptions().AllowErrors(),
	)
	if err != nil {
		return
	}

	logx.Infof("blob %s squashed successfully", blobs.Names())
	return
}
Esempio n. 8
0
func main() {
	fs := flags.New().SetVersion(Version)
	fs.UseEnv = false
	fs.Boot()

	logx.SetLevel(log_level)
	prog, err := program.ParseFiles(flag.Args()...)
	logx.OnFatal(err)

	opts := options.NewGenOptions(layer, pack, core)

	gen_root := gen.NewGenRoot(prog, opts)

	// make package dir
	err = os.MkdirAll(filepath.Join(out, pack), os.ModePerm)
	logx.OnFatal(err)

	// Write types
	res, err := gen_root.Out()
	logx.OnFatal(err)
	for filename, box_res := range res {
		target := filepath.Join(out, pack, filename)
		err = ioutil.WriteFile(target, box_res, os.ModePerm)
		logx.OnFatal(err)
		logx.Infof("==> %s", target)
	}

	// Write Fns
	//	fn_fname, fn_res, err := gen_root.PipeOut()
	//	logx.OnFatal(err)
	//	target := filepath.Join(out, pack, fn_fname)
	//	err = ioutil.WriteFile(target, fn_res, os.ModePerm)
	//	logx.Infof("==> %s", target)
	//
	//	// Write package
	//	pack_res, err := gen_root.PackageOut()
	//	logx.OnFatal(err)
	//	target = filepath.Join(out, pack, pack) + ".go"
	//	err = ioutil.WriteFile(target, pack_res, os.ModePerm)
	//	logx.OnFatal(err)
	//	logx.Infof("==> %s", target)
}
Esempio n. 9
0
func (p *Probe) addContainer(id string) (err error) {
	info, err := p.dockerclient.InspectContainer(id)
	if err != nil {
		return
	}

	p.mu.Lock()
	defer p.mu.Unlock()
	_, ok := p.containers[id]
	if !ok {
		cn := newContainer(info, p.endpoint, p.dropper, p.annotator)
		err = cn.Start(p.ctx)
		if err != nil {
			err = logx.OnError(err)
			return
		}
		p.containers[id] = cn
		logx.Infof("add %s to watch", id)
	}
	return
}
Esempio n. 10
0
func (c *SpecExportCmd) Run(args ...string) (err error) {
	var mod *model.Model
	if mod, err = model.New(c.WD, c.UseGit, c.ChunkSize, c.PoolSize); err != nil {
		return
	}

	feed := lists.NewFileList(args...).ListDir(c.WD)

	isDirty, dirty, err := mod.Check(feed...)
	if err != nil {
		return
	}
	if isDirty {
		err = fmt.Errorf("dirty files in working tree %s", dirty)
		return
	}

	if c.UseGit {
		// filter by attrs
		feed, err = mod.Git.FilterByAttr("bar", feed...)
	}

	blobs, err := mod.FeedManifests(true, true, true, feed...)
	if err != nil {
		return
	}

	// make specmap
	nameMap := map[string]proto.ID{}
	for name, m := range blobs {
		nameMap[name] = m.ID
	}

	spec, err := proto.NewSpec(time.Now().UnixNano(), nameMap, []string{})
	if err != nil {
		return
	}

	if c.DoCC {
		ccName := fmt.Sprintf("bar-spec-%d-%s.json",
			time.Now().UnixNano(), spec.ID)
		logx.Infof("storing carbon copy to %s", ccName)
		ccf, err := os.Create(lists.OSFromSlash(lists.OSJoin(c.WD, ccName)))
		if err != nil {
			return err
		}
		defer ccf.Close()

		if err = json.NewEncoder(ccf).Encode(&spec); err != nil {
			return err
		}
	}

	if !c.Upload {
		err = json.NewEncoder(c.Stdout).Encode(&spec)
		return
	}

	trans := transport.NewTransport(mod, "", c.Endpoint, c.PoolSize)
	if err = trans.UploadSpec(spec); err != nil {
		return
	}
	fmt.Fprint(c.Stdout, spec.ID)
	return
}