Exemple #1
0
func (d *deployer) buildWantedUnits() (map[string]*schema.Unit, error) {
	units := make(map[string]*schema.Unit)
	servicesDefinition, err := d.serviceDefinitionClient.servicesDefinition()
	if err != nil {
		return nil, err
	}
	for _, srv := range servicesDefinition.Services {
		vars := make(map[string]interface{})
		serviceTemplate, err := d.serviceDefinitionClient.serviceFile(srv)
		if err != nil {
			log.Printf("%v", err)
			continue
		}
		vars["version"] = srv.Version
		serviceFile, err := renderedServiceFile(serviceTemplate, vars)
		if err != nil {
			log.Printf("%v", err)
			return nil, err
		}

		// fleet deploy
		uf, err := unit.NewUnitFile(serviceFile)
		if err != nil {
			//Broken service file, skip it and continue
			log.Printf("WARNING service file %s is incorrect: %v [SKIPPING]", srv.Name, err)
			continue
		}

		if srv.Count == 0 && !strings.Contains(srv.Name, "@") {
			u := &schema.Unit{
				Name:         srv.Name,
				Options:      schema.MapUnitFileToSchemaUnitOptions(uf),
				DesiredState: srv.DesiredState,
			}

			units[srv.Name] = u
		} else if srv.Count > 0 && strings.Contains(srv.Name, "@") {
			for i := 0; i < srv.Count; i++ {
				xName := strings.Replace(srv.Name, "@", fmt.Sprintf("@%d", i+1), -1)

				u := &schema.Unit{
					Name:         xName,
					Options:      schema.MapUnitFileToSchemaUnitOptions(uf),
					DesiredState: srv.DesiredState,
				}

				units[u.Name] = u
			}
		} else {
			log.Printf("WARNING skipping service: %s, incorrect service definition", srv.Name)
		}
	}
	return units, nil
}
Exemple #2
0
func createUnit(name string, uf *unit.UnitFile) (*schema.Unit, error) {
	if uf == nil {
		return nil, fmt.Errorf("nil unit provided")
	}
	u := schema.Unit{
		Name:    name,
		Options: schema.MapUnitFileToSchemaUnitOptions(uf),
	}
	// TODO(jonboulle): this dependency on the API package is awkward, and
	// redundant with the check in api.unitsResource.set, but it is a
	// workaround to implementing the same check in the RegistryClient. It
	// will disappear once RegistryClient is deprecated.
	if err := api.ValidateName(name); err != nil {
		return nil, err
	}
	if err := api.ValidateOptions(u.Options); err != nil {
		return nil, err
	}
	j := &job.Job{Unit: *uf}
	if err := j.ValidateRequirements(); err != nil {
		log.Warningf("Unit %s: %v", name, err)
	}
	err := cAPI.CreateUnit(&u)
	if err != nil {
		return nil, fmt.Errorf("failed creating unit %s: %v", name, err)
	}

	log.Debugf("Created Unit(%s) in Registry", name)
	return &u, nil
}
Exemple #3
0
// Create schedules a new unit for the given component
// and blocks until the unit is loaded
func (c *FleetClient) Create(targets []string) error {
	units := make([]*schema.Unit, len(targets))
	for i, target := range targets {
		unitName, unitFile, err := c.createUnitFile(target)
		if err != nil {
			return err
		}
		units[i] = &schema.Unit{
			Name:    unitName,
			Options: schema.MapUnitFileToSchemaUnitOptions(unitFile),
		}
	}
	for _, unit := range units {
		// schedule unit
		if err := c.Fleet.CreateUnit(unit); err != nil {
			// ignore units that already exist
			if err.Error() != "job already exists" {
				return fmt.Errorf("failed creating job %s: %v", unit.Name, err)
			}
		}
		desiredState := string(job.JobStateLoaded)
		if err := c.Fleet.SetUnitTargetState(unit.Name, desiredState); err != nil {
			return err
		}
	}
	for _, unit := range units {
		desiredState := string(job.JobStateLoaded)
		outchan, errchan := waitForUnitStates([]string{unit.Name}, desiredState)
		if err := printUnitState(unit.Name, outchan, errchan); err != nil {
			return err
		}
	}
	return nil
}
func (srv *Service) unitToOptions(desiredUnit Unit) ([]*schema.UnitOption, error) {
	uf, err := unit.NewUnitFile(srv.UnitTemplate)
	if err != nil {
		return nil, mask(err)
	}
	options := schema.MapUnitFileToSchemaUnitOptions(uf)
	options = append(options, &schema.UnitOption{
		Section: "X-Fleet",
		Name:    "MachineID",
		Value:   desiredUnit.MachineID,
	})
	return options, nil
}
Exemple #5
0
func createUnit(name string, uf *unit.UnitFile) (*schema.Unit, error) {
	u := schema.Unit{
		Name:    name,
		Options: schema.MapUnitFileToSchemaUnitOptions(uf),
	}
	err := cAPI.CreateUnit(&u)
	if err != nil {
		return nil, fmt.Errorf("failed creating unit %s: %v", name, err)
	}

	log.V(1).Infof("Created Unit(%s) in Registry", name)
	return &u, nil
}
Exemple #6
0
func (b *Builder) buildCustomService() []*schema.UnitOption {
	unitOptions := schema.MapUnitFileToSchemaUnitOptions(b.unitFile)

	nomiNotifiers := []*schema.UnitOption{
		{
			Section: "Service",
			Name:    "ExecStartPre",
			Value:   "/bin/sh -c '/usr/bin/curl -s http://" + b.listenAddr + "/hello/%i'",
		},
		{
			Section: "Service",
			Name:    "ExecStopPost",
			Value:   "/usr/bin/curl -s http://" + b.listenAddr + "/bye/%i",
		},
	}

	unitOptions = append(unitOptions, nomiNotifiers...)

	return unitOptions
}
Exemple #7
0
// Create schedules unit files for the given components.
func (c *FleetClient) Create(
	targets []string, wg *sync.WaitGroup, outchan chan string, errchan chan error) {

	units := make([]*schema.Unit, len(targets))

	for i, target := range targets {
		unitName, unitFile, err := c.createUnitFile(target)
		if err != nil {
			errchan <- err
			return
		}
		units[i] = &schema.Unit{
			Name:    unitName,
			Options: schema.MapUnitFileToSchemaUnitOptions(unitFile),
		}
	}

	for _, unit := range units {
		wg.Add(1)
		go doCreate(c, unit, wg, outchan, errchan)
	}
}
Exemple #8
0
// Create schedules unit files for the given components.
func (c *FleetClient) Create(
	targets []string, wg *sync.WaitGroup, out, ew io.Writer) {

	units := make([]*schema.Unit, len(targets))

	for i, target := range targets {
		unitName, unitFile, err := c.createUnitFile(target)
		if err != nil {
			fmt.Fprintf(ew, "Error creating: %s\n", err)
			return
		}
		units[i] = &schema.Unit{
			Name:    unitName,
			Options: schema.MapUnitFileToSchemaUnitOptions(unitFile),
		}
	}

	for _, unit := range units {
		wg.Add(1)
		go doCreate(c, unit, wg, out, ew)
	}
}