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
3029var SaptuneVersion = ""
3130
3231func 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}
0 commit comments