Skip to content

Commit 40a3be1

Browse files
claireguyotteo
authored andcommitted
[OCTRL-701] High-level configuration files are accepted as parameter for the creation of an environment with coconut.
1 parent 62b1579 commit 40a3be1

2 files changed

Lines changed: 69 additions & 14 deletions

File tree

coconut/cmd/environment_create.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ import (
3434

3535
// environmentCreateCmd represents the environment list command
3636
var environmentCreateCmd = &cobra.Command{
37-
Use: "create",
37+
Use: "create",
3838
Aliases: []string{"new", "c", "n"},
39-
Short: "create a new environment",
39+
Short: "create a new environment",
4040
Long: fmt.Sprintf(`The environment create command requests from %s the
4141
creation of a new environment.
4242
@@ -46,21 +46,21 @@ A valid workflow template (sometimes called simply "workflow" for brevity) must
4646
4747
Workflows and tasks are managed with a git based configuration system, so the workflow template may be provided simply by name or with repository and branch/tag/hash constraints.
4848
Examples:
49-
* ` + "`coconut env create -w myworkflow`" + ` - loads workflow ` + "`myworkflow`" + ` from default configuration repository at HEAD of master branch
50-
* ` + "`coconut env create -w github.com/AliceO2Group/MyConfRepo/myworkflow`" + ` - loads a workflow from a specific git repository, HEAD of master branch
51-
* ` + "`coconut env create -w myworkflow@rev`" + ` - loads a workflow from default repository, on branch, tag or revision ` + "`rev`" + `
52-
* ` + "`coconut env create -w github.com/AliceO2Group/MyConfRepo/myworkflow@rev`" + ` - loads a workflow from a specific git repository, on branch, tag or revision ` + "`rev`" + `
53-
54-
For more information on the %s workflow configuration system, see documentation for the ` + "`coconut repository`" + ` command.`, product.PRETTY_SHORTNAME, product.PRETTY_SHORTNAME),
55-
Run: control.WrapCall(control.CreateEnvironment),
49+
* `+"`coconut env create -w myworkflow`"+` - loads workflow `+"`myworkflow`"+` from default configuration repository at HEAD of master branch
50+
* `+"`coconut env create -w github.com/AliceO2Group/MyConfRepo/myworkflow`"+` - loads a workflow from a specific git repository, HEAD of master branch
51+
* `+"`coconut env create -w myworkflow@rev`"+` - loads a workflow from default repository, on branch, tag or revision `+"`rev`"+`
52+
* `+"`coconut env create -w github.com/AliceO2Group/MyConfRepo/myworkflow@rev`"+` - loads a workflow from a specific git repository, on branch, tag or revision `+"`rev`"+`
5653
54+
For more information on the %s workflow configuration system, see documentation for the `+"`coconut repository`"+` command.`, product.PRETTY_SHORTNAME, product.PRETTY_SHORTNAME),
55+
Run: control.WrapCall(control.CreateEnvironment),
5756
}
5857

5958
func init() {
6059
environmentCmd.AddCommand(environmentCreateCmd)
6160

61+
environmentCreateCmd.Flags().StringP("configuration", "c", "", "high-level configuration payload to be loaded for the new environment")
62+
environmentCreateCmd.MarkFlagRequired("configuration")
6263
environmentCreateCmd.Flags().StringP("workflow-template", "w", "", "workflow to be loaded in the new environment")
63-
environmentCreateCmd.MarkFlagRequired("workflow-template")
6464
environmentCreateCmd.Flags().BoolP("auto", "a", false, "create an autorun environment")
6565
environmentCreateCmd.Flags().BoolP("public", "p", true, "control public rights of the environment")
6666

coconut/control/control.go

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ package control
2828

2929
import (
3030
"context"
31+
"encoding/json"
3132
"errors"
3233
"fmt"
3334
"io"
@@ -67,6 +68,15 @@ type RunFunc func(*cobra.Command, []string)
6768

6869
type ControlCall func(context.Context, *coconut.RpcClient, *cobra.Command, []string, io.Writer) error
6970

71+
type ConfigurationPayload struct {
72+
Name string `json:"name"`
73+
Workflow string `json:"workflow"`
74+
Revision string `json:"revision"`
75+
Repository string `json:"repository"`
76+
Vars map[string]string `json:"variables"`
77+
Detectors []string `json:"detectors"`
78+
}
79+
7080
func WrapCall(call ControlCall) RunFunc {
7181
return func(cmd *cobra.Command, args []string) {
7282
endpoint := viper.GetString("endpoint")
@@ -250,14 +260,49 @@ func GetEnvironments(cxt context.Context, rpc *coconut.RpcClient, cmd *cobra.Com
250260
}
251261

252262
func CreateEnvironment(cxt context.Context, rpc *coconut.RpcClient, cmd *cobra.Command, args []string, o io.Writer) (err error) {
253-
wfPath, err := cmd.Flags().GetString("workflow-template")
263+
configPayload, err := cmd.Flags().GetString("configuration")
264+
if err != nil {
265+
err = errors.New("cannot get configuration payload value")
266+
return
267+
}
268+
if len(configPayload) == 0 {
269+
err = errors.New("cannot create environment with empty configuration payload")
270+
return
271+
}
272+
configPayloadDoc, err := os.ReadFile(configPayload)
273+
if err != nil {
274+
err = errors.New("cannot read configuration payload file")
275+
return
276+
}
277+
payloadData := new(ConfigurationPayload)
278+
err = json.Unmarshal(configPayloadDoc, &payloadData)
254279
if err != nil {
280+
err = errors.New("cannot unmarshal configuration payload")
255281
return
256282
}
257-
if len(wfPath) == 0 {
258-
err = errors.New("cannot create empty environment")
283+
284+
var wfPath string
285+
userWfPath, err := cmd.Flags().GetString("workflow-template")
286+
if err != nil {
259287
return
260288
}
289+
if cmd.Flags().Changed("workflow-template") && len(userWfPath) == 0 {
290+
if len(payloadData.Workflow) > 0 {
291+
wfPath = payloadData.Workflow
292+
} else {
293+
err = errors.New("empty workflow template provided")
294+
return
295+
}
296+
} else if cmd.Flags().Changed("workflow-template") && len(userWfPath) > 0 {
297+
wfPath = userWfPath
298+
} else {
299+
if len(payloadData.Workflow) > 0 {
300+
wfPath = payloadData.Workflow
301+
} else {
302+
err = errors.New("no workflow template provided in config file")
303+
return
304+
}
305+
}
261306

262307
var extraVars string
263308
extraVars, err = cmd.Flags().GetString("extra-vars")
@@ -271,11 +316,21 @@ func CreateEnvironment(cxt context.Context, rpc *coconut.RpcClient, cmd *cobra.C
271316
return
272317
}
273318

274-
extraVarsMap, err := utils.ParseExtraVars(extraVars)
319+
userExtraVarsMap, err := utils.ParseExtraVars(extraVars)
275320
if err != nil {
276321
return
277322
}
278323

324+
extraVarsMap := make(map[string]string, 0)
325+
for k, v := range userExtraVarsMap {
326+
extraVarsMap[k] = v
327+
}
328+
for k, v := range payloadData.Vars {
329+
if _, exists := extraVarsMap[k]; !exists {
330+
extraVarsMap[k] = v
331+
}
332+
}
333+
279334
public, _ := cmd.Flags().GetBool("public")
280335

281336
auto, _ := cmd.Flags().GetBool("auto")

0 commit comments

Comments
 (0)