OpenStack Testing (Tempest) of an existing cloud
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cleanup.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. # Copyright 2014 Dell Inc.
  2. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  3. # not use this file except in compliance with the License. You may obtain
  4. # a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  11. # License for the specific language governing permissions and limitations
  12. # under the License.
  13. """
  14. Utility for cleaning up environment after Tempest test run
  15. **Usage:** ``tempest cleanup [--help] [OPTIONS]``
  16. If run with no arguments, ``tempest cleanup`` will query your OpenStack
  17. deployment and build a list of resources to delete and destroy them. This list
  18. will exclude the resources from ``saved_state.json`` and will include the
  19. configured admin account if the ``--delete-tempest-conf-objects`` flag is
  20. specified. By default the admin project is not deleted and the admin user
  21. specified in ``tempest.conf`` is never deleted.
  22. Example Run
  23. -----------
  24. .. warning::
  25. If step 1 is skipped in the example below, the cleanup procedure
  26. may delete resources that existed in the cloud before the test run. This
  27. may cause an unwanted destruction of cloud resources, so use caution with
  28. this command.
  29. Examples::
  30. $ tempest cleanup --init-saved-state
  31. $ # Actual running of Tempest tests
  32. $ tempest cleanup
  33. Runtime Arguments
  34. -----------------
  35. * ``--init-saved-state``: Initializes the saved state of the OpenStack
  36. deployment and will output a ``saved_state.json`` file containing resources
  37. from your deployment that will be preserved from the cleanup command. This
  38. should be done prior to running Tempest tests.
  39. * ``--delete-tempest-conf-objects``: If option is present, then the command
  40. will delete the admin project in addition to the resources associated with
  41. them on clean up. If option is not present, the command will delete the
  42. resources associated with the Tempest and alternate Tempest users and
  43. projects but will not delete the projects themselves.
  44. * ``--dry-run``: Creates a report (``./dry_run.json``) of the projects that
  45. will be cleaned up (in the ``_projects_to_clean`` dictionary [1]_) and the
  46. global objects that will be removed (domains, flavors, images, roles,
  47. projects, and users). Once the cleanup command is executed (e.g. run without
  48. parameters), running it again with ``--dry-run`` should yield an empty
  49. report.
  50. * ``--help``: Print the help text for the command and parameters.
  51. .. [1] The ``_projects_to_clean`` dictionary in ``dry_run.json`` lists the
  52. projects that ``tempest cleanup`` will loop through to delete child
  53. objects, but the command will, by default, not delete the projects
  54. themselves. This may differ from the ``projects`` list as you can clean
  55. the Tempest and alternate Tempest users and projects but they will not be
  56. deleted unless the ``--delete-tempest-conf-objects`` flag is used to
  57. force their deletion.
  58. .. note::
  59. If during execution of ``tempest cleanup`` NotImplemented exception
  60. occurres, ``tempest cleanup`` won't fail on that, it will be logged only.
  61. NotImplemented errors are ignored because they are an outcome of some
  62. extensions being disabled and ``tempest cleanup`` is not checking their
  63. availability as it tries to clean up as much as possible without any
  64. complicated logic.
  65. """
  66. import sys
  67. import traceback
  68. from cliff import command
  69. from oslo_log import log as logging
  70. from oslo_serialization import jsonutils as json
  71. from tempest import clients
  72. from tempest.cmd import cleanup_service
  73. from tempest.common import credentials_factory as credentials
  74. from tempest.common import identity
  75. from tempest import config
  76. from tempest.lib import exceptions
  77. SAVED_STATE_JSON = "saved_state.json"
  78. DRY_RUN_JSON = "dry_run.json"
  79. LOG = logging.getLogger(__name__)
  80. CONF = config.CONF
  81. class TempestCleanup(command.Command):
  82. GOT_EXCEPTIONS = []
  83. def take_action(self, parsed_args):
  84. try:
  85. self.init(parsed_args)
  86. if not parsed_args.init_saved_state:
  87. self._cleanup()
  88. except Exception:
  89. LOG.exception("Failure during cleanup")
  90. traceback.print_exc()
  91. raise
  92. # ignore NotImplemented errors as those are an outcome of some
  93. # extensions being disabled and cleanup is not checking their
  94. # availability as it tries to clean up as much as possible without
  95. # any complicated logic
  96. critical_exceptions = [ex for ex in self.GOT_EXCEPTIONS if
  97. not isinstance(ex, exceptions.NotImplemented)]
  98. if critical_exceptions:
  99. raise Exception(self.GOT_EXCEPTIONS)
  100. def init(self, parsed_args):
  101. cleanup_service.init_conf()
  102. self.options = parsed_args
  103. self.admin_mgr = clients.Manager(
  104. credentials.get_configured_admin_credentials())
  105. self.dry_run_data = {}
  106. self.json_data = {}
  107. self.admin_id = ""
  108. self.admin_role_id = ""
  109. self.admin_project_id = ""
  110. self._init_admin_ids()
  111. self.admin_role_added = []
  112. # available services
  113. self.project_services = cleanup_service.get_project_cleanup_services()
  114. self.global_services = cleanup_service.get_global_cleanup_services()
  115. if parsed_args.init_saved_state:
  116. self._init_state()
  117. return
  118. self._load_json()
  119. def _cleanup(self):
  120. print("Begin cleanup")
  121. is_dry_run = self.options.dry_run
  122. is_preserve = not self.options.delete_tempest_conf_objects
  123. is_save_state = False
  124. if is_dry_run:
  125. self.dry_run_data["_projects_to_clean"] = {}
  126. admin_mgr = self.admin_mgr
  127. # Always cleanup tempest and alt tempest projects unless
  128. # they are in saved state json. Therefore is_preserve is False
  129. kwargs = {'data': self.dry_run_data,
  130. 'is_dry_run': is_dry_run,
  131. 'saved_state_json': self.json_data,
  132. 'is_preserve': False,
  133. 'is_save_state': is_save_state}
  134. project_service = cleanup_service.ProjectService(admin_mgr, **kwargs)
  135. projects = project_service.list()
  136. print("Process %s projects" % len(projects))
  137. # Loop through list of projects and clean them up.
  138. for project in projects:
  139. self._add_admin(project['id'])
  140. self._clean_project(project)
  141. kwargs = {'data': self.dry_run_data,
  142. 'is_dry_run': is_dry_run,
  143. 'saved_state_json': self.json_data,
  144. 'is_preserve': is_preserve,
  145. 'is_save_state': is_save_state,
  146. 'got_exceptions': self.GOT_EXCEPTIONS}
  147. for service in self.global_services:
  148. svc = service(admin_mgr, **kwargs)
  149. svc.run()
  150. if is_dry_run:
  151. with open(DRY_RUN_JSON, 'w+') as f:
  152. f.write(json.dumps(self.dry_run_data, sort_keys=True,
  153. indent=2, separators=(',', ': ')))
  154. self._remove_admin_user_roles()
  155. def _remove_admin_user_roles(self):
  156. project_ids = self.admin_role_added
  157. LOG.debug("Removing admin user roles where needed for projects: %s",
  158. project_ids)
  159. for project_id in project_ids:
  160. self._remove_admin_role(project_id)
  161. def _clean_project(self, project):
  162. print("Cleaning project: %s " % project['name'])
  163. is_dry_run = self.options.dry_run
  164. dry_run_data = self.dry_run_data
  165. is_preserve = not self.options.delete_tempest_conf_objects
  166. project_id = project['id']
  167. project_name = project['name']
  168. project_data = None
  169. if is_dry_run:
  170. project_data = dry_run_data["_projects_to_clean"][project_id] = {}
  171. project_data['name'] = project_name
  172. kwargs = {"username": CONF.auth.admin_username,
  173. "password": CONF.auth.admin_password,
  174. "project_name": project['name']}
  175. mgr = clients.Manager(credentials=credentials.get_credentials(
  176. **kwargs))
  177. kwargs = {'data': project_data,
  178. 'is_dry_run': is_dry_run,
  179. 'saved_state_json': self.json_data,
  180. 'is_preserve': is_preserve,
  181. 'is_save_state': False,
  182. 'project_id': project_id,
  183. 'got_exceptions': self.GOT_EXCEPTIONS}
  184. for service in self.project_services:
  185. svc = service(mgr, **kwargs)
  186. svc.run()
  187. def _init_admin_ids(self):
  188. pr_cl = self.admin_mgr.projects_client
  189. rl_cl = self.admin_mgr.roles_v3_client
  190. rla_cl = self.admin_mgr.role_assignments_client
  191. us_cl = self.admin_mgr.users_v3_client
  192. project = identity.get_project_by_name(pr_cl,
  193. CONF.auth.admin_project_name)
  194. self.admin_project_id = project['id']
  195. user = identity.get_user_by_project(us_cl, rla_cl,
  196. self.admin_project_id,
  197. CONF.auth.admin_username)
  198. self.admin_id = user['id']
  199. roles = rl_cl.list_roles()['roles']
  200. for role in roles:
  201. if role['name'] == CONF.identity.admin_role:
  202. self.admin_role_id = role['id']
  203. break
  204. def get_parser(self, prog_name):
  205. parser = super(TempestCleanup, self).get_parser(prog_name)
  206. parser.add_argument('--init-saved-state', action="store_true",
  207. dest='init_saved_state', default=False,
  208. help="Creates JSON file: " + SAVED_STATE_JSON +
  209. ", representing the current state of your "
  210. "deployment, specifically object types "
  211. "tempest creates and destroys during a run. "
  212. "You must run with this flag prior to "
  213. "executing cleanup in normal mode, which is with "
  214. "no arguments.")
  215. parser.add_argument('--delete-tempest-conf-objects',
  216. action="store_true",
  217. dest='delete_tempest_conf_objects',
  218. default=False,
  219. help="Force deletion of the tempest and "
  220. "alternate tempest users and projects.")
  221. parser.add_argument('--dry-run', action="store_true",
  222. dest='dry_run', default=False,
  223. help="Generate JSON file:" + DRY_RUN_JSON +
  224. ", that reports the objects that would have "
  225. "been deleted had a full cleanup been run.")
  226. return parser
  227. def get_description(self):
  228. return 'Cleanup after tempest run'
  229. def _add_admin(self, project_id):
  230. rl_cl = self.admin_mgr.roles_v3_client
  231. needs_role = True
  232. roles = rl_cl.list_user_roles_on_project(project_id,
  233. self.admin_id)['roles']
  234. for role in roles:
  235. if role['id'] == self.admin_role_id:
  236. needs_role = False
  237. LOG.debug("User already had admin privilege for this project")
  238. if needs_role:
  239. LOG.debug("Adding admin privilege for : %s", project_id)
  240. rl_cl.create_user_role_on_project(project_id, self.admin_id,
  241. self.admin_role_id)
  242. self.admin_role_added.append(project_id)
  243. def _remove_admin_role(self, project_id):
  244. LOG.debug("Remove admin user role for projectt: %s", project_id)
  245. # Must initialize Admin Manager for each user role
  246. # Otherwise authentication exception is thrown, weird
  247. id_cl = clients.Manager(
  248. credentials.get_configured_admin_credentials()).identity_client
  249. if (self._project_exists(project_id)):
  250. try:
  251. id_cl.delete_role_from_user_on_project(project_id,
  252. self.admin_id,
  253. self.admin_role_id)
  254. except Exception as ex:
  255. LOG.exception("Failed removing role from project which still "
  256. "exists, exception: %s", ex)
  257. def _project_exists(self, project_id):
  258. pr_cl = self.admin_mgr.projects_client
  259. try:
  260. p = pr_cl.show_project(project_id)
  261. LOG.debug("Project is: %s", str(p))
  262. return True
  263. except Exception as ex:
  264. LOG.debug("Project no longer exists? %s", ex)
  265. return False
  266. def _init_state(self):
  267. print("Initializing saved state.")
  268. data = {}
  269. admin_mgr = self.admin_mgr
  270. kwargs = {'data': data,
  271. 'is_dry_run': False,
  272. 'saved_state_json': data,
  273. 'is_preserve': False,
  274. 'is_save_state': True,
  275. 'got_exceptions': self.GOT_EXCEPTIONS}
  276. for service in self.global_services:
  277. svc = service(admin_mgr, **kwargs)
  278. svc.run()
  279. for service in self.project_services:
  280. svc = service(admin_mgr, **kwargs)
  281. svc.run()
  282. with open(SAVED_STATE_JSON, 'w+') as f:
  283. f.write(json.dumps(data, sort_keys=True,
  284. indent=2, separators=(',', ': ')))
  285. def _load_json(self, saved_state_json=SAVED_STATE_JSON):
  286. try:
  287. with open(saved_state_json, 'rb') as json_file:
  288. self.json_data = json.load(json_file)
  289. except IOError as ex:
  290. LOG.exception("Failed loading saved state, please be sure you"
  291. " have first run cleanup with --init-saved-state "
  292. "flag prior to running tempest. Exception: %s", ex)
  293. sys.exit(ex)
  294. except Exception as ex:
  295. LOG.exception("Exception parsing saved state json : %s", ex)
  296. sys.exit(ex)