本文整理汇总了Golang中github.com/coreos/fleet/unit.NewUnitFile函数的典型用法代码示例。如果您正苦于以下问题:Golang NewUnitFile函数的具体用法?Golang NewUnitFile怎么用?Golang NewUnitFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewUnitFile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: NewUnit
// NewUnit takes a component type and returns a Fleet unit
// that includes the relevant systemd service template
func NewUnit(component string, templatePaths []string, decorate bool) (uf *unit.UnitFile, err error) {
template, err := readTemplate(component, templatePaths)
if err != nil {
return
}
if decorate {
decorator, err := readDecorator(component)
if err != nil {
return nil, err
}
uf, err = unit.NewUnitFile(string(template) + "\n" + string(decorator))
} else {
uf, err = unit.NewUnitFile(string(template))
}
return
}
示例2: newUnitFile
func newUnitFile(t *testing.T, contents string) *unit.UnitFile {
uf, err := unit.NewUnitFile(contents)
if err != nil {
t.Fatalf("error creating NewUnitFile from %s: %v", contents, err)
}
return uf
}
示例3: getUnitByHash
// getUnitByHash retrieves from the Registry the Unit associated with the given Hash
func (r *EtcdRegistry) getUnitByHash(hash unit.Hash) *unit.UnitFile {
key := r.hashedUnitPath(hash)
opts := &etcd.GetOptions{
Recursive: true,
}
resp, err := r.kAPI.Get(r.ctx(), key, opts)
if err != nil {
if isEtcdError(err, etcd.ErrorCodeKeyNotFound) {
err = nil
}
return nil
}
var um unitModel
if err := unmarshal(resp.Node.Value, &um); err != nil {
log.Errorf("error unmarshaling Unit(%s): %v", hash, err)
return nil
}
u, err := unit.NewUnitFile(um.Raw)
if err != nil {
log.Errorf("error parsing Unit(%s): %v", hash, err)
return nil
}
return u
}
示例4: newUF
func newUF(t *testing.T, contents string) unit.UnitFile {
uf, err := unit.NewUnitFile(contents)
if err != nil {
t.Fatalf("error creating new unit file from %v: %v", contents, err)
}
return *uf
}
示例5: newUnit
func newUnit(t *testing.T, str string) *unit.UnitFile {
u, err := unit.NewUnitFile(str)
if err != nil {
t.Fatalf("Unexpected error creating unit from %q: %v", str, err)
}
return u
}
示例6: getUnitByHash
// getUnitByHash retrieves from the Registry the Unit associated with the given Hash
func (r *EtcdRegistry) getUnitByHash(hash unit.Hash) *unit.UnitFile {
req := etcd.Get{
Key: r.hashedUnitPath(hash),
Recursive: true,
}
resp, err := r.etcd.Do(&req)
if err != nil {
if isKeyNotFound(err) {
err = nil
}
return nil
}
var um unitModel
if err := unmarshal(resp.Node.Value, &um); err != nil {
log.Errorf("error unmarshaling Unit(%s): %v", hash, err)
return nil
}
u, err := unit.NewUnitFile(um.Raw)
if err != nil {
log.Errorf("error parsing Unit(%s): %v", hash, err)
return nil
}
return u
}
示例7: StartUnitsInDir
func StartUnitsInDir(path string) {
files, _ := ioutil.ReadDir(path)
for _, f := range files {
unitpath := fmt.Sprintf("v1-alpha/units/%s", f.Name())
url := getFullAPIURL("10001", unitpath)
filepath := fmt.Sprintf("%s/%s", path, f.Name())
readfile, err := ioutil.ReadFile(filepath)
checkForErrors(err)
content := string(readfile)
u, _ := unit.NewUnitFile(content)
options_bytes, _ := json.Marshal(u.Options)
options_str := lowerCasingOfUnitOptionsStr(string(options_bytes))
json_str := fmt.Sprintf(
`{"name": "%s", "desiredState":"launched", "options": %s}`,
f.Name(),
options_str)
resp := httpPutRequest(url, []byte(json_str))
if resp.StatusCode != 204 {
body, err := ioutil.ReadAll(resp.Body)
log.Printf("[Error] in HTTP Body: %s", body)
checkForErrors(err)
}
}
}
示例8: TestRpcUnitToJobUnit
func TestRpcUnitToJobUnit(t *testing.T) {
contents := `
[Unit]
Description = Foo
`
unitFile, err := unit.NewUnitFile(contents)
if err != nil {
t.Fatalf("unexpected error parsing unit %q: %v", contents, err)
}
want := &pb.Unit{
Name: "foo",
Unit: unitFile.ToPB(),
DesiredState: pb.TargetState_LOADED,
}
expect := &job.Unit{
Name: "foo",
Unit: *unitFile,
TargetState: job.JobStateLoaded,
}
got := rpcUnitToJobUnit(want)
if !reflect.DeepEqual(got, expect) {
t.Fatalf("got %#v, expected %#v", got, expect)
}
}
示例9: newUnitWithMetadata
func newUnitWithMetadata(t *testing.T, metadata string) unit.UnitFile {
contents := fmt.Sprintf("[X-Fleet]\nMachineMetadata=%s", metadata)
u, err := unit.NewUnitFile(contents)
if err != nil {
t.Fatalf("error creating unit from %q: %v", contents, err)
}
return *u
}
示例10: getUnitFromFile
// getUnitFromFile attempts to load a Unit from a given filename
// It returns the Unit or nil, and any error encountered
func getUnitFromFile(file string) (*unit.UnitFile, error) {
out, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
return unit.NewUnitFile(string(out))
}
示例11: getUnitFromStdin
// getUnitFromStdin attempts to load a Unit from stdin
func getUnitFromStdin() (*unit.UnitFile, error) {
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
return unit.NewUnitFile(string(bytes))
}
示例12: TestFakeRegistryUnitLifecycle
func TestFakeRegistryUnitLifecycle(t *testing.T) {
reg := NewFakeRegistry()
units, err := reg.Units()
if err != nil {
t.Fatalf("Received error while calling Jobs: %v", err)
}
if !reflect.DeepEqual([]job.Unit{}, units) {
t.Fatalf("Expected no units, got %v", units)
}
uf, _ := unit.NewUnitFile("")
u1 := job.Unit{Name: "u1.service", Unit: *uf, TargetState: job.JobStateLoaded}
err = reg.CreateUnit(&u1)
if err != nil {
t.Fatalf("Received error while calling CreateUnit: %v", err)
}
units, err = reg.Units()
if err != nil {
t.Fatalf("Received error while calling Units: %v", err)
}
if len(units) != 1 {
t.Fatalf("Expected 1 Unit, got %v", units)
}
if !reflect.DeepEqual(u1, units[0]) {
t.Fatalf("Expected unit %v, got %v", u1, units[0])
}
err = reg.ScheduleUnit("u1.service", "XXX")
if err != nil {
t.Fatalf("Received error while calling ScheduleUnit: %v", err)
}
su, err := reg.ScheduledUnit("u1.service")
if err != nil {
t.Fatalf("Received error while calling ScheduledUnit: %v", err)
}
if su.TargetMachineID != "XXX" {
t.Fatalf("Unit should be scheduled to XXX, got %v", su.TargetMachineID)
}
err = reg.DestroyUnit("u1.service")
if err != nil {
t.Fatalf("Received error while calling DestroyUnit: %v", err)
}
units, err = reg.Units()
if err != nil {
t.Fatalf("Received error while calling Units: %v", err)
}
if !reflect.DeepEqual([]job.Unit{}, units) {
t.Fatalf("Expected no units, got %v", units)
}
}
示例13: NewUnit
// NewUnit takes a component type and returns a Fleet unit
// that includes the relevant systemd service template
func NewUnit(component string) (uf *unit.UnitFile, err error) {
template, err := readTemplate(component)
if err != nil {
return
}
uf, err = unit.NewUnitFile(string(template))
if err != nil {
return
}
return
}
示例14: getUnitFromFile
// getUnitFromFile attempts to load a Unit from a given filename
// It returns the Unit or nil, and any error encountered
func getUnitFromFile(file string) (*unit.UnitFile, error) {
out, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
unitName := path.Base(file)
log.Debugf("Unit(%s) found in local filesystem", unitName)
return unit.NewUnitFile(string(out))
}
示例15: buildWantedUnits
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
}