|
| 1 | +/* |
| 2 | + Licensed under the MIT License <http://opensource.org/licenses/MIT>. |
| 3 | +
|
| 4 | + Copyright © 2023-2025 Seagate Technology LLC and/or its Affiliates |
| 5 | + Copyright © 2020-2024 Microsoft Corporation. All rights reserved. |
| 6 | +
|
| 7 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + of this software and associated documentation files (the "Software"), to deal |
| 9 | + in the Software without restriction, including without limitation the rights |
| 10 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + copies of the Software, and to permit persons to whom the Software is |
| 12 | + furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | + The above copyright notice and this permission notice shall be included in all |
| 15 | + copies or substantial portions of the Software. |
| 16 | +
|
| 17 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + SOFTWARE |
| 24 | +*/ |
| 25 | + |
| 26 | +package cmd |
| 27 | + |
| 28 | +import ( |
| 29 | + "fmt" |
| 30 | + "os" |
| 31 | + "strings" |
| 32 | + |
| 33 | + "github.com/Seagate/cloudfuse/common" |
| 34 | + "github.com/Seagate/cloudfuse/common/config" |
| 35 | + "github.com/Seagate/cloudfuse/internal" |
| 36 | + "github.com/spf13/cobra" |
| 37 | +) |
| 38 | + |
| 39 | +type genConfigParams struct { |
| 40 | + blockCache bool `config:"block-cache" yaml:"block-cache,omitempty"` |
| 41 | + directIO bool `config:"direct-io" yaml:"direct-io,omitempty"` |
| 42 | + readOnly bool `config:"ro" yaml:"ro,omitempty"` |
| 43 | + tmpPath string `config:"tmp-path" yaml:"tmp-path,omitempty"` |
| 44 | + outputFile string `config:"o" yaml:"o,omitempty"` |
| 45 | +} |
| 46 | + |
| 47 | +var optsGenCfg genConfigParams |
| 48 | + |
| 49 | +var generatedConfig = &cobra.Command{ |
| 50 | + Use: "gen-config", |
| 51 | + Short: "Generate default config file.", |
| 52 | + Long: "Generate default config file with the values pre-caculated by cloudfuse.", |
| 53 | + SuggestFor: []string{"generate default config", "generate config"}, |
| 54 | + Hidden: true, |
| 55 | + Args: cobra.ExactArgs(0), |
| 56 | + FlagErrorHandling: cobra.ExitOnError, |
| 57 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 58 | + |
| 59 | + // Check if configTmp is not provided when component is fc |
| 60 | + if (!optsGenCfg.blockCache) && optsGenCfg.tmpPath == "" { |
| 61 | + return fmt.Errorf("temp path is required for file cache mode. Use flag --tmp-path to provide the path") |
| 62 | + } |
| 63 | + |
| 64 | + // Set the configs |
| 65 | + if optsGenCfg.readOnly { |
| 66 | + config.Set("read-only", "true") |
| 67 | + } |
| 68 | + |
| 69 | + if optsGenCfg.directIO { |
| 70 | + config.Set("direct-io", "true") |
| 71 | + } |
| 72 | + |
| 73 | + config.Set("tmp-path", optsGenCfg.tmpPath) |
| 74 | + |
| 75 | + // Create the pipeline |
| 76 | + pipeline := []string{"libfuse"} |
| 77 | + if optsGenCfg.blockCache { |
| 78 | + pipeline = append(pipeline, "block_cache") |
| 79 | + } else { |
| 80 | + pipeline = append(pipeline, "file_cache") |
| 81 | + } |
| 82 | + |
| 83 | + if !optsGenCfg.directIO { |
| 84 | + pipeline = append(pipeline, "attr_cache") |
| 85 | + } |
| 86 | + pipeline = append(pipeline, "azstorage") |
| 87 | + |
| 88 | + var sb strings.Builder |
| 89 | + |
| 90 | + if optsGenCfg.directIO { |
| 91 | + sb.WriteString("direct-io: true\n") |
| 92 | + } |
| 93 | + |
| 94 | + if optsGenCfg.readOnly { |
| 95 | + sb.WriteString("read-only: true\n\n") |
| 96 | + } |
| 97 | + |
| 98 | + sb.WriteString("# Logger configuration\n#logging:\n # type: syslog|silent|base\n # level: log_off|log_crit|log_err|log_warning|log_info|log_trace|log_debug\n") |
| 99 | + sb.WriteString(" # file-path: <path where log files shall be stored. Default - '$HOME/.cloudfuse/cloudfuse.log'>\n") |
| 100 | + |
| 101 | + sb.WriteString("\ncomponents:\n") |
| 102 | + for _, component := range pipeline { |
| 103 | + sb.WriteString(fmt.Sprintf(" - %s\n", component)) |
| 104 | + } |
| 105 | + |
| 106 | + for _, component := range pipeline { |
| 107 | + c := internal.GetComponent(component) |
| 108 | + if c == nil { |
| 109 | + return fmt.Errorf("generatedConfig:: error getting component [%s]", component) |
| 110 | + } |
| 111 | + sb.WriteString("\n") |
| 112 | + sb.WriteString(c.GenConfig()) |
| 113 | + } |
| 114 | + |
| 115 | + sb.WriteString("\n#Required\n#azstorage:\n # type: block|adls \n # account-name: <name of the storage account>\n # container: <name of the storage container to be mounted>\n # endpoint: <example - https://account-name.blob.core.windows.net>\n ") |
| 116 | + sb.WriteString("# mode: key|sas|spn|msi|azcli \n # account-key: <storage account key>\n # OR\n # sas: <storage account sas>\n # OR\n # appid: <storage account app id / client id for MSI>\n # OR\n # tenantid: <storage account tenant id for SPN") |
| 117 | + |
| 118 | + filePath := "" |
| 119 | + if optsGenCfg.outputFile == "" { |
| 120 | + filePath = "./cloudfuse.yaml" |
| 121 | + } else { |
| 122 | + filePath = optsGenCfg.outputFile |
| 123 | + } |
| 124 | + |
| 125 | + var err error = nil |
| 126 | + if optsGenCfg.outputFile == "console" { |
| 127 | + fmt.Println(sb.String()) |
| 128 | + } else { |
| 129 | + err = common.WriteToFile(filePath, sb.String(), common.WriteToFileOptions{Flags: os.O_TRUNC, Permission: 0644}) |
| 130 | + } |
| 131 | + |
| 132 | + return err |
| 133 | + }, |
| 134 | +} |
| 135 | + |
| 136 | +func init() { |
| 137 | + rootCmd.AddCommand(generatedConfig) |
| 138 | + |
| 139 | + generatedConfig.Flags().BoolVar(&optsGenCfg.blockCache, "block-cache", false, "Block-Cache shall be used as caching strategy") |
| 140 | + generatedConfig.Flags().BoolVar(&optsGenCfg.directIO, "direct-io", false, "Direct-io mode shall be used") |
| 141 | + generatedConfig.Flags().BoolVar(&optsGenCfg.readOnly, "ro", false, "Mount in read-only mode") |
| 142 | + generatedConfig.Flags().StringVar(&optsGenCfg.tmpPath, "tmp-path", "", "Temp cache path to be used") |
| 143 | + generatedConfig.Flags().StringVar(&optsGenCfg.outputFile, "o", "", "Output file location") |
| 144 | +} |
0 commit comments