// CreateService creates a service
// kubeURL - the URL to the kube
// podName - the pod name
// return an error if anything goes wrong
func CreateService(kubeURL string, serviceInfo template.KubeServiceParams) error {
	var s1data []byte
	var err error

	logit.Info.Println("create service called")
	/**
	client, err := getHttpClient()
	if err != nil {
		logit.Error.Println(err.Error())
		return err
	}
	*/

	s1data, err = template.KubeNodeService(serviceInfo)
	if err != nil {
		logit.Error.Println("CreateService:" + err.Error())
		return err
	}
	logit.Info.Println("create service request...")
	logit.Info.Println(string(s1data[:]))

	/**
	// POST admin SERVICE at port 13000
	resp1, err1 := client.Post(serviceurl, bodyType, bytes.NewReader(s1data))
	if err1 != nil {
		logit.Error.Println(err1.Error())
		return err1
	}
	defer resp1.Body.Close()

	// Dump response
	data, err4 := ioutil.ReadAll(resp1.Body)
	if err4 != nil {
		logit.Error.Println(err4.Error())
		return err4
	}
	logit.Info.Println("create service response..." + string(data))
	*/
	return nil
}
func provisionImpl(dbConn *sql.DB, params *cpmserverapi.DockerRunRequest, PROFILE string, standby bool) error {
	logit.Info.Println("PROFILE: provisionImpl starts 1")

	var errorStr string
	//make sure the container name is not already taken
	_, err := admindb.GetContainerByName(dbConn, params.ContainerName)
	if err != nil {
		if err != sql.ErrNoRows {
			return err
		}
	} else {
		errorStr = "container name" + params.ContainerName + " already used can't provision"
		logit.Error.Println("Provision error" + errorStr)
		return errors.New(errorStr)
	}

	//create the container by constructing a template and calling openshift

	params.CPU, params.MEM, err = getDockerResourceSettings(dbConn, PROFILE)
	if err != nil {
		logit.Error.Println("Provision: problem in getting profiles call" + err.Error())
		return err
	}

	//remove any existing pods and services with this name
	var username = "******"
	var password = "******"
	var objectName = params.ContainerName
	var objectType = "pod"

	err = OpenshiftDelete(username, password, objectName, objectType)
	if err != nil {
		logit.Info.Println("Provision:" + err.Error())
	}

	objectName = params.ContainerName
	err = OpenshiftDelete(username, password, objectName, objectType)
	if err != nil {
		logit.Info.Println("Provision:" + err.Error())
	}

	podInfo := template.KubePodParams{
		NAME:                 params.ContainerName,
		ID:                   params.ContainerName,
		PODID:                params.ContainerName,
		CPU:                  params.CPU,
		MEM:                  params.MEM,
		IMAGE:                params.Image,
		VOLUME:               params.PGDataPath,
		PORT:                 "13000",
		BACKUP_NAME:          "",
		BACKUP_SERVERNAME:    "",
		BACKUP_SERVERIP:      "",
		BACKUP_SCHEDULEID:    "",
		BACKUP_PROFILENAME:   "",
		BACKUP_CONTAINERNAME: "",
		BACKUP_PATH:          "",
		BACKUP_HOST:          "",
		BACKUP_PORT:          "",
		BACKUP_USER:          "",
		BACKUP_SERVER_URL:    "",
	}

	//generate the pod template
	var podTemplateData []byte
	podTemplateData, err = template.KubeNodePod(podInfo)
	if err != nil {
		logit.Error.Println("Provision:" + err.Error())
		return err
	}
	logit.Info.Println("pod template=" + string(podTemplateData[:]))

	//create the pod
	file, err := ioutil.TempFile("/tmp", "openshift-template")
	if err != nil {
		logit.Error.Println("Provision:" + err.Error())
		return err
	}
	defer os.Remove(file.Name())
	err = ioutil.WriteFile(file.Name(), podTemplateData, 0644)
	if err != nil {
		logit.Error.Println("Provision:" + err.Error())
		return err
	}

	err = OpenshiftCreate(username, password, file.Name())
	if err != nil {
		logit.Info.Println("Provision:" + err.Error())
	}

	var pgport admindb.Setting
	pgport, err = admindb.GetSetting(dbConn, "PG-PORT")
	if err != nil {
		logit.Error.Println("Provision:PG-PORT setting error " + err.Error())
		return err
	}

	//generate the admin service template
	serviceInfo := template.KubeServiceParams{
		SERVICENAME: params.ContainerName,
		NAME:        params.ContainerName,
		PORT:        "10001",
		DBPORT:      pgport.Value,
	}

	//create the admin service template
	var serviceTemplateData []byte
	serviceTemplateData, err = template.KubeNodeService(serviceInfo)
	if err != nil {
		logit.Error.Println("Provision:" + err.Error())
		return err
	}
	logit.Info.Println("service template=" + string(serviceTemplateData[:]))

	file, err = ioutil.TempFile("/tmp", "openshift-template")
	if err != nil {
		logit.Error.Println("Provision:" + err.Error())
		return err
	}
	defer os.Remove(file.Name())
	err = ioutil.WriteFile(file.Name(), serviceTemplateData, 0644)
	if err != nil {
		logit.Error.Println("Provision:" + err.Error())
		return err
	}

	//create the service
	err = OpenshiftCreate(username, password, file.Name())
	if err != nil {
		logit.Info.Println("Provision:" + err.Error())
	}

	dbnode := admindb.Container{}
	dbnode.ID = ""
	dbnode.Name = params.ContainerName
	dbnode.Image = params.Image
	dbnode.ClusterID = "-1"
	dbnode.ProjectID = params.ProjectID
	dbnode.ServerID = params.ServerID

	if params.Standalone == "true" {
		dbnode.Role = "standalone"
	} else {
		dbnode.Role = "unassigned"
	}

	var strid int
	strid, err = admindb.InsertContainer(dbConn, dbnode)
	newid := strconv.Itoa(strid)
	if err != nil {
		logit.Error.Println("Provision:" + err.Error())
		return err
	}
	dbnode.ID = newid

	//register default db users on the new node
	err = createDBUsers(dbConn, dbnode)

	return err

}