Merge pull request #1480 from yashsinghcodes/fix-for-git-proxy

fix to use http_proxy for git clone
This commit is contained in:
Frikky
2024-08-19 18:06:25 +02:00
committed by GitHub
2 changed files with 59 additions and 9 deletions
+52
View File
@@ -36,6 +36,7 @@ import (
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory" "github.com/go-git/go-git/v5/storage/memory"
gitProxy "github.com/go-git/go-git/v5/plumbing/transport"
// Random // Random
xj "github.com/basgys/goxml2json" xj "github.com/basgys/goxml2json"
@@ -396,6 +397,52 @@ func checkUsername(Username string) error {
return nil return nil
} }
func isGitNoProxy(rawURL string) bool {
noProxy := os.Getenv("NO_PROXY")
if noProxy == "" {
return false
}
if noProxy == "*" {
return true
}
noProxyList := strings.Split(noProxy, ",")
parsedURL, err := url.Parse(rawURL)
if err != nil {
return false
}
host := parsedURL.Hostname()
for _,value := range noProxyList {
value = strings.TrimSpace(value)
if host == value {
return true
}
if strings.HasPrefix(value, "*.") && strings.HasSuffix(host, value[2:]){
return true
}
}
return false
}
func checkGitProxy(cloneOptions *git.CloneOptions) *git.CloneOptions {
if os.Getenv("HTTP_PROXY") != "" && !isGitNoProxy(cloneOptions.URL){
cloneOptions.ProxyOptions = gitProxy.ProxyOptions{
URL: os.Getenv("HTTP_PROXY"),
}
}
if os.Getenv("HTTPS_PROXY") != "" && !isGitNoProxy(cloneOptions.URL) {
cloneOptions.ProxyOptions = gitProxy.ProxyOptions{
URL: os.Getenv("HTTPS_PROXY"),
}
}
return cloneOptions
}
func createNewUser(username, password, role, apikey string, org shuffle.OrgMini) error { func createNewUser(username, password, role, apikey string, org shuffle.OrgMini) error {
// Returns false if there is an issue // Returns false if there is an issue
// Use this for register // Use this for register
@@ -4201,6 +4248,8 @@ func runInitEs(ctx context.Context) {
} }
} }
cloneOptions = checkGitProxy(cloneOptions)
branch := os.Getenv("SHUFFLE_DOWNLOAD_AUTH_BRANCH") branch := os.Getenv("SHUFFLE_DOWNLOAD_AUTH_BRANCH")
if len(branch) > 0 && branch != "master" && branch != "main" { if len(branch) > 0 && branch != "master" && branch != "main" {
cloneOptions.ReferenceName = plumbing.ReferenceName(branch) cloneOptions.ReferenceName = plumbing.ReferenceName(branch)
@@ -4245,6 +4294,9 @@ func runInitEs(ctx context.Context) {
cloneOptions := &git.CloneOptions{ cloneOptions := &git.CloneOptions{
URL: apis, URL: apis,
} }
cloneOptions = checkGitProxy(cloneOptions)
_, err = git.Clone(storer, fs, cloneOptions) _, err = git.Clone(storer, fs, cloneOptions)
if err != nil { if err != nil {
log.Printf("[ERROR] Failed loading repo %s into memory: %s", apis, err) log.Printf("[ERROR] Failed loading repo %s into memory: %s", apis, err)
+7 -9
View File
@@ -106,7 +106,6 @@ func createSchedule(ctx context.Context, scheduleId, workflowId, name, startNode
} }
log.Printf("[INFO] Starting frequency for execution: %d", newfrequency) log.Printf("[INFO] Starting frequency for execution: %d", newfrequency)
//jobret, err := newscheduler.Every(newfrequency).Seconds().NotImmediately().Run(job) //jobret, err := newscheduler.Every(newfrequency).Seconds().NotImmediately().Run(job)
jobret, err := newscheduler.Every(newfrequency).Seconds().Run(job) jobret, err := newscheduler.Every(newfrequency).Seconds().Run(job)
@@ -309,7 +308,7 @@ func handleGetWorkflowqueue(resp http.ResponseWriter, request *http.Request) {
if envData.Swarm { if envData.Swarm {
env.Licensed = true env.Licensed = true
env.RunType = "docker" env.RunType = "docker"
} }
if envData.Kubernetes { if envData.Kubernetes {
env.RunType = "k8s" env.RunType = "k8s"
@@ -741,7 +740,6 @@ func handleWorkflowQueue(resp http.ResponseWriter, request *http.Request) {
func runWorkflowExecutionTransaction(ctx context.Context, attempts int64, workflowExecutionId string, actionResult shuffle.ActionResult, resp http.ResponseWriter) { func runWorkflowExecutionTransaction(ctx context.Context, attempts int64, workflowExecutionId string, actionResult shuffle.ActionResult, resp http.ResponseWriter) {
log.Printf("[DEBUG][%s] Running workflow execution update", workflowExecutionId) log.Printf("[DEBUG][%s] Running workflow execution update", workflowExecutionId)
// Should start a tx for the execution here // Should start a tx for the execution here
workflowExecution, err := shuffle.GetWorkflowExecution(ctx, workflowExecutionId) workflowExecution, err := shuffle.GetWorkflowExecution(ctx, workflowExecutionId)
if err != nil { if err != nil {
@@ -925,7 +923,7 @@ func deleteWorkflow(resp http.ResponseWriter, request *http.Request) {
if len(workflow.ParentWorkflowId) > 0 { if len(workflow.ParentWorkflowId) > 0 {
resp.WriteHeader(403) resp.WriteHeader(403)
resp.Write([]byte(`{"success": false, "reason": "Can't delete a workflow distributed from your parent org"}`)) resp.Write([]byte(`{"success": false, "reason": "Can't delete a workflow distributed from your parent org"}`))
return return
} }
if user.Id != workflow.Owner || len(user.Id) == 0 { if user.Id != workflow.Owner || len(user.Id) == 0 {
@@ -984,8 +982,6 @@ func deleteWorkflow(resp http.ResponseWriter, request *http.Request) {
resp.Write([]byte(`{"success": true}`)) resp.Write([]byte(`{"success": true}`))
} }
func handleExecution(id string, workflow shuffle.Workflow, request *http.Request, orgId string) (shuffle.WorkflowExecution, string, error) { func handleExecution(id string, workflow shuffle.Workflow, request *http.Request, orgId string) (shuffle.WorkflowExecution, string, error) {
//go func() { //go func() {
// log.Printf("\n\nPRE TIME: %s\n\n", time.Now().Format("2006-01-02 15:04:05")) // log.Printf("\n\nPRE TIME: %s\n\n", time.Now().Format("2006-01-02 15:04:05"))
@@ -1080,7 +1076,6 @@ func handleExecution(id string, workflow shuffle.Workflow, request *http.Request
} }
} }
err := imageCheckBuilder(execInfo.ImageNames) err := imageCheckBuilder(execInfo.ImageNames)
if err != nil { if err != nil {
log.Printf("[ERROR] Failed building the required images from %#v: %s", execInfo.ImageNames, err) log.Printf("[ERROR] Failed building the required images from %#v: %s", execInfo.ImageNames, err)
@@ -2665,6 +2660,8 @@ func loadGithubWorkflows(url, username, password, userId, branch, orgId string)
cloneOptions.ReferenceName = plumbing.ReferenceName(branch) cloneOptions.ReferenceName = plumbing.ReferenceName(branch)
} }
cloneOptions = checkGitProxy(cloneOptions)
storer := memory.NewStorage() storer := memory.NewStorage()
r, err := git.Clone(storer, fs, cloneOptions) r, err := git.Clone(storer, fs, cloneOptions)
if err != nil { if err != nil {
@@ -3410,7 +3407,7 @@ func executeSingleAction(resp http.ResponseWriter, request *http.Request) {
// FIXME: Should use environment that is in the source workflow if it exists // FIXME: Should use environment that is in the source workflow if it exists
for i, _ := range workflowExecution.Workflow.Actions { for i, _ := range workflowExecution.Workflow.Actions {
workflowExecution.Workflow.Actions[i].Environment = environment workflowExecution.Workflow.Actions[i].Environment = environment
workflowExecution.Workflow.Actions[i].Label = "TMP" workflowExecution.Workflow.Actions[i].Label = "TMP"
} }
shuffle.SetWorkflowExecution(ctx, workflowExecution, false) shuffle.SetWorkflowExecution(ctx, workflowExecution, false)
@@ -3940,6 +3937,8 @@ func LoadSpecificApps(resp http.ResponseWriter, request *http.Request) {
} }
} }
cloneOptions = checkGitProxy(cloneOptions)
storer := memory.NewStorage() storer := memory.NewStorage()
r, err := git.Clone(storer, fs, cloneOptions) r, err := git.Clone(storer, fs, cloneOptions)
if err != nil { if err != nil {
@@ -4193,7 +4192,6 @@ func checkUnfinishedExecution(resp http.ResponseWriter, request *http.Request) {
log.Printf("[ERROR] Failed adding execution to db: %s", err) log.Printf("[ERROR] Failed adding execution to db: %s", err)
} }
resp.WriteHeader(200) resp.WriteHeader(200)
resp.Write([]byte(fmt.Sprintf(`{"success": true, "reason": "Reran workflow in %s"}`, parsedEnv))) resp.Write([]byte(fmt.Sprintf(`{"success": true, "reason": "Reran workflow in %s"}`, parsedEnv)))