func create(localfilepath string, etcdpath string) error { log.Infoln("cli.create():Start Create") log.Debugln("cli.create():ConfigFile Path:" + localfilepath) filename, _ := filepath.Abs(localfilepath) yamlconfig, err := ioutil.ReadFile(filename) if err != nil { log.Fatalf("cli.create():%+v\n", err) fmt.Printf("[error]cli.create():%+v\n", err) return err } // unmarshal yaml file config, err := service.UnmarshalYaml(yamlconfig) if err != nil { log.Fatalf("cli.create():%+v\n", err) fmt.Printf("[error]cli.create():%+v\n", err) return err } fmt.Printf("[Info] yaml config:%+v\n", config) log.Debugf("cli.create():yaml config:%+v\n", config) //create service on swarm containerIds, err := service.CreateService(config) if err != nil { fmt.Printf("[error]cli.create():%+v\n", err) return err } fmt.Printf("[Info] containerIDs:%+v\n ", containerIds) log.Infof("cli.create():containerIDs :%+v\n", containerIds) //create service on etcd client, err := etcdclient.NewEtcdClient(etcdpath) if err != nil { log.Fatalf("cli.create():%+v\n", err) fmt.Printf("[error]cli.create():%+v\n", err) return err } err = client.CreateService(config.Metadata.Name, string(yamlconfig), containerIds) if err != nil { log.Fatalf("cli.create():%+v\n", err) fmt.Printf("[error]cli.create():%+v\n", err) return err } return nil }
func (e *Etcd) GetService(serviceName string) (*service.Service, error) { s := new(service.Service) servicePath := path.Join(TycoonDir, serviceName) goption := new(client.GetOptions) goption.Recursive = true // etcd get Response, err := e.client.Get(context.Background(), servicePath, goption) log.Debugf("etcdclient.GetService():GetService On etcd Respone %+v", Response) if err != nil { return s, err } nodes := Response.Node.Nodes // Unmarshal for index := range nodes { if nodes[index].Key == servicePath+"/ServiceConfig" { sc, err := service.UnmarshalYaml([]byte(nodes[index].Value)) if err != nil { return s, err } s.ServiceConfig = *sc } if nodes[index].Key == servicePath+"/ContainerIds" { idnodes := nodes[index].Nodes ids := make([]string, 0) for ids_index := range idnodes { ids = append(ids, idnodes[ids_index].Value) } s.ContainersIds = ids } } return s, err }