@@ -183,6 +183,33 @@ def vmccleanup(self, obj_attr_list) :
183183 _msg += " were terminated"
184184 cbdebug (_msg )
185185
186+ _running_volumes = True
187+ while _running_volumes :
188+ _running_volumes = False
189+ for credential_pair in obj_attr_list ["credentials" ].split ("," ):
190+ credentials = credential_pair .split (":" )
191+ tenant = credentials [0 ]
192+
193+ _volumes = catalogs .digitalocean [credential_pair ].list_volumes ()
194+ for _volume in _volumes :
195+ if _volume .name .count ("cb-" + obj_attr_list ["username" ]) :
196+ try :
197+ cbdebug ("Destroying: " + _volume .name + " (" + tenant + ")" , True )
198+ _volume .destroy ()
199+ except :
200+ pass
201+ _running_volumes = True
202+ else :
203+ _msg = "Cleaning up DigitalOcean. Ignoring volume: " + _volume .name
204+ cbdebug (_msg )
205+
206+ if _running_volumes :
207+ sleep (int (obj_attr_list ["update_frequency" ]))
208+
209+ _msg = "All volumes on DigitalOcean " + obj_attr_list ["name" ]
210+ _msg += " were destroyed"
211+ cbdebug (_msg )
212+
186213 _status = 0
187214
188215 except CldOpsException , obj :
@@ -411,15 +438,13 @@ def is_vm_ready(self, obj_attr_list) :
411438
412439 @trace
413440 def vmcreate (self , obj_attr_list ) :
414- '''
415- TBD
416- '''
417- try :
418- _status = 100
419- _fmsg = "An error has occurred when creating new Droplet, but no error message was captured"
441+ _status = 100
442+ _fmsg = "An error has occurred when creating new Droplet, but no error message was captured"
443+ obj_attr_list ["cloud_vm_uuid" ] = "NA"
444+ _instance = False
445+ volume = False
420446
421- obj_attr_list ["cloud_vm_uuid" ] = "NA"
422- _instance = False
447+ try :
423448
424449 obj_attr_list ["cloud_vm_name" ] = "cb-" + obj_attr_list ["username" ]
425450 obj_attr_list ["cloud_vm_name" ] += '-' + "vm" + obj_attr_list ["name" ].split ("_" )[1 ]
@@ -529,14 +554,72 @@ def vmcreate(self, obj_attr_list) :
529554 del obj_attr_list ["instance_obj" ]
530555
531556 _status = 0
532-
533557 else :
534558 obj_attr_list ["last_known_state" ] = "vm creation failed"
535559 _fmsg = "Failed to obtain instance's (cloud-assigned) uuid. The "
536560 _fmsg += "instance creation failed for some unknown reason."
537561 cberr (_fmsg )
538562 _status = 100
539563
564+
565+ if "cloud_vv" in obj_attr_list :
566+ _status = 101
567+
568+ obj_attr_list ["last_known_state" ] = "about to send volume create request"
569+
570+ obj_attr_list ["cloud_vv_name" ] = "cb-" + obj_attr_list ["username" ]
571+ obj_attr_list ["cloud_vv_name" ] += '-' + "vv"
572+ obj_attr_list ["cloud_vv_name" ] += obj_attr_list ["name" ].split ("_" )[1 ]
573+ obj_attr_list ["cloud_vv_name" ] += '-' + obj_attr_list ["role" ]
574+ if obj_attr_list ["ai" ] != "none" :
575+ obj_attr_list ["cloud_vv_name" ] += '-' + obj_attr_list ["ai_name" ]
576+
577+ obj_attr_list ["cloud_vv_name" ] = obj_attr_list ["cloud_vv_name" ].replace ("_" , "-" )
578+
579+ _msg = "Creating a volume, with size "
580+ _msg += obj_attr_list ["cloud_vv" ] + " GB, on VMC \" "
581+ _msg += obj_attr_list ["vmc_name" ] + "\" with name " + obj_attr_list ["cloud_vv_name" ] + "..."
582+ cbdebug (_msg , True )
583+
584+ _mark1 = int (time ())
585+
586+ volume = catalogs .digitalocean [credential_pair ].create_volume (int (obj_attr_list ["cloud_vv" ]),
587+ obj_attr_list ["cloud_vv_name" ],
588+ location = [x for x in self .locations if x .id == obj_attr_list ["vmc_name" ]][0 ])
589+
590+ sleep (int (obj_attr_list ["update_frequency" ]))
591+
592+ obj_attr_list ["cloud_vv_uuid" ] = volume .id
593+
594+ _mark2 = int (time ())
595+ obj_attr_list ["do_015_create_volume_time" ] = _mark2 - _mark1
596+
597+ if volume :
598+ _mark3 = int (time ())
599+ _msg = "Attaching the newly created Volume \" "
600+ _msg += obj_attr_list ["cloud_vv_name" ] + "\" (cloud-assigned uuid \" "
601+ _msg += obj_attr_list ["cloud_vv_uuid" ] + "\" ) to instance \" "
602+ _msg += obj_attr_list ["cloud_vm_name" ] + "\" (cloud-assigned uuid \" "
603+ _msg += obj_attr_list ["cloud_vm_uuid" ] + "\" )"
604+ cbdebug (_msg )
605+
606+ if not volume .attach (_reservation ) :
607+ msg = "Volume attach failed. Aborting VM creation..."
608+ cbdebug (msg , True )
609+ volume .destroy ()
610+ raise CldOpsException (msg , _status )
611+
612+ cbdebug ("Volume attach success." , True )
613+ _mark4 = int (time ())
614+ obj_attr_list ["do_015_create_volume_time" ] += (_mark4 - _mark3 )
615+ _status = 0
616+ else :
617+ msg = "Volume creation failed. Aborting VM creation..."
618+ cbdebug (msg , True )
619+ raise CldOpsException (msg , _status )
620+ else :
621+ obj_attr_list ["cloud_vv_uuid" ] = "none"
622+
540623 except CldOpsException , obj :
541624 _status = obj .status
542625 _fmsg = str (obj .msg )
@@ -563,6 +646,7 @@ def vmcreate(self, obj_attr_list) :
563646 else :
564647 if _reservation :
565648 _reservation .destroy ()
649+
566650 raise CldOpsException (_msg , _status )
567651 else :
568652 _msg = "VM " + obj_attr_list ["uuid" ] + " was successfully "
@@ -631,6 +715,21 @@ def vmdestroy(self, obj_attr_list) :
631715 _time_mark_drc = int (time ())
632716 obj_attr_list ["mgt_903_deprovisioning_request_completed" ] = _time_mark_drc - _time_mark_drs
633717
718+ if "cloud_vv_name" in obj_attr_list :
719+ tenant = credential_pair .split (":" )[0 ]
720+ cbdebug ("Checking for volumes from tenant: " + tenant )
721+ _volumes = catalogs .digitalocean [credential_pair ].list_volumes ()
722+ for _volume in _volumes :
723+ if _volume .name == obj_attr_list ["cloud_vv_name" ] :
724+ try :
725+ cbdebug ("Destroying: " + _volume .name + " (" + tenant + ")" , True )
726+ _volume .destroy ()
727+ break
728+ except :
729+ pass
730+ else :
731+ cbdebug ("Ignoring volume: " + _volume .name )
732+
634733 _status = 0
635734
636735 except CldOpsException , obj :
@@ -912,3 +1011,4 @@ def aiundefine(self, obj_attr_list, current_step) :
9121011 _msg += "\" ."
9131012 cbdebug (_msg )
9141013 return _status , _msg
1014+
0 commit comments