func AdminStartpg(w rest.ResponseWriter, r *rest.Request) { dbConn, err := util.GetConnection(CLUSTERADMIN_DB) if err != nil { logit.Error.Println("BackupNow: error " + err.Error()) rest.Error(w, err.Error(), 400) return } defer dbConn.Close() err = secimpl.Authorize(dbConn, r.PathParam("Token"), "perm-cluster") if err != nil { logit.Error.Println("AdminStartpg: authorize error " + err.Error()) rest.Error(w, err.Error(), http.StatusUnauthorized) return } ID := r.PathParam("ID") if ID == "" { logit.Error.Println("AdminStartpg: error node ID required") rest.Error(w, "node ID required", http.StatusBadRequest) return } dbNode, err := admindb.GetContainer(dbConn, ID) if err != nil { logit.Error.Println("AdminStartpg: " + err.Error()) rest.Error(w, err.Error(), http.StatusBadRequest) return } if dbNode.Role == "pgpool" { var spgresp cpmcontainerapi.StartPgpoolResponse spgresp, err = cpmcontainerapi.StartPgpoolClient(dbNode.Name) logit.Info.Println("AdminStartpg:" + spgresp.Output) } else { var srep cpmcontainerapi.StartPGResponse srep, err = cpmcontainerapi.StartPGClient(dbNode.Name) logit.Info.Println("AdminStartpg:" + srep.Output) } if err != nil { logit.Error.Println("AdminStartpg:" + err.Error()) rest.Error(w, err.Error(), http.StatusBadRequest) return } //give the UI a chance to see the start time.Sleep(3000 * time.Millisecond) w.WriteHeader(http.StatusOK) status := SimpleStatus{} status.Status = "OK" w.WriteJson(&status) }
func provisionImplInit(dbConn *sql.DB, params *cpmserverapi.DockerRunRequest, PROFILE string, standby bool) error { //go get the domain name from the settings var domainname admindb.Setting var pgport admindb.Setting var err error domainname, err = admindb.GetSetting(dbConn, "DOMAIN-NAME") if err != nil { logit.Error.Println("Provision:DOMAIN-NAME setting error " + err.Error()) return err } pgport, err = admindb.GetSetting(dbConn, "PG-PORT") if err != nil { logit.Error.Println("Provision:PG-PORT setting error " + err.Error()) return err } fqdn := params.ContainerName + "." + domainname.Value //we are depending on a DNS entry being created shortly after //creating the node in Docker //you might need to wait here until you can reach the new node's agent logit.Info.Println("PROFILE waiting till DNS ready") err = waitTillReady(fqdn) if err != nil { logit.Error.Println("Provision:" + err.Error()) return err } logit.Info.Println("checkpt 1") if standby { logit.Info.Println("standby node being created, will not initdb") } else { //initdb on the new node logit.Info.Println("PROFILE running initdb on the node") var resp cpmcontainerapi.InitdbResponse logit.Info.Println("checkpt 2") resp, err = cpmcontainerapi.InitdbClient(fqdn) if err != nil { logit.Error.Println("Provision:" + err.Error()) return err } logit.Info.Println("checkpt 3") logit.Info.Println("initdb output was" + resp.Output) logit.Info.Println("PROFILE initdb completed") //create postgresql.conf var data string var mode = "standalone" data, err = template.Postgresql(mode, pgport.Value, "") //place postgresql.conf on new node _, err = cpmcontainerapi.RemoteWritefileClient("/pgdata/"+params.ContainerName+"/postgresql.conf", data, fqdn) if err != nil { logit.Error.Println("Provision:" + err.Error()) return err } //create pg_hba.conf rules := make([]template.Rule, 0) data, err = template.Hba(dbConn, mode, params.ContainerName, pgport.Value, "", domainname.Value, rules) if err != nil { logit.Error.Println("Provision:" + err.Error()) return err } //place pg_hba.conf on new node _, err = cpmcontainerapi.RemoteWritefileClient("/pgdata/"+params.ContainerName+"/pg_hba.conf", data, fqdn) if err != nil { logit.Error.Println("Provision:" + err.Error()) return err } logit.Info.Println("PROFILE templates all built and copied to node") //start pg on new node var startResp cpmcontainerapi.StartPGResponse startResp, err = cpmcontainerapi.StartPGClient(fqdn) if err != nil { logit.Error.Println("Provision:" + err.Error()) return err } logit.Info.Println("startpg output was" + startResp.Output) //seed database with initial objects var seedResp cpmcontainerapi.SeedResponse seedResp, err = cpmcontainerapi.SeedClient(fqdn) if err != nil { logit.Error.Println("Provision:" + err.Error()) return err } logit.Info.Println("seed output was" + seedResp.Output) } logit.Info.Println("PROFILE node provisioning completed") return nil }
func configureCluster(dbConn *sql.DB, cluster admindb.Cluster, autocluster bool) error { logit.Info.Println("configureCluster:GetCluster") //get master node for this cluster master, err := admindb.GetContainerMaster(dbConn, cluster.ID) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } var pgport admindb.Setting pgport, err = admindb.GetSetting(dbConn, "PG-PORT") if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:GetContainerMaster") //configure master postgresql.conf file var data string if cluster.ClusterType == "synchronous" { data, err = template.Postgresql("master", pgport.Value, "*") } else { data, err = template.Postgresql("master", pgport.Value, "") } if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:master postgresql.conf generated") //write master postgresql.conf file remotely _, err = cpmcontainerapi.RemoteWritefileClient("/pgdata/postgresql.conf", data, master.Name) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:master postgresql.conf copied to remote") //get domain name var domainname admindb.Setting domainname, err = admindb.GetSetting(dbConn, "DOMAIN-NAME") if err != nil { logit.Error.Println("configureCluster: DOMAIN-NAME err " + err.Error()) return err } //configure master pg_hba.conf file rules := make([]template.Rule, 0) data, err = template.Hba(dbConn, "master", master.Name, pgport.Value, cluster.ID, domainname.Value, rules) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:master pg_hba.conf generated") //write master pg_hba.conf file remotely _, err = cpmcontainerapi.RemoteWritefileClient("/pgdata/pg_hba.conf", data, master.Name) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:master pg_hba.conf copied remotely") //restart postgres after the config file changes var stopResp cpmcontainerapi.StopPGResponse stopResp, err = cpmcontainerapi.StopPGClient(master.Name) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster: master stoppg output was" + stopResp.Output) var startResp cpmcontainerapi.StartPGResponse startResp, err = cpmcontainerapi.StartPGClient(master.Name) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:master startpg output was" + startResp.Output) //sleep loop until the master's PG can respond var found = false var currentStatus string var masterhost = master.Name for i := 0; i < 20; i++ { currentStatus, err = GetPGStatus2(dbConn, master.Name, masterhost) if currentStatus == "RUNNING" { logit.Info.Println("master is running...continuing") found = true break } else { logit.Info.Println("sleeping 1 sec waiting on master..") time.Sleep(1000 * time.Millisecond) } } if !found { logit.Info.Println("configureCluster: timed out waiting on master pg to start") return errors.New("timeout waiting for master pg to respond") } standbynodes, err2 := admindb.GetAllStandbyContainers(dbConn, cluster.ID) if err2 != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } //configure all standby nodes var stopPGResp cpmcontainerapi.StopPGResponse i := 0 for i = range standbynodes { if standbynodes[i].Role == STANDBY { //stop standby if !autocluster { stopPGResp, err = cpmcontainerapi.StopPGClient(standbynodes[i].Name) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:stop output was" + stopPGResp.Output) } //create base backup from master var backupresp cpmcontainerapi.BasebackupResponse backupresp, err = cpmcontainerapi.BasebackupClient(masterhost+"."+domainname.Value, standbynodes[i].Name) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:basebackup output was" + backupresp.Output) data, err = template.Recovery(masterhost, pgport.Value, "postgres") if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:standby recovery.conf generated") //write standby recovery.conf file remotely _, err = cpmcontainerapi.RemoteWritefileClient("/pgdata/recovery.conf", data, standbynodes[i].Name) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:standby recovery.conf copied remotely") data, err = template.Postgresql(STANDBY, pgport.Value, "") if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } //write standby postgresql.conf file remotely _, err = cpmcontainerapi.RemoteWritefileClient("/pgdata/postgresql.conf", data, standbynodes[i].Name) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:standby postgresql.conf copied remotely") //configure standby pg_hba.conf file data, err = template.Hba(dbConn, STANDBY, standbynodes[i].Name, pgport.Value, cluster.ID, domainname.Value, rules) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:standby pg_hba.conf generated") //write standby pg_hba.conf file remotely _, err = cpmcontainerapi.RemoteWritefileClient("/pgdata/pg_hba.conf", data, standbynodes[i].Name) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:standby pg_hba.conf copied remotely") //start standby var stResp cpmcontainerapi.StartPGOnStandbyResponse stResp, err = cpmcontainerapi.StartPGOnStandbyClient(standbynodes[i].Name) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:standby startpg output was" + stResp.Output) } i++ } logit.Info.Println("configureCluster: sleeping 5 seconds before configuring pgpool...") time.Sleep(5000 * time.Millisecond) pgpoolNode, err4 := admindb.GetContainerPgpool(dbConn, cluster.ID) logit.Info.Println("configureCluster: lookup pgpool node") if err4 != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:" + pgpoolNode.Name) //configure the pgpool includes all standby nodes AND the master node poolnames := make([]string, len(standbynodes)+1) i = 0 for i = range standbynodes { poolnames[i] = standbynodes[i].Name + "." + domainname.Value i++ } poolnames[i] = master.Name + "." + domainname.Value //generate pgpool.conf HOST_LIST data, err = template.Poolconf(poolnames) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:pgpool pgpool.conf generated") //write pgpool.conf to remote pool node _, err = cpmcontainerapi.RemoteWritefileClient(util.GetBase()+"/bin/"+"pgpool.conf", data, pgpoolNode.Name) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:pgpool pgpool.conf copied remotely") //generate pool_passwd data, err = template.Poolpasswd() if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:pgpool pool_passwd generated") //write pgpool.conf to remote pool node _, err = cpmcontainerapi.RemoteWritefileClient(util.GetBase()+"/bin/"+"pool_passwd", data, pgpoolNode.Name) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:pgpool pool_passwd copied remotely") //generate pool_hba.conf data, err = template.Poolhba() if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:pgpool pool_hba generated") //write pgpool.conf to remote pool node _, err = cpmcontainerapi.RemoteWritefileClient(util.GetBase()+"/bin/"+"pool_hba.conf", data, pgpoolNode.Name) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } logit.Info.Println("configureCluster:pgpool pool_hba copied remotely") //start pgpool var startPoolResp cpmcontainerapi.StartPgpoolResponse startPoolResp, err = cpmcontainerapi.StartPgpoolClient(pgpoolNode.Name) if err != nil { logit.Error.Println("configureCluster: " + err.Error()) return err } logit.Info.Println("configureCluster: pgpool startpgpool output was" + startPoolResp.Output) //finally, update the cluster to show that it is //initialized! cluster.Status = "initialized" err = admindb.UpdateCluster(dbConn, cluster) if err != nil { logit.Error.Println("configureCluster:" + err.Error()) return err } return nil }
func performConfigUpdate(dbConn *sql.DB, ContainerID string) error { logit.Info.Println("performConfigUpdate....") cars, err := GetAllContainerAccessRule(dbConn, ContainerID) if err != nil { logit.Error.Println(err.Error()) return err } container, err := admindb.GetContainer(dbConn, ContainerID) if err != nil { logit.Error.Println("GetNode: " + err.Error()) return err } var currentStatus string currentStatus, err = GetPGStatus2(dbConn, container.Name, container.Name) if err != nil { logit.Error.Println("GetNode:" + err.Error()) return err } if currentStatus != "RUNNING" { logit.Info.Println("performConfigUpdate....starting postgres") if container.Role == "pgpool" { var spgresp cpmcontainerapi.StartPgpoolResponse spgresp, err = cpmcontainerapi.StartPgpoolClient(container.Name) logit.Info.Println("AdminStartpg:" + spgresp.Output) } else { var srep cpmcontainerapi.StartPGResponse srep, err = cpmcontainerapi.StartPGClient(container.Name) logit.Info.Println("AdminStartpg:" + srep.Output) } if err != nil { logit.Error.Println("AdminStartpg:" + err.Error()) return err } //give the UI a chance to see the start time.Sleep(5000 * time.Millisecond) } //make template changes here logit.Info.Println("performConfigUpdate....making template changes") templateChange(dbConn, container.Name, cars, container.Role) //restart postgres logit.Info.Println("performConfigUpdate....stopping postgres") if container.Role == "pgpool" { var stoppoolResp cpmcontainerapi.StopPgpoolResponse stoppoolResp, err = cpmcontainerapi.StopPgpoolClient(container.Name) logit.Info.Println("AdminStoppg:" + stoppoolResp.Output) } else { var stoppgResp cpmcontainerapi.StopPGResponse stoppgResp, err = cpmcontainerapi.StopPGClient(container.Name) logit.Info.Println("AdminStoppg:" + stoppgResp.Output) } if err != nil { logit.Error.Println("AdminStoppg:" + err.Error()) return err } //give the UI a chance to see the stop time.Sleep(5000 * time.Millisecond) logit.Info.Println("performConfigUpdate....starting postgres") if container.Role == "pgpool" { var spgresp cpmcontainerapi.StartPgpoolResponse spgresp, err = cpmcontainerapi.StartPgpoolClient(container.Name) logit.Info.Println("AdminStartpg:" + spgresp.Output) } else { var srep cpmcontainerapi.StartPGResponse srep, err = cpmcontainerapi.StartPGClient(container.Name) logit.Info.Println("AdminStartpg:" + srep.Output) } if err != nil { logit.Error.Println("AdminStartpg:" + err.Error()) return err } return err }