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

Commit

Permalink
Add extra environment variables support.
Browse files Browse the repository at this point in the history
The child process may expects a set of typical environment variables be
present in the target system. In this patch, we add a configuratio entry
to the env plugin for configuration of extra variables that should be
passed to the sandbox.

We also set the HOME environment variable in the native engine, as many
shell commands expects for it.
  • Loading branch information
Wander Lairson Costa committed Jan 18, 2017
1 parent 8ac197c commit 8114b2b
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 6 deletions.
11 changes: 10 additions & 1 deletion engines/native/sandbox.go
Expand Up @@ -54,11 +54,20 @@ func newSandbox(b *sandboxBuilder) (*sandbox, error) {
return nil, fmt.Errorf("Failed to create temporary system user, error: %s", err)
}

env := map[string]string{}
for k, v := range b.env {
env[k] = v
}

env["HOME"] = homeFolder.Path()
env["USER"] = user.Name()
env["LOGNAME"] = user.Name()

// Start process
debug("StartProcess: %v", b.payload.Command)
process, err := system.StartProcess(system.ProcessOptions{
Arguments: b.payload.Command,
Environment: b.env,
Environment: env,
WorkingFolder: homeFolder.Path(),
Owner: user,
Stdout: ioext.WriteNopCloser(b.context.LogDrain()),
Expand Down
7 changes: 6 additions & 1 deletion engines/native/system/user_darwin.go
Expand Up @@ -103,7 +103,7 @@ func CreateUser(homeFolder string, groups []*Group) (*User, error) {
}
}

err = d.create(userPath, "NFSHomeDirectory", userPath)
err = d.create(userPath, "NFSHomeDirectory", homeFolder)
if err != nil {
panic(fmt.Errorf("Error setting home directory: %v", err))
}
Expand Down Expand Up @@ -146,6 +146,11 @@ func (u *User) Remove() {
}
}

// Name returns the user name
func (u *User) Name() string {
return u.name
}

// return the next uid available
func getMaxUID(d dscl) (int, error) {
uids, err := d.list("/Users", "uid")
Expand Down
5 changes: 5 additions & 0 deletions engines/native/system/user_linux.go
Expand Up @@ -103,3 +103,8 @@ func (u *User) Remove() {
))
}
}

// Name returns the user name
func (u *User) Name() string {
return u.name
}
5 changes: 5 additions & 0 deletions engines/native/system/user_windows.go
Expand Up @@ -19,3 +19,8 @@ func (u *User) Remove() {

panic("Not implemented")
}

// Name returns the user name
func (u *User) Name() string {
return u.name
}
11 changes: 10 additions & 1 deletion examples/macosx-config.yml
Expand Up @@ -20,7 +20,16 @@ config:
userGroups: ['staff']
logLevel: info
plugins:
disabled: ['success', 'interactive']
disabled: ['interactive', 'maxruntime']
env:
extra:
PATH: {$env: PATH}
TMPDIR: {$env: TMPDIR}
LANG: {$env: LANG}
LC_ALL: {$env: LC_ALL}
TERM: {$env: TERM}
TERM_PROGRAM: {$env: TERM_PROGRAM}
SHELL: /bin/bash
pollingInterval: 10
queueBaseUrl: https://queue.taskcluster.net/v1
reclaimOffset: 120
Expand Down
39 changes: 36 additions & 3 deletions plugins/env/env.go
Expand Up @@ -13,12 +13,29 @@ import (

type plugin struct {
plugins.PluginBase
extraVars map[string]string
}

type payloadType struct {
Env map[string]string `json:"env"`
}

type config struct {
Extra map[string]string `json:"extra"`
}

var configSchema = schematypes.Object{
Properties: schematypes.Properties{
"extra": schematypes.Map{
MetaData: schematypes.MetaData{
Title: "Extra environment variables",
Description: "This defines extra environment variables to add to the engine.",
},
Values: schematypes.String{},
},
},
}

var payloadSchema = schematypes.Object{
Properties: schematypes.Properties{
"env": schematypes.Map{
Expand All @@ -35,7 +52,7 @@ func (plugin) PayloadSchema() schematypes.Object {
return payloadSchema
}

func (plugin) NewTaskPlugin(options plugins.TaskPluginOptions) (plugins.TaskPlugin, error) {
func (pl plugin) NewTaskPlugin(options plugins.TaskPluginOptions) (plugins.TaskPlugin, error) {
var p payloadType
err := payloadSchema.Map(options.Payload, &p)
if err == schematypes.ErrTypeMismatch {
Expand All @@ -44,6 +61,10 @@ func (plugin) NewTaskPlugin(options plugins.TaskPluginOptions) (plugins.TaskPlug
return nil, engines.ErrContractViolation
}

for k, v := range pl.extraVars {
p.Env[k] = v
}

return taskPlugin{
TaskPluginBase: plugins.TaskPluginBase{},
variables: p.Env,
Expand Down Expand Up @@ -82,8 +103,20 @@ type pluginProvider struct {
plugins.PluginProviderBase
}

func (pluginProvider) NewPlugin(plugins.PluginOptions) (plugins.Plugin, error) {
return plugin{}, nil
func (pluginProvider) NewPlugin(options plugins.PluginOptions) (plugins.Plugin, error) {
var c config
if err := schematypes.MustMap(configSchema, options.Config, &c); err != nil {
return nil, engines.ErrContractViolation
}

return plugin{
PluginBase: plugins.PluginBase{},
extraVars: c.Extra,
}, nil
}

func (pluginProvider) ConfigSchema() schematypes.Schema {
return configSchema
}

func init() {
Expand Down
25 changes: 25 additions & 0 deletions plugins/env/env_test.go
Expand Up @@ -13,6 +13,7 @@ func TestEnvNone(*testing.T) {
"function": "true",
"argument": "whatever"
}`,
PluginConfig: `{}`,
Plugin: "env",
PluginSuccess: true,
EngineSuccess: true,
Expand All @@ -29,6 +30,7 @@ func TestEnvDefinition(*testing.T) {
"ENV1": "env1"
}
}`,
PluginConfig: `{}`,
Plugin: "env",
PluginSuccess: true,
EngineSuccess: true,
Expand All @@ -46,9 +48,32 @@ func TestEnvUnDefinition(*testing.T) {
"ENV2": "env2"
}
}`,
PluginConfig: `{}`,
Plugin: "env",
PluginSuccess: true,
EngineSuccess: false,
NotMatchLog: "env1",
}.Test()
}

func TestEnvConfig(*testing.T) {
plugintest.Case{
Payload: `{
"delay": 0,
"function": "print-env-var",
"argument": "ENV1",
"env": {
"ENV2" : "env2"
}
}`,
PluginConfig: `{
"extra": {
"ENV1": "env1"
}
}`,
Plugin: "env",
PluginSuccess: true,
EngineSuccess: true,
MatchLog: "env1",
}.Test()
}
3 changes: 3 additions & 0 deletions worker/worker_test.go
Expand Up @@ -37,6 +37,9 @@ func TestStart(t *testing.T) {
},
"plugins": map[string]interface{}{
"disabled": []string{},
"env": map[string]interface{}{
"extra": map[string]interface{}{},
},
},
"capacity": 1,
"credentials": map[string]interface{}{
Expand Down

0 comments on commit 8114b2b

Please sign in to comment.