-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.go
More file actions
141 lines (116 loc) · 4.36 KB
/
run.go
File metadata and controls
141 lines (116 loc) · 4.36 KB
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
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
* Copyright 2025 HPC-Gridware GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************/
/*___INFO__MARK_END__*/
package main
import (
"encoding/json"
"fmt"
"os"
qconf "github.com/hpc-gridware/go-clusterscheduler/pkg/qconf/v9.0"
"github.com/spf13/cobra"
)
func run(cmd *cobra.Command, args []string) {
configFile := args[0]
// Read cluster configuration from JSON file
config, err := readClusterConfig(configFile)
FatalOnError(err)
// Initialize qconf client
cs, err := qconf.NewCommandLineQConf(qconf.CommandLineQConfConfig{
Executable: "qconf",
})
FatalOnError(err)
// Add simulated hosts to /etc/hosts for name resolution
err = addHostsToEtcHosts(config)
FatalOnError(err)
fmt.Println("Hosts added to /etc/hosts")
// Prepare simulation configuration (add complex, params, etc.)
err = prepareSimulationConfig(cs, &config)
FatalOnError(err)
// Get current cluster configuration
currentConfig, err := cs.GetClusterConfiguration()
FatalOnError(err)
// Ensure default entities (master host, root user) exist
ensureDefaultEntities(&config)
// Clean up invalid PE references in the configuration
cleanupInvalidPEReferences(&config)
// Compare configurations to determine changes needed
comparison, err := currentConfig.CompareTo(config)
FatalOnError(err)
// Apply changes: Add, Modify, then Delete
applyConfigurationChanges(cs, currentConfig, comparison)
fmt.Println("Simulated cluster configuration applied")
// Restart qmaster to activate simulation
restartQmaster(currentConfig, cs)
}
// readClusterConfig reads and parses a cluster configuration from a JSON file
func readClusterConfig(configFile string) (qconf.ClusterConfig, error) {
var config qconf.ClusterConfig
file, err := os.Open(configFile)
if err != nil {
return config, err
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
return config, err
}
// reconcileExecHosts moves exec hosts from DiffAdded to DiffModified when they
// already exist in the cluster. This handles the race between reading the cluster
// state and applying changes (e.g. the execd registering after GetClusterConfiguration).
func reconcileExecHosts(cs *qconf.CommandLineQConf, comparison *qconf.ClusterConfigComparison) {
if comparison.DiffAdded == nil || len(comparison.DiffAdded.ExecHosts) == 0 {
return
}
for name, eh := range comparison.DiffAdded.ExecHosts {
if _, err := cs.ShowExecHost(name); err == nil {
if comparison.DiffModified.ExecHosts == nil {
comparison.DiffModified.ExecHosts = make(map[string]qconf.HostExecConfig)
}
comparison.DiffModified.ExecHosts[name] = eh
delete(comparison.DiffAdded.ExecHosts, name)
fmt.Printf("Reconciled exec host %s: already exists, will modify instead of add\n", name)
}
}
}
// applyConfigurationChanges applies the configuration differences to the cluster
func applyConfigurationChanges(cs *qconf.CommandLineQConf,
currentConfig qconf.ClusterConfig, comparison *qconf.ClusterConfigComparison) {
reconcileExecHosts(cs, comparison)
// Add new entries
if comparison.DiffAdded != nil {
_, err := qconf.AddAllEntries(cs, *comparison.DiffAdded)
FatalOnError(err)
}
// Modify existing entries
if comparison.DiffModified != nil {
_, err := qconf.ModifyAllEntries(cs, *comparison.DiffModified)
FatalOnError(err)
}
// Handle deletions with dependency cleanup
if comparison.DiffRemoved != nil {
// Before deleting PEs, remove references from queues
if len(comparison.DiffRemoved.ParallelEnvironments) > 0 {
err := removeQueuePEReferences(cs, currentConfig,
comparison.DiffRemoved.ParallelEnvironments)
PrintOnError(err)
}
// Delete removed entries
_, err := qconf.DeleteAllEnries(cs, *comparison.DiffRemoved, true)
PrintOnError(err)
}
}