diff --git a/init.go b/init.go index d40a236..22b083f 100644 --- a/init.go +++ b/init.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "time" "github.com/storageos/init/info" "github.com/storageos/init/info/k8s" @@ -21,7 +22,18 @@ const ( nodeImageEnvVar = "NODE_IMAGE" ) +const timeFormat = time.RFC3339 + +type logWriter struct{} + +func (lw *logWriter) Write(bytes []byte) (int, error) { + return fmt.Print(time.Now().UTC().Format(timeFormat), " ", string(bytes)) +} + func main() { + log := log.New(new(logWriter), "", log.LstdFlags) + log.SetFlags(0) + scriptsDir := flag.String("scripts", "", "absolute path of the scripts directory") dsName := flag.String("dsName", "", "name of the StorageOS DaemonSet") dsNamespace := flag.String("dsNamespace", "", "namespace of the StorageOS DaemonSet") @@ -82,10 +94,10 @@ func main() { log.Println("scripts:", allScripts) // Create a script runner. - run := runner.NewRun() + run := runner.NewRun(log) // Run all the scripts. - if err := runScripts(run, allScripts, scriptEnvVar); err != nil { + if err := runScripts(log, run, allScripts, scriptEnvVar); err != nil { log.Fatalf("init failed: %v", err) } } @@ -132,7 +144,7 @@ func getParamsForK8SImageInfo(dsName, dsNamespace string) (name, namespace strin // event. // Any preliminary checks that need to be performed before running a script can // be performed here. -func runScripts(run script.Runner, scripts []string, envVars map[string]string) error { +func runScripts(log *log.Logger, run script.Runner, scripts []string, envVars map[string]string) error { for _, script := range scripts { // TODO: Check if the script has any preliminary checks to be performed // before execution. diff --git a/script/runner/runner.go b/script/runner/runner.go index 411388e..7bfe297 100644 --- a/script/runner/runner.go +++ b/script/runner/runner.go @@ -13,11 +13,13 @@ import ( ) // Run implements Runner interface. -type Run struct{} +type Run struct { + logger *log.Logger +} // NewRun returns an initialized Run. -func NewRun() *Run { - return &Run{} +func NewRun(log *log.Logger) *Run { + return &Run{log} } // RunScript runs a given script with arguments if specified, and attaches a diff --git a/scripts/01-lio/enable-lio.sh b/scripts/01-lio/enable-lio.sh index 65e727a..8676ea1 100755 --- a/scripts/01-lio/enable-lio.sh +++ b/scripts/01-lio/enable-lio.sh @@ -2,6 +2,12 @@ set -e +function log() { + local msg="$1" + timestamp_utc=$(date -u --rfc-3339=ns | sed 's/ /T/; s/\(\....\).*\([+-]\)/\1\2/g') + echo $timestamp_utc $msg +} + function module_error_log() { local mod="$1" local mod_dir="$2" @@ -13,23 +19,23 @@ function module_error_log() { # Configfs can be built in the kernel, hence the module # initstate file will not exist. Even though, the mount # is present and working -echo "Checking configfs" +log "Checking configfs" if mount | grep -q "^configfs on /sys/kernel/config"; then - echo "configfs mounted on sys/kernel/config" + log "configfs mounted on sys/kernel/config" else - echo "configfs not mounted, checking if kmod is loaded" + log "configfs not mounted, checking if kmod is loaded" state_file=/sys/module/configfs/initstate if [ -f "$state_file" ] && grep -q live "$state_file"; then - echo "configfs mod is loaded" + log "configfs mod is loaded" else - echo "configfs not loaded, executing: modprobe -b configfs" + log "configfs not loaded, executing: modprobe -b configfs" modprobe -b configfs fi if mount | grep -q configfs; then - echo "configfs mounted" + log "configfs mounted" else - echo "mounting configfs /sys/kernel/config" + log "mounting configfs /sys/kernel/config" mount -t configfs configfs /sys/kernel/config fi fi @@ -44,16 +50,16 @@ loop_dir="$target_dir"/loopback for mod in target_core_mod tcm_loop target_core_file uio target_core_user; do state_file=/sys/module/$mod/initstate if [ -f "$state_file" ] && grep -q live "$state_file"; then - echo "Module $mod is running" + log "Module $mod is running" else - echo "Module $mod is not running" - echo "--> executing \"modprobe -b $mod\"" + log "Module $mod is not running" + log "--> executing \"modprobe -b $mod\"" if ! modprobe -b $mod; then # core_user and uio are not mandatory if [ "$mod" != "target_core_user" ] && [ "$mod" != "uio" ]; then exit 1 else - echo "Couldn't enable $mod" + log "Couldn't enable $mod" fi fi # Enable module at boot @@ -65,8 +71,8 @@ done # Check if the modules loaded have its # directories available on top of configfs -[ ! -d "$target_dir" ] && echo "$target_dir doesn't exist" && module_error_log "target_core_mod" "$target_dir" -[ ! -d "$core_dir" ] && echo "$core_dir doesn't exist" && module_error_log "target_core_file" "$core_dir" -[ ! -d "$loop_dir" ] && echo "$loop_dir doesn't exist. Creating dir manually..." && mkdir $loop_dir +[ ! -d "$target_dir" ] && log "$target_dir doesn't exist" && module_error_log "target_core_mod" "$target_dir" +[ ! -d "$core_dir" ] && log "$core_dir doesn't exist" && module_error_log "target_core_file" "$core_dir" +[ ! -d "$loop_dir" ] && log "$loop_dir doesn't exist. Creating dir manually..." && mkdir $loop_dir -echo "LIO set up is ready!" +log "LIO set up is ready!" diff --git a/scripts/02-limits/limits.sh b/scripts/02-limits/limits.sh index abcb980..59fccca 100755 --- a/scripts/02-limits/limits.sh +++ b/scripts/02-limits/limits.sh @@ -2,6 +2,12 @@ set -e +function log() { + local msg="$1" + timestamp_utc=$(date -u --rfc-3339=ns | sed 's/ /T/; s/\(\....\).*\([+-]\)/\1\2/g') + echo $timestamp_utc $msg +} + # For a directory containeing the cgroup slice information, return the value of # pids.max, or 0 if set to "max". Return -1 exit code if the file doesn't exist. function read_max_pids() { @@ -46,24 +52,24 @@ done # TBC: Don't fail if we can't determine limit. if [ $max_pids_limit -eq $default_max_pids_limit ]; then - echo "WARNING: Unable to determine effective max.pids limit" + log "WARNING: Unable to determine effective max.pids limit" exit 0 fi # Fail if MINIMUM_MAX_PIDS_LIMIT is set and is greater than current limit. if [[ -n "${MINIMUM_MAX_PIDS_LIMIT}" && $MINIMUM_MAX_PIDS_LIMIT -gt $max_pids_limit ]]; then - echo "ERROR: Effective max.pids limit ($max_pids_limit) less than MINIMUM_MAX_PIDS_LIMIT ($MINIMUM_MAX_PIDS_LIMIT)" + log "ERROR: Effective max.pids limit ($max_pids_limit) less than MINIMUM_MAX_PIDS_LIMIT ($MINIMUM_MAX_PIDS_LIMIT)" exit 1 fi if [ -n "${RECOMMENDED_MAX_PIDS_LIMIT}" ]; then if [ $RECOMMENDED_MAX_PIDS_LIMIT -gt $max_pids_limit ]; then - echo "WARNING: Effective max.pids limit ($max_pids_limit) less than RECOMMENDED_MAX_PIDS_LIMIT ($RECOMMENDED_MAX_PIDS_LIMIT)" + log "WARNING: Effective max.pids limit ($max_pids_limit) less than RECOMMENDED_MAX_PIDS_LIMIT ($RECOMMENDED_MAX_PIDS_LIMIT)" else - echo "OK: Effective max.pids limit ($max_pids_limit) at least RECOMMENDED_MAX_PIDS_LIMIT ($RECOMMENDED_MAX_PIDS_LIMIT)" + log "OK: Effective max.pids limit ($max_pids_limit) at least RECOMMENDED_MAX_PIDS_LIMIT ($RECOMMENDED_MAX_PIDS_LIMIT)" fi exit 0 fi # No requirements set, just output current limit. -echo "Effective max.pids limit: $max_pids_limit" +log "Effective max.pids limit: $max_pids_limit"