#54 Add instance sync command
Merged 6 years ago by frantisekz. Opened 6 years ago by frantisekz.

file modified
+14
@@ -156,6 +156,15 @@ 

  

      tc_instance.remove(autostop=args.force)

  

+ def _clean_instances(args):

+     """Handler for 'instance clean' command. Expects the following elements in args:

+         * name(str)

+ 

+     :param args: args from argparser

+     """

+ 

+     instance.clean_instances()

+ 

  

  def _reboot_instance(args):

      """Handler for 'instance reboot' command. Expects the following elements in args:
@@ -256,6 +265,11 @@ 

                                   help="Stop the instance if it's running",

                                   action="store_true")

      instarg_destroy.set_defaults(func=_remove_instance)

+ 

+     # instance clean

+     instarg_clean = instarg_subp.add_parser("clean", help="remove non-existing libvirt vms from testcloud")

+     instarg_clean.set_defaults(func=_clean_instances)

+ 

      # instance reboot

      instarg_reboot = instarg_subp.add_parser("reboot", help="reboot instance")

      instarg_reboot.add_argument("name",

file modified
+15 -1
@@ -144,7 +144,7 @@ 

  

      for instance in all_instances:

          if instance['name'] not in domains.keys():

-             log.warn('{} is not registered, might want to delete it.'.format(instance['name']))

+             log.warn("{} is not registered, might want to delete it via 'testcloud instance clean'.".format(instance['name']))

              instance['state'] = 'de-sync'

  

              instances.append(instance)
@@ -158,6 +158,20 @@ 

  

      return instances

  

+ def clean_instances(connection='qemu:///system'):

+     """

+     Removes all instances in 'de-sync' state.

+     """

+     domains = _list_domains(connection)

+     all_instances = _list_instances()

+ 

+     for instance in all_instances:

+         if instance['name'] not in domains.keys():

+             log.debug("Removing de-synced instance {}".format(instance['name']))

+             instance_path = "{}/instances/{}".format(config_data.DATA_DIR, instance['name'])

+ 

+             # remove from disk

+             shutil.rmtree(instance_path)

  

  class Instance(object):

      """Handles creating, starting, stopping and removing virtual machines

Please rename the command to testcloud instance clean.

Use quotes to better show what command should be run.

rebased onto b102b2b7048a0f6d445dcaf71bcd916d22a58ec1

6 years ago

rebased onto d81b39c

6 years ago

Looks good, but I haven't tested it. @lbrabec, can you review and test? Thanks.

Pull-Request has been merged by frantisekz

6 years ago