Exemple #1
0
func pullOne(c *cli.Context, name, driverName, registry string) error {
	t, err := common.NewTenet(common.TenetConfig{
		Name:     name,
		Driver:   driverName,
		Registry: registry,
	})
	if err != nil {
		return errors.Trace(err)
	}

	return t.Pull(c.Bool("update"))
}
Exemple #2
0
func newTenetCMDs(ctx *cli.Context, cfg common.TenetConfig) (*tenetCMDs, error) {
	tn, err := common.NewTenet(cfg)
	if err != nil {
		return nil, errors.Trace(err)
	}

	return &tenetCMDs{
		ctx: ctx,
		cfg: cfg,
		tn:  tn,
	}, nil
}
Exemple #3
0
func writeTenetDoc(c *cli.Context, src string, w io.Writer) error {
	// Find every applicable tenet for this project
	cfgPath, err := common.TenetCfgPath(c)
	if err != nil {
		return err
	}
	cfg, err := common.BuildConfig(cfgPath, common.CascadeUp)
	if err != nil {
		return err
	}

	r := strings.NewReplacer("/", "_")

	// Add the description of every tenet to the var map and special All array
	// Add keys for each tenet group name
	// DEMOWARE: This structure could be a lot simpler

	type result struct {
		name     string
		template string
		err      error
	}

	var wg sync.WaitGroup
	var ts []TenetMeta
	var results []result
	for _, group := range cfg.TenetGroups {
		for range group.Tenets {
			results = append(results, result{
				name: r.Replace(group.Name),
			})
		}
	}
	var i int
	for _, group := range cfg.TenetGroups {
		for _, tenetCfg := range group.Tenets {
			wg.Add(1)
			go func(group common.TenetGroup, tenetCfg common.TenetConfig, result *result) {
				defer wg.Done()

				t, err := common.NewTenet(tenetCfg)
				if err != nil {
					result.err = err
					return
				}

				s, err := t.OpenService()
				if err != nil {
					result.err = err
					return
				}
				defer s.Close()
				info, err := s.Info()
				if err != nil {
					result.err = err
					return
				}

				d, err := renderedDescription(info, tenetCfg)
				if err != nil {
					result.err = err
					return
				}

				ts = append(ts, TenetMeta{
					VarName:     r.Replace(tenetCfg.Name),
					GroupName:   r.Replace(group.Name),
					Description: d,
				})
				result.template = group.Template
			}(group, tenetCfg, &results[i])
			i++
		}
	}
	wg.Wait()

	// If any of the tenets could not be rendered, return an error now.
	var errs []error
	for _, result := range results {
		if result.err != nil {
			errs = append(errs, result.err)
		}
	}
	switch len(errs) {
	case 0:
	case 1:
		return errs[0]
	default:
		errorStrings := make([]string, len(errs))
		for i, err := range errs {
			errorStrings[i] = err.Error()
		}
		return errors.New(strings.Join(errorStrings, "\n"))
	}

	// Make the description available in multiple places:
	// Top level template
	// ├── All (array)
	// │   ├── desc1
	// │   └── desc2
	// ├── Groups (array)
	// │   ├── renderedGroup1
	// │   └── renderedGroup2
	// ├── tenetName1 (string) desc1
	// ├── tenetName2 (string) desc2
	// ├── groupName1 (string) renderedGroup1
	// └── groupName2 (string) renderedGroup2
	//
	// Group template
	// ├── All (array)
	// │   └── desc1
	// ├── GroupName (string) name from config
	// └── tenetName1 (string) desc1

	// Render groups first
	renderedGroups := make(map[string]string)
	for _, result := range results {
		g := make(map[string]interface{})
		g["All"] = []string{}
		g["GroupName"] = result.name
		for _, tm := range ts {
			if tm.GroupName == result.name {
				g["All"] = append(g["All"].([]string), tm.Description)
				g[tm.VarName] = tm.Description
			}
		}

		tpl, err := makeTemplate(result.template, defaultGroupTemplate)
		if err != nil {
			return err
		}

		var rg bytes.Buffer
		if err = tpl.Execute(&rg, g); err != nil {
			return err
		}
		renderedGroups[result.name] = rg.String()
	}

	v := make(map[string]interface{})
	v["All"] = []string{}
	v["Groups"] = renderedGroups
	for _, tm := range ts {
		v["All"] = append(v["All"].([]string), tm.Description)
		v[tm.VarName] = tm.Description
		for _, result := range results {
			v[result.name] = renderedGroups[result.name]
		}
	}

	if src == "" {
		src = cfg.Template
	}
	tpl, err := makeTemplate(src, defaultTemplate)
	if err != nil {
		return err
	}

	return tpl.Execute(w, v)
}