forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_docker.go
155 lines (135 loc) · 4.03 KB
/
simple_docker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//go:build simple || docker
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/taskcluster/shell"
"github.com/taskcluster/taskcluster/v47/workers/generic-worker/host"
"github.com/taskcluster/taskcluster/v47/workers/generic-worker/process"
)
func (task *TaskRun) formatCommand(index int) string {
return shell.Escape(task.Payload.Command[index]...)
}
func PlatformTaskEnvironmentSetup(taskDirName string) (reboot bool) {
taskContext = &TaskContext{
TaskDir: filepath.Join(config.TasksDir, taskDirName),
}
err := os.MkdirAll(taskContext.TaskDir, 0777)
if err != nil {
panic(err)
}
return false
}
func platformFeatures() []Feature {
return []Feature{}
}
func deleteDir(path string) error {
log.Print("Removing directory '" + path + "'...")
err := host.Run("/bin/chmod", "-R", "u+w", path)
if err != nil {
log.Print("WARNING: could not chmod -R u+w '" + path + "'")
log.Printf("%v", err)
}
err = host.Run("/bin/rm", "-rf", path)
if err != nil {
log.Print("WARNING: could not delete directory '" + path + "'")
log.Printf("%v", err)
return err
}
return nil
}
func (task *TaskRun) generateCommand(index int) error {
var err error
task.Commands[index], err = process.NewCommand(task.Payload.Command[index], taskContext.TaskDir, task.EnvVars())
if err != nil {
return err
}
task.logMux.RLock()
defer task.logMux.RUnlock()
task.Commands[index].DirectOutput(task.logWriter)
return nil
}
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 {
task.Commands[i].SetEnv(variable, value)
}
return nil
}
func purgeOldTasks() error {
if !config.CleanUpTaskDirs {
log.Printf("WARNING: Not purging previous task directories/users since config setting cleanUpTaskDirs is false")
return nil
}
// Use filepath.Base(taskContext.TaskDir) rather than taskContext.User.Name
// since taskContext.User is nil if running tasks as current user.
deleteTaskDirs(config.TasksDir, filepath.Base(taskContext.TaskDir))
return nil
}
func install(arguments map[string]interface{}) (err error) {
return nil
}
func RenameCrossDevice(oldpath, newpath string) (err error) {
// TODO: here we should be able to rename when oldpath and newpath are on
// different partitions - for now this will cover 99% of cases.
return os.Rename(oldpath, newpath)
}
func defaultTasksDir() string {
// Issue 3779; default tasks directory is `tasks` relative to working directory
return "tasks"
}
func rebootBetweenTasks() bool {
return false
}
func platformTargets(arguments map[string]interface{}) ExitCode {
log.Print("Internal error - no target found to run, yet command line parsing successful")
return INTERNAL_ERROR
}
// we put this in init() instead of startup() as we want tests to be able to change
// it - note we shouldn't have these nasty global vars, I can only apologise, and
// say taskcluster-worker will be much nicer
func init() {
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
taskContext = &TaskContext{
TaskDir: pwd,
}
}
func (task *TaskRun) EnvVars() []string {
workerEnv := os.Environ()
taskEnv := map[string]string{}
taskEnvArray := []string{}
for _, j := range workerEnv {
if !strings.HasPrefix(j, "TASKCLUSTER_ACCESS_TOKEN=") {
spl := strings.SplitN(j, "=", 2)
if len(spl) != 2 {
panic(fmt.Errorf("Could not interpret string %q as `key=value`", j))
}
taskEnv[spl[0]] = spl[1]
}
}
for k, v := range task.Payload.Env {
taskEnv[k] = v
}
taskEnv["TASK_ID"] = task.TaskID
taskEnv["RUN_ID"] = strconv.Itoa(int(task.RunID))
taskEnv["TASKCLUSTER_ROOT_URL"] = config.RootURL
if config.WorkerLocation != "" {
taskEnv["TASKCLUSTER_WORKER_LOCATION"] = config.WorkerLocation
}
for i, j := range taskEnv {
taskEnvArray = append(taskEnvArray, i+"="+j)
}
log.Printf("Environment: %#v", taskEnvArray)
return taskEnvArray
}