// WriteUnit writes a systemd unit in the given path with the given unit options
// if no previous error occurred.
8078
func (uw *UnitWriter) WriteUnit(path string, errmsg string, opts ...*unit.UnitOption) {
	if uw.err != nil {
		return
	}

	file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		uw.err = errwrap.Wrap(errors.New(errmsg), err)
		return
	}
	defer file.Close()

	if _, err = io.Copy(file, unit.Serialize(opts)); err != nil {
		uw.err = errwrap.Wrap(errors.New(errmsg), err)
		return
	}
	if err := user.ShiftFiles([]string{path}, &uw.p.UidRange); err != nil {
		uw.err = errwrap.Wrap(errors.New(errmsg), err)
		return
	}
}
805432
func (s *stage) createUnits(config types.Config) error {
	enabledOneUnit := false
	for _, unit := range config.Systemd.Units {
		if err := s.writeSystemdUnit(unit, false); err != nil {
			return err
		}
		if unit.Enabled != nil {
			if *unit.Enabled {
				if err := s.Logger.LogOp(
					func() error { return s.EnableUnit(unit) },
					"enabling unit %q", unit.Name,
				); err != nil {
					return err
				}
			} else {
				if err := s.Logger.LogOp(
					func() error { return s.DisableUnit(unit) },
					"disabling unit %q", unit.Name,
				); err != nil {
					return err
				}
			}
			enabledOneUnit = true
		}
		if unit.Mask != nil && *unit.Mask {
			if err := s.Logger.LogOp(
				func() error { return s.MaskUnit(unit) },
				"masking unit %q", unit.Name,
			); err != nil {
				return err
			}
		}
	}
	// and relabel the preset file itself if we enabled/disabled something
	if enabledOneUnit {
		s.relabel(util.PresetPath)
	}
	return nil
}
1177155
func WriteFile(t *testing.T, filepath string, bytes []byte) {
	t.Helper()
	if err := ioutil.WriteFile(filepath, bytes, 0644); err != nil {
		t.Fatalf("failed writing file: %s", err)
	}
}
625062
func (s *systemdServiceManager) WriteServiceFiles() error {
	machineAgent, unitAgents, _, err := s.FindAgents(paths.NixDataDir)
	if err != nil {
		return errors.Trace(err)
	}

	serviceNames, linkNames, failed, err := s.WriteSystemdAgents(
		machineAgent,
		unitAgents,
		paths.NixDataDir,
		systemd.EtcSystemdDir,
		systemd.EtcSystemdMultiUserDir,
	)
	if err != nil {
		for _, agent := range failed {
			logger.Errorf("failed to write service for %s: %s", agent, err)
		}
		logger.Errorf("%s", err)
		return errors.Trace(err)
	}
	for _, s := range serviceNames {
		logger.Infof("wrote %s agent, enabled and linked by systemd", s)
	}
	for _, s := range linkNames {
		logger.Infof("wrote %s agent, enabled and linked by symlink", s)
	}

	return errors.Trace(systemd.SysdReload())
}
25807
func Write(path string, buf []byte) error {
	return ioutil.WriteFile(path, buf, 0644)
}
1255485
func (s *Spend) SetUnit(v string) *Spend {
	s.Unit = &v
	return s
}
1065079
func (s *CfgServiceLBState) Write() error {
	key := fmt.Sprintf(serviceLBConfigPath, s.ID)
	err := s.StateDriver.WriteState(key, s, json.Marshal)
	return err
}
817478
func (g *Oper) Write() error {
	key := operGlobalPath
	return g.StateDriver.WriteState(key, g, json.Marshal)
}
870878
func WriteUUIDToFile(uuid *types.UUID, path string) error {
	return ioutil.WriteFile(path, []byte(uuid.String()), 0644)
}
543066
func (m *systemdUnitManager) Load(name string, u unit.UnitFile) error {
	m.mutex.Lock()
	defer m.mutex.Unlock()
	err := m.writeUnit(name, u.String())
	if err != nil {
		return err
	}
	if _, exists := u.Contents["Install"]; exists {
		log.Debugf("Detected [Install] section in the systemd unit (%s)", name)
		ok, err := m.enableUnit(name)
		if err != nil || !ok {
			m.removeUnit(name)
			return fmt.Errorf("Failed to enable systemd unit %s: %v", name, err)
		}
	}
	m.hashes[name] = u.Hash()
	return nil
}
269863
func (s *ConsumedCapacity) SetWriteCapacityUnits(v float64) *ConsumedCapacity {
	s.WriteCapacityUnits = &v
	return s
}
1029901
func (s *GetInstanceMetricDataInput) SetUnit(v string) *GetInstanceMetricDataInput {
	s.Unit = &v
	return s
}
591476
func (r *EtcdRegistry) CreateUnit(u *job.Unit) error {
	if err := r.storeOrGetUnitFile(u.Unit); err != nil {
		return err
	}

	jm := jobModel{
		Name:     u.Name,
		UnitHash: u.Unit.Hash(),
	}
	val, err := marshal(jm)
	if err != nil {
		return err
	}

	opts := &etcd.SetOptions{
		// Since we support replacing units, just ignore previous
		// job keys if they exist, this allows us to update the
		// job object key with a new unit.
		PrevExist: etcd.PrevIgnore,
	}
	key := r.prefixed(jobPrefix, u.Name, "object")
	_, err = r.kAPI.Set(context.Background(), key, val, opts)
	if err != nil {
		return err
	}

	return r.SetUnitTargetState(u.Name, u.TargetState)
}
1230203
func (c *FleetClient) nextUnit(component string) (num int, err error) {
	units, err := c.Units(component)
	if err != nil {
		return
	}
	num, err = nextUnitNum(units)
	if err != nil {
		return
	}
	return
}
1287754
func (s *systemd) MaskUnit(u Unit) error {
	masked := u.Destination(s.root)
	if _, err := os.Stat(masked); os.IsNotExist(err) {
		if err := os.MkdirAll(path.Dir(masked), os.FileMode(0755)); err != nil {
			return err
		}
	} else if err := os.Remove(masked); err != nil {
		return err
	}
	return os.Symlink("/dev/null", masked)
}
5799
func (n *Narcissus) Write(val interface{}) error {
	ref, err := structRef(val)
	if err != nil {
		return fmt.Errorf("invalid interface: %v", err)
	}

	err = n.Autoload(ref)
	if err != nil {
		return fmt.Errorf("failed to autoload file: %v", err)
	}

	path, err := getPath(ref)
	if err != nil {
		return fmt.Errorf("undefined path: %v", err)
	}

	err = n.writeStruct(ref, path)
	if err != nil {
		return fmt.Errorf("failed to write interface to tree: %v", err)
	}

	err = n.Augeas.Save()
	if err != nil {
		return fmt.Errorf("failed to save Augeas tree: %v", err)
	}

	return nil
}
29130
func (c *Conn) GetUnitPathProperties(path dbus.ObjectPath) (map[string]interface{}, error) {
	return c.getProperties(path, "org.freedesktop.systemd1.Unit")
}