|
| 1 | +/* |
| 2 | + (c) Copyright [2023-2024] Open Text. |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + You may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +package commands |
| 17 | + |
| 18 | +import ( |
| 19 | + "github.com/spf13/cobra" |
| 20 | + "github.com/spf13/viper" |
| 21 | + "github.com/vertica/vcluster/vclusterops" |
| 22 | + "github.com/vertica/vcluster/vclusterops/vlog" |
| 23 | +) |
| 24 | + |
| 25 | +/* CmdCreateArchive |
| 26 | + * |
| 27 | + * Parses arguments to create-archive and calls |
| 28 | + * the high-level function for create-archive. |
| 29 | + * |
| 30 | + * Implements ClusterCommand interface |
| 31 | + */ |
| 32 | + |
| 33 | +type CmdCreateArchive struct { |
| 34 | + CmdBase |
| 35 | + createArchiveOptions *vclusterops.VCreateArchiveOptions |
| 36 | +} |
| 37 | + |
| 38 | +func makeCmdCreateArchive() *cobra.Command { |
| 39 | + newCmd := &CmdCreateArchive{} |
| 40 | + opt := vclusterops.VCreateArchiveFactory() |
| 41 | + newCmd.createArchiveOptions = &opt |
| 42 | + |
| 43 | + cmd := makeBasicCobraCmd( |
| 44 | + newCmd, |
| 45 | + createArchiveCmd, |
| 46 | + "Create an archive in a given archive name and number.", |
| 47 | + `Create an archive in a given archive name and number. |
| 48 | +
|
| 49 | +Examples: |
| 50 | + # Create an archive in a given archive name |
| 51 | + vcluster create_archive --db-name DBNAME --archive-name ARCHIVE_ONE |
| 52 | +
|
| 53 | + # Create an archive in a given archive name and number of restore point(default 3) |
| 54 | + vcluster create_archive --db-name DBNAME --archive-name ARCHIVE_ONE \ |
| 55 | + --num-restore-points 6 |
| 56 | +
|
| 57 | + # Create an archive in main cluster with user input password |
| 58 | + vcluster create_archive --db-name DBNAME --archive-name ARCHIVE_ONE \ |
| 59 | + --hosts 10.20.30.40,10.20.30.41,10.20.30.42 --password "PASSWORD" |
| 60 | +
|
| 61 | + # Create an archive for a sandbox |
| 62 | + vcluster create_archive --db-name DBNAME --archive-name ARCHIVE_ONE \ |
| 63 | + --sandbox SANDBOX_ONE --password "PASSWORD" |
| 64 | +
|
| 65 | +`, |
| 66 | + []string{dbNameFlag, configFlag, passwordFlag, |
| 67 | + hostsFlag, ipv6Flag, eonModeFlag}, |
| 68 | + ) |
| 69 | + |
| 70 | + // local flags |
| 71 | + newCmd.setLocalFlags(cmd) |
| 72 | + |
| 73 | + // require archive-name |
| 74 | + markFlagsRequired(cmd, archiveNameFlag) |
| 75 | + |
| 76 | + // hide this subcommand |
| 77 | + cmd.Hidden = true |
| 78 | + |
| 79 | + return cmd |
| 80 | +} |
| 81 | + |
| 82 | +// setLocalFlags will set the local flags the command has |
| 83 | +func (c *CmdCreateArchive) setLocalFlags(cmd *cobra.Command) { |
| 84 | + cmd.Flags().StringVar( |
| 85 | + &c.createArchiveOptions.ArchiveName, |
| 86 | + archiveNameFlag, |
| 87 | + "", |
| 88 | + "The name of archive to be created.", |
| 89 | + ) |
| 90 | + cmd.Flags().IntVar( |
| 91 | + &c.createArchiveOptions.NumRestorePoint, |
| 92 | + "num-restore-points", |
| 93 | + vclusterops.CreateArchiveDefaultNumRestore, |
| 94 | + "Maximum number of restore points that archive can contain."+ |
| 95 | + "If you provide 0, the number of restore points will be unlimited. "+ |
| 96 | + "By default, the value is 0. Negative number is disallowed.", |
| 97 | + ) |
| 98 | + cmd.Flags().StringVar( |
| 99 | + &c.createArchiveOptions.Sandbox, |
| 100 | + sandboxFlag, |
| 101 | + "", |
| 102 | + "The name of target sandbox", |
| 103 | + ) |
| 104 | +} |
| 105 | + |
| 106 | +func (c *CmdCreateArchive) Parse(inputArgv []string, logger vlog.Printer) error { |
| 107 | + c.argv = inputArgv |
| 108 | + logger.LogArgParse(&c.argv) |
| 109 | + |
| 110 | + // for some options, we do not want to use their default values, |
| 111 | + // if they are not provided in cli, |
| 112 | + // reset the value of those options to nil |
| 113 | + c.ResetUserInputOptions(&c.createArchiveOptions.DatabaseOptions) |
| 114 | + |
| 115 | + // create_archive only works for an Eon db so we assume the user always runs this subcommand |
| 116 | + // on an Eon db. When Eon mode cannot be found in config file, we set its value to true. |
| 117 | + if !viper.IsSet(eonModeKey) { |
| 118 | + c.createArchiveOptions.IsEon = true |
| 119 | + } |
| 120 | + |
| 121 | + return c.validateParse(logger) |
| 122 | +} |
| 123 | + |
| 124 | +// all validations of the arguments should go in here |
| 125 | +func (c *CmdCreateArchive) validateParse(logger vlog.Printer) error { |
| 126 | + logger.Info("Called validateParse()") |
| 127 | + |
| 128 | + err := c.ValidateParseBaseOptions(&c.createArchiveOptions.DatabaseOptions) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + err = c.setConfigParam(&c.createArchiveOptions.DatabaseOptions) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + if !c.usePassword() { |
| 139 | + err = c.getCertFilesFromCertPaths(&c.createArchiveOptions.DatabaseOptions) |
| 140 | + if err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + err = c.setDBPassword(&c.createArchiveOptions.DatabaseOptions) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +func (c *CmdCreateArchive) Analyze(logger vlog.Printer) error { |
| 154 | + logger.Info("Called method Analyze()") |
| 155 | + return nil |
| 156 | +} |
| 157 | + |
| 158 | +func (c *CmdCreateArchive) Run(vcc vclusterops.ClusterCommands) error { |
| 159 | + vcc.LogInfo("Called method Run()") |
| 160 | + |
| 161 | + options := c.createArchiveOptions |
| 162 | + |
| 163 | + err := vcc.VCreateArchive(options) |
| 164 | + if err != nil { |
| 165 | + vcc.LogError(err, "failed to create archive", "archiveName", options.ArchiveName) |
| 166 | + return err |
| 167 | + } |
| 168 | + |
| 169 | + vcc.DisplayInfo("Successfully created archive: %s", options.ArchiveName) |
| 170 | + return nil |
| 171 | +} |
| 172 | + |
| 173 | +// SetDatabaseOptions will assign a vclusterops.DatabaseOptions instance to the one in CmdCreateArchive |
| 174 | +func (c *CmdCreateArchive) SetDatabaseOptions(opt *vclusterops.DatabaseOptions) { |
| 175 | + c.createArchiveOptions.DatabaseOptions = *opt |
| 176 | +} |
0 commit comments