github.com/pingcap/tiup@v1.15.1/components/dm/command/import.go (about) 1 // Copyright 2020 PingCAP, Inc. 2 // 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package command 15 16 import ( 17 "context" 18 "fmt" 19 "os" 20 21 "github.com/fatih/color" 22 "github.com/pingcap/errors" 23 "github.com/pingcap/tiup/components/dm/ansible" 24 cansible "github.com/pingcap/tiup/pkg/cluster/ansible" 25 "github.com/pingcap/tiup/pkg/cluster/ctxt" 26 "github.com/pingcap/tiup/pkg/cluster/manager" 27 "github.com/pingcap/tiup/pkg/tui" 28 tiuputils "github.com/pingcap/tiup/pkg/utils" 29 "github.com/spf13/cobra" 30 "gopkg.in/yaml.v2" 31 ) 32 33 func newImportCmd() *cobra.Command { 34 var ansibleDir string 35 var inventoryFileName string 36 var rename string 37 var clusterVersion string 38 39 cmd := &cobra.Command{ 40 Use: "import", 41 Short: "Import an exist DM 1.0 cluster from dm-ansible and re-deploy 2.0 version", 42 RunE: func(cmd *cobra.Command, args []string) error { 43 if err := supportVersion(clusterVersion); err != nil { 44 return err 45 } 46 47 importer, err := ansible.NewImporter(ansibleDir, inventoryFileName, gOpt.SSHType, gOpt.SSHTimeout) 48 if err != nil { 49 return err 50 } 51 52 ctx := ctxt.New(context.Background(), 0, log) 53 clusterName, meta, err := importer.ImportFromAnsibleDir(ctx) 54 if err != nil { 55 return err 56 } 57 58 if rename != "" { 59 clusterName = rename 60 } 61 62 err = importer.ScpSourceToMaster(ctx, meta.Topology) 63 if err != nil { 64 return err 65 } 66 67 data, err := yaml.Marshal(meta.Topology) 68 if err != nil { 69 return errors.AddStack(err) 70 } 71 72 f, err := os.CreateTemp("", "tiup-*") 73 if err != nil { 74 return errors.AddStack(err) 75 } 76 77 _, err = f.Write(data) 78 if err != nil { 79 return errors.AddStack(err) 80 } 81 82 fmt.Println(color.HiYellowString("Will use the following topology to deploy a DM cluster: ")) 83 fmt.Println(string(data)) 84 85 if !skipConfirm { 86 err = tui.PromptForConfirmOrAbortError( 87 color.HiYellowString("Using the Topology to deploy DM %s cluster %s, Please Stop the DM cluster from ansible side first.\nDo you want to continue? [y/N]: ", 88 clusterVersion, 89 clusterName, 90 )) 91 if err != nil { 92 return err 93 } 94 } 95 96 err = cm.Deploy( 97 clusterName, 98 clusterVersion, 99 f.Name(), 100 manager.DeployOptions{ 101 IdentityFile: cansible.SSHKeyPath(), 102 User: tiuputils.CurrentUser(), 103 }, 104 nil, 105 skipConfirm, 106 gOpt, 107 ) 108 109 if err != nil { 110 return err 111 } 112 113 return nil 114 }, 115 } 116 117 cmd.Flags().StringVarP(&ansibleDir, "dir", "d", "./", "The path to DM-Ansible directory") 118 cmd.Flags().StringVar(&inventoryFileName, "inventory", cansible.AnsibleInventoryFile, "The name of inventory file") 119 cmd.Flags().StringVarP(&rename, "rename", "r", "", "Rename the imported cluster to `NAME`") 120 cmd.Flags().StringVarP(&clusterVersion, "cluster-version", "v", "", "cluster version of DM to deploy (required)") 121 122 err := cmd.MarkFlagRequired("cluster-version") 123 if err != nil { // if no this flag 124 panic(err) 125 } 126 127 return cmd 128 }