-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
177 lines (147 loc) · 4.57 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/pouchcontainer/d2p-migrator/ctrd"
"github.com/pouchcontainer/d2p-migrator/migrator"
"github.com/pouchcontainer/d2p-migrator/version"
"github.com/docker/docker/pkg/reexec"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// Config of pouch-migrator
type Config struct {
DockerRpmName string
PouchRpmPath string
MigrateAll bool
ImageProxy string
LiveMigrate bool
PrepareImage bool
ImageManifestOnly bool
User string
}
var (
printVersion bool
cfg = &Config{}
)
func main() {
if reexec.Init() {
return
}
var cmdServe = &cobra.Command{
Use: "d2p-migrator",
Args: cobra.NoArgs,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return runCmd()
},
}
setupFlags(cmdServe)
parseFlags(cmdServe, os.Args[1:])
if err := cmdServe.Execute(); err != nil {
fmt.Printf("failed to execute pouch-migrator: %v", err)
os.Exit(1)
}
os.Exit(0)
}
// initLog initializes log Level and log format of daemon.
func initLog() {
logrus.SetLevel(logrus.DebugLevel)
logrus.Infof("start daemon at debug level")
formatter := &logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.RFC3339Nano,
}
logrus.SetFormatter(formatter)
}
func setupFlags(cmd *cobra.Command) {
flagSet := cmd.Flags()
flagSet.StringVar(&cfg.DockerRpmName, "docker-pkg", "docker", "Specify docker package name")
flagSet.StringVar(&cfg.PouchRpmPath, "pouch-pkg-path", "pouch", "Specify pouch package file path")
flagSet.BoolVar(&cfg.MigrateAll, "migrate-all", false, "If true, do all migration things, otherwise, just prepare data for migration")
flagSet.BoolVar(&cfg.LiveMigrate, "live-migrate", false, "Auto takeover the docker running containers when migration, which will not affect the whole containers at all")
flagSet.StringVar(&cfg.ImageProxy, "image-proxy", "", "Http proxy to pull image")
flagSet.StringVar(&cfg.User, "user", "", "user[:password] Registry user and password")
flagSet.BoolVar(&cfg.PrepareImage, "pull-images", false, "If this flag set, we will just pull container images")
flagSet.BoolVar(&cfg.ImageManifestOnly, "image-manifest-only", false, "Pull image manifest only or not")
flagSet.BoolVarP(&printVersion, "version", "v", false, "Print d2p-migrator version")
}
func parseFlags(cmd *cobra.Command, flags []string) {
err := cmd.Flags().Parse(flags)
if err == nil || err == pflag.ErrHelp {
return
}
cmd.SetOutput(os.Stderr)
cmd.Usage()
}
// runCmd prepares configs, setups essential details and runs pouch-migrator
func runCmd() error {
// initialize log
initLog()
//user specifies --version or -v, print version and return.
if printVersion {
fmt.Printf("d2p-migrator version: %s, build: %s, build at: %s\n", version.Version, version.GitCommit, version.BuildTime)
return nil
}
// set proxy for image pull
if cfg.ImageProxy != "" {
ctrd.SetImageProxy(cfg.ImageProxy)
}
// set credential info for private registry
if cfg.User != "" {
ctrd.SetCredential(cfg.User)
}
ctx := context.Background()
migratorCfg := migrator.Config{
Type: "cold-migrate",
DockerRpmName: cfg.DockerRpmName,
PouchRpmPath: cfg.PouchRpmPath,
ImageManifestOnly: cfg.ImageManifestOnly,
}
if cfg.LiveMigrate {
migratorCfg.Type = "live-migrate"
}
migrator, err := migrator.NewD2pMigrator(migratorCfg)
if err != nil {
logrus.Errorf("failed to new pouch migrator: %v\n", err)
return err
}
defer migrator.Cleanup()
if cfg.PrepareImage {
return migrator.PrepareImages(ctx)
}
if err := migrator.PreMigrate(ctx); err != nil {
logrus.Errorf("failed to execute PreMigrage: %v", err)
return err
}
if !cfg.MigrateAll {
logrus.Infof("Just prepare data:\n * Pull Image \n * Prepare snapshots \n * Set disk quota \n * Convert docker container meta json file to pouch container meta json file ")
return nil
}
// If migration failed, revert it.
needRevert := false
defer func() {
if needRevert {
logrus.Info("Migration has failed, start revert...")
if err := migrator.RevertMigration(ctx); err != nil {
logrus.Errorf("failed to revert migration: %v", err)
return
}
logrus.Info("Revert migration done!\n")
}
}()
if err := migrator.Migrate(ctx); err != nil {
logrus.Errorf("failed to migrate: %v\n", err)
needRevert = true
return err
}
// If PostMigrate failed, should handle by manual.
if err := migrator.PostMigrate(ctx); err != nil {
logrus.Errorf("PostMigrate error: %v, need handle by manual!!!", err)
return err
}
return nil
}