/
tasks.go
94 lines (73 loc) · 2.12 KB
/
tasks.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
/*
* @Author: 0xe8998e@gmail.com
* @Date: 2022-01-01 00:22:03
* @LastEditTime: 2022-01-28 23:01:27
* @LastEditors: 0xe8998e@gmail.com
* @FilePath: /gosible/pkg/gosible/tasks.go
* @Description: gosible Contol Servers of DevOps's Tool
*/
package gosible
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
type Tasks struct { //注意此处
Tasks []Task `yaml:"tasks"`
}
func (tasks *Tasks) Append(data Task) *Tasks {
tasks.Tasks = append(tasks.Tasks, data)
return tasks
}
/**
* @description:
* @param {*}
* @return {*}
*/
func (tasks *Tasks) Sort() *Tasks { // 重写 Less() 方法
sort.Sort(SortTasks{tasks.Tasks, func(p, q *Task) bool {
return q.Priority < p.Priority // Priority 递减排序
}})
return tasks
}
/**
* @description:
* @param {string} configName
* @return {*}
*/
func ParseTasks(configName string, role Role) (Tasks, error) {
tasks := Tasks{}
playBooksRootDir := GetProjectAbsPath(configName)
roleFile := playBooksRootDir + "/roles/" + role.Name + "/tasks/main.yaml"
rendedFile, err := TemplateFile(roleFile, role.Vars)
if err != nil {
return tasks, errors.Wrap(err, fmt.Sprintf("Read Template File[%s] Error", roleFile))
}
fileName := filepath.Base(rendedFile)
workDir := strings.Replace(filepath.Dir(rendedFile), "\\", "/", -1)
// you can create a new viper instance
// use it keep to read new config every time
// otherwise it will keep old data
runtime_viper := viper.New()
runtime_viper.SetConfigName(fileName)
runtime_viper.SetConfigType("yaml")
runtime_viper.AddConfigPath(workDir)
if err := runtime_viper.ReadInConfig(); err != nil {
// Error parsing file
return tasks, errors.Wrap(err, fmt.Sprintf("Read In Config[%s] Happend Error", configName))
}
if err := runtime_viper.Unmarshal(&tasks); nil != err {
return tasks, errors.Wrap(err, fmt.Sprintf("Unmarshal Config[%s] Error", configName))
}
newTasks := Tasks{}
for _, task := range tasks.Tasks {
task.FileName = playBooksRootDir + "/roles/" + role.Name
newTasks.Tasks = append(newTasks.Tasks, task)
}
defer os.Remove(rendedFile)
return newTasks, nil
}