@@ -143,7 +143,7 @@ func (vm *Vm) WaitResponse(match matchResponse, timeout int) chan error {
143143}
144144
145145func (vm * Vm ) ReleaseVm () error {
146- if vm .ctx .current != StateRunning {
146+ if ! vm .ctx .IsRunning () {
147147 return nil
148148 }
149149
@@ -226,58 +226,6 @@ func (vm *Vm) WaitProcess(isContainer bool, ids []string, timeout int) <-chan *a
226226 return result
227227}
228228
229- //func (vm *Vm) handlePodEvent(mypod *PodStatus) {
230- // glog.V(1).Infof("hyperHandlePodEvent pod %s, vm %s", mypod.Id, vm.Id)
231- //
232- // Status, err := vm.GetResponseChan()
233- // if err != nil {
234- // return
235- // }
236- // defer vm.ReleaseResponseChan(Status)
237- //
238- // exit := false
239- // mypod.Wg.Add(1)
240- // for {
241- // Response, ok := <-Status
242- // if !ok {
243- // break
244- // }
245- //
246- // switch Response.Code {
247- // case types.E_CONTAINER_FINISHED:
248- // ps, ok := Response.Data.(*types.ProcessFinished)
249- // if ok {
250- // mypod.SetOneContainerStatus(ps.Id, ps.Code)
251- // close(ps.Ack)
252- // }
253- // case types.E_EXEC_FINISHED:
254- // ps, ok := Response.Data.(*types.ProcessFinished)
255- // if ok {
256- // mypod.SetExecStatus(ps.Id, ps.Code)
257- // close(ps.Ack)
258- // }
259- // case types.E_VM_SHUTDOWN: // vm exited, sucessful or not
260- // if mypod.Status == types.S_POD_RUNNING { // not received finished pod before
261- // mypod.Status = types.S_POD_FAILED
262- // mypod.FinishedAt = time.Now().Format("2006-01-02T15:04:05Z")
263- // mypod.SetContainerStatus(types.S_POD_FAILED)
264- // }
265- // mypod.Vm = ""
266- // exit = true
267- // }
268- //
269- // if mypod.Handler != nil {
270- // mypod.Handler.Handle(Response, mypod.Handler.Data, mypod, vm)
271- // }
272- //
273- // if exit {
274- // vm.clients = nil
275- // break
276- // }
277- // }
278- // mypod.Wg.Done()
279- //}
280-
281229func (vm * Vm ) InitSandbox (config * api.SandboxConfig ) {
282230 vm .ctx .SetNetworkEnvironment (config )
283231 vm .ctx .startPod ()
@@ -299,7 +247,7 @@ func (vm *Vm) WaitInit() api.Result {
299247}
300248
301249func (vm * Vm ) Shutdown () api.Result {
302- if vm .ctx .current != StateRunning {
250+ if ! vm .ctx .IsRunning () {
303251 return api .NewResultBase (vm .Id , false , "not in running state" )
304252 }
305253
@@ -352,10 +300,6 @@ func (vm *Vm) AddRoute() error {
352300}
353301
354302func (vm * Vm ) AddNic (info * api.InterfaceDescription ) error {
355- if vm .ctx .current != StateRunning {
356- return NewNotReadyError (vm .Id )
357- }
358-
359303 client := make (chan api.Result , 1 )
360304 vm .ctx .AddInterface (info , client )
361305
@@ -375,10 +319,6 @@ func (vm *Vm) AddNic(info *api.InterfaceDescription) error {
375319}
376320
377321func (vm * Vm ) DeleteNic (id string ) error {
378- if vm .ctx .current != StateRunning {
379- return NewNotReadyError (vm .Id )
380- }
381-
382322 client := make (chan api.Result , 1 )
383323 vm .ctx .RemoveInterface (id , client )
384324
@@ -403,7 +343,7 @@ func (vm *Vm) SetCpus(cpus int) error {
403343 return nil
404344 }
405345
406- if vm .ctx .current != StateRunning {
346+ if ! vm .ctx .IsRunning () {
407347 return NewNotReadyError (vm .Id )
408348 }
409349
@@ -420,7 +360,7 @@ func (vm *Vm) AddMem(totalMem int) error {
420360 }
421361
422362 size := totalMem - vm .Mem
423- if vm .ctx .current != StateRunning {
363+ if ! vm .ctx .IsRunning () {
424364 return NewNotReadyError (vm .Id )
425365 }
426366
@@ -525,6 +465,10 @@ func (vm *Vm) Exec(container, execId, cmd string, terminal bool, tty *TtyIO) err
525465}
526466
527467func (vm * Vm ) AddProcess (container , execId string , terminal bool , args []string , env []string , workdir string , tty * TtyIO ) error {
468+ if ! vm .ctx .IsRunning () {
469+ return NewNotReadyError (vm .Id )
470+ }
471+
528472 envs := []hyperstartapi.EnvironmentVar {}
529473
530474 for _ , v := range env {
@@ -553,21 +497,12 @@ func (vm *Vm) AddProcess(container, execId string, terminal bool, args []string,
553497}
554498
555499func (vm * Vm ) AddVolume (vol * api.VolumeDescription ) api.Result {
556- if vm .ctx .current != StateRunning {
557- vm .Log (ERROR , "VM is not ready for insert volume %#v" , vol )
558- return NewNotReadyError (vm .Id )
559- }
560-
561500 result := make (chan api.Result , 1 )
562501 vm .ctx .AddVolume (vol , result )
563502 return <- result
564503}
565504
566505func (vm * Vm ) AddContainer (c * api.ContainerDescription ) api.Result {
567- if vm .ctx .current != StateRunning {
568- return NewNotReadyError (vm .Id )
569- }
570-
571506 result := make (chan api.Result , 1 )
572507 vm .ctx .AddContainer (c , result )
573508 return <- result
@@ -631,10 +566,6 @@ func (vm *Vm) batchWaitResult(names []string, op waitResultOp) (bool, map[string
631566}
632567
633568func (vm * Vm ) StartContainer (id string ) error {
634- if vm .ctx .current != StateRunning {
635- return NewNotReadyError (vm .Id )
636- }
637-
638569 err := vm .ctx .newContainer (id )
639570 if err != nil {
640571 return fmt .Errorf ("Create new container failed: %v" , err )
@@ -652,10 +583,6 @@ func (vm *Vm) Tty(containerId, execId string, row, column int) error {
652583}
653584
654585func (vm * Vm ) Attach (tty * TtyIO , container string ) error {
655- if vm .ctx .current != StateRunning {
656- return NewNotReadyError (vm .Id )
657- }
658-
659586 cmd := & AttachCommand {
660587 Streams : tty ,
661588 Container : container ,
@@ -666,7 +593,7 @@ func (vm *Vm) Attach(tty *TtyIO, container string) error {
666593func (vm * Vm ) Stats () * types.PodStats {
667594 ctx := vm .ctx
668595
669- if ctx .current != StateRunning {
596+ if ! vm . ctx .IsRunning () {
670597 vm .ctx .Log (WARNING , "could not get stats from non-running pod" )
671598 return nil
672599 }
@@ -681,7 +608,7 @@ func (vm *Vm) Stats() *types.PodStats {
681608
682609func (vm * Vm ) Pause (pause bool ) error {
683610 ctx := vm .ctx
684- if ctx .current != StateRunning {
611+ if ! vm . ctx .IsRunning () {
685612 return NewNotReadyError (vm .Id )
686613 }
687614
@@ -713,10 +640,13 @@ func (vm *Vm) Pause(pause bool) error {
713640
714641func (vm * Vm ) Save (path string ) error {
715642 ctx := vm .ctx
643+ if ! vm .ctx .IsRunning () {
644+ return NewNotReadyError (vm .Id )
645+ }
716646
717647 ctx .pauseLock .Lock ()
718648 defer ctx .pauseLock .Unlock ()
719- if ctx .current != StateRunning || ctx . PauseState != PauseStatePaused {
649+ if ctx .PauseState != PauseStatePaused {
720650 return NewNotReadyError (vm .Id )
721651 }
722652
@@ -726,7 +656,7 @@ func (vm *Vm) Save(path string) error {
726656func (vm * Vm ) GetIPAddrs () []string {
727657 ips := []string {}
728658
729- if vm .ctx .current != StateRunning {
659+ if ! vm .ctx .IsRunning () {
730660 vm .Log (ERROR , "get pod ip failed: %v" , NewNotReadyError (vm .Id ))
731661 return ips
732662 }
0 commit comments