...

Source file src/scheduler/config/utils.go

Documentation: scheduler/config

     1  /*
     2   * P2PFaaS - A framework for FaaS Load Balancing
     3   * Copyright (c) 2019 - 2022. Gabriele Proietti Mattia <pm.gabriele@outlook.com>
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17   */
    18  
    19  package config
    20  
    21  import (
    22  	"encoding/json"
    23  	"io/ioutil"
    24  	"os"
    25  	"scheduler/log"
    26  	"scheduler/types"
    27  )
    28  
    29  func GetConfigFilePath() string {
    30  	return GetDataPath() + "/" + ConfigurationFileName
    31  }
    32  
    33  func GetConfigSchedulerFilePath() string {
    34  	return GetDataPath() + "/" + ConfigurationSchedulerFileName
    35  }
    36  
    37  func SaveConfigurationDynamicToConfigFile() error {
    38  	// create folder if not exists
    39  	err := CreateDataFolder() // os.Mkdir(GetDataPath(), 0664)
    40  	if err != nil {
    41  		log.Log.Errorf("Cannot create folder %s: %s", GetDataPath(), err.Error())
    42  		return err
    43  	}
    44  
    45  	// save configuration to file
    46  	configJson, err := json.MarshalIndent(configurationDynamic, "", "  ")
    47  	err = os.WriteFile(GetConfigFilePath(), configJson, 0644)
    48  	if err != nil {
    49  		log.Log.Errorf("Cannot save configuration to file %s: %s", GetConfigFilePath(), err.Error())
    50  		return err
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func SaveConfigurationSchedulerToConfigFile(descriptor *types.SchedulerDescriptor) error {
    57  	// create folder if not exists
    58  	err := CreateDataFolder() // os.Mkdir(GetDataPath(), 0664)
    59  	if err != nil {
    60  		log.Log.Errorf("Cannot create folder %s: %s", GetDataPath(), err.Error())
    61  		return err
    62  	}
    63  
    64  	// save configuration to file
    65  	configJson, err := json.MarshalIndent(descriptor, "", "  ")
    66  	err = ioutil.WriteFile(GetConfigSchedulerFilePath(), configJson, 0644)
    67  	if err != nil {
    68  		log.Log.Errorf("Cannot save configuration to file %s: %s", GetConfigSchedulerFilePath(), err.Error())
    69  		return err
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  func CreateDataFolder() error {
    76  	// check if folder exists
    77  	if _, err := os.Stat(GetDataPath()); !os.IsNotExist(err) {
    78  		return nil
    79  	}
    80  
    81  	// create folder if not exists
    82  	err := os.Mkdir(GetDataPath(), 0664)
    83  	if err != nil {
    84  		log.Log.Errorf("Cannot create folder %s: %s", GetDataPath(), err.Error())
    85  		return err
    86  	}
    87  
    88  	return nil
    89  }
    90  

View as plain text