This repository has been archived by the owner on Apr 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
machine_config.go
160 lines (135 loc) · 4.83 KB
/
machine_config.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
package config
import (
"encoding/json"
"fmt"
"path"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/maputil"
)
const (
machineConfigFileName = "bitrise.machine.config.json"
// CleanupModeRollback ...
CleanupModeRollback = "rollback"
// CleanupModeRecreate ...
CleanupModeRecreate = "recreate"
// CleanupModeDestroy ...
CleanupModeDestroy = "destroy"
// CleanupModeCustomCommand ...
CleanupModeCustomCommand = "custom-command"
)
// EnvItemsModel ...
type EnvItemsModel map[string]string
// MachineConfigModel ...
type MachineConfigModel struct {
CleanupMode string `json:"cleanup_mode"`
// IsCleanupBeforeSetup - if true do a cleanup before setup, unless
// if the host is already in "prepared" state
// You can force to do a cleanup for every setup if you specify
// the --force flag as well for the setup command.
IsCleanupBeforeSetup bool `json:"is_cleanup_before_setup"`
// IsAllowVagrantCreateInSetup - if true `vagrant create` will be called
// in Setup in case the VM is not yet created
IsAllowVagrantCreateInSetup bool `json:"is_allow_vagrant_create_in_setup"`
// IsDoTimesyncAtSetup - OS X only at the moment
IsDoTimesyncAtSetup bool `json:"is_do_timesync_at_setup"`
CustomCleanupCommand string `json:"custom_cleanup_command"`
// Envs - these will be set as Environment Variables
// for setup, cleanup and destroy
Envs EnvItemsModel `json:"envs"`
// ConfigTypeEnvs - these envs will be added to the
// other Envs based on the "config-type-id" paramter/flag
ConfigTypeEnvs map[string]EnvItemsModel `json:"config_type_envs"`
}
func (configModel *MachineConfigModel) normalizeAndValidate() error {
if configModel.CleanupMode != CleanupModeRollback &&
configModel.CleanupMode != CleanupModeRecreate &&
configModel.CleanupMode != CleanupModeCustomCommand &&
configModel.CleanupMode != CleanupModeDestroy {
return fmt.Errorf("Invalid CleanupMode: %s", configModel.CleanupMode)
}
if configModel.Envs == nil {
configModel.Envs = EnvItemsModel{}
}
if configModel.ConfigTypeEnvs == nil {
configModel.ConfigTypeEnvs = map[string]EnvItemsModel{}
}
return nil
}
// toCmdEnvs ...
func (envItmsModel *EnvItemsModel) toCmdEnvs() []string {
res := make([]string, len(*envItmsModel))
idx := 0
for key, value := range *envItmsModel {
res[idx] = fmt.Sprintf("%s=%s", key, value)
idx++
}
return res
}
func (configModel MachineConfigModel) allEnvsForConfigType(configTypeID string) EnvItemsModel {
allEnvs := EnvItemsModel{}
if configModel.Envs != nil {
allEnvs = maputil.CloneStringStringMap(configModel.Envs)
}
if configTypeID != "" {
if configModel.ConfigTypeEnvs == nil {
log.Warningf("No Config Type Envs defined, but Config Type ID was: %s", configTypeID)
} else {
configSpecEnvs, isFound := configModel.ConfigTypeEnvs[configTypeID]
if !isFound {
log.Warningf("No Config Type Envs found for the specified Config Type ID: %s", configTypeID)
} else {
allEnvs = maputil.MergeStringStringMap(allEnvs, configSpecEnvs)
}
}
}
return allEnvs
}
// AllCmdEnvsForConfigType ...
func (configModel MachineConfigModel) AllCmdEnvsForConfigType(configTypeID string) []string {
allEnvs := configModel.allEnvsForConfigType(configTypeID)
return allEnvs.toCmdEnvs()
}
// CreateEnvItemsModelFromSlice ...
func CreateEnvItemsModelFromSlice(envsArr []string) (EnvItemsModel, error) {
envItemsModel := EnvItemsModel{}
for _, aEnvStr := range envsArr {
splits := strings.Split(aEnvStr, "=")
key := splits[0]
if key == "" {
return EnvItemsModel{}, fmt.Errorf("Invalid item, empty key. (Parameter was: %s)", aEnvStr)
}
if len(splits) < 2 {
return EnvItemsModel{}, fmt.Errorf("Invalid item, no value defined. Key was: %s", splits[0])
}
value := strings.Join(splits[1:], "=")
envItemsModel[key] = value
}
return envItemsModel, nil
}
func readMachineConfigFromBytes(configBytes []byte, appendEnvs EnvItemsModel) (MachineConfigModel, error) {
configModel := MachineConfigModel{}
if err := json.Unmarshal(configBytes, &configModel); err != nil {
return configModel, err
}
if err := configModel.normalizeAndValidate(); err != nil {
return configModel, err
}
for k, v := range appendEnvs {
configModel.Envs[k] = v
}
return configModel, nil
}
// ReadMachineConfigFileFromDir ...
func ReadMachineConfigFileFromDir(workdirPth string, appendEnvs EnvItemsModel) (MachineConfigModel, error) {
configBytes, err := fileutil.ReadBytesFromFile(path.Join(workdirPth, machineConfigFileName))
if err != nil {
return MachineConfigModel{}, fmt.Errorf("ReadMachineConfigFileFromDir: failed to read file: %s", err)
}
machineConfig, err := readMachineConfigFromBytes(configBytes, appendEnvs)
if err != nil {
return MachineConfigModel{}, fmt.Errorf("ReadMachineConfigFileFromDir: failed to parse configuration: %s", err)
}
return machineConfig, nil
}