Skip to content

Commit d75abfc

Browse files
authored
Merge pull request #177 from angelabriel/master
fix refresh and logging
2 parents 9bef0df + a7ae932 commit d75abfc

File tree

8 files changed

+110
-121
lines changed

8 files changed

+110
-121
lines changed

app/parameter.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,9 @@ func collectChangedParameterInfo(noteID, fileName string, comparisons map[string
3131
// to get the changes: diff between note in working area and info stored
3232
// in sections file /run/saptune/sections/<noteid>.sections
3333
for _, param := range content.AllValues {
34-
switch param.Section {
35-
case note.INISectionVersion, note.INISectionRpm, note.INISectionGrub, note.INISectionFS, note.INISectionReminder:
36-
// These parameters are only checked, but not applied.
37-
// So nothing to do during refresh
38-
continue
39-
case note.INISectionCPU, note.INISectionMEM, note.INISectionService, note.INISectionBlock, note.INISectionLimits, note.INISectionLogin, note.INISectionPagecache:
40-
// currently not supported for 'refresh'
41-
system.InfoLog("parameters from section '%s' currently not supported and not evaluated for 'refresh' operation", param.Section)
34+
if !isSectionSupportedForRefresh(param.Key, param.Section) {
4235
continue
4336
}
44-
4537
// initialise parameter entry
4638
paramEntry := initChangedParams(param, noteID, "newchange")
4739
// check for new parameter
@@ -74,6 +66,22 @@ func collectChangedParameterInfo(noteID, fileName string, comparisons map[string
7466
return chgParams
7567
}
7668

69+
// isSectionSupportedForRefresh skips these sections which are currently not
70+
// supported for refresh
71+
func isSectionSupportedForRefresh(param, section string) bool {
72+
switch section {
73+
case note.INISectionVersion, note.INISectionRpm, note.INISectionGrub, note.INISectionFS, note.INISectionReminder:
74+
// These parameters are only checked, but not applied.
75+
// So nothing to do during refresh
76+
return false
77+
case note.INISectionCPU, note.INISectionMEM, note.INISectionService, note.INISectionBlock, note.INISectionLimits, note.INISectionLogin, note.INISectionPagecache:
78+
// currently not supported for 'refresh'
79+
system.InfoLog("parameters (%s) from section '%s' currently not supported and not evaluated for 'refresh' operation", param, section)
80+
return false
81+
}
82+
return true
83+
}
84+
7785
// initChangedParams initialises the parameter entry
7886
func initChangedParams(param txtparser.INIEntry, noteID, mode string) map[string]interface{} {
7987
paramEntry := make(map[string]interface{})
@@ -279,8 +287,11 @@ func chkParameterValue(paramEntry map[string]interface{}, changed bool, app *App
279287
// available in the note definition file
280288
func chkForDeletedParams(noteID string, sectCont []txtparser.INIEntry, noteCont map[string]map[string]txtparser.INIEntry, ovCont map[string]map[string]txtparser.INIEntry, chgParams map[string]map[string]interface{}) {
281289
system.DebugLog("chkForDeletedParams - chgParams is '%+v'", chgParams)
282-
deleted := false
283290
for _, sectParam := range sectCont {
291+
if !isSectionSupportedForRefresh(sectParam.Key, sectParam.Section) {
292+
continue
293+
}
294+
deleted := false
284295
paramEntry := initChangedParams(sectParam, noteID, "del")
285296
if _, ok := noteCont[sectParam.Section]; !ok {
286297
// section no longer available in note definition file

development.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ the sources should be available at $GOPATH/src/github.com/SUSE/saptune
1111
version="3.2.0-test"
1212
bdate=$(date +"%Y/%m/%d")
1313
bvers=15
14-
go build -ldflags "-X 'github.com/SUSE/saptune/actions.RPMVersion=$version' -X 'github.com/SUSE/saptune/actions.RPMDate=$bdate' -X 'github.com/SUSE/saptune/system.RPMBldVers=$bvers'"
14+
go build -buildmode=pie -ldflags "-X 'github.com/SUSE/saptune/actions.RPMVersion=$version' -X 'github.com/SUSE/saptune/actions.RPMDate=$bdate' -X 'github.com/SUSE/saptune/system.RPMBldVers=$bvers'"
1515

1616
## lint and format checks for the sources before committing changes
1717

@@ -22,7 +22,7 @@ the sources should be available at $GOPATH/src/github.com/SUSE/saptune
2222
and run the unit tests (in a docker container)
2323

2424
## unit tests for saptune:
25-
after committing the changes to git travis is used for automatic testing
25+
after committing the changes to git github actions are used for automatic testing
2626

2727
But before committing the sources, run the tests locally by using docker and the same workflow as with github actions
2828

main.go

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/SUSE/saptune/sap/solution"
99
"github.com/SUSE/saptune/system"
1010
"github.com/SUSE/saptune/txtparser"
11-
"io"
1211
"os"
1312
"os/exec"
1413
"strings"
@@ -30,13 +29,23 @@ var logSwitch = map[string]string{"verbose": os.Getenv("SAPTUNE_VERBOSE"), "debu
3029
var SaptuneVersion = ""
3130

3231
func main() {
32+
logSwitchFromConfig(system.SaptuneConfigFile(), logSwitch)
33+
// special log switch settings for json
3334
system.InitOut(logSwitch)
35+
36+
// activate logging
37+
system.LogInit(logFile, logSwitch)
38+
// now system.ErrorExit can write to log and os.Stderr. No longer extra
39+
// care is needed.
40+
system.InfoLog("saptune (%s) started with '%s'", actions.RPMVersion, strings.Join(os.Args, " "))
41+
system.InfoLog("build for '%d'", system.IfdefVers())
42+
3443
if !system.ChkCliSyntax() {
3544
actions.PrintHelpAndExit(os.Stdout, 1)
3645
}
3746

38-
// get saptune version and log switches from saptune sysconfig file
39-
SaptuneVersion = checkSaptuneConfigFile(os.Stderr, system.SaptuneConfigFile(), logSwitch)
47+
// get saptune version from saptune config file and check file content
48+
SaptuneVersion = checkSaptuneConfigFile(system.SaptuneConfigFile())
4049

4150
arg1 := system.CliArg(1)
4251
if arg1 == "version" || system.IsFlagSet("version") {
@@ -54,17 +63,9 @@ func main() {
5463

5564
// All other actions require super user privilege
5665
if os.Geteuid() != 0 {
57-
fmt.Fprintf(os.Stderr, "Please run saptune with root privilege.\n")
58-
system.ErrorExit("", 1)
66+
system.ErrorExit("Please run saptune with root privilege.\n", 1)
5967
}
6068

61-
// activate logging
62-
system.LogInit(logFile, logSwitch)
63-
// now system.ErrorExit can write to log and os.Stderr. No longer extra
64-
// care is needed.
65-
system.InfoLog("saptune (%s) started with '%s'", actions.RPMVersion, strings.Join(os.Args, " "))
66-
system.InfoLog("build for '%d'", system.IfdefVers())
67-
6869
if arg1 == "lock" {
6970
if arg2 := system.CliArg(2); arg2 == "remove" {
7071
system.JnotSupportedYet()
@@ -265,27 +266,17 @@ func checkWorkingArea() {
265266
// checkSaptuneConfigFile checks the config file /etc/sysconfig/saptune
266267
// if it exists, if it contains all needed variables and for some variables
267268
// checks, if the values is valid
268-
// returns the saptune version and changes some log switches
269-
func checkSaptuneConfigFile(writer io.Writer, saptuneConf string, lswitch map[string]string) string {
269+
// returns the saptune version
270+
func checkSaptuneConfigFile(saptuneConf string) string {
270271
if system.CliArg(1) == "configure" && system.CliArg(2) == "reset" {
271-
if lswitch["debug"] == "" {
272-
lswitch["debug"] = "off"
273-
}
274-
if lswitch["verbose"] == "" {
275-
lswitch["verbose"] = "on"
276-
}
277-
if lswitch["error"] == "" {
278-
lswitch["error"] = "on"
279-
}
272+
// skip check
280273
return "3"
281274
}
282-
283275
missingKey := []string{}
284276
keyList := actions.MandKeyList()
285277
sconf, err := txtparser.ParseSysconfigFile(saptuneConf, false)
286278
if err != nil {
287-
fmt.Fprintf(writer, "Error: Checking saptune configuration file - Unable to read file '%s': %v\n", saptuneConf, err)
288-
system.ErrorExit("", 128)
279+
system.ErrorExit("Checking saptune configuration file - Unable to read file '%s': %v", saptuneConf, err, 128)
289280
}
290281
// check, if all needed variables are available in the saptune
291282
// config file
@@ -295,14 +286,12 @@ func checkSaptuneConfigFile(writer io.Writer, saptuneConf string, lswitch map[st
295286
}
296287
}
297288
if len(missingKey) != 0 {
298-
fmt.Fprintf(writer, "Error: File '%s' is broken. Missing variables '%s'\n", saptuneConf, strings.Join(missingKey, ", "))
299-
system.ErrorExit("", 128)
289+
system.ErrorExit("File '%s' is broken. Missing variables '%s'", saptuneConf, strings.Join(missingKey, ", "), 128)
300290
}
301291
txtparser.GetSysctlExcludes(sconf.GetString("SKIP_SYSCTL_FILES", ""))
302292
stageVal := sconf.GetString("STAGING", "")
303293
if stageVal != "true" && stageVal != "false" {
304-
fmt.Fprintf(writer, "Error: Variable 'STAGING' from file '%s' contains a wrong value '%s'. Needs to be 'true' or 'false'\n", saptuneConf, stageVal)
305-
system.ErrorExit("", 128)
294+
system.ErrorExit("Variable 'STAGING' from file '%s' contains a wrong value '%s'. Needs to be 'true' or 'false'", saptuneConf, stageVal, 128)
306295
}
307296

308297
// check saptune-discovery-period of the Trento Agent
@@ -313,10 +302,18 @@ func checkSaptuneConfigFile(writer io.Writer, saptuneConf string, lswitch map[st
313302
// set values read from the config file
314303
saptuneVers := sconf.GetString("SAPTUNE_VERSION", "")
315304
if saptuneVers != "1" && saptuneVers != "2" && saptuneVers != "3" {
316-
fmt.Fprintf(writer, "Error: Wrong saptune version in file '%s': %s\n", saptuneConf, saptuneVers)
317-
system.ErrorExit("", 128)
305+
system.ErrorExit("Wrong saptune version in file '%s': %s", saptuneConf, saptuneVers, 128)
318306
}
307+
return saptuneVers
308+
}
319309

310+
// logSwitchFromConfig reads log switch settings from the saptune
311+
// config file
312+
func logSwitchFromConfig(saptuneConf string, lswitch map[string]string) {
313+
sconf, err := txtparser.ParseSysconfigFile(saptuneConf, false)
314+
if err != nil {
315+
system.ErrorExit("Checking saptune configuration file - Unable to read file '%s': %v", saptuneConf, err, 128)
316+
}
320317
// Switch Debug on ("on") or off ("off" - default)
321318
// Switch verbose mode on ("on" - default) or off ("off")
322319
// Switch error mode on ("on" - default) or off ("off")
@@ -330,5 +327,4 @@ func checkSaptuneConfigFile(writer io.Writer, saptuneConf string, lswitch map[st
330327
if lswitch["error"] == "" {
331328
lswitch["error"] = sconf.GetString("ERROR", "on")
332329
}
333-
return saptuneVers
334330
}

main_test.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,58 +137,60 @@ func TestCheckSaptuneConfigFile(t *testing.T) {
137137
logSwitch := map[string]string{"verbose": "", "debug": ""}
138138
// check saptune version and debug
139139
saptuneConf := fmt.Sprintf("%s/saptune_VersAndDebug", TstFilesInGOPATH)
140-
buffer := bytes.Buffer{}
140+
//buffer := bytes.Buffer{}
141141

142142
lSwitch := logSwitch
143-
saptuneVers := checkSaptuneConfigFile(&buffer, saptuneConf, lSwitch)
143+
saptuneVers := checkSaptuneConfigFile(saptuneConf)
144144
if saptuneVers != "5" {
145145
t.Errorf("wrong value for 'SAPTUNE_VERSION' - '%+v' instead of ''\n", saptuneVers)
146146
}
147+
logSwitchFromConfig(saptuneConf, lSwitch)
147148
if lSwitch["debug"] != "on" {
148149
t.Errorf("wrong value for 'DEBUG' - '%+v' instead of 'on'\n", lSwitch["debug"])
149150
}
150151
if lSwitch["verbose"] != "on" {
151152
t.Errorf("wrong value for 'VERBOSE' - '%+v' instead of 'on'\n", lSwitch["debug"])
152153
}
153154

154-
buffer.Reset()
155+
//buffer.Reset()
156+
155157
errExitbuffer := bytes.Buffer{}
156158
tstwriter = &errExitbuffer
157159

158160
// check missing variable
159161
saptuneConf = fmt.Sprintf("%s/saptune_MissingVar", TstFilesInGOPATH)
160-
matchTxt := fmt.Sprintf("Error: File '%s' is broken. Missing variables 'COLOR_SCHEME'\n", saptuneConf)
161-
lSwitch = logSwitch
162-
_ = checkSaptuneConfigFile(&buffer, saptuneConf, lSwitch)
162+
matchTxt := fmt.Sprintf("ERROR: File '%s' is broken. Missing variables 'COLOR_SCHEME'\n", saptuneConf)
163+
//lSwitch = logSwitch
164+
_ = checkSaptuneConfigFile(saptuneConf)
163165

164-
txt := buffer.String()
165-
checkOut(t, txt, matchTxt)
166+
//txt := buffer.String()
167+
//checkOut(t, txt, matchTxt)
166168
if tstRetErrorExit != 128 {
167169
t.Errorf("error exit should be '128' and NOT '%v'\n", tstRetErrorExit)
168170
}
169171
errExOut := errExitbuffer.String()
170-
if errExOut != "" {
172+
if errExOut != matchTxt {
171173
t.Errorf("wrong text returned by ErrorExit: '%v' instead of '%v'\n", errExOut, matchTxt)
172174
}
173175

174176
// initialise next test
175-
buffer.Reset()
177+
//buffer.Reset()
176178
errExitbuffer.Reset()
177179

178180
// check wrong STAGING value
179181
saptuneConf = fmt.Sprintf("%s/saptune_WrongStaging", TstFilesInGOPATH)
180182
saptuneVers = ""
181-
matchTxt = fmt.Sprintf("Error: Variable 'STAGING' from file '%s' contains a wrong value 'hugo'. Needs to be 'true' or 'false'\n", saptuneConf)
182-
lSwitch = logSwitch
183-
_ = checkSaptuneConfigFile(&buffer, saptuneConf, lSwitch)
183+
matchTxt = fmt.Sprintf("ERROR: Variable 'STAGING' from file '%s' contains a wrong value 'hugo'. Needs to be 'true' or 'false'\n", saptuneConf)
184+
//lSwitch = logSwitch
185+
_ = checkSaptuneConfigFile(saptuneConf)
184186

185-
txt = buffer.String()
186-
checkOut(t, txt, matchTxt)
187+
//txt = buffer.String()
188+
//checkOut(t, txt, matchTxt)
187189
if tstRetErrorExit != 128 {
188190
t.Errorf("error exit should be '128' and NOT '%v'\n", tstRetErrorExit)
189191
}
190192
errExOut = errExitbuffer.String()
191-
if errExOut != "" {
192-
t.Errorf("wrong text returned by ErrorExit: '%v' instead of ''\n", errExOut)
193+
if errExOut != matchTxt {
194+
t.Errorf("wrong text returned by ErrorExit: '%v' instead of '%v'\n", errExOut, matchTxt)
193195
}
194196
}

ospackage/usr/share/saptune/notes/2205917

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ THP=never
3636
#
3737
KSM=0
3838

39-
[cpu]
39+
[cpu:arch=x86_64]
4040
# Energy Performance Bias EPB (applies to Intel-based systems only)
4141
# energy_perf_bias: performance - 0, normal - 6, powersave - 15
4242
# cpupower set -b 0, if system supports Intel's performance bias setting
@@ -127,7 +127,7 @@ kernel-default 4.12.14-95.13.1
127127
[rpm:os=12-SP5:arch=ppc64le]
128128
kernel-default 4.12.14-122.17
129129

130-
[grub]
130+
[grub:arch=x86_64]
131131
# /etc/default/grub GRUB_CMDLINE_LINUX_DEFAULT
132132
# saptune only checks the values. Changing the grub configuration is not
133133
# supported by saptune

system/cpu.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,6 @@ func GetGovernor() map[string]string {
225225
gGov[cpuName] = gov
226226
}
227227
}
228-
fmt.Printf("setAll is '%+v'\n", setAll)
229-
fmt.Printf("gGov is '%+v'\n", gGov)
230228
if setAll {
231229
gGov = make(map[string]string)
232230
gGov["all"] = oldgov

0 commit comments

Comments
 (0)