Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
packagemain
import(
"fmt"
"os"
"path/filepath"
"github.com/golang/glog"
"github.com/spf13/cobra"
)
const(
versionFilename="version.txt"
defaultPortuint64=18629
)
var(
migrateCmd=&cobra.Command{
Short:"Upgrade/downgrade etcd data across multiple versions",
Long:`Upgrade or downgrade etcd data across multiple versions to the target version
Given a 'bin-dir' directory of etcd and etcdctl binaries, an etcd 'data-dir' with a 'version.txt' file and
a target etcd version, this tool will upgrade or downgrade the etcd data from the version specified in
'version.txt' to the target version.
`,
Run:func(cmd*cobra.Command,args[]string){
runMigrate()
},
}
opts=migrateOpts{}
)
typemigrateOptsstruct{
namestring
portuint64
peerListenUrlsstring
peerAdvertiseUrlsstring
binDirstring
dataDirstring
bundledVersionStringstring
etcdDataPrefixstring
ttlKeysDirectorystring
initialClusterstring
targetVersionstring
targetStoragestring
etcdServerArgsstring
}
funcmain(){
flags:=migrateCmd.Flags()
flags.StringVar(&opts.name,"name","","etcd cluster member name. Defaults to etcd-{hostname}")
flags.Uint64Var(&opts.port,"port",defaultPort,"etcd client port to use during migration operations. This should be a different port than typically used by etcd to avoid clients accidentally connecting during upgrade/downgrade operations.")
flags.StringVar(&opts.peerListenUrls,"listen-peer-urls","","etcd --listen-peer-urls flag, required for HA clusters")
flags.StringVar(&opts.peerAdvertiseUrls,"initial-advertise-peer-urls","","etcd --initial-advertise-peer-urls flag, required for HA clusters")
flags.StringVar(&opts.binDir,"bin-dir","/usr/local/bin","directory of etcd and etcdctl binaries, must contain etcd-<version> and etcdctl-<version> for each version listed in bindled-versions")
flags.StringVar(&opts.dataDir,"data-dir","","etcd data directory of etcd server to migrate")
flags.StringVar(&opts.bundledVersionString,"bundled-versions","","comma separated list of etcd binary versions present under the bin-dir")
flags.StringVar(&opts.etcdDataPrefix,"etcd-data-prefix","/registry","etcd key prefix under which all objects are kept")
flags.StringVar(&opts.ttlKeysDirectory,"ttl-keys-directory","","etcd key prefix under which all keys with TTLs are kept. Defaults to {etcd-data-prefix}/events")
flags.StringVar(&opts.initialCluster,"initial-cluster","","comma separated list of name=endpoint pairs. Defaults to etcd-{hostname}=http://localhost:2380")
flags.StringVar(&opts.targetVersion,"target-version","","version of etcd to migrate to. Format must be '<major>.<minor>.<patch>'")
flags.StringVar(&opts.targetStorage,"target-storage","","storage version of etcd to migrate to, one of: etcd2, etcd3")
flags.StringVar(&opts.etcdServerArgs,"etcd-server-extra-args","","additional etcd server args for starting etcd servers during migration steps, --peer-* TLS cert flags should be added for etcd clusters with more than 1 member that use mutual TLS for peer communication.")
migrateCmd.Execute()
}
// runMigrate validates the command line flags and starts the migration.
funcrunMigrate(){
ifopts.name==""{
hostname,err:=os.Hostname()
iferr!=nil{
glog.Errorf("Error while getting hostname to supply default --name: %v",err)
returnnil,fmt.Errorf("rollback from %s to %s not supported, only rollbacks to the previous minor version are supported",current.version,target.version)