// CreateUnit attempts to store a Unit and its associated unit file in the registry func (r *EtcdRegistry) CreateUnit(u *job.Unit) (err error) { if err := r.storeOrGetUnitFile(u.Unit); err != nil { return err } jm := jobModel{ Name: u.Name, UnitHash: u.Unit.Hash(), } json, err := marshal(jm) if err != nil { return } req := etcd.Create{ Key: path.Join(r.keyPrefix, jobPrefix, u.Name, "object"), Value: json, } _, err = r.etcd.Do(&req) if err != nil { if etcd.IsNodeExist(err) { err = errors.New("job already exists") } return } return r.SetUnitTargetState(u.Name, u.TargetState) }
func (r *EtcdRegistry) storeOrGetUnitFile(u unit.UnitFile) (err error) { um := unitModel{ Raw: u.String(), } json, err := marshal(um) if err != nil { return err } req := etcd.Create{ Key: r.hashedUnitPath(u.Hash()), Value: json, } _, err = r.etcd.Do(&req) // unit is already stored if err != nil && etcd.IsNodeExist(err) { // TODO(jonboulle): verify more here? err = nil } return }