github.com/swiftstack/ProxyFS@v0.0.0-20210203235616-4017c267d62f/confgen/template.go (about)

     1  // Copyright (c) 2015-2021, NVIDIA CORPORATION.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package confgen
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"text/template"
    10  )
    11  
    12  // createSMBConf writes the per VG smb.conf file
    13  func createSMBConf(initialDirPath string, localVolumeGroupMap volumeGroupMap) (err error) {
    14  	var (
    15  		vipDirPath  string
    16  		volumeGroup *VolumeGroup
    17  	)
    18  
    19  	// Locate the proxyfs config directory with the templates
    20  	var proxyfsConfDir string
    21  	confDirs := []string{proxyfsConfDir0, proxyfsConfDir1, proxyfsConfDir2}
    22  	for _, proxyfsConfDir = range confDirs {
    23  
    24  		// if the directory has a "smb_globals.tmpl"
    25  		_, err = os.Stat(proxyfsConfDir + "/" + "smb_globals.tmpl")
    26  		if err == nil {
    27  			break
    28  		}
    29  	}
    30  	if err != nil {
    31  		fmt.Printf("Could not find '%s' in any of %v\n", "smb_globals.tmpl", confDirs)
    32  		return
    33  	}
    34  
    35  	// Load the template for the global section of smb.conf
    36  	// from one of several possible locations
    37  	globalTplate, err := template.ParseFiles(proxyfsConfDir + "/" + "smb_globals.tmpl")
    38  	if nil != err {
    39  
    40  		// TODO - log this appropriately
    41  		fmt.Printf("Parse of template file returned err: %v\n", err)
    42  		return
    43  	}
    44  
    45  	// Load the template for the share section of smb.conf
    46  	sharesTplate, err := template.ParseFiles(proxyfsConfDir + "/" + "smb_shares.tmpl")
    47  	if nil != err {
    48  		// TODO - log this appropriately
    49  		fmt.Printf("Parse of template file returned err: %v\n", err)
    50  		return
    51  	}
    52  
    53  	for _, volumeGroup = range localVolumeGroupMap {
    54  		vipDirPath = initialDirPath + "/" + vipsDirName + "/" + volumeGroup.VirtualIPAddr.String()
    55  
    56  		err = os.Mkdir(vipDirPath, confDirPerm)
    57  		if nil != err {
    58  			return
    59  		}
    60  
    61  		fileName := vipDirPath + "/smb-VG-" + volumeGroup.VolumeGroupName + ".conf"
    62  		f, openErr := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, smbConfPerm)
    63  		if nil != openErr {
    64  			err = openErr
    65  			return
    66  		}
    67  
    68  		err = globalTplate.Execute(f, volumeGroup)
    69  		if nil != err {
    70  			return
    71  		}
    72  
    73  		err = sharesTplate.Execute(f, volumeGroup)
    74  		if nil != err {
    75  			return
    76  		}
    77  	}
    78  
    79  	return
    80  }