-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(worker,api): allow to override cds.version value (#5365)
- Loading branch information
Showing
30 changed files
with
1,056 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-- +migrate Up | ||
ALTER TABLE "workflow_run" ADD COLUMN IF NOT EXISTS "version" VARCHAR(256); | ||
SELECT create_unique_index('workflow_run','IDX_WORKFLOW_RUN_WORKFLOW_ID_VERSION','workflow_id,version'); | ||
|
||
-- +migrate Down | ||
DROP INDEX idx_workflow_run_workflow_id_version; | ||
ALTER TABLE "workflow_run" DROP COLUMN IF EXISTS "version"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/ovh/cds/engine/worker/internal" | ||
"github.com/ovh/cds/engine/worker/pkg/workerruntime" | ||
"github.com/ovh/cds/sdk" | ||
) | ||
|
||
var ( | ||
cmdCDSSetVersionValue string | ||
) | ||
|
||
func cmdCDSVersionSet() *cobra.Command { | ||
c := &cobra.Command{ | ||
Use: "set-version", | ||
Short: "Override {{.cds.version}} value with given string. This value should be unique for the workflow and can't be changed when set.", | ||
Run: cdsVersionSetCmd(), | ||
} | ||
return c | ||
} | ||
|
||
func cdsVersionSetCmd() func(cmd *cobra.Command, args []string) { | ||
return func(cmd *cobra.Command, args []string) { | ||
f := func() error { | ||
if len(args) < 1 || args[0] == "" { | ||
return fmt.Errorf("invalid given value for CDS version") | ||
} | ||
|
||
portS := os.Getenv(internal.WorkerServerPort) | ||
if portS == "" { | ||
return fmt.Errorf("%s not found, are you running inside a CDS worker job?", internal.WorkerServerPort) | ||
} | ||
|
||
port, err := strconv.Atoi(portS) | ||
if err != nil { | ||
return fmt.Errorf("cannot parse '%s' as a port number", portS) | ||
} | ||
|
||
a := workerruntime.CDSVersionSet{ | ||
Value: args[0], | ||
} | ||
|
||
data, err := json.Marshal(a) | ||
if err != nil { | ||
return sdk.WithStack(err) | ||
} | ||
|
||
req, err := http.NewRequest("POST", fmt.Sprintf("http://127.0.0.1:%d/version", port), bytes.NewReader(data)) | ||
if err != nil { | ||
return fmt.Errorf("cannot post set version (Request): %s", err) | ||
} | ||
|
||
client := http.DefaultClient | ||
client.Timeout = 5 * time.Minute | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("command failed: %v", err) | ||
} | ||
|
||
if resp.StatusCode >= 300 { | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return fmt.Errorf("set version failed: unable to read body %v", err) | ||
} | ||
defer resp.Body.Close() | ||
return sdk.DecodeError(body) | ||
} | ||
|
||
fmt.Printf("CDS version was set to %s\n", a.Value) | ||
|
||
return nil | ||
} | ||
|
||
if err := f(); err != nil { | ||
if sdk.IsErrorWithStack(err) { | ||
httpErr := sdk.ExtractHTTPError(err, "") | ||
sdk.Exit("%v", httpErr.Error()) | ||
} else { | ||
sdk.Exit("%v", err) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/ovh/cds/engine/worker/pkg/workerruntime" | ||
"github.com/ovh/cds/sdk" | ||
) | ||
|
||
func setVersionHandler(ctx context.Context, wk *CurrentWorker) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
data, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
writeError(w, r, sdk.NewError(sdk.ErrWrongRequest, err)) | ||
return | ||
} | ||
defer r.Body.Close() | ||
|
||
var req workerruntime.CDSVersionSet | ||
if err := json.Unmarshal(data, &req); err != nil { | ||
writeError(w, r, sdk.NewError(sdk.ErrWrongRequest, err)) | ||
return | ||
} | ||
|
||
if req.Value == "" { | ||
writeError(w, r, sdk.NewErrorFrom(sdk.ErrWrongRequest, "invalid given CDS version value")) | ||
return | ||
} | ||
|
||
if err := wk.client.QueueJobSetVersion(ctx, wk.currentJob.wJob.ID, sdk.WorkflowRunVersion{ | ||
Value: req.Value, | ||
}); err != nil { | ||
writeError(w, r, err) | ||
return | ||
} | ||
|
||
// Override cds.version value in params to allow usage of this value in others steps | ||
for i := range wk.currentJob.params { | ||
if wk.currentJob.params[i].Name == "cds.version" { | ||
wk.currentJob.params[i].Value = req.Value | ||
break | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.