func delete(serviceName string, etcdPath string) error { log.Infoln("cli.delete():Start Delete") client, err := etcdclient.NewEtcdClient(etcdPath) if err != nil { log.Fatalf("cli.delete():%+v\n", err) fmt.Printf("[error]cli.delete():%+v\n", err) return err } //quary service s, err := client.GetService(serviceName) if err != nil { log.Fatalf("cli.delete():%+v\n", err) fmt.Printf("[error]cli.delete():%+v\n", err) return err } //delete from etcd err = client.DeleteService(serviceName) if err != nil { log.Fatalf("cli.delete():%+v\n", err) fmt.Printf("[error]cli.delete():%+v\n", err) } //delete from swarm err = service.DeleteService(s) if err != nil { log.Fatalf("cli.delete():%+v\n", err) fmt.Printf("[error]cli.delete():%+v\n", err) return err } return nil }
func getServices(w http.ResponseWriter, r *http.Request) { // servicesName save on etcd etcd, err := etcdclient.NewEtcdClient(etcdclient.EtcdPath) if err != nil { httpError(w, err.Error(), http.StatusInternalServerError) return } servicesName, err := etcd.GetServices() if err != nil { httpError(w, err.Error(), http.StatusInternalServerError) return } json.NewEncoder(w).Encode(servicesName) }
func servicesWatcher(etcdPath string) error { log.Infoln("cli.servicesWatcher():Start servicesWatcher") heartbeat := time.Duration(HeartbeatSecond) * time.Second log.Debugf("cli.servicesWatcher(): heartbeat: %d second\n", HeartbeatSecond) etcd, err := etcdclient.NewEtcdClient(etcdPath) if err != nil { log.Fatalf("cli.servicesWatcher():%+v\n", err) return err } swarm, err := swarmclient.NewSwarmClient() if err != nil { log.Fatalf("cli.servicesWatcher():%+v\n", err) return err } go func() { for { // get services name from etcd servicesName, err := etcd.GetServices() log.Infof("cli.servicesWatcher(): Services: %+v\n", servicesName) fmt.Printf("[info]:manage: Services %+v\n", servicesName) if err != nil { time.Sleep(heartbeat) continue } for _, serviceName := range servicesName { //get service info with serviceName s, err := etcd.GetService(serviceName) if err != nil { log.Fatalf("cli.servicesWatcher():%+v\n", err) } //get containers info with containerIds containers, status := swarm.GetContainersInfo(s.ContainersIds) if status != 1 { // try to restart container , if it can't restart remove it status = swarm.ContainersHealthCheck(containers) } si := &service.ServiceInfo{Service: *s, Containers: containers, Status: status} service.ServicesInfo = append(service.ServicesInfo, *si) log.Debugf("cli.servicesWatcher(): %s info: %+v\n", serviceName, si) } time.Sleep(heartbeat) } }() return nil }
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 getService(w http.ResponseWriter, r *http.Request) { serviceName := mux.Vars(r)["name"] etcd, err := etcdclient.NewEtcdClient(etcdclient.EtcdPath) if err != nil { httpError(w, err.Error(), http.StatusInternalServerError) return } s, err := etcd.GetService(serviceName) if err != nil { httpError(w, err.Error(), http.StatusInternalServerError) return } si, err := service.GetService(s) if err != nil { httpError(w, err.Error(), http.StatusInternalServerError) return } json.NewEncoder(w).Encode(si) }
// for start,stop,restart,delete func doService(w http.ResponseWriter, r *http.Request, commond service.ServiceFunc, comstr string) { serviceName := mux.Vars(r)["name"] etcd, err := etcdclient.NewEtcdClient(etcdclient.EtcdPath) if err != nil { httpError(w, err.Error(), http.StatusInternalServerError) return } s, err := etcd.GetService(serviceName) if err != nil { httpError(w, err.Error(), http.StatusInternalServerError) return } err = commond(s) if err != nil { httpError(w, err.Error(), http.StatusInternalServerError) return } else { json.NewEncoder(w).Encode(comstr + " Service [" + serviceName + "] success") } }
//**** func createService(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { httpError(w, err.Error(), http.StatusInternalServerError) return } jsonconfig, _ := ioutil.ReadAll(r.Body) config := new(service.ServiceConfig) json.Unmarshal([]byte(jsonconfig), &config) //create service on swarm containerIds, err := service.CreateService(config) if err != nil { httpError(w, err.Error(), http.StatusInternalServerError) return } //fmt.Print("[Info] containerIDs ") //fmt.Println(containerIds) //create service on etcd client, err := etcdclient.NewEtcdClient(etcdclient.EtcdPath) if err != nil { httpError(w, err.Error(), http.StatusInternalServerError) return } yamlconfig, err := service.MarshalYaml(config) if err != nil { httpError(w, err.Error(), http.StatusInternalServerError) return } err = client.CreateService(config.Metadata.Name, string(yamlconfig), containerIds) if err != nil { httpError(w, err.Error(), http.StatusInternalServerError) return } json.NewEncoder(w).Encode("CreateService" + " Service [" + config.Metadata.Name + "] success") }