Example #1
0
 def test_get_resource_classes_without_excluded(self):
     config = "Networks"
     classes = utils.get_resource_classes(None, config)
     self.assertIsInstance(classes, typing.List)
     for klass in classes:
         self.assertTrue(issubclass(klass, ServiceResource))
         self.assertNotEqual(config, klass.__name__)
Example #2
0
 def test_get_resource_classes(self):
     resources = ['Stacks', 'Networks']
     classes = utils.get_resource_classes(resources)
     self.assertTrue(len(classes) == 2)
     self.assertIsInstance(classes, typing.List)
     for klass in classes:
         self.assertTrue(issubclass(klass, ServiceResource))
         self.assertIn(klass.__name__, resources)
Example #3
0
def create_argument_parser():
    parser = argparse.ArgumentParser(
        description="Purge resources from an Openstack project.")
    parser.add_argument("--verbose",
                        action="store_true",
                        help="Make output verbose")
    parser.add_argument("--dry-run",
                        action="store_true",
                        help="List project's resources")
    parser.add_argument(
        "--delete-shared-resources",
        action="store_true",
        help="Whether to delete shared resources (public images and external "
        "networks)")
    parser.add_argument(
        "--admin-role-name",
        default="admin",
        help="Name of admin role. Defaults to 'admin'. This role will be "
        "temporarily granted on the project to purge to the "
        "authenticated user.")
    parser.add_argument(
        "--resource",
        action="append",
        choices=[cls.__name__ for cls in utils.get_resource_classes()],
        help="Purge only the specified resource type. Repeat to delete "
        "several types at once.")
    parser.add_argument(
        "--excluderesource",
        action="append",
        choices=[cls.__name__ for cls in utils.get_resource_classes()],
        help="Do not purge the specified resource type. Repeat to exclude "
        "several types at once.")

    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument(
        "--purge-project",
        metavar="ID_OR_NAME",
        help="ID or Name of project to purge. This option requires "
        "to authenticate with admin credentials.")
    group.add_argument(
        "--purge-own-project",
        action="store_true",
        help="Purge resources of the project used to authenticate. Useful "
        "if you don't have the admin credentials of the cloud.")
    return parser
Example #4
0
def main():
    parser = create_argument_parser()

    cloud_config = os_client_config.OpenStackConfig()
    cloud_config.register_argparse_arguments(parser, sys.argv)

    options = parser.parse_args()
    configure_logging(options.verbose)

    creds_manager = CredentialsManager(options=options)
    creds_manager.ensure_enabled_project()
    creds_manager.ensure_role_on_project()

    resource_managers = sorted([
        cls(creds_manager)
        for cls in utils.get_resource_classes(options.resource)
    ],
                               key=operator.methodcaller('order'))

    # This is an `Event` used to signal whether one of the threads encountered
    # an unrecoverable error, at which point all threads should exit because
    # otherwise there's a chance the cleanup process never finishes.
    exit = threading.Event()

    # Dummy function to work around `ThreadPoolExecutor.map()` not accepting
    # a callable with arguments.
    def partial_runner(resource_manager):
        runner(resource_manager, options=options,
               exit=exit)  # pragma: no cover

    try:
        with concurrent.futures.ThreadPoolExecutor(8) as executor:
            executor.map(partial_runner, resource_managers)
    except KeyboardInterrupt:
        exit.set()

    if creds_manager.revoke_role_after_purge:
        creds_manager.revoke_role_on_project()

    if creds_manager.disable_project_after_purge:
        creds_manager.disable_project()

    sys.exit(int(exit.is_set()))
Example #5
0
def main() -> None:
    parser = create_argument_parser()

    cloud_config = os_client_config.OpenStackConfig()
    cloud_config.register_argparse_arguments(parser, sys.argv)

    options = parser.parse_args()
    configure_logging(options.verbose)

    creds_manager = CredentialsManager(options=options)
    creds_manager.ensure_enabled_project()
    creds_manager.ensure_role_on_project()

    resource_managers = sorted(
        [cls(creds_manager)
         for cls in utils.get_resource_classes(options.resource)],
        key=operator.methodcaller('order')
    )

    # This is an `Event` used to signal whether one of the threads encountered
    # an unrecoverable error, at which point all threads should exit because
    # otherwise there's a chance the cleanup process never finishes.
    exit = threading.Event()

    # Dummy function to work around `ThreadPoolExecutor.map()` not accepting
    # a callable with arguments.
    def partial_runner(resource_manager: ServiceResource) -> None:
        runner(resource_manager, options=options,
               exit=exit)  # pragma: no cover

    try:
        with concurrent.futures.ThreadPoolExecutor(8) as executor:
            executor.map(partial_runner, resource_managers)
    except KeyboardInterrupt:
        exit.set()

    if creds_manager.revoke_role_after_purge:
        creds_manager.revoke_role_on_project()

    if creds_manager.disable_project_after_purge:
        creds_manager.disable_project()

    sys.exit(int(exit.is_set()))
Example #6
0
def create_argument_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(
        description="Purge resources from an Openstack project."
    )
    parser.add_argument(
        "--verbose", action="store_true",
        help="Make output verbose"
    )
    parser.add_argument(
        "--dry-run", action="store_true",
        help="List project's resources"
    )
    parser.add_argument(
        "--delete-shared-resources", action="store_true",
        help="Whether to delete shared resources (public images and external "
             "networks)"
    )
    parser.add_argument(
        "--admin-role-name", default="admin",
        help="Name of admin role. Defaults to 'admin'. This role will be "
             "temporarily granted on the project to purge to the "
             "authenticated user."
    )
    parser.add_argument(
        "--resource", action="append",
        choices=[cls.__name__ for cls in utils.get_resource_classes()],
        help="Purge only the specified resource type. Repeat to delete "
             "several types at once."
    )

    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument(
        "--purge-project", metavar="ID_OR_NAME",
        help="ID or Name of project to purge. This option requires "
             "to authenticate with admin credentials."
    )
    group.add_argument(
        "--purge-own-project", action="store_true",
        help="Purge resources of the project used to authenticate. Useful "
             "if you don't have the admin credentials of the cloud."
    )
    return parser
Example #7
0
#!/usr/bin/env python3
#  Licensed under the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License. You may obtain
#  a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#  License for the specific language governing permissions and limitations
#  under the License.
import operator

from ospurge import utils

resource_managers = sorted([cls for cls in utils.get_resource_classes()],
                           key=operator.methodcaller('order'))

for cls in resource_managers:
    print("{} => {}".format(cls.__name__, cls.ORDER))
Example #8
0
 def test_get_resource_classes(self):
     config = "Networks"
     classes = utils.get_resource_classes(config)
     self.assertIsInstance(classes, typing.List)
     for klass in classes:
         self.assertTrue(issubclass(klass, ServiceResource))
Example #9
0
 def test_get_resource_classes(self):
     config = "Networks"
     classes = utils.get_resource_classes(config)
     self.assertIsInstance(classes, typing.List)
     for klass in classes:
         self.assertTrue(issubclass(klass, ServiceResource))