Statistics
| Branch: | Tag: | Revision:

one / src / lcm / LifeCycleStates.cc @ aab0fe21

History | View | Annotate | Download (43.8 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2015, OpenNebula Project (OpenNebula.org), C12G Labs        */
3
/*                                                                            */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
5
/* not use this file except in compliance with the License. You may obtain    */
6
/* a copy of the License at                                                   */
7
/*                                                                            */
8
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
9
/*                                                                            */
10
/* Unless required by applicable law or agreed to in writing, software        */
11
/* distributed under the License is distributed on an "AS IS" BASIS,          */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
13
/* See the License for the specific language governing permissions and        */
14
/* limitations under the License.                                             */
15
/* -------------------------------------------------------------------------- */
16

    
17
#include "LifeCycleManager.h"
18
#include "TransferManager.h"
19
#include "DispatchManager.h"
20
#include "VirtualMachineManager.h"
21

    
22
/* -------------------------------------------------------------------------- */
23
/* -------------------------------------------------------------------------- */
24

    
25
void  LifeCycleManager::save_success_action(int vid)
26
{
27
    VirtualMachine *    vm;
28
    ostringstream       os;
29

    
30
    vm = vmpool->get(vid,true);
31

    
32
    if ( vm == 0 )
33
    {
34
        return;
35
    }
36

    
37
    if ( vm->get_lcm_state() == VirtualMachine::SAVE_MIGRATE )
38
    {
39
        int                 cpu,mem,disk;
40
        time_t              the_time = time(0);
41

    
42
        //----------------------------------------------------
43
        //                PROLOG_MIGRATE STATE
44
        //----------------------------------------------------
45

    
46
        vm->set_state(VirtualMachine::PROLOG_MIGRATE);
47

    
48
        vm->delete_snapshots();
49

    
50
        map<string, string> empty;
51
        vm->update_info(0, 0, -1, -1, empty);
52

    
53
        vmpool->update(vm);
54

    
55
        vm->set_previous_etime(the_time);
56

    
57
        vm->set_previous_vm_info();
58

    
59
        vm->set_previous_running_etime(the_time);
60

    
61
        vm->set_previous_reason(History::USER);
62

    
63
        vmpool->update_previous_history(vm);
64

    
65
        vm->set_prolog_stime(the_time);
66

    
67
        vmpool->update_history(vm);
68

    
69
        vm->get_requirements(cpu,mem,disk);
70

    
71
        hpool->del_capacity(vm->get_previous_hid(), vm->get_oid(), cpu, mem, disk);
72

    
73
        //----------------------------------------------------
74

    
75
        tm->trigger(TransferManager::PROLOG_MIGR,vid);
76
    }
77
    else if (vm->get_lcm_state() == VirtualMachine::SAVE_SUSPEND)
78
    {
79
        time_t              the_time = time(0);
80

    
81
        //----------------------------------------------------
82
        //                SUSPENDED STATE
83
        //----------------------------------------------------
84

    
85
        vm->delete_snapshots();
86

    
87
        map<string, string> empty;
88
        vm->update_info(0, 0, -1, -1, empty);
89

    
90
        vmpool->update(vm);
91

    
92
        vm->set_running_etime(the_time);
93

    
94
        vm->set_etime(the_time);
95

    
96
        vm->set_vm_info();
97

    
98
        vm->set_reason(History::USER);
99

    
100
        vmpool->update_history(vm);
101

    
102
        //----------------------------------------------------
103

    
104
        dm->trigger(DispatchManager::SUSPEND_SUCCESS,vid);
105
    }
106
    else if ( vm->get_lcm_state() == VirtualMachine::SAVE_STOP)
107
    {
108
        time_t              the_time = time(0);
109

    
110
        //----------------------------------------------------
111
        //                 EPILOG_STOP STATE
112
        //----------------------------------------------------
113

    
114
        vm->set_state(VirtualMachine::EPILOG_STOP);
115

    
116
        vm->delete_snapshots();
117

    
118
        map<string, string> empty;
119
        vm->update_info(0, 0, -1, -1, empty);
120

    
121
        vmpool->update(vm);
122

    
123
        vm->set_epilog_stime(the_time);
124

    
125
        vm->set_running_etime(the_time);
126

    
127
        vm->set_reason(History::USER);
128

    
129
        vmpool->update_history(vm);
130

    
131
        //----------------------------------------------------
132

    
133
        tm->trigger(TransferManager::EPILOG_STOP,vid);
134
    }
135
    else
136
    {
137
        vm->log("LCM",Log::ERROR,"save_success_action, VM in a wrong state");
138
    }
139

    
140
    vm->unlock();
141
}
142

    
143
/* -------------------------------------------------------------------------- */
144
/* -------------------------------------------------------------------------- */
145

    
146
void  LifeCycleManager::save_failure_action(int vid)
147
{
148
    VirtualMachine *    vm;
149

    
150
    vm = vmpool->get(vid,true);
151

    
152
    if ( vm == 0 )
153
    {
154
        return;
155
    }
156

    
157
    if ( vm->get_lcm_state() == VirtualMachine::SAVE_MIGRATE )
158
    {
159
        int                     cpu,mem,disk;
160
        time_t                  the_time = time(0);
161

    
162
        //----------------------------------------------------
163
        //           RUNNING STATE FROM SAVE_MIGRATE
164
        //----------------------------------------------------
165

    
166
        vm->set_state(VirtualMachine::RUNNING);
167

    
168
        vm->set_etime(the_time);
169

    
170
        vm->set_vm_info();
171

    
172
        vm->set_reason(History::ERROR);
173

    
174
        vmpool->update_history(vm);
175

    
176
        vm->get_requirements(cpu,mem,disk);
177

    
178
        hpool->del_capacity(vm->get_hid(), vm->get_oid(), cpu, mem, disk);
179

    
180
        vm->set_previous_etime(the_time);
181

    
182
        vm->set_previous_vm_info();
183

    
184
        vm->set_previous_running_etime(the_time);
185

    
186
        vm->set_previous_reason(History::USER);
187

    
188
        vmpool->update_previous_history(vm);
189

    
190
        // --- Add new record by copying the previous one
191

    
192
        vm->cp_previous_history();
193

    
194
        vmpool->update(vm); //update last_seq & state
195

    
196
        vm->set_stime(the_time);
197

    
198
        vm->set_running_stime(the_time);
199

    
200
        vm->set_last_poll(0);
201

    
202
        vmpool->update_history(vm);
203

    
204
        vm->log("LCM", Log::INFO, "Fail to save VM state while migrating."
205
                " Assuming that the VM is still RUNNING (will poll VM).");
206

    
207
        //----------------------------------------------------
208

    
209
        vmm->trigger(VirtualMachineManager::POLL,vid);
210
    }
211
    else if ( vm->get_lcm_state() == VirtualMachine::SAVE_SUSPEND ||
212
              vm->get_lcm_state() == VirtualMachine::SAVE_STOP )
213
    {
214
        //----------------------------------------------------
215
        //    RUNNING STATE FROM SAVE_SUSPEND OR SAVE_STOP
216
        //----------------------------------------------------
217

    
218
        vm->set_state(VirtualMachine::RUNNING);
219

    
220
        vmpool->update(vm);
221

    
222
        vm->set_action(History::NONE_ACTION);
223

    
224
        vmpool->update_history(vm);
225

    
226
        vm->log("LCM", Log::INFO, "Fail to save VM state."
227
                " Assuming that the VM is still RUNNING (will poll VM).");
228

    
229
        //----------------------------------------------------
230

    
231
        vmm->trigger(VirtualMachineManager::POLL,vid);
232
    }
233
    else
234
    {
235
        vm->log("LCM",Log::ERROR,"save_failure_action, VM in a wrong state");
236
    }
237

    
238
    vm->unlock();
239
}
240

    
241
/* -------------------------------------------------------------------------- */
242
/* -------------------------------------------------------------------------- */
243

    
244
void  LifeCycleManager::deploy_success_action(int vid)
245
{
246
    VirtualMachine *    vm;
247

    
248
    vm = vmpool->get(vid,true);
249

    
250
    if ( vm == 0 )
251
    {
252
        return;
253
    }
254

    
255
    //----------------------------------------------------
256
    //                 RUNNING STATE
257
    //----------------------------------------------------
258

    
259
    if ( vm->get_lcm_state() == VirtualMachine::MIGRATE )
260
    {
261
        int     cpu,mem,disk;
262
        time_t  the_time = time(0);
263

    
264
        vm->set_running_stime(the_time);
265

    
266
        vm->set_last_poll(0);
267

    
268
        vmpool->update_history(vm);
269

    
270
        vm->set_previous_etime(the_time);
271

    
272
        vm->set_previous_vm_info();
273

    
274
        vm->set_previous_running_etime(the_time);
275

    
276
        vm->set_previous_reason(History::USER);
277

    
278
        vmpool->update_previous_history(vm);
279

    
280
        vm->get_requirements(cpu,mem,disk);
281

    
282
        hpool->del_capacity(vm->get_previous_hid(), vm->get_oid(), cpu, mem, disk);
283

    
284
        vm->set_state(VirtualMachine::RUNNING);
285

    
286
        vm->delete_snapshots();
287

    
288
        vmpool->update(vm);
289
    }
290
    else if ( vm->get_lcm_state() == VirtualMachine::BOOT ||
291
              vm->get_lcm_state() == VirtualMachine::BOOT_POWEROFF ||
292
              vm->get_lcm_state() == VirtualMachine::BOOT_UNKNOWN  ||
293
              vm->get_lcm_state() == VirtualMachine::BOOT_SUSPENDED||
294
              vm->get_lcm_state() == VirtualMachine::BOOT_STOPPED ||
295
              vm->get_lcm_state() == VirtualMachine::BOOT_UNDEPLOY ||
296
              vm->get_lcm_state() == VirtualMachine::BOOT_MIGRATE ||
297
              vm->get_lcm_state() == VirtualMachine::BOOT_MIGRATE_FAILURE ||
298
              vm->get_lcm_state() == VirtualMachine::BOOT_FAILURE )
299
    {
300
        vm->set_state(VirtualMachine::RUNNING);
301

    
302
        vmpool->update(vm);
303
    }
304
    else
305
    {
306
        vm->log("LCM",Log::ERROR,"deploy_success_action, VM in a wrong state");
307
    }
308

    
309
    vm->unlock();
310
}
311

    
312
/* -------------------------------------------------------------------------- */
313
/* -------------------------------------------------------------------------- */
314

    
315
void  LifeCycleManager::deploy_failure_action(int vid)
316
{
317

    
318
    VirtualMachine *    vm;
319

    
320
    vm = vmpool->get(vid,true);
321

    
322
    if ( vm == 0 )
323
    {
324
        return;
325
    }
326

    
327
    time_t  the_time = time(0);
328

    
329
    if ( vm->get_lcm_state() == VirtualMachine::MIGRATE )
330
    {
331
        int     cpu,mem,disk;
332
        time_t  the_time = time(0);
333

    
334
        //----------------------------------------------------
335
        //           RUNNING STATE FROM MIGRATE
336
        //----------------------------------------------------
337

    
338
        vm->set_state(VirtualMachine::RUNNING);
339

    
340
        vmpool->update(vm);
341

    
342
        vm->set_etime(the_time);
343

    
344
        vm->set_vm_info();
345

    
346
        vm->set_reason(History::ERROR);
347

    
348
        vmpool->update_history(vm);
349

    
350
        vm->set_previous_etime(the_time);
351

    
352
        vm->set_previous_vm_info();
353

    
354
        vm->set_previous_running_etime(the_time);
355

    
356
        vm->set_previous_reason(History::USER);
357

    
358
        vmpool->update_previous_history(vm);
359

    
360
        vm->get_requirements(cpu,mem,disk);
361

    
362
        hpool->del_capacity(vm->get_hid(), vm->get_oid(), cpu, mem, disk);
363

    
364
        // --- Add new record by copying the previous one
365

    
366
        vm->cp_previous_history();
367

    
368
        vmpool->update(vm); //update last_seq & state
369

    
370
        vm->set_stime(the_time);
371

    
372
        vm->set_running_stime(the_time);
373

    
374
        vm->set_last_poll(0);
375

    
376
        vmpool->update_history(vm);
377

    
378
        vm->log("LCM", Log::INFO, "Fail to live migrate VM."
379
                " Assuming that the VM is still RUNNING (will poll VM).");
380

    
381
        //----------------------------------------------------
382

    
383
        vmm->trigger(VirtualMachineManager::POLL,vid);
384
    }
385
    else if (vm->get_lcm_state() == VirtualMachine::BOOT)
386
    {
387
        vm->set_state(VirtualMachine::BOOT_FAILURE);
388

    
389
        vmpool->update(vm);
390
    }
391
    else if (vm->get_lcm_state() == VirtualMachine::BOOT_MIGRATE)
392
    {
393
        vm->set_state(VirtualMachine::BOOT_MIGRATE_FAILURE);
394

    
395
        vmpool->update(vm);
396
    }
397
    else if (vm->get_lcm_state() == VirtualMachine::BOOT_UNKNOWN)
398
    {
399
        vm->set_state(VirtualMachine::UNKNOWN);
400

    
401
        vmpool->update(vm);
402
    }
403
    else if (vm->get_lcm_state() == VirtualMachine::BOOT_POWEROFF)
404
    {
405
        vm->set_etime(the_time);
406
        vm->set_running_etime(the_time);
407

    
408
        vm->set_reason(History::ERROR);
409
        vm->set_action(History::RESUME_ACTION);
410

    
411
        vm->set_state(VirtualMachine::POWEROFF);
412
        vm->set_state(VirtualMachine::LCM_INIT);
413

    
414
        vmpool->update(vm);
415
        vmpool->update_history(vm);
416
    }
417
    else if (vm->get_lcm_state() == VirtualMachine::BOOT_SUSPENDED)
418
    {
419
        vm->set_etime(the_time);
420
        vm->set_running_etime(the_time);
421

    
422
        vm->set_reason(History::ERROR);
423
        vm->set_action(History::RESUME_ACTION);
424

    
425
        vm->set_state(VirtualMachine::SUSPENDED);
426
        vm->set_state(VirtualMachine::LCM_INIT);
427

    
428
        vmpool->update(vm);
429
        vmpool->update_history(vm);
430
    }
431
    else if (vm->get_lcm_state() == VirtualMachine::BOOT_STOPPED)
432
    {
433
        time_t              the_time = time(0);
434

    
435
        //----------------------------------------------------
436
        //             EPILOG_STOP STATE FROM BOOT
437
        //----------------------------------------------------
438

    
439
        vm->set_state(VirtualMachine::EPILOG_STOP);
440

    
441
        vmpool->update(vm);
442

    
443
        vm->set_epilog_stime(the_time);
444

    
445
        vm->set_running_etime(the_time);
446

    
447
        vm->set_reason(History::ERROR);
448

    
449
        vmpool->update_history(vm);
450

    
451
        //----------------------------------------------------
452

    
453
        tm->trigger(TransferManager::EPILOG_STOP,vid);
454
    }
455
    else if (vm->get_lcm_state() == VirtualMachine::BOOT_UNDEPLOY)
456
    {
457
        time_t              the_time = time(0);
458

    
459
        //----------------------------------------------------
460
        //            EPILOG_UNDEPLOY STATE FROM BOOT
461
        //----------------------------------------------------
462

    
463
        vm->set_state(VirtualMachine::EPILOG_UNDEPLOY);
464

    
465
        vmpool->update(vm);
466

    
467
        vm->set_epilog_stime(the_time);
468

    
469
        vm->set_running_etime(the_time);
470

    
471
        vm->set_reason(History::ERROR);
472

    
473
        vmpool->update_history(vm);
474

    
475
        //----------------------------------------------------
476

    
477
        tm->trigger(TransferManager::EPILOG_STOP,vid);
478
    }
479
    //wrong state + recover failure from failure state
480
    else if ( vm->get_lcm_state() != VirtualMachine::BOOT_FAILURE &&
481
              vm->get_lcm_state() != VirtualMachine::BOOT_MIGRATE_FAILURE )
482
    {
483
        vm->log("LCM",Log::ERROR,"deploy_failure_action, VM in a wrong state");
484
    }
485

    
486
    vm->unlock();
487
}
488

    
489
/* -------------------------------------------------------------------------- */
490
/* -------------------------------------------------------------------------- */
491

    
492
void  LifeCycleManager::shutdown_success_action(int vid)
493
{
494
    VirtualMachine *    vm;
495
    time_t              the_time = time(0);
496

    
497
    vm = vmpool->get(vid,true);
498

    
499
    if ( vm == 0 )
500
    {
501
        return;
502
    }
503

    
504
    if ( vm->get_lcm_state() == VirtualMachine::SHUTDOWN )
505
    {
506
        //----------------------------------------------------
507
        //                   EPILOG STATE
508
        //----------------------------------------------------
509

    
510
        vm->set_state(VirtualMachine::EPILOG);
511

    
512
        vm->delete_snapshots();
513

    
514
        map<string, string> empty;
515
        vm->update_info(0, 0, -1, -1, empty);
516

    
517
        vmpool->update(vm);
518

    
519
        vm->set_epilog_stime(the_time);
520

    
521
        vm->set_running_etime(the_time);
522

    
523
        vm->set_reason(History::USER);
524

    
525
        vmpool->update_history(vm);
526

    
527
        //----------------------------------------------------
528

    
529
        tm->trigger(TransferManager::EPILOG,vid);
530
    }
531
    else if (vm->get_lcm_state() == VirtualMachine::SHUTDOWN_POWEROFF)
532
    {
533
        //----------------------------------------------------
534
        //                POWEROFF STATE
535
        //----------------------------------------------------
536

    
537
        vm->delete_snapshots();
538

    
539
        map<string, string> empty;
540
        vm->update_info(0, 0, -1, -1, empty);
541

    
542
        vmpool->update(vm);
543

    
544
        vm->set_running_etime(the_time);
545

    
546
        vm->set_etime(the_time);
547

    
548
        vm->set_vm_info();
549

    
550
        vm->set_reason(History::USER);
551

    
552
        vmpool->update_history(vm);
553

    
554
        //----------------------------------------------------
555

    
556
        dm->trigger(DispatchManager::POWEROFF_SUCCESS,vid);
557
    }
558
    else if (vm->get_lcm_state() == VirtualMachine::SHUTDOWN_UNDEPLOY)
559
    {
560
        //----------------------------------------------------
561
        //            EPILOG_UNDEPLOY STATE
562
        //----------------------------------------------------
563

    
564
        vm->set_state(VirtualMachine::EPILOG_UNDEPLOY);
565

    
566
        vm->delete_snapshots();
567

    
568
        map<string, string> empty;
569
        vm->update_info(0, 0, -1, -1, empty);
570

    
571
        vmpool->update(vm);
572

    
573
        vm->set_epilog_stime(the_time);
574

    
575
        vm->set_running_etime(the_time);
576

    
577
        vm->set_reason(History::USER);
578

    
579
        vmpool->update_history(vm);
580

    
581
        //----------------------------------------------------
582

    
583
        tm->trigger(TransferManager::EPILOG_STOP,vid);
584
    }
585
    else
586
    {
587
        vm->log("LCM",Log::ERROR,"shutdown_success_action, VM in a wrong state");
588
    }
589

    
590
    vm->unlock();
591
}
592

    
593
/* -------------------------------------------------------------------------- */
594
/* -------------------------------------------------------------------------- */
595

    
596
void  LifeCycleManager::shutdown_failure_action(int vid)
597
{
598
    VirtualMachine *        vm;
599

    
600
    vm = vmpool->get(vid,true);
601

    
602
    if ( vm == 0 )
603
    {
604
        return;
605
    }
606

    
607
    if ( vm->get_lcm_state() == VirtualMachine::SHUTDOWN ||
608
         vm->get_lcm_state() == VirtualMachine::SHUTDOWN_POWEROFF ||
609
         vm->get_lcm_state() == VirtualMachine::SHUTDOWN_UNDEPLOY )
610
    {
611
        //----------------------------------------------------
612
        //    RUNNING STATE FROM SHUTDOWN
613
        //----------------------------------------------------
614

    
615
        vm->set_state(VirtualMachine::RUNNING);
616

    
617
        vmpool->update(vm);
618

    
619
        vm->set_action(History::NONE_ACTION);
620

    
621
        vmpool->update_history(vm);
622

    
623
        vm->log("LCM", Log::INFO, "Fail to shutdown VM."
624
                " Assuming that the VM is still RUNNING (will poll VM).");
625

    
626
        //----------------------------------------------------
627

    
628
        vmm->trigger(VirtualMachineManager::POLL,vid);
629
    }
630
    else
631
    {
632
        vm->log("LCM",Log::ERROR,"shutdown_failure_action, VM in a wrong state");
633
    }
634

    
635
    vm->unlock();
636
}
637

    
638
/* -------------------------------------------------------------------------- */
639
/* -------------------------------------------------------------------------- */
640

    
641
void LifeCycleManager::prolog_success_action(int vid)
642
{
643
    VirtualMachine *        vm;
644
    time_t                  the_time = time(0);
645
    ostringstream           os;
646
    map<string, string>     empty;
647

    
648
    VirtualMachineManager::Actions action;
649

    
650
    vm = vmpool->get(vid, true);
651

    
652
    if ( vm == 0 )
653
    {
654
        return;
655
    }
656

    
657
        VirtualMachine::LcmState lcm_state = vm->get_lcm_state();
658

    
659
    switch (lcm_state)
660
    {
661
        //---------------------------------------------------------------------
662
        //                             BOOT STATE
663
        //---------------------------------------------------------------------
664
        case VirtualMachine::PROLOG_RESUME:
665
        case VirtualMachine::PROLOG_UNDEPLOY:
666
        case VirtualMachine::PROLOG_MIGRATE:
667
        case VirtualMachine::PROLOG_MIGRATE_FAILURE: //recover success
668
        case VirtualMachine::PROLOG:
669
        case VirtualMachine::PROLOG_FAILURE: //recover success
670
            switch (lcm_state)
671
            {
672
                case VirtualMachine::PROLOG_RESUME:
673
                    action = VirtualMachineManager::RESTORE;
674
                    vm->set_state(VirtualMachine::BOOT_STOPPED);
675
                    break;
676

    
677
                case VirtualMachine::PROLOG_UNDEPLOY:
678
                    action = VirtualMachineManager::DEPLOY;
679
                    vm->set_state(VirtualMachine::BOOT_UNDEPLOY);
680
                    break;
681

    
682
                case VirtualMachine::PROLOG_MIGRATE:
683
                case VirtualMachine::PROLOG_MIGRATE_FAILURE: //recover success
684
                    action = VirtualMachineManager::RESTORE;
685
                    vm->set_state(VirtualMachine::BOOT_MIGRATE);
686
                    break;
687

    
688
                case VirtualMachine::PROLOG:
689
                case VirtualMachine::PROLOG_FAILURE: //recover success
690
                    action = VirtualMachineManager::DEPLOY;
691
                    vm->set_state(VirtualMachine::BOOT);
692
                    break;
693

    
694
                default:
695
                    return;
696
            }
697

    
698
            vmpool->update(vm);
699

    
700
            vm->set_prolog_etime(the_time);
701

    
702
            vm->set_running_stime(the_time);
703

    
704
            vm->set_last_poll(0);
705

    
706
            vmpool->update_history(vm);
707

    
708
            vmm->trigger(action,vid);
709
            break;
710

    
711
        //---------------------------------------------------------------------
712
        //                      POWEROFF/SUSPEND STATE
713
        //---------------------------------------------------------------------
714
        case VirtualMachine::PROLOG_MIGRATE_POWEROFF:
715
        case VirtualMachine::PROLOG_MIGRATE_POWEROFF_FAILURE: //recover success
716
        case VirtualMachine::PROLOG_MIGRATE_SUSPEND:
717
        case VirtualMachine::PROLOG_MIGRATE_SUSPEND_FAILURE: //recover success
718
            vm->delete_snapshots();
719

    
720
            vm->update_info(0, 0, -1, -1, empty);
721

    
722
            vmpool->update(vm);
723

    
724
            vm->set_etime(the_time);
725

    
726
            vm->set_prolog_etime(the_time);
727

    
728
            vm->set_last_poll(0);
729

    
730
            vm->set_vm_info();
731

    
732
            vm->set_reason(History::USER);
733
            vm->set_action(History::MIGRATE_ACTION);
734

    
735
            vmpool->update_history(vm);
736

    
737
                        if (lcm_state == VirtualMachine::PROLOG_MIGRATE_POWEROFF||
738
                            lcm_state == VirtualMachine::PROLOG_MIGRATE_POWEROFF_FAILURE)
739
                        {
740
                                dm->trigger(DispatchManager::POWEROFF_SUCCESS,vid);
741
                        }
742
                        else //PROLOG_MIGRATE_SUSPEND, PROLOG_MIGRATE_SUSPEND_FAILURE
743
                        {
744
                                dm->trigger(DispatchManager::SUSPEND_SUCCESS,vid);
745
                        }
746
            break;
747

    
748
        default:
749
            vm->log("LCM",Log::ERROR,"prolog_success_action, VM in a wrong state");
750
            break;
751
    }
752

    
753
    vm->unlock();
754

    
755
    return;
756
}
757

    
758
/* -------------------------------------------------------------------------- */
759
/* -------------------------------------------------------------------------- */
760

    
761
void  LifeCycleManager::prolog_failure_action(int vid)
762
{
763
    VirtualMachine::LcmState    state;
764
    VirtualMachine *            vm;
765

    
766
    time_t  the_time = time(0);
767

    
768
    vm = vmpool->get(vid,true);
769

    
770
    if ( vm == 0 )
771
    {
772
        return;
773
    }
774

    
775
    state = vm->get_lcm_state();
776

    
777
    if ( state == VirtualMachine::PROLOG )
778
    {
779
        vm->set_state(VirtualMachine::PROLOG_FAILURE);
780
        vmpool->update(vm);
781
    }
782
    else if ( state == VirtualMachine::PROLOG_MIGRATE )
783
    {
784
        vm->set_state(VirtualMachine::PROLOG_MIGRATE_FAILURE);
785

    
786
        vmpool->update(vm);
787
    }
788
    else if ( state == VirtualMachine::PROLOG_MIGRATE_POWEROFF )
789
    {
790
        vm->set_state(VirtualMachine::PROLOG_MIGRATE_POWEROFF_FAILURE);
791
        vmpool->update(vm);
792
    }
793
    else if ( state == VirtualMachine::PROLOG_MIGRATE_SUSPEND )
794
    {
795
        vm->set_state(VirtualMachine::PROLOG_MIGRATE_SUSPEND_FAILURE);
796
        vmpool->update(vm);
797
    }
798
    else if ( state == VirtualMachine::PROLOG_RESUME )
799
    {
800
        //----------------------------------------------------
801
        //    STOPPED STATE FROM PROLOG_RESUME
802
        //----------------------------------------------------
803

    
804
        int                 cpu,mem,disk;
805

    
806
        vm->set_prolog_etime(the_time);
807

    
808
        vm->set_resched(false);
809

    
810
        vmpool->update(vm);
811

    
812
        vm->set_etime(the_time);
813

    
814
        vm->set_vm_info();
815

    
816
        vm->set_reason(History::ERROR);
817

    
818
        vmpool->update_history(vm);
819

    
820
        vm->get_requirements(cpu,mem,disk);
821

    
822
        hpool->del_capacity(vm->get_hid(), vm->get_oid(), cpu, mem, disk);
823

    
824
        //----------------------------------------------------
825

    
826
        dm->trigger(DispatchManager::STOP_SUCCESS,vid);
827
    }
828
    else if ( state == VirtualMachine::PROLOG_UNDEPLOY )
829
    {
830
        //----------------------------------------------------
831
        //    UNDEPLOY STATE FROM PROLOG_UNDEPLOY
832
        //----------------------------------------------------
833

    
834
        int                 cpu,mem,disk;
835

    
836
        vm->set_prolog_etime(the_time);
837

    
838
        vm->set_resched(false);
839

    
840
        vmpool->update(vm);
841

    
842
        vm->set_etime(the_time);
843

    
844
        vm->set_vm_info();
845

    
846
        vm->set_reason(History::ERROR);
847

    
848
        vmpool->update_history(vm);
849

    
850
        vm->get_requirements(cpu,mem,disk);
851

    
852
        hpool->del_capacity(vm->get_hid(), vm->get_oid(), cpu, mem, disk);
853

    
854
        //----------------------------------------------------
855

    
856
        dm->trigger(DispatchManager::UNDEPLOY_SUCCESS,vid);
857
    }
858
    //wrong state + recover failure from failure state
859
    else if ( state != VirtualMachine::PROLOG_MIGRATE_FAILURE &&
860
              state != VirtualMachine::PROLOG_MIGRATE_POWEROFF_FAILURE &&
861
              state != VirtualMachine::PROLOG_MIGRATE_SUSPEND_FAILURE &&
862
              state != VirtualMachine::PROLOG_FAILURE )
863
    {
864
        vm->log("LCM",Log::ERROR,"prolog_failure_action, VM in a wrong state");
865
    }
866

    
867
    vm->unlock();
868

    
869
    return;
870
}
871

    
872
/* -------------------------------------------------------------------------- */
873
/* -------------------------------------------------------------------------- */
874

    
875
void  LifeCycleManager::epilog_success_action(int vid)
876
{
877
    VirtualMachine *    vm;
878
    time_t              the_time = time(0);
879
    int                 cpu,mem,disk;
880

    
881
    VirtualMachine::LcmState state;
882
    DispatchManager::Actions action;
883

    
884
    vm = vmpool->get(vid,true);
885

    
886
    if ( vm == 0 )
887
    {
888
        return;
889
    }
890

    
891
    state = vm->get_lcm_state();
892

    
893
        //Recover failure epilog states with success
894
    if ( state == VirtualMachine::EPILOG_STOP_FAILURE )
895
    {
896
        vm->set_state(VirtualMachine::EPILOG_STOP);
897
        vmpool->update(vm);
898
    }
899
    else if ( state == VirtualMachine::EPILOG_UNDEPLOY_FAILURE )
900
    {
901
        vm->set_state(VirtualMachine::EPILOG_UNDEPLOY);
902
        vmpool->update(vm);
903
    }
904
    else if ( state == VirtualMachine::EPILOG_FAILURE )
905
    {
906
        vm->set_state(VirtualMachine::EPILOG);
907
        vmpool->update(vm);
908
    }
909

    
910
    state = vm->get_lcm_state();
911

    
912
    if ( state == VirtualMachine::EPILOG_STOP )
913
    {
914
        action = DispatchManager::STOP_SUCCESS;
915
    }
916
    else if ( state == VirtualMachine::EPILOG_UNDEPLOY )
917
    {
918
        action = DispatchManager::UNDEPLOY_SUCCESS;
919
    }
920
    else if ( state == VirtualMachine::EPILOG )
921
    {
922
        action = DispatchManager::DONE;
923
    }
924
    else if ( state == VirtualMachine::CLEANUP_RESUBMIT )
925
    {
926
        dm->trigger(DispatchManager::RESUBMIT, vid);
927

    
928
        vm->unlock();
929

    
930
        return;
931
    }
932
    else
933
    {
934
        vm->log("LCM",Log::ERROR,"epilog_success_action, VM in a wrong state");
935
        vm->unlock();
936

    
937
        return;
938
    }
939

    
940
    vm->set_epilog_etime(the_time);
941

    
942
    vm->set_etime(the_time);
943

    
944
    vm->set_vm_info();
945

    
946
    vmpool->update_history(vm);
947

    
948
    vm->get_requirements(cpu,mem,disk);
949

    
950
    hpool->del_capacity(vm->get_hid(), vm->get_oid(), cpu, mem, disk);
951

    
952
    //----------------------------------------------------
953

    
954
    dm->trigger(action,vid);
955

    
956
    vm->unlock();
957

    
958
    return;
959
}
960

    
961
/* -------------------------------------------------------------------------- */
962
/* -------------------------------------------------------------------------- */
963

    
964
void  LifeCycleManager::cleanup_callback_action(int vid)
965
{
966
    VirtualMachine *    vm;
967

    
968
    VirtualMachine::LcmState state;
969

    
970
    vm = vmpool->get(vid,true);
971

    
972
    if ( vm == 0 )
973
    {
974
        return;
975
    }
976

    
977
    state = vm->get_lcm_state();
978

    
979
    if ( state == VirtualMachine::CLEANUP_RESUBMIT )
980
    {
981
        dm->trigger(DispatchManager::RESUBMIT, vid);
982

    
983
    }
984
    else
985
    {
986
        vm->log("LCM",Log::ERROR,"cleanup_callback_action, VM in a wrong state");
987
    }
988

    
989
    vm->unlock();
990

    
991
    return;
992
}
993

    
994
/* -------------------------------------------------------------------------- */
995
/* -------------------------------------------------------------------------- */
996

    
997
void  LifeCycleManager::epilog_failure_action(int vid)
998
{
999
    VirtualMachine * vm;
1000

    
1001
    VirtualMachine::LcmState state;
1002

    
1003
    vm = vmpool->get(vid,true);
1004

    
1005
    if ( vm == 0 )
1006
    {
1007
        return;
1008
    }
1009

    
1010
    state = vm->get_lcm_state();
1011

    
1012
    if ( state == VirtualMachine::CLEANUP_RESUBMIT )
1013
    {
1014
        dm->trigger(DispatchManager::RESUBMIT, vid);
1015
    }
1016
    else if ( state == VirtualMachine::EPILOG )
1017
    {
1018
        vm->set_state(VirtualMachine::EPILOG_FAILURE);
1019
        vmpool->update(vm);
1020
    }
1021
    else if ( state == VirtualMachine::EPILOG_STOP )
1022
    {
1023
        vm->set_state(VirtualMachine::EPILOG_STOP_FAILURE);
1024
        vmpool->update(vm);
1025
    }
1026
    else if ( state == VirtualMachine::EPILOG_UNDEPLOY )
1027
    {
1028
        vm->set_state(VirtualMachine::EPILOG_UNDEPLOY_FAILURE);
1029
        vmpool->update(vm);
1030
    }
1031
    //wrong state + recover failure from failure state
1032
    else if ( state != VirtualMachine::EPILOG_FAILURE &&
1033
              state != VirtualMachine::EPILOG_UNDEPLOY_FAILURE &&
1034
              state != VirtualMachine::EPILOG_STOP_FAILURE )
1035
    {
1036
        vm->log("LCM",Log::ERROR,"epilog_failure_action, VM in a wrong state");
1037
    }
1038

    
1039
    vm->unlock();
1040

    
1041
    return;
1042
}
1043

    
1044
/* -------------------------------------------------------------------------- */
1045
/* -------------------------------------------------------------------------- */
1046

    
1047
void  LifeCycleManager::monitor_suspend_action(int vid)
1048
{
1049
    VirtualMachine *    vm;
1050

    
1051
    time_t  the_time = time(0);
1052

    
1053
    vm = vmpool->get(vid,true);
1054

    
1055
    if ( vm == 0 )
1056
    {
1057
        return;
1058
    }
1059

    
1060
    if ( vm->get_lcm_state() == VirtualMachine::RUNNING ||
1061
         vm->get_lcm_state() == VirtualMachine::UNKNOWN )
1062
    {
1063
        //----------------------------------------------------
1064
        //                  SAVE_SUSPEND STATE
1065
        //----------------------------------------------------
1066

    
1067
        vm->log("LCM", Log::INFO, "Polling reports that the VM is suspended.");
1068

    
1069
        vm->set_state(VirtualMachine::SAVE_SUSPEND);
1070

    
1071
        vm->set_resched(false);
1072

    
1073
        vm->delete_snapshots();
1074

    
1075
        map<string, string> empty;
1076
        vm->update_info(0, 0, -1, -1, empty);
1077

    
1078
        vmpool->update(vm);
1079

    
1080
        vm->set_running_etime(the_time);
1081

    
1082
        vm->set_etime(the_time);
1083

    
1084
        vm->set_vm_info();
1085

    
1086
        vm->set_reason(History::ERROR);
1087

    
1088
        vmpool->update_history(vm);
1089

    
1090
        //----------------------------------------------------
1091

    
1092
        dm->trigger(DispatchManager::SUSPEND_SUCCESS,vid);
1093
    }
1094
    else
1095
    {
1096
        vm->log("LCM",Log::ERROR,"monitor_suspend_action, VM in a wrong state");
1097
    }
1098

    
1099
    vm->unlock();
1100
}
1101

    
1102
/* -------------------------------------------------------------------------- */
1103
/* -------------------------------------------------------------------------- */
1104

    
1105
void  LifeCycleManager::monitor_done_action(int vid)
1106
{
1107
    VirtualMachine *    vm;
1108

    
1109
    vm = vmpool->get(vid,true);
1110

    
1111
    if ( vm == 0 )
1112
    {
1113
        return;
1114
    }
1115

    
1116
    if ( vm->get_lcm_state() == VirtualMachine::RUNNING )
1117
    {
1118
        //----------------------------------------------------
1119
        //                   UNKNWON STATE
1120
        //----------------------------------------------------
1121

    
1122
        vm->set_state(VirtualMachine::UNKNOWN);
1123

    
1124
        vm->set_resched(false);
1125

    
1126
        vmpool->update(vm);
1127
    }
1128
    // This event can be received when the VM is in PROLOG, BOOT...
1129
    // and other transient states (through host monitor probe).
1130
    // Just ignore the callback if VM is not in RUNNING.
1131

    
1132
    vm->unlock();
1133
}
1134

    
1135
/* -------------------------------------------------------------------------- */
1136
/* -------------------------------------------------------------------------- */
1137

    
1138
void  LifeCycleManager::monitor_poweroff_action(int vid)
1139
{
1140
    VirtualMachine *    vm;
1141

    
1142
    vm = vmpool->get(vid,true);
1143

    
1144
    if ( vm == 0 )
1145
    {
1146
        return;
1147
    }
1148

    
1149
    //This event should be ignored if the VM is not RUNNING
1150
    if ( vm->get_lcm_state() == VirtualMachine::RUNNING )
1151
    {
1152
        //----------------------------------------------------
1153
        //                POWEROFF STATE
1154
        //----------------------------------------------------
1155
        map<string, string> empty;
1156
        time_t              the_time = time(0);
1157

    
1158
        vm->delete_snapshots();
1159

    
1160
        vm->update_info(0, 0, -1, -1, empty);
1161

    
1162
        vm->set_resched(false);
1163

    
1164
        vm->set_state(VirtualMachine::SHUTDOWN_POWEROFF);
1165

    
1166
        vmpool->update(vm);
1167

    
1168
        vm->set_running_etime(the_time);
1169

    
1170
        vm->set_etime(the_time);
1171

    
1172
        vm->set_vm_info();
1173

    
1174
        vm->set_reason(History::USER);
1175

    
1176
        vmpool->update_history(vm);
1177

    
1178
        //----------------------------------------------------
1179

    
1180
        dm->trigger(DispatchManager::POWEROFF_SUCCESS,vid);
1181
    }
1182

    
1183
    vm->unlock();
1184
}
1185

    
1186
/* -------------------------------------------------------------------------- */
1187
/* -------------------------------------------------------------------------- */
1188

    
1189
void  LifeCycleManager::monitor_poweron_action(int vid)
1190
{
1191
    VirtualMachine *    vm;
1192

    
1193
    vm = vmpool->get(vid,true);
1194

    
1195
    if ( vm == 0 )
1196
    {
1197
        return;
1198
    }
1199

    
1200
    //This event should be ignored if the VM is not POWEROFF
1201
    if ( vm->get_state() == VirtualMachine::POWEROFF )
1202
    {
1203
            time_t the_time = time(0);
1204

    
1205
            vm->set_state(VirtualMachine::ACTIVE);
1206

    
1207
            vm->set_state(VirtualMachine::RUNNING);
1208

    
1209
            vm->cp_history();
1210

    
1211
            vmpool->update(vm);
1212

    
1213
            vm->set_stime(the_time);
1214

    
1215
            vm->set_running_stime(the_time);
1216

    
1217
            vm->set_last_poll(the_time);
1218

    
1219
            vmpool->update_history(vm);
1220
    }
1221

    
1222
    vm->unlock();
1223
}
1224

    
1225
/* -------------------------------------------------------------------------- */
1226
/* -------------------------------------------------------------------------- */
1227

    
1228
void LifeCycleManager::attach_success_action(int vid)
1229
{
1230
    VirtualMachine *    vm;
1231

    
1232
    vm = vmpool->get(vid,true);
1233

    
1234
    if ( vm == 0 )
1235
    {
1236
        return;
1237
    }
1238

    
1239
    if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG )
1240
    {
1241
        vm->clear_attach_disk();
1242

    
1243
        vm->set_state(VirtualMachine::RUNNING);
1244

    
1245
        vmpool->update(vm);
1246
    }
1247
    else if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG_PROLOG_POWEROFF )
1248
    {
1249
        vm->log("LCM", Log::INFO, "VM Disk successfully attached.");
1250

    
1251
        vm->clear_attach_disk();
1252
        vmpool->update(vm);
1253

    
1254
        dm->trigger(DispatchManager::POWEROFF_SUCCESS,vid);
1255
    }
1256
    else
1257
    {
1258
        vm->log("LCM",Log::ERROR,"attach_success_action, VM in a wrong state");
1259
    }
1260

    
1261
    vm->unlock();
1262
}
1263

    
1264
/* -------------------------------------------------------------------------- */
1265
/* -------------------------------------------------------------------------- */
1266

    
1267
void LifeCycleManager::attach_failure_action(int vid)
1268
{
1269
    VirtualMachine *  vm;
1270

    
1271
    vm = vmpool->get(vid,true);
1272

    
1273
    if ( vm == 0 )
1274
    {
1275
        return;
1276
    }
1277

    
1278
    if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG ||
1279
         vm->get_lcm_state() == VirtualMachine::HOTPLUG_PROLOG_POWEROFF )
1280
    {
1281
        vm->unlock();
1282

    
1283
        vmpool->delete_attach_disk(vid, false);
1284

    
1285
        vm = vmpool->get(vid,true);
1286

    
1287
        if ( vm == 0 )
1288
        {
1289
            return;
1290
        }
1291

    
1292
        if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG )
1293
        {
1294
            vm->set_state(VirtualMachine::RUNNING);
1295
        }
1296
        else
1297
        {
1298
            vm->log("LCM", Log::INFO, "VM Disk attach failure.");
1299

    
1300
            dm->trigger(DispatchManager::POWEROFF_SUCCESS,vid);
1301
        }
1302

    
1303
        vmpool->update(vm);
1304

    
1305
        vm->unlock();
1306
    }
1307
    else
1308
    {
1309
        vm->log("LCM",Log::ERROR,"attach_failure_action, VM in a wrong state");
1310
        vm->unlock();
1311
    }
1312
}
1313

    
1314
/* -------------------------------------------------------------------------- */
1315
/* -------------------------------------------------------------------------- */
1316

    
1317
void LifeCycleManager::detach_success_action(int vid)
1318
{
1319
    VirtualMachine *  vm;
1320

    
1321
    vm = vmpool->get(vid,true);
1322

    
1323
    if ( vm == 0 )
1324
    {
1325
        return;
1326
    }
1327

    
1328
    if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG ||
1329
         vm->get_lcm_state() == VirtualMachine::HOTPLUG_EPILOG_POWEROFF )
1330
    {
1331
        vm->unlock();
1332

    
1333
        vmpool->delete_attach_disk(vid, true);
1334

    
1335
        vm = vmpool->get(vid,true);
1336

    
1337
        if ( vm == 0 )
1338
        {
1339
            return;
1340
        }
1341

    
1342
        if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG )
1343
        {
1344
            vm->set_state(VirtualMachine::RUNNING);
1345
        }
1346
        else
1347
        {
1348
            vm->log("LCM", Log::INFO, "VM Disk successfully detached.");
1349

    
1350
            dm->trigger(DispatchManager::POWEROFF_SUCCESS,vid);
1351
        }
1352

    
1353
        vmpool->update(vm);
1354

    
1355
        vm->unlock();
1356
    }
1357
    else
1358
    {
1359
        vm->log("LCM",Log::ERROR,"detach_success_action, VM in a wrong state");
1360
        vm->unlock();
1361
    }
1362
}
1363

    
1364
/* -------------------------------------------------------------------------- */
1365
/* -------------------------------------------------------------------------- */
1366

    
1367
void LifeCycleManager::detach_failure_action(int vid)
1368
{
1369
    VirtualMachine *    vm;
1370

    
1371
    vm = vmpool->get(vid,true);
1372

    
1373
    if ( vm == 0 )
1374
    {
1375
        return;
1376
    }
1377

    
1378
    if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG )
1379
    {
1380
        vm->clear_attach_disk();
1381

    
1382
        vm->set_state(VirtualMachine::RUNNING);
1383

    
1384
        vmpool->update(vm);
1385
    }
1386
    else if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG_EPILOG_POWEROFF )
1387
    {
1388
        vm->log("LCM", Log::INFO, "VM Disk detach failure.");
1389

    
1390
        vm->clear_attach_disk();
1391
        vmpool->update(vm);
1392

    
1393
        dm->trigger(DispatchManager::POWEROFF_SUCCESS,vid);
1394
    }
1395
    else
1396
    {
1397
        vm->log("LCM",Log::ERROR,"detach_failure_action, VM in a wrong state");
1398
    }
1399

    
1400
    vm->unlock();
1401
}
1402

    
1403
/* -------------------------------------------------------------------------- */
1404
/* -------------------------------------------------------------------------- */
1405

    
1406
void LifeCycleManager::snapshot_create_success(int vid)
1407
{
1408
    VirtualMachine *    vm;
1409

    
1410
    vm = vmpool->get(vid,true);
1411

    
1412
    if ( vm == 0 )
1413
    {
1414
        return;
1415
    }
1416

    
1417
    if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG_SNAPSHOT )
1418
    {
1419
        vm->clear_active_snapshot();
1420

    
1421
        vm->set_state(VirtualMachine::RUNNING);
1422

    
1423
        vmpool->update(vm);
1424
    }
1425
    else
1426
    {
1427
        vm->log("LCM",Log::ERROR,"snapshot_create_success, VM in a wrong state");
1428
    }
1429

    
1430
    vm->unlock();
1431
}
1432

    
1433
/* -------------------------------------------------------------------------- */
1434
/* -------------------------------------------------------------------------- */
1435

    
1436
void LifeCycleManager::snapshot_create_failure(int vid)
1437
{
1438
    VirtualMachine *  vm;
1439

    
1440
    vm = vmpool->get(vid,true);
1441

    
1442
    if ( vm == 0 )
1443
    {
1444
        return;
1445
    }
1446

    
1447
    if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG_SNAPSHOT )
1448
    {
1449
        vm->delete_active_snapshot();
1450

    
1451
        vm->set_state(VirtualMachine::RUNNING);
1452

    
1453
        vmpool->update(vm);
1454
    }
1455
    else
1456
    {
1457
        vm->log("LCM",Log::ERROR,"snapshot_create_failure, VM in a wrong state");
1458
    }
1459

    
1460
    vm->unlock();
1461
}
1462

    
1463
/* -------------------------------------------------------------------------- */
1464
/* -------------------------------------------------------------------------- */
1465

    
1466
void LifeCycleManager::snapshot_revert_success(int vid)
1467
{
1468
    // TODO: snapshot list may be inconsistent with hypervisor info
1469
    // after a revert operation
1470

    
1471
    VirtualMachine *  vm;
1472

    
1473
    vm = vmpool->get(vid,true);
1474

    
1475
    if ( vm == 0 )
1476
    {
1477
        return;
1478
    }
1479

    
1480
    if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG_SNAPSHOT )
1481
    {
1482
        vm->clear_active_snapshot();
1483

    
1484
        vm->set_state(VirtualMachine::RUNNING);
1485

    
1486
        vmpool->update(vm);
1487
    }
1488
    else
1489
    {
1490
        vm->log("LCM",Log::ERROR,"snapshot_revert_success, VM in a wrong state");
1491
    }
1492

    
1493
    vm->unlock();
1494
}
1495

    
1496
/* -------------------------------------------------------------------------- */
1497
/* -------------------------------------------------------------------------- */
1498

    
1499
void LifeCycleManager::snapshot_revert_failure(int vid)
1500
{
1501
    // TODO: for now, it is the same code
1502

    
1503
    snapshot_revert_success(vid);
1504
}
1505

    
1506
/* -------------------------------------------------------------------------- */
1507
/* -------------------------------------------------------------------------- */
1508

    
1509
void LifeCycleManager::snapshot_delete_success(int vid)
1510
{
1511
    VirtualMachine *  vm;
1512

    
1513
    vm = vmpool->get(vid,true);
1514

    
1515
    if ( vm == 0 )
1516
    {
1517
        return;
1518
    }
1519

    
1520
    if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG_SNAPSHOT )
1521
    {
1522
        vm->delete_active_snapshot();
1523

    
1524
        vm->set_state(VirtualMachine::RUNNING);
1525

    
1526
        vmpool->update(vm);
1527
    }
1528
    else
1529
    {
1530
        vm->log("LCM",Log::ERROR,"snapshot_delete_success, VM in a wrong state");
1531
    }
1532

    
1533
    vm->unlock();
1534
}
1535

    
1536
/* -------------------------------------------------------------------------- */
1537
/* -------------------------------------------------------------------------- */
1538

    
1539
void LifeCycleManager::snapshot_delete_failure(int vid)
1540
{
1541
    VirtualMachine *  vm;
1542

    
1543
    vm = vmpool->get(vid,true);
1544

    
1545
    if ( vm == 0 )
1546
    {
1547
        return;
1548
    }
1549

    
1550
    if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG_SNAPSHOT )
1551
    {
1552
        vm->clear_active_snapshot();
1553

    
1554
        vm->set_state(VirtualMachine::RUNNING);
1555

    
1556
        vmpool->update(vm);
1557
    }
1558
    else
1559
    {
1560
        vm->log("LCM",Log::ERROR,"snapshot_delete_failure, VM in a wrong state");
1561
    }
1562

    
1563
    vm->unlock();
1564
}
1565

    
1566
/* -------------------------------------------------------------------------- */
1567
/* -------------------------------------------------------------------------- */
1568

    
1569
void LifeCycleManager::attach_nic_success_action(int vid)
1570
{
1571
    VirtualMachine * vm;
1572

    
1573
    vm = vmpool->get(vid,true);
1574

    
1575
    if ( vm == 0 )
1576
    {
1577
        return;
1578
    }
1579

    
1580
    if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG_NIC )
1581
    {
1582
        vm->clear_attach_nic();
1583

    
1584
        vm->set_state(VirtualMachine::RUNNING);
1585

    
1586
        vmpool->update(vm);
1587
    }
1588
    else
1589
    {
1590
        vm->log("LCM",Log::ERROR,"attach_nic_success_action, VM in a wrong state");
1591
    }
1592

    
1593
    vm->unlock();
1594
}
1595

    
1596
/* -------------------------------------------------------------------------- */
1597
/* -------------------------------------------------------------------------- */
1598

    
1599
void LifeCycleManager::attach_nic_failure_action(int vid)
1600
{
1601
    VirtualMachine *  vm;
1602

    
1603
    vm = vmpool->get(vid,true);
1604

    
1605
    if ( vm == 0 )
1606
    {
1607
        return;
1608
    }
1609

    
1610
    if ( vm->get_lcm_state() == VirtualMachine::HOTPLUG_NIC )
1611
    {
1612
        vm->unlock();
1613

    
1614
        vmpool->delete_attach_nic(vid);
1615

    
1616
        vm = vmpool->get(vid,true);
1617

    
1618
        if ( vm == 0 )
1619
        {
1620
            return;
1621
        }
1622

    
1623
        vm->set_state(VirtualMachine::RUNNING);
1624

    
1625
        vmpool->update(vm);
1626

    
1627
        vm->unlock();
1628
    }
1629
    else
1630
    {
1631
        vm->log("LCM",Log::ERROR,"attach_nic_failure_action, VM in a wrong state");
1632
        vm->unlock();
1633
    }
1634
}
1635

    
1636
/* -------------------------------------------------------------------------- */
1637
/* -------------------------------------------------------------------------- */
1638

    
1639
void LifeCycleManager::detach_nic_success_action(int vid)
1640
{
1641
    attach_nic_failure_action(vid);
1642
}
1643

    
1644
/* -------------------------------------------------------------------------- */
1645
/* -------------------------------------------------------------------------- */
1646

    
1647
void LifeCycleManager::detach_nic_failure_action(int vid)
1648
{
1649
    attach_nic_success_action(vid);
1650
}
1651

    
1652
/* -------------------------------------------------------------------------- */
1653
/* -------------------------------------------------------------------------- */
1654

    
1655
void LifeCycleManager::saveas_hot_success_action(int vid)
1656
{
1657
    VirtualMachine * vm;
1658
    Image * image;
1659

    
1660
    int image_id;
1661
    int disk_id;
1662
    string source;
1663

    
1664
    vm = vmpool->get(vid,true);
1665

    
1666
    if ( vm == 0 )
1667
    {
1668
        return;
1669
    }
1670

    
1671
    int rc = vm->get_saveas_disk_hot(disk_id, source, image_id);
1672

    
1673
    if (vm->clear_saveas_state(disk_id, true) == -1)
1674
    {
1675
        vm->log("LCM", Log::ERROR, "saveas_hot_success_action, VM in a wrong state");
1676
        vm->unlock();
1677

    
1678
        return;
1679
    }
1680

    
1681
    vmpool->update(vm);
1682

    
1683
    vm->unlock();
1684

    
1685
    if ( rc != 0 )
1686
    {
1687
        return;
1688
    }
1689

    
1690
    image = ipool->get(image_id, true);
1691

    
1692
    if ( image == 0 )
1693
    {
1694
        return;
1695
    }
1696

    
1697
    image->set_state(Image::READY);
1698

    
1699
    ipool->update(image);
1700

    
1701
    image->unlock();
1702
}
1703

    
1704
/* -------------------------------------------------------------------------- */
1705
/* -------------------------------------------------------------------------- */
1706

    
1707
void LifeCycleManager::saveas_hot_failure_action(int vid)
1708
{
1709
    VirtualMachine * vm;
1710
    Image * image;
1711

    
1712
    int image_id;
1713
    int disk_id;
1714
    string source;
1715

    
1716
    vm = vmpool->get(vid,true);
1717

    
1718
    if ( vm == 0 )
1719
    {
1720
        return;
1721
    }
1722

    
1723
    int rc = vm->get_saveas_disk_hot(disk_id, source, image_id);
1724

    
1725
    if (vm->clear_saveas_state(disk_id, true) == -1)
1726
    {
1727
        vm->log("LCM", Log::ERROR, "saveas_hot_success_action, VM in a wrong state");
1728
        vm->unlock();
1729

    
1730
        return;
1731
    }
1732

    
1733
    vmpool->update(vm);
1734

    
1735
    vm->unlock();
1736

    
1737
    if ( rc != 0 )
1738
    {
1739
        return;
1740
    }
1741

    
1742
    image = ipool->get(image_id, true);
1743

    
1744
    if ( image == 0 )
1745
    {
1746
        return;
1747
    }
1748

    
1749
    image->set_state(Image::ERROR);
1750

    
1751
    ipool->update(image);
1752

    
1753
    image->unlock();
1754
}
1755

    
1756
/* -------------------------------------------------------------------------- */
1757
/* -------------------------------------------------------------------------- */