Skip to content

Commit

Permalink
add args
Browse files Browse the repository at this point in the history
  • Loading branch information
gojoy committed Mar 17, 2018
1 parent b5aea88 commit 2abc9e0
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 240 deletions.
2 changes: 2 additions & 0 deletions api/grpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ func (s *apiServer) Migration(ctx context.Context, r *types.MigrationRequest) (*
e := &supervisor.MigrationTask{}
e.WithContext(ctx)
e.Id = r.Id
e.Args = r.Args
e.TargetMachine = struct {
Host string
Port uint32
Expand All @@ -524,6 +525,7 @@ func (s *apiServer) PreMigration(ctx context.Context, r *types.PreMigrationReque
e.Cname = r.CName
e.SrcIp = r.SrcIp
e.UpperId = r.Upperid
e.Args = r.Args
for _, v := range r.Vol {
e.Vol = append(e.Vol, struct {
Src string
Expand Down
375 changes: 196 additions & 179 deletions api/grpc/types/api.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions api/grpc/types/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ message PreMigrationRequest {
string cName=4;
string srcIp=5;
string upperid=6;
repeated string args=7;
}

message Volumes {
Expand All @@ -42,6 +43,7 @@ message PreMigrationResponse {
message MigrationRequest {
string id=1;
TargetMachine targetmachine=2;
repeated string args=3;
}

message MigrationResponse {
Expand Down
19 changes: 13 additions & 6 deletions ctr/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

var migrationCommand = cli.Command{
Name: "migration",
Usage: "migration containers",
ArgsUsage: "<container-id> <ip> <port> || mysql 192.168.18.2 9001",
Name: "migration",
Usage: "migration containers",
ArgsUsage: "-H <ip> -p <port> -A <args> <container-id> ||" +
" 192.168.18.2 9001 -A -e MYSQL_ROOT_PASSWORD=123456 mysql",
Flags: []cli.Flag{
cli.StringFlag{
Name: "host,H",
Expand All @@ -21,6 +22,10 @@ var migrationCommand = cli.Command{
Name: "port,p",
Usage: "host port",
},
cli.StringSliceFlag{
Name: "args,A",
Usage: "create docker containers args",
},
},
Action: func(context *cli.Context) error {
if err := checkArgs(context, 1); err != nil {
Expand All @@ -30,7 +35,8 @@ var migrationCommand = cli.Command{
id := context.Args().First()
ip := context.String("host")
port := context.Uint("port")
fmt.Printf("id:%v, ip %v, port:%v\n", id, ip, port)
args := context.StringSlice("args")
fmt.Printf("id:%v, ip: %v, port:%v, args: %v\n", id, ip, port, args)
c := getClient(context)
machine := &types.TargetMachine{
Ip: ip,
Expand All @@ -39,20 +45,21 @@ var migrationCommand = cli.Command{
_, err := c.Migration(netcontext.Background(), &types.MigrationRequest{
Id: id,
Targetmachine: machine,
Args: args,
})
if err != nil {
fmt.Println(err)
return err
}
fmt.Println("after rpc!")
fmt.Println("finish rpc!")
return nil
},
}

func checkArgs(context *cli.Context, expected int) error {
var err error
cmdName := context.Command.Name
fmt.Printf("nums is %v\n", context.NArg())
fmt.Printf("nums is %v,args is %v\n", context.NArg(), context.Args())
if context.NArg() != expected {
err = fmt.Errorf("%s: %q requires exactly %d argument(s)", os.Args[0], cmdName, expected)
}
Expand Down
25 changes: 4 additions & 21 deletions supervisor/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,22 @@ import (
type MigrationTask struct {
baseTask
TargetMachine
Id string
Id string
Args []string
}

type TargetMachine struct {
Host string
Port uint32
}

//
//type localMigration struct {
// *containerInfo
// Rootfs string
// Bundle string
// CheckpointDir string
// IsDump bool
//
//}
//
//type remoteMigration struct {
// Id string
// Rootfs string
// Bundle string
// CheckpointDir string
//}

var (
TimeLogger *log.Logger
TimeLogPos = "/run/migration/time.log"
)

func (s *Supervisor) StartMigration(t *MigrationTask) error {

logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp:true})
f, err := os.OpenFile(TimeLogPos, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
logrus.Printf("set timelog err:%v\n", err)
Expand Down Expand Up @@ -208,10 +192,9 @@ func (t *MigrationTask) startMigrationTask(c *containerInfo) error {
return err
}

//r,_:=migration.NewRemoteMigration(t,l)
//在目的主机进行premigration准备操作
logrus.Println("start premigration")
if err = r.PreRemoteMigration(t.Id, l.Imagedir.GetUpperId()); err != nil {
if err = r.PreRemoteMigration(t.Id, l.Imagedir.GetUpperId(), t.Args); err != nil {
logrus.Printf("premigration error: %v\n", err)
return err
}
Expand Down
3 changes: 1 addition & 2 deletions supervisor/migration/lazycopydir/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lazycopydir

import "time"


//crwdir 挂载的nfs目录 monidiruper读写层目录 lazydir 惰复制目录
func StartLazyCopy(crwdir, monidir, lazydir string) error {

Expand Down Expand Up @@ -76,4 +75,4 @@ func (replicator *LazyReplicator) Dolazycopy() error {
glog.Println("finish lazycopy!")

return nil
}
}
8 changes: 4 additions & 4 deletions supervisor/migration/lazycopydir/replicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func (l *LazyReplicator) Replicate() error {
targetdir = filepath.Join(l.LazyDir, file)

if isdir(file) {
_,err:=os.Stat(targetdir)
_, err := os.Stat(targetdir)
if os.IsNotExist(err) {
os.MkdirAll(targetdir,0755)
os.MkdirAll(targetdir, 0755)
}
} else {
if err = LocalCopy(sourcedir, targetdir); err != nil {
Expand All @@ -66,8 +66,8 @@ func (l *LazyReplicator) Replicate() error {
return nil
}

func isdir(v string)bool {
if v[len(v)-1]=='/' {
func isdir(v string) bool {
if v[len(v)-1] == '/' {
return true
}
return false
Expand Down
66 changes: 40 additions & 26 deletions supervisor/migration/preMigration.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,29 @@ type PreMigrationInTargetMachine struct {
ImageName string
Vol []Volumes
SrcIp string
Args []string
}

var (
lazyreplicator=make([]*lazycopydir.LazyReplicator,0)
lazyreplicator = make([]*lazycopydir.LazyReplicator, 0)
)

func (p *PreMigrationInTargetMachine) StartPre() error {
var (
err error
)

if len(p.Vol) == 0 {
goto CREATCONT
}

glog.Println("premkdir")
if err = p.PreMkVolDir(); err != nil {
glog.Println(err)
return err
}

CREATCONT:
glog.Println("create docker container")
if err = p.CreateDockerContainer(); err != nil {
glog.Println(err)
Expand All @@ -49,6 +56,9 @@ func (p *PreMigrationInTargetMachine) StartPre() error {
glog.Println(err)
return err
}
if len(p.Vol) == 0 {
goto STARTCONT
}

glog.Println("mount nfs")
if err = p.MountNfs(); err != nil {
Expand All @@ -63,18 +73,19 @@ func (p *PreMigrationInTargetMachine) StartPre() error {
}

glog.Println("pre lazycopy")
if err=p.StartPreLazyCopy();err!=nil {
if err = p.StartPreLazyCopy(); err != nil {
glog.Println(err)
return err
}

STARTCONT:
glog.Println("start docker container")
if err=p.StartDockerContainer();err!=nil {
if err = p.StartDockerContainer(); err != nil {
glog.Println(err)
return err
}

glog.Printf("now container start run! %v\n",time.Now())
glog.Printf("now container start run! %v\n", time.Now())

glog.Println("start lazycopy")
if err = p.StartLazyCopy(); err != nil {
Expand Down Expand Up @@ -119,18 +130,22 @@ func (p *PreMigrationInTargetMachine) CreateDockerContainer() error {
var (
err error
)
args1:=append([]string{"rm"},p.Cname+"copy")
cmd1:=exec.Command("docker",args1...)
args1 := append([]string{"rm"}, p.Cname+"copy")
cmd1 := exec.Command("docker", args1...)
cmd1.Run()

args := append([]string{"create", "-P", "--security-opt", "seccomp:unconfined",
"-e", "MYSQL_ROOT_PASSWORD=123456", "--name"},
p.Cname+"copy")
//args=append(args,"-v")
args := append([]string{"create", "-P", "--security-opt",
"seccomp:unconfined", "--name"}, p.Cname+"copy")

if len(p.Args) != 0 {
args = append(args, p.Args...)
}

for i, v := range p.Vol {
args = append(args, "-v", fmt.Sprintf("%s:%s",
filepath.Join(preVolume, p.Id, strconv.Itoa(i), "merge"), v.dst))
}

args = append(args, p.ImageName)
cmd := exec.Command("docker", args...)
glog.Printf("create cmd is %v\n", cmd.Args)
Expand All @@ -141,14 +156,14 @@ func (p *PreMigrationInTargetMachine) CreateDockerContainer() error {
return nil
}

func (p *PreMigrationInTargetMachine) StartDockerContainer() error {
name:=p.Cname+"copy"
args:=[]string{"start","--checkpoint-dir"}
args=append(args,filepath.Join(RemoteCheckpointDir,p.Id+"copy"))
args=append(args,"--checkpoint",DumpAll,name)
cmd:=exec.Command("docker",args...)
glog.Printf("start docker cmd is %v\n",cmd.Args)
if err:=cmd.Run();err!=nil {
func (p *PreMigrationInTargetMachine) StartDockerContainer() error {
name := p.Cname + "copy"
args := []string{"start", "--checkpoint-dir"}
args = append(args, filepath.Join(RemoteCheckpointDir, p.Id+"copy"))
args = append(args, "--checkpoint", DumpAll, name)
cmd := exec.Command("docker", args...)
glog.Printf("start docker cmd is %v\n", cmd.Args)
if err := cmd.Run(); err != nil {
glog.Println(err)
return err
}
Expand Down Expand Up @@ -266,35 +281,34 @@ func (p *PreMigrationInTargetMachine) StartPreLazyCopy() error {
var (
err error
crwdir, monidir, lazydir string

)

for i := 0; i < len(p.Vol); i++ {
glog.Printf("start lazy vol %d\n", i)
crwdir = filepath.Join(RemoteGetVolume(p.Id, i), "nfs")
monidir = filepath.Join(RemoteGetVolume(p.Id, i), "upper")
lazydir = filepath.Join(RemoteGetVolume(p.Id, i), "lazy")
r:=lazycopydir.NewLazyReplicator(crwdir,monidir,lazydir)
if err=r.Prelazy();err!=nil {
r := lazycopydir.NewLazyReplicator(crwdir, monidir, lazydir)
if err = r.Prelazy(); err != nil {
glog.Println(err)
return err
}
lazyreplicator=append(lazyreplicator,r)
lazyreplicator = append(lazyreplicator, r)

}
glog.Printf("finish pre lazy copy:%v",time.Now())
glog.Printf("finish pre lazy copy:%v", time.Now())
return nil
}

func (p *PreMigrationInTargetMachine) StartLazyCopy() error {
var (
err error
)
for _,v:=range lazyreplicator {
if err=v.Dolazycopy();err!=nil {
for _, v := range lazyreplicator {
if err = v.Dolazycopy(); err != nil {
glog.Println(err)
return err
}
}
return nil
}
}
3 changes: 2 additions & 1 deletion supervisor/migration/remoteMigration.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (r *remoteMigration) SetSpec(l *localMigration) error {
}

//向目的主机发送grpc请求
func (r *remoteMigration) PreRemoteMigration(id, upperid string) error {
func (r *remoteMigration) PreRemoteMigration(id, upperid string, args []string) error {

//glog.Printf("upperid is %v\n",upperid)
var (
Expand Down Expand Up @@ -195,6 +195,7 @@ func (r *remoteMigration) PreRemoteMigration(id, upperid string) error {
ImageName: imagename,
SrcIp: srcip,
CName: Cname,
Args: args,
}
pvol := make([]*types.Volumes, 0)
for _, v := range vol {
Expand Down
4 changes: 3 additions & 1 deletion supervisor/preMigration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type PreMigrationTask struct {
Vol []Volumes
Cname string
SrcIp string
Args []string
}

type Volumes struct {
Expand Down Expand Up @@ -44,9 +45,10 @@ func (s *Supervisor) PreMigration(t *PreMigrationTask) error {
ImageName: t.ImageName,
Vol: vols,
SrcIp: t.SrcIp,
Args: t.Args,
}

logrus.Printf("start preMigration at %v\n",time.Now())
logrus.Printf("start preMigration at %v\n", time.Now())
if err = pre.StartPre(); err != nil {
logrus.Printf("start pre in supervisor error:%v\n", err)
return err
Expand Down

0 comments on commit 2abc9e0

Please sign in to comment.