-
Notifications
You must be signed in to change notification settings - Fork 23
/
convert.go
247 lines (207 loc) · 7.22 KB
/
convert.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* === This file is part of ALICE O² ===
*
* Copyright 2020 CERN and copyright holders of ALICE O².
* Author: Ayaan Zaidi <azaidi@cern.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In applying this license CERN does not waive the privileges and
* immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
package cmd
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/AliceO2Group/Control/common/utils"
"github.com/AliceO2Group/Control/core/task"
"github.com/AliceO2Group/Control/core/workflow"
"github.com/AliceO2Group/Control/walnut/converter"
)
var outputDir string
var modules []string
var graft string
var workflowName string
var configURIVarname string
var staticConfigURI string
var extraVars string
var taskNamePrefix string
// convertCmd represents the convert command
var convertCmd = &cobra.Command{
Use: "convert",
Short: "converts a DPL Dump to the required formats",
Long: `The convert command takes a DPL input and outputs task and workflow templates. Optional flags can be provided to
specify which modules should be used when generating task templates. Control-OCCPlugin is always used as module.`,
Run: func(cmd *cobra.Command, args []string) {
for _, dumpFile := range args {
// Strip .json from end of filename
nameOfDump := filepath.Base(dumpFile)[:len(filepath.Base(dumpFile))-5]
defaults := map[string]string {
"user": "flp",
nameOfDump + "_monitoring_url": "no-op://",
"dpl_config": "",
}
// Open specified DPL Dump
file, err := ioutil.ReadFile(dumpFile)
if err != nil {
err = fmt.Errorf("failed to open file &s: &w", dumpFile, err)
fmt.Println(err.Error())
os.Exit(1)
}
if configURIVarname != "" {
defaults[configURIVarname] = staticConfigURI
} else {
defaults[nameOfDump + "_config_uri"] = staticConfigURI
}
// Import the dump and convert it to []*task.Class
dplDump, err := converter.DPLImporter(file)
taskClass, err := converter.ExtractTaskClasses(dplDump, taskNamePrefix, modules)
if outputDir == "" {
outputDir, _ = os.Getwd()
}
outputDir, _ = homedir.Expand(outputDir)
extraVars = strings.TrimSpace(extraVars)
if cmd.Flags().Changed("extra-vars") && len(extraVars) == 0 {
fmt.Println(errors.New("empty list of extra-vars supplied").Error())
os.Exit(1)
}
extraVarsMap, err := utils.ParseExtraVars(extraVars)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
fmt.Printf("OPENED: %s", dumpFile)
if graft == "" {
if workflowName == "" {
workflowName = nameOfDump
}
// If not grafting, simply convert dump to WFTs and TTs
err = WriteTemplates(taskClass, workflowName, extraVarsMap, defaults)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
} else {
rolePath := strings.Split(graft, ":")
workflowPath := rolePath[0]
targetRolePath := rolePath[1]
if workflowName == "" {
workflowName = filepath.Base(workflowPath)
}
// Open existing workflow
f, err := ioutil.ReadFile(workflowPath)
if err != nil {
log.Fatal(err)
}
root, err := workflow.LoadWorkflow(f)
if err != nil {
log.Fatal(err)
}
// Convert DPL to yaml.Node
roleToGraft, err := workflow.LoadDPL(taskClass, dumpFile[:len(dumpFile)-5], extraVarsMap)
if err != nil {
log.Fatal(err)
}
roleData, err := workflow.RoleToYAML(roleToGraft)
if err != nil {
log.Fatal(err)
}
grafted, err := workflow.Graft(&root, targetRolePath, roleData, workflowName)
if err != nil {
log.Fatal(err)
}
path := filepath.Join(outputDir, workflowName+".yaml")
fmt.Println("Writing to: ", path)
err = ioutil.WriteFile(path, grafted, 0644)
if err != nil {
log.Fatal(err)
}
}
}
isGitRepo, _ := strconv.ParseBool(strings.TrimSpace(
runGitCmd([]string{"rev-parse", "--is-inside-work-tree"})))
if isGitRepo {
fmt.Printf(runGitCmd([]string{"status"}))
result := true
prompt := &survey.Confirm{
Message: "Would you like to view the git diff?",
Default: true,
}
_ = survey.AskOne(prompt, &result)
if result {
fmt.Printf(runGitCmd([]string{"diff"}))
}
}
},
Args: cobra.MinimumNArgs(1),
}
func runGitCmd(args []string) string {
cmd := exec.Command("git", args...)
cmd.Dir = outputDir
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
return string(out)
}
func WriteTemplates(taskClass []*task.Class, nameOfDump string, extraVarsMap map[string]string, defaults map[string]string) (err error) {
err = converter.GenerateTaskTemplate(taskClass, outputDir, defaults)
if err != nil {
return fmt.Errorf("conversion to task failed for %s: %w", nameOfDump, err)
}
role, err := workflow.LoadDPL(taskClass, nameOfDump, extraVarsMap)
err = converter.GenerateWorkflowTemplate(role, outputDir)
if err != nil {
return fmt.Errorf("conversion to workflow failed for %s: %w", nameOfDump, err)
}
return nil
}
func init() {
convertCmd.Flags().StringArrayVarP(&modules, "modules", "m", []string{}, "modules to load")
_ = viper.BindPFlag("modules", convertCmd.Flags().Lookup("modules"))
convertCmd.PersistentFlags().StringVarP(&outputDir, "output-dir", "o", "",
"optional output directory")
_ = viper.BindPFlag("output-dir", rootCmd.Flags().Lookup("output-dir"))
convertCmd.PersistentFlags().StringVarP(&graft, "graft", "g", "",
"graft converted DPL to an existing template")
_ = viper.BindPFlag("graft", rootCmd.Flags().Lookup("graft"))
convertCmd.PersistentFlags().StringVarP(&workflowName, "workflow-name", "w", "",
"workflow to graft to")
_ = viper.BindPFlag("workflow-name", rootCmd.Flags().Lookup("workflow-name"))
convertCmd.PersistentFlags().StringVarP(&configURIVarname, "config-uri-varname", "", "",
"name of config uri var")
_ = viper.BindPFlag("config-uri-varname", rootCmd.Flags().Lookup("config-uri-varname"))
convertCmd.PersistentFlags().StringVarP(&staticConfigURI, "static-config-uri", "", "",
"path of config uri")
_ = viper.BindPFlag("static-config-uri", rootCmd.Flags().Lookup("static-config-uri"))
convertCmd.PersistentFlags().StringVarP(&extraVars, "extra-vars", "", "",
"extra vars")
_ = viper.BindPFlag("extra-vars", rootCmd.Flags().Lookup("extra-vars"))
convertCmd.PersistentFlags().StringVarP(&taskNamePrefix, "task-name-prefix", "", "",
"prefix for task name fields")
_ = viper.BindPFlag("task-name-prefix", rootCmd.Flags().Lookup("task-name-prefix"))
rootCmd.AddCommand(convertCmd)
}