Skip to content

Commit

Permalink
feat(cdsctl): allow to set CDN url as env variable (#6322)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt committed Oct 10, 2022
1 parent 3696ac1 commit 07d3583
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 77 deletions.
1 change: 1 addition & 0 deletions cli/cdsctl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func loadConfig(cmd *cobra.Command) (string, *cdsclient.Config, error) {

config := &cdsclient.Config{
Host: cdsctx.Host,
CDNHost: os.Getenv("CDS_CDN_URL"),
SessionToken: cdsctx.Session,
BuitinConsumerAuthenticationToken: cdsctx.Token,
Verbose: verbose,
Expand Down
4 changes: 4 additions & 0 deletions cli/cdsctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ Advanced usages:
CDS_API_URL="https://instance.cds.api" CDS_SESSION_TOKEN="yourtoken" CDS_HTTP_MAX_RETRY=10 cdsctl [command]
* you can override CDN url if needed using the variable CDS_CDN_URL:
CDS_API_URL="https://instance.cds.api" CDS_TOKEN="yourtoken" CDS_CDN_URL="https://instance.cds.cdn" cdsctl [command]
`,
}

Expand Down
15 changes: 8 additions & 7 deletions cli/cdsctl/workflow_artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var workflowArtifactDownloadCmd = cli.Command{
},
{
Name: "cdn-url",
Usage: "overwrite cdn url",
Usage: "overwrite cdn url (deprecated)",
Default: "",
},
},
Expand All @@ -59,12 +59,13 @@ func workflowArtifactDownloadRun(v cli.Values) error {
}

cdnURL := v.GetString("cdn-url")
if cdnURL == "" {
confCDN, err := client.ConfigCDN()
if err != nil {
return err
}
cdnURL = confCDN.HTTPURL
if cdnURL != "" {
fmt.Printf("Flag cdn-url is deprecated, use CDS_CDN_URL env variable instead\n")
}

cdnURL, err = client.CDNURL()
if err != nil {
return err
}

// Search in result
Expand Down
30 changes: 13 additions & 17 deletions cli/cdsctl/workflow_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,6 @@ func workflowLogDownloadRun(v cli.Values) error {
}
}

feature, err := client.FeatureEnabled(sdk.FeatureCDNJobLogs, map[string]string{
"project_key": projectKey,
})
if err != nil {
return err
}

var ok bool
for _, log := range logs {
if reg != nil && !reg.MatchString(log.getFilename()) {
Expand All @@ -301,15 +294,13 @@ func workflowLogDownloadRun(v cli.Values) error {

// If cdn logs is enabled for current project, first check if logs can be downloaded from it
var link *sdk.CDNLogLink
if !feature.Exists || feature.Enabled {
if log.detailType == workflowLogDetailTypeService {
link, err = client.WorkflowNodeRunJobServiceLink(context.Background(), projectKey, workflowName, log.runID, log.jobID, log.serviceName)
} else {
link, err = client.WorkflowNodeRunJobStepLink(context.Background(), projectKey, workflowName, log.runID, log.jobID, log.stepOrder)
}
if err != nil {
return err
}
if log.detailType == workflowLogDetailTypeService {
link, err = client.WorkflowNodeRunJobServiceLink(context.Background(), projectKey, workflowName, log.runID, log.jobID, log.serviceName)
} else {
link, err = client.WorkflowNodeRunJobStepLink(context.Background(), projectKey, workflowName, log.runID, log.jobID, log.stepOrder)
}
if err != nil {
return err
}

data, err := client.WorkflowLogDownload(context.Background(), *link)
Expand Down Expand Up @@ -353,6 +344,11 @@ func workflowLogStreamRun(v cli.Values) error {
projectKey := v.GetString(_ProjectKey)
workflowName := v.GetString(_WorkflowName)

cdnURL, err := client.CDNURL()
if err != nil {
return err
}

runNumber, err := workflowLogSearchNumber(v)
if err != nil {
return err
Expand Down Expand Up @@ -423,7 +419,7 @@ func workflowLogStreamRun(v cli.Values) error {
goRoutines := sdk.NewGoRoutines(ctx)
goRoutines.Exec(ctx, "WebsocketEventsListenCmd", func(ctx context.Context) {
for ctx.Err() == nil {
if err := client.RequestWebsocket(ctx, goRoutines, fmt.Sprintf("%s/item/stream", link.CDNURL), chanMessageToSend, chanMsgReceived, chanErrorReceived); err != nil {
if err := client.RequestWebsocket(ctx, goRoutines, fmt.Sprintf("%s/item/stream", cdnURL), chanMessageToSend, chanMsgReceived, chanErrorReceived); err != nil {
fmt.Printf("Error: %s\n", err)
}
time.Sleep(1 * time.Second)
Expand Down
22 changes: 6 additions & 16 deletions cli/cdsctl/workflow_run_interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ func workflowRunInteractive(v cli.Values, w *sdk.WorkflowRun, baseURL string) er
projectKey := v.GetString(_ProjectKey)
workflowName := v.GetString(_WorkflowName)

feature, err := client.FeatureEnabled(sdk.FeatureCDNJobLogs, map[string]string{
"project_key": projectKey,
})
if err != nil {
return err
}

for {
var errrg error
wo, errrg = client.WorkflowRunGet(projectKey, v.GetString(_WorkflowName), w.Number)
Expand All @@ -51,23 +44,20 @@ func workflowRunInteractive(v cli.Values, w *sdk.WorkflowRun, baseURL string) er

jobLine := fmt.Sprintf("%s %s/%s/%s/%s %s %s \n", status, v.GetString(_WorkflowName), wnr.WorkflowNodeName, stage.Name, job.Job.Action.Name, start, end)
if job.Status == sdk.StatusFail {
newOutput += fmt.Sprintf(tm.Color(tm.Bold(jobLine), tm.RED))
newOutput += tm.Color(tm.Bold(jobLine), tm.RED)
} else {
newOutput += fmt.Sprintf(tm.Bold(jobLine))
newOutput += tm.Bold(jobLine)
}

for _, info := range job.SpawnInfos {
newOutput += fmt.Sprintf("\nInformations: %s - %s", info.APITime, info.UserMessage)
}
newOutput += fmt.Sprintf("\n")
newOutput += "\n"

for _, step := range job.Job.StepStatus {
var link *sdk.CDNLogLink
if feature.Enabled {
link, err = client.WorkflowNodeRunJobStepLink(context.Background(), projectKey, workflowName, wnr.ID, job.ID, int64(step.StepOrder))
if err != nil {
return err
}
link, err := client.WorkflowNodeRunJobStepLink(context.Background(), projectKey, workflowName, wnr.ID, job.ID, int64(step.StepOrder))
if err != nil {
return err
}

buf, err := client.WorkflowLogDownload(context.Background(), *link)
Expand Down
4 changes: 2 additions & 2 deletions cli/cdsctl/workflow_run_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func workflowRunResultGet(v cli.Values) error {
}
resultID := v.GetString("result-id")

confCDN, err := client.ConfigCDN()
cdnURL, err := client.CDNURL()
if err != nil {
return err
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func workflowRunResultGet(v cli.Values) error {
return cli.NewError("unable to open file %s: %s", fileName, err)
}
fmt.Printf("Downloading %s...\n", fileName)
if err := client.CDNItemDownload(context.Background(), confCDN.HTTPURL, cdnHash, sdk.CDNTypeItemRunResult, md5, f); err != nil {
if err := client.CDNItemDownload(context.Background(), cdnURL, cdnHash, sdk.CDNTypeItemRunResult, md5, f); err != nil {
_ = f.Close()
return err
}
Expand Down
6 changes: 0 additions & 6 deletions engine/api/workflow_run_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ func (api *API) getWorkflowNodeRunJobLogLinkHandler(ctx context.Context, w http.
return sdk.NewErrorFrom(err, "invalid node job id")
}

httpURL, err := services.GetCDNPublicHTTPAdress(ctx, api.mustDB())
if err != nil {
return err
}

nodeRun, err := workflow.LoadNodeRun(api.mustDB(), projectKey, workflowName, nodeRunID, workflow.LoadRunOptions{DisableDetailledNodeRun: true})
if err != nil {
return sdk.WrapError(err, "cannot find nodeRun %d for workflow %s in project %s", nodeRunID, workflowName, projectKey)
Expand Down Expand Up @@ -221,7 +216,6 @@ func (api *API) getWorkflowNodeRunJobLogLinkHandler(ctx context.Context, w http.
apiRefHash := strconv.FormatUint(apiRefHashU, 10)

return service.WriteJSON(w, sdk.CDNLogLink{
CDNURL: httpURL,
ItemType: itemType,
APIRef: apiRefHash,
}, http.StatusOK)
Expand Down
1 change: 0 additions & 1 deletion engine/api/workflow_run_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func Test_getWorkflowNodeRunJobLinkHandler(t *testing.T) {

var link sdk.CDNLogLink
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &link))
require.Equal(t, "http://cdn.net:8080", link.CDNURL)
require.Equal(t, sdk.CDNTypeItemStepLog, link.ItemType)
require.NotEmpty(t, link.APIRef)
}
22 changes: 0 additions & 22 deletions engine/cdn/cdn_log_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package cdn

import (
"context"
"fmt"
"strings"
"time"

gocache "github.com/patrickmn/go-cache"
"github.com/rockbears/log"

"github.com/ovh/cds/engine/cache"
Expand Down Expand Up @@ -180,23 +178,3 @@ func (s *Service) canDequeue(ctx context.Context, jobID string) (string, error)
}
return jobQueueKey, nil
}

// Check if storage on CDN is enabled
func (s *Service) cdnEnabled(ctx context.Context, projectKey string) bool {
cacheKey := fmt.Sprintf("cdn-job-logs-enabled-project-%s", projectKey)
enabledI, has := runCache.Get(cacheKey)
if has {
return enabledI.(bool)
}

resp, err := s.Client.FeatureEnabled(sdk.FeatureCDNJobLogs, map[string]string{
"project_key": projectKey,
})
if err != nil {
log.Error(ctx, "unable to get job logs feature for project %s: %v", projectKey, err)
return false
}
enabled := !resp.Exists || resp.Enabled
runCache.Set(cacheKey, enabled, gocache.DefaultExpiration)
return enabled
}
2 changes: 2 additions & 0 deletions engine/worker/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (

// CDS API URL
CDSApiUrl = "CDS_API_URL"
CDSCDNUrl = "CDS_CDN_URL"
)

type logger struct {
Expand Down Expand Up @@ -280,6 +281,7 @@ func (wk *CurrentWorker) Environ() []string {
newEnv = append(newEnv, "CDS_KEY=********") //We have to let it here for some legacy reason
newEnv = append(newEnv, fmt.Sprintf("%s=%d", WorkerServerPort, wk.HTTPPort()))
newEnv = append(newEnv, fmt.Sprintf("%s=%s", CDSApiUrl, wk.cfg.APIEndpoint))
newEnv = append(newEnv, fmt.Sprintf("%s=%s", CDSCDNUrl, wk.cfg.CDNEndpoint))

if wk.currentJob.wJob != nil {
data := []byte(wk.currentJob.wJob.Job.Job.Action.Name)
Expand Down
1 change: 0 additions & 1 deletion sdk/cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ type CDNLogLinkData struct {
}

type CDNLogLink struct {
CDNURL string `json:"cdn_url,omitempty"`
ItemType CDNItemType `json:"item_type"`
APIRef string `json:"api_ref"`
}
Expand Down
11 changes: 11 additions & 0 deletions sdk/cdsclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ func (c *client) APIURL() string {
return c.config.Host
}

func (c *client) CDNURL() (string, error) {
if c.config.CDNHost == "" {
confCDN, err := c.ConfigCDN()
if err != nil {
return "", err
}
c.config.CDNHost = confCDN.HTTPURL
}
return c.config.CDNHost, nil
}

func (c *client) HTTPClient() *http.Client {
return c.httpClient
}
Expand Down
6 changes: 5 additions & 1 deletion sdk/cdsclient/client_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,11 @@ func (c *client) WorkflowAccess(ctx context.Context, projectKey string, workflow
}

func (c *client) WorkflowLogDownload(ctx context.Context, link sdk.CDNLogLink) ([]byte, error) {
downloadURL := fmt.Sprintf("%s/item/%s/%s/download", link.CDNURL, link.ItemType, link.APIRef)
cdnURL, err := c.CDNURL()
if err != nil {
return nil, err
}
downloadURL := fmt.Sprintf("%s/item/%s/%s/download", cdnURL, link.ItemType, link.APIRef)
data, _, _, err := c.Request(context.Background(), http.MethodGet, downloadURL, nil, func(req *http.Request) {
auth := "Bearer " + c.config.SessionToken
req.Header.Add("Authorization", auth)
Expand Down
7 changes: 4 additions & 3 deletions sdk/cdsclient/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"github.com/ovh/cds/sdk"
)

//Config is the configuration data used by the cdsclient interface implementation
// Config is the configuration data used by the cdsclient interface implementation
type Config struct {
Host string
CDNHost string
User string
SessionToken string
BuitinConsumerAuthenticationToken string
Expand All @@ -20,15 +21,15 @@ type Config struct {
Mutex *sync.Mutex
}

//ProviderConfig is the configuration data used by the cdsclient ProviderClient interface implementation
// ProviderConfig is the configuration data used by the cdsclient ProviderClient interface implementation
type ProviderConfig struct {
Host string
Token string
RequestSecondsTimeout int
InsecureSkipVerifyTLS bool
}

//ServiceConfig is the configuration data used by the cdsclient interface implementation
// ServiceConfig is the configuration data used by the cdsclient interface implementation
type ServiceConfig struct {
Host string
Token string
Expand Down
1 change: 1 addition & 0 deletions sdk/cdsclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ type Interface interface {
ActionClient
Admin
APIURL() string
CDNURL() (string, error)
ApplicationClient
ConfigUser() (sdk.ConfigUser, error)
ConfigCDN() (sdk.CDNConfig, error)
Expand Down
15 changes: 15 additions & 0 deletions sdk/cdsclient/mock_cdsclient/interface_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion sdk/featureflipping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package sdk
type FeatureName string

const (
FeatureCDNJobLogs FeatureName = "cdn-job-logs"
FeatureMFARequired FeatureName = "mfa_required"
FeaturePurgeName FeatureName = "workflow-retention-policy"
FeaturePurgeMaxRuns FeatureName = "workflow-retention-maxruns"
Expand Down

0 comments on commit 07d3583

Please sign in to comment.