func (this *ProjectEnvironmentController) ExecutionStatus() {

	executionid := this.Input().Get("executionid")
	envid := this.Input().Get("envid")
	projectenvid := this.Input().Get("projectenvid")

	o := orm.NewOrm()

	now := time.Now()

	fmt.Println(now)

	//获取项目环境信息进行编译部署
	var env models.Environmentinfo
	o.QueryTable("Environmentinfo").Filter("id", envid).One(&env)

	r := models.RunJobExecutions{}
	response := utility.RundeckExecutionInfo(env.Rundeckapiurl, env.Rundeckapiauthtoken, executionid)

	xml_err := xml.Unmarshal([]byte(response), &r)

	if xml_err != nil {
		fmt.Printf("error: %v", xml_err)
		this.Data["json"] = xml_err
		this.ServeJson()
	}

	switch r.Exs[0].Status {
	case "succeeded":
		o.QueryTable("Projectbuild").Filter("Executionid", executionid).Update(orm.Params{
			"BuildStatus": 3,
		})

		//获取项目环境信息进行编译部署
		var pb models.Projectbuild
		o.QueryTable("Projectbuild").Filter("Executionid", executionid).One(&pb)

		o.QueryTable("Projectenvironment").Filter("Id", projectenvid).Update(orm.Params{
			"Lastexcutiontime":    time.Now(),
			"LastBuildNumber":     pb.Buildnumber,
			"Lastbuildbranchname": pb.Branchname,
			"LastBuildBranchHash": pb.Branchhash,
		})
	case "failed":
		o.QueryTable("Projectbuild").Filter("Executionid", executionid).Update(orm.Params{
			"BuildStatus": 4,
		})
	}

	this.Data["json"] = models.ExecutionStatusApiModel{models.JsonResultBaseStruct{Result: true, Message: "操作成功"}, r.Exs[0].Status} //models.JsonResultBaseStruct{Result: true, Message: "操作成功"}
	this.ServeJson()
}
Exemple #2
0
func (this *TestController) Rundeck() {

	/*
	   	var data = []byte(`<executions count='1'>
	     <execution id='1081' href='http://192.168.50.20:4440/execution/follow/1081' status='running' project='CI'>
	       <user>admin</user>
	       <date-started unixtime='1441462368695'>2015-09-05T14:12:48Z</date-started>
	       <job id='305afa2d-82eb-435d-88ee-2b1d12b353cb' averageDuration='24812'>
	         <name>Config-Web</name>
	         <group>Go</group>
	         <project>CI</project>
	         <description></description>
	       </job>
	       <description>#!/bin/bash
	   pwd
	   export GOROOT=/opt/go/go
	   export GOPATH=/home/mtimegit/go
	   export PATH=$PATH:/opt/go/go/bin:/home/mtimegit/go/bin

	   sh contract.sh
	   echo @option.JOB_NAME@
	   /home/mtimegit/go/bin/config-web/config-web -s quit
	   /home/mtimegit/go/bin/mtimewall-service/mtimewall-service -s quit
	   /home/mtimegit/go/bin/captcha-service/captcha-service -s quit
	   rm -rf /home/mtimegit/go/src/mtime.com/basis
	   git clone -b @option.Branch_NAME@ [email protected]:go/basis /home/mtimegit/go/src/mtime.com/basis
	   gobuild -v mtime.com/basis/config-web
	   status=$?
	   echo "gobuild command exit stats - $status"
	   if [ $status -gt 0 ]
	   then
	   	echo fail
	   	exit 1
	   fi
	   #输出git分支名和Hash值
	   cd /home/mtimegit/go/src/mtime.com/basis
	   git rev-parse --abbrev-ref HEAD &gt; /home/mtimegit/go/bin/config-web/GitBranchName
	   git rev-parse HEAD &gt; /home/mtimegit/go/bin/config-web/GitBranchHash
	   /home/mtimegit/go/bin/config-web/config-web

	   #将原始程序部署到公共目录
	   rm -rf /mnt/025/MtimeGoConfigWeb/@option.BUILD_NUMBER@
	   mkdir /mnt/025/MtimeGoConfigWeb/@option.BUILD_NUMBER@
	   cp -r /home/mtimegit/go/bin/config-web /mnt/025/MtimeGoConfigWeb/@option.BUILD_NUMBER@/

	   gobuild -v mtime.com/basis/mtimewall-service
	   if [ $? -gt 0 ]
	   then
	   	echo fail
	   	exit 1
	   fi
	   /home/mtimegit/go/bin/mtimewall-service/mtimewall-service

	   gobuild -v mtime.com/basis/captcha-service
	   if [ $? -gt 0 ]
	   then
	   	echo fail
	   	exit 1
	   fi
	   /home/mtimegit/go/bin/captcha-service/captcha-service</description>
	       <argstring />
	     </execution>
	   </executions>`)
	*/

	o := orm.NewOrm()

	var logs []*models.Executionlog
	num, err := o.QueryTable("Executionlog").Filter("id", 1).All(&logs)
	fmt.Printf("Returned Rows Num: %s, %s", num, err)

	args := map[string]string{"BUILD_NUMBER": "10", "Branch_NAME": "develop"}

	response := utility.RundeckRunJob("http://192.168.50.20:4440/api/13/", "E4rNvVRV378knO9dp3d73O0cs1kd0kCd", "305afa2d-82eb-435d-88ee-2b1d12b353cb", args)
	fmt.Println(response)

	r := models.RunJobExecutions{}
	xml_err := xml.Unmarshal([]byte(response), &r)

	if xml_err != nil {
		fmt.Printf("error: %v", xml_err)
		this.Data["json"] = xml_err //url.QueryEscape(logs[0].Packagepath)
		this.ServeJson()
	}

	if r.Exs != nil {
		fmt.Println(r.Exs[0].Id)
	}

	fmt.Println(r)

	i := 0
	for i < 1 {
		r2 := models.RunJobExecutions{}
		response_2 := utility.RundeckExecutionInfo("http://192.168.50.20:4440/api/13/", "E4rNvVRV378knO9dp3d73O0cs1kd0kCd", r.Exs[0].Id)

		xml_err2 := xml.Unmarshal([]byte(response_2), &r2)

		if xml_err2 != nil {
			fmt.Printf("error: %v", xml_err2)
			this.Data["json"] = xml_err2 //url.QueryEscape(logs[0].Packagepath)
			this.ServeJson()
		}

		if r2.Exs != nil {
			fmt.Println(r.Exs[0].Id)
		}

		fmt.Println(r2)

		if r2.Exs[0].Status == "succeeded" {
			i = 1
		}

		time.Sleep(time.Second * 3)
	}

	/*
		client := &http.Client{}

		//reqest, _ := http.NewRequest("GET", "http://10.10.130.221:4440/api/13/job/30eb1ae2-540b-4cff-bcad-976762bc33d2/run?authtoken=E4rNvVRV378knO9dp3d73O0cs1kd0kCd&argString=-url+http%3A%2F%2F192.168.0.25%3A6666%2FMtimeGoConfigWeb%2F20150805164616%2Fconfig-web.tar.gz", nil)
		reqest, _ := http.NewRequest("GET", "http://10.10.130.221:4440/api/13/job/30eb1ae2-540b-4cff-bcad-976762bc33d2/run?authtoken=E4rNvVRV378knO9dp3d73O0cs1kd0kCd&argString=-url+"+url.QueryEscape(logs[0].Packagepath), nil)

		//reqest.Header.Set("Connection", "keep-alive")

		resp, err := client.Do(reqest)

		//resp, err := http.Get("http://api.m.mtime.cn/PageSubArea/HotPlayMovies.api?locationId=290")
		//req.Header.Add("If-None-Match", `W/"wyzzy"`)
		if err != nil {
			//handle error
		}
		defer resp.Body.Close()
		body, err := ioutil.ReadAll(resp.Body)
	*/

	this.Data["json"] = r //url.QueryEscape(logs[0].Packagepath)
	this.ServeJson()

	//this.Data["Website"] = "beego.me" //+ string(body)
	//this.Data["Email"] = "*****@*****.**"
	//this.TplNames = "index.tpl"
}
func (this *ProjectEnvironmentController) PublishPreApi() {

	//projectenvid := this.Input().Get("projectenvid")
	envid := this.Input().Get("envid")
	rundeckbuildjobid := this.Input().Get("rundeckbuildjobid")
	//	rundeckpackagejobid := this.Input().Get("rundeckpackagejobid")
	projectid := this.Input().Get("projectid")
	buildnumber := this.Input().Get("buildnumber")

	o := orm.NewOrm()

	var project models.Projectinfo
	o.QueryTable("Projectinfo").Filter("Id", projectid).One(&project)

	var preenv models.Environmentinfo
	o.QueryTable("Environmentinfo").Filter("id", envid).One(&preenv)

	var projectbuild models.Projectbuild
	o.QueryTable("Projectbuild").Filter("projectid", projectid).Filter("BuildNumber", buildnumber).One(&projectbuild)

	var localenv models.Environmentinfo
	o.QueryTable("Environmentinfo").Filter("id", projectbuild.Envid).One(&localenv)

	var localprojectenv models.Projectenvironment
	o.QueryTable("Projectenvironment").Filter("Projectid", projectid).Filter("Envid", projectbuild.Envid).One(&localprojectenv)

	//打包操作
	args := map[string]string{"BUILD_NUMBER": buildnumber, "Repository_Path": project.Repositorypath}

	response := utility.RundeckRunJob(localenv.Rundeckapiurl, localenv.Rundeckapiauthtoken, localprojectenv.Rundeckpackagejobid, args)

	fmt.Println(response)

	//parse rundeck api xml response
	r := models.RunJobExecutions{}
	xml_err := xml.Unmarshal([]byte(response), &r)

	if xml_err != nil {
		fmt.Printf("error: %v", xml_err)
		this.Data["json"] = xml_err //url.QueryEscape(logs[0].Packagepath)
		this.ServeJson()
	}

	//status_r := models.RunJobExecutions{}

	for i := 0; i < 1; {

		status_r := models.RunJobExecutions{}

		status_response := utility.RundeckExecutionInfo(localenv.Rundeckapiurl, localenv.Rundeckapiauthtoken, r.Exs[0].Id)

		status_xml_err := xml.Unmarshal([]byte(status_response), &status_r)

		if status_xml_err != nil {
			fmt.Printf("error: %v", status_xml_err)
			this.Data["json"] = status_xml_err
			this.ServeJson()
		}

		if status_r.Exs[0].Status == "succeeded" {
			i = 1
		} else {
			fmt.Println(status_r.Exs[0].Status)
		}

		time.Sleep(time.Second * 3)
	}

	//调用预上线环境rundeck
	pre_args := map[string]string{"url": project.Repositoryurl + buildnumber + "/pre/" + project.Packagefilename}

	prejob_response := utility.RundeckRunJob(preenv.Rundeckapiurl, preenv.Rundeckapiauthtoken, rundeckbuildjobid, pre_args)

	fmt.Println(prejob_response)

	//parse rundeck api xml response
	pre_r := models.RunJobExecutions{}
	pre_xml_err := xml.Unmarshal([]byte(prejob_response), &pre_r)

	if pre_xml_err != nil {
		fmt.Printf("error: %v", pre_xml_err)
		this.Data["json"] = pre_xml_err //url.QueryEscape(logs[0].Packagepath)
		this.ServeJson()
	}

	/*
		fmt.Println(projectenvid)
		fmt.Println(envid)
		fmt.Println(rundeckbuildjobid)
		fmt.Println(rundeckpackagejobid)
		fmt.Println(projectid)
		fmt.Println(buildnumber)
	*/

	this.Data["json"] = models.BuildApiModel{models.JsonResultBaseStruct{Result: true, Message: "操作成功"}, pre_r.Exs[0].Id} //status_r.Exs[0].Id}
	this.ServeJson()
}