github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/config/callhome/callhome.go (about)

     1  // Copyright (c) 2015-2022 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero 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 Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package callhome
    19  
    20  import (
    21  	"sync"
    22  	"time"
    23  
    24  	"github.com/minio/minio/internal/config"
    25  	"github.com/minio/pkg/v2/env"
    26  )
    27  
    28  // Callhome related keys
    29  const (
    30  	Enable    = "enable"
    31  	Frequency = "frequency"
    32  )
    33  
    34  // DefaultKVS - default KV config for subnet settings
    35  var DefaultKVS = config.KVS{
    36  	config.KV{
    37  		Key:   Enable,
    38  		Value: "off",
    39  	},
    40  	config.KV{
    41  		Key:   Frequency,
    42  		Value: "24h",
    43  	},
    44  }
    45  
    46  // callhomeCycleDefault is the default interval between two callhome cycles (24hrs)
    47  const callhomeCycleDefault = 24 * time.Hour
    48  
    49  // Config represents the subnet related configuration
    50  type Config struct {
    51  	// Flag indicating whether callhome is enabled.
    52  	Enable bool `json:"enable"`
    53  
    54  	// The interval between callhome cycles
    55  	Frequency time.Duration `json:"frequency"`
    56  }
    57  
    58  var configLock sync.RWMutex
    59  
    60  // Enabled - indicates if callhome is enabled or not
    61  func (c *Config) Enabled() bool {
    62  	configLock.RLock()
    63  	defer configLock.RUnlock()
    64  
    65  	return c.Enable
    66  }
    67  
    68  // FrequencyDur - returns the currently configured callhome frequency
    69  func (c *Config) FrequencyDur() time.Duration {
    70  	configLock.RLock()
    71  	defer configLock.RUnlock()
    72  
    73  	if c.Frequency == 0 {
    74  		return callhomeCycleDefault
    75  	}
    76  
    77  	return c.Frequency
    78  }
    79  
    80  // Update updates new callhome frequency
    81  func (c *Config) Update(ncfg Config) {
    82  	configLock.Lock()
    83  	defer configLock.Unlock()
    84  
    85  	c.Enable = ncfg.Enable
    86  	c.Frequency = ncfg.Frequency
    87  }
    88  
    89  // LookupConfig - lookup config and override with valid environment settings if any.
    90  func LookupConfig(kvs config.KVS) (cfg Config, err error) {
    91  	if err = config.CheckValidKeys(config.CallhomeSubSys, kvs, DefaultKVS); err != nil {
    92  		return cfg, err
    93  	}
    94  
    95  	cfg.Enable = env.Get(config.EnvMinIOCallhomeEnable,
    96  		kvs.GetWithDefault(Enable, DefaultKVS)) == config.EnableOn
    97  	cfg.Frequency, err = time.ParseDuration(env.Get(config.EnvMinIOCallhomeFrequency,
    98  		kvs.GetWithDefault(Frequency, DefaultKVS)))
    99  	return cfg, err
   100  }