Skip to content
This repository has been archived by the owner on Feb 20, 2020. It is now read-only.

Commit

Permalink
Bug 1516458 - pass --root-url to tc-proxy and set TASKCLUSTER_PROXY_URL
Browse files Browse the repository at this point in the history
With this change, the proxy knows the rootUrl, and the task has a
reliable indication of the proxy URL

There is no provision for a feature to set a task environment variable,
and in fact features are not even initialized yet when the task commands
are created, so this special-cases the TaskclusterProxyFeature for
inclusion in the task environment.
  • Loading branch information
djmitche committed Jan 3, 2019
1 parent 4b043b1 commit 4c39491
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 6 deletions.
11 changes: 11 additions & 0 deletions plat_all-unix-style.go
Expand Up @@ -104,6 +104,15 @@ func install(arguments map[string]interface{}) (err error) {
return nil
}

// Set an environment variable in each command. This can be called from a feature's
// NewTaskFeature method to set variables for the task.
func (task *TaskRun) setVariable(variable string, value string) error {
for i := range task.Commands {
task.Commands[i].Cmd.Env = append(task.Commands[i].Cmd.Env, fmt.Sprintf("%s=%s", variable, value))
}
return nil
}

func (task *TaskRun) EnvVars() []string {
workerEnv := os.Environ()
taskEnv := map[string]string{}
Expand All @@ -121,6 +130,8 @@ func (task *TaskRun) EnvVars() []string {
taskEnv[k] = v
}
taskEnv["TASK_ID"] = task.TaskID
taskEnv["TASKCLUSTER_ROOT_URL"] = config.RootURL

for i, j := range taskEnv {
taskEnvArray = append(taskEnvArray, i+"="+j)
}
Expand Down
14 changes: 14 additions & 0 deletions plat_windows.go
Expand Up @@ -397,6 +397,20 @@ func (task *TaskRun) prepareCommand(index int) *CommandExecutionError {
return nil
}

// Set an environment variable in each command. This can be called from a feature's
// NewTaskFeature method to set variables for the task.
func (task *TaskRun) setVariable(variable string, value string) error {
for i := range task.Commands {
newEnv := []string{fmt.Sprintf("%s=%s", variable, value)}
combined, err := win32.MergeEnvLists(&task.Commands[i].Cmd.Env, &newEnv)
if err != nil {
return err
}
task.Commands[i].Cmd.Env = *combined
}
return nil
}

// Only return critical errors
func purgeOldTasks() error {
if config.CleanUpTaskDirs {
Expand Down
8 changes: 8 additions & 0 deletions taskcluster_proxy.go
Expand Up @@ -53,13 +53,21 @@ func (l *TaskclusterProxyTask) RequiredScopes() scopes.Required {
}

func (l *TaskclusterProxyTask) Start() *CommandExecutionError {
// Set TASKCLUSTER_PROXY_URL in the task environment
err := l.task.setVariable("TASKCLUSTER_PROXY_URL",
fmt.Sprintf("http://localhost:%d", config.TaskclusterProxyPort))
if err != nil {
return MalformedPayloadError(err)
}

// include all scopes from task.scopes, as well as the scope to create artifacts on
// this task (which cannot be represented in task.scopes)
scopes := append(l.task.Definition.Scopes,
fmt.Sprintf("queue:create-artifact:%s/%d", l.task.TaskID, l.task.RunID))
taskclusterProxy, err := tcproxy.New(
config.TaskclusterProxyExecutable,
config.TaskclusterProxyPort,
config.RootURL,
&tcclient.Credentials{
AccessToken: l.task.TaskClaimResponse.Credentials.AccessToken,
Certificate: l.task.TaskClaimResponse.Credentials.Certificate,
Expand Down
10 changes: 9 additions & 1 deletion taskcluster_proxy_test.go
Expand Up @@ -7,16 +7,24 @@ import (
)

func TestTaskclusterProxy(t *testing.T) {
if os.Getenv("TASKCLUSTER_CLIENT_ID") == "" ||
os.Getenv("TASKCLUSTER_ACCESS_TOKEN") == "" ||
os.Getenv("TASKCLUSTER_ROOT_URL") == "" {
t.Skip("Skipping test since TASKCLUSTER_{CLIENT_ID,ACCESS_TOKEN,ROOT_URL} env vars not set")
}

defer setup(t)()
payload := GenericWorkerPayload{
Command: append(
append(
goEnv(),
// long enough to reclaim and get new credentials
sleep(12)...,
),
goRun(
"curlget.go",
fmt.Sprintf("http://localhost:%v/queue/v1/task/KTBKfEgxR5GdfIIREQIvFQ/runs/0/artifacts/SampleArtifacts/_/X.txt", config.TaskclusterProxyPort),
// note that curlget.go supports substituting the proxy URL from its runtime environment
fmt.Sprintf("TASKCLUSTER_PROXY_URL/queue/v1/task/KTBKfEgxR5GdfIIREQIvFQ/runs/0/artifacts/SampleArtifacts/_/X.txt"),
)...,
),
MaxRunTime: 60,
Expand Down
3 changes: 2 additions & 1 deletion tcproxy/tcproxy.go
Expand Up @@ -25,9 +25,10 @@ type TaskclusterProxy struct {

// New starts a tcproxy OS process using the executable specified, and returns
// a *TaskclusterProxy.
func New(taskclusterProxyExecutable string, httpPort uint16, creds *tcclient.Credentials) (*TaskclusterProxy, error) {
func New(taskclusterProxyExecutable string, httpPort uint16, rootURL string, creds *tcclient.Credentials) (*TaskclusterProxy, error) {
args := []string{
"--port", strconv.Itoa(int(httpPort)),
"--root-url", rootURL,
"--client-id", creds.ClientID,
"--access-token", creds.AccessToken,
"--ip-address", "127.0.0.1",
Expand Down
10 changes: 8 additions & 2 deletions tcproxy/tcproxy_test.go
Expand Up @@ -10,7 +10,13 @@ import (
tcclient "github.com/taskcluster/taskcluster-client-go"
)

func TestTaskclusterProxy(t *testing.T) {
func TestTcProxy(t *testing.T) {
if os.Getenv("TASKCLUSTER_CLIENT_ID") == "" ||
os.Getenv("TASKCLUSTER_ACCESS_TOKEN") == "" ||
os.Getenv("TASKCLUSTER_ROOT_URL") == "" {
t.Skip("Skipping test since TASKCLUSTER_{CLIENT_ID,ACCESS_TOKEN,ROOT_URL} env vars not set")
}

var executable string
switch runtime.GOOS {
case "windows":
Expand All @@ -24,7 +30,7 @@ func TestTaskclusterProxy(t *testing.T) {
Certificate: os.Getenv("TASKCLUSTER_CERTIFICATE"),
AuthorizedScopes: []string{"queue:get-artifact:SampleArtifacts/_/X.txt"},
}
ll, err := New(executable, 34569, creds)
ll, err := New(executable, 34569, os.Getenv("TASKCLUSTER_ROOT_URL"), creds)
// Do defer before checking err since err could be a different error and
// process may have already started up.
defer func() {
Expand Down
7 changes: 5 additions & 2 deletions testdata/curlget.go
Expand Up @@ -5,13 +5,16 @@ import (
"log"
"net/http"
"os"
"strings"
)

func main() {
if len(os.Args) != 2 {
log.Fatal("Usage: go run curlget.go <url>")
log.Fatal("Usage: go run curlget.go <url>\n<url> will have the current $TASKCLUSTER_PROXY_URL substituted for the string TASKCLUSTER_PROXY_URL")
}
res, err := http.Get(os.Args[1])
url := os.Args[1]
url = strings.Replace(url, "TASKCLUSTER_PROXY_URL", os.Getenv("TASKCLUSTER_PROXY_URL"), -1)
res, err := http.Get(url)
if err != nil {
log.Fatalf("%v", err)
}
Expand Down

0 comments on commit 4c39491

Please sign in to comment.