add jenkins pre-check & fix jenkins flag

Signed-off-by: soulseen <sunzhu@yunify.com>
This commit is contained in:
soulseen
2019-05-05 22:37:01 +08:00
committed by zryfish
parent 2857071bdd
commit a57947fd45
5 changed files with 33 additions and 6 deletions

View File

@@ -30,6 +30,7 @@ import (
"kubesphere.io/kubesphere/pkg/apiserver/servicemesh/tracing" "kubesphere.io/kubesphere/pkg/apiserver/servicemesh/tracing"
"kubesphere.io/kubesphere/pkg/filter" "kubesphere.io/kubesphere/pkg/filter"
"kubesphere.io/kubesphere/pkg/informers" "kubesphere.io/kubesphere/pkg/informers"
"kubesphere.io/kubesphere/pkg/models/devops"
logging "kubesphere.io/kubesphere/pkg/models/log" logging "kubesphere.io/kubesphere/pkg/models/log"
"kubesphere.io/kubesphere/pkg/signals" "kubesphere.io/kubesphere/pkg/signals"
"kubesphere.io/kubesphere/pkg/simple/client/admin_jenkins" "kubesphere.io/kubesphere/pkg/simple/client/admin_jenkins"
@@ -98,6 +99,7 @@ func Run(s *options.ServerRunOptions) error {
} }
func initializeAdminJenkins() { func initializeAdminJenkins() {
devops.PreCheckJenkins()
admin_jenkins.Client() admin_jenkins.Client()
} }

View File

@@ -35,7 +35,7 @@ func GetPipeline(req *restful.Request, resp *restful.Response) {
return return
} }
resp.Header().Set(restful.HEADER_ContentType, "application/json") resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON)
resp.Write(res) resp.Write(res)
} }

View File

@@ -19,27 +19,47 @@ package devops
import ( import (
"compress/gzip" "compress/gzip"
"flag"
"fmt" "fmt"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
"github.com/emicklei/go-restful"
log "github.com/golang/glog" log "github.com/golang/glog"
"io" "io"
"io/ioutil" "io/ioutil"
"kubesphere.io/kubesphere/pkg/simple/client/admin_jenkins"
"net/http" "net/http"
"net/url" "net/url"
"strings"
"time" "time"
) )
const MIME_FORM = "application/x-www-form-urlencoded"
var ( var (
jenkinsUrl string jenkinsUrl string
jenkinsAdminUsername string jenkinsAdminUsername string
jenkinsAdminPassword string jenkinsAdminPassword string
) )
func init() { func PreCheckJenkins() {
flag.StringVar(&jenkinsUrl, "jenkins-url", "http://ks-jenkins.kubesphere-devops-system.svc.cluster.local:80", "jenkins server host") jenkinsUrl, jenkinsAdminUsername, jenkinsAdminPassword = admin_jenkins.GetJenkinsFlag()
flag.StringVar(&jenkinsAdminUsername, "jenkins-adminusername", "admin", "admin username of jenkins") baseUrl := jenkinsUrl + LoginUrl
flag.StringVar(&jenkinsAdminPassword, "jenkins-adminpassword", "passw0rd", "admin password of jenkins")
client := &http.Client{Timeout: 30 * time.Second}
loginReq, _ := http.NewRequest(http.MethodPost, baseUrl, strings.NewReader(""))
loginReq.Header.Add(restful.HEADER_ContentType, MIME_FORM)
loginReq.SetBasicAuth(jenkinsAdminUsername, jenkinsAdminPassword)
resp, err := client.Do(loginReq)
if err != nil {
log.Error("Check Jenkins Error: ", err)
}
if resp.StatusCode != http.StatusOK {
log.Error("Check Jenkins Error: ", resp.StatusCode, http.StatusText(resp.StatusCode))
} else {
log.Infof("Client Jenkins Success: " + baseUrl)
}
} }
func GetPipeline(projectName, pipelineName string, req *http.Request) ([]byte, error) { func GetPipeline(projectName, pipelineName string, req *http.Request) ([]byte, error) {

View File

@@ -49,4 +49,5 @@ const (
ToJsonUrl = "/pipeline-model-converter/toJson" ToJsonUrl = "/pipeline-model-converter/toJson"
GetNotifyCommitUrl = "/git/notifyCommit/?" GetNotifyCommitUrl = "/git/notifyCommit/?"
GithubWebhookUrl = "/github-webhook/" GithubWebhookUrl = "/github-webhook/"
LoginUrl = "/j_acegi_security_check"
) )

View File

@@ -40,6 +40,10 @@ func init() {
flag.IntVar(&jenkinsMaxConn, "jenkins-max-conn", 20, "max conn to jenkins") flag.IntVar(&jenkinsMaxConn, "jenkins-max-conn", 20, "max conn to jenkins")
} }
func GetJenkinsFlag() (string,string,string){
return jenkinsAdminAddress, jenkinsAdminUsername, jenkinsAdminPassword
}
func Client() *gojenkins.Jenkins { func Client() *gojenkins.Jenkins {
jenkinsClientOnce.Do(func() { jenkinsClientOnce.Do(func() {
jenkins := gojenkins.CreateJenkins(nil, jenkinsAdminAddress, jenkinsMaxConn, jenkinsAdminUsername, jenkinsAdminPassword) jenkins := gojenkins.CreateJenkins(nil, jenkinsAdminAddress, jenkinsMaxConn, jenkinsAdminUsername, jenkinsAdminPassword)