lsup_admin.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import click
  2. import click_log
  3. import csv
  4. import json
  5. import logging
  6. import sys
  7. from os import getcwd, path
  8. import arrow
  9. from lakesuperior import env
  10. from lakesuperior.api import admin as admin_api
  11. from lakesuperior.config_parser import config
  12. from lakesuperior.exceptions import ChecksumValidationError
  13. from lakesuperior.globals import AppGlobals
  14. __doc__="""
  15. Utility to perform core maintenance tasks via console command-line.
  16. The command-line tool is self-documented. Type::
  17. lsup-admin --help
  18. for a list of tools and options.
  19. """
  20. logger = logging.getLogger(__name__)
  21. click_log.basic_config(logger)
  22. @click.group()
  23. def admin():
  24. pass
  25. @click.command()
  26. def bootstrap():
  27. """
  28. Bootstrap binary and graph stores.
  29. This script will parse configuration files and initialize a filesystem and
  30. triplestore with an empty FCREPO repository.
  31. It is used in test suites and on a first run.
  32. Additional scaffolding files may be parsed to create initial contents.
  33. """
  34. import lakesuperior.env_setup
  35. rdfly = env.app_globals.rdfly
  36. nonrdfly = env.app_globals.nonrdfly
  37. click.echo(
  38. click.style(
  39. 'WARNING: This operation will WIPE ALL YOUR DATA.\n',
  40. bold=True, fg='red')
  41. + 'Are you sure? (Please type `yes` to continue) > ', nl=False)
  42. choice = input().lower()
  43. if choice != 'yes':
  44. click.echo('Aborting.')
  45. sys.exit(1)
  46. click.echo('Initializing graph store at {}'.format(rdfly.store.env_path))
  47. rdfly.bootstrap()
  48. click.echo('Graph store initialized.')
  49. click.echo('Initializing binary store at {}'.format(nonrdfly.root))
  50. nonrdfly.bootstrap()
  51. click.echo('Binary store initialized.')
  52. click.echo('\nRepository successfully set up. Go to town.')
  53. click.echo('If the HTTP server is running, it must be restarted.')
  54. @click.command()
  55. @click.option(
  56. '--human', '-h', is_flag=True, flag_value=True,
  57. help='Print a human-readable string. By default, JSON is printed.')
  58. def stats(human=False):
  59. """
  60. Print repository statistics.
  61. @param human (bool) Whether to output the data in human-readable
  62. format.
  63. """
  64. stat_data = admin_api.stats()
  65. if human:
  66. click.echo(
  67. 'This option is not supported yet. Sorry.\nUse the `/admin/stats`'
  68. ' endpoint in the web UI for a pretty printout.')
  69. else:
  70. click.echo(json.dumps(stat_data))
  71. @click.command()
  72. @click.argument('uid')
  73. def check_fixity(uid):
  74. """
  75. Check the fixity of a resource.
  76. """
  77. import lakesuperior.env_setup
  78. try:
  79. admin_api.fixity_check(uid)
  80. except ChecksumValidationError:
  81. print(f'Checksum for {uid} failed.')
  82. sys.exit(1)
  83. print(f'Checksum for {uid} passed.')
  84. @click.option(
  85. '--config-folder', '-c', default=None, help='Alternative configuration '
  86. 'folder to look up. If not set, the location set in the environment or '
  87. 'the default configuration is used.')
  88. @click.option(
  89. '--output', '-o', default=None, help='Output file. If not specified, a '
  90. 'timestamp-named file will be generated automatically.')
  91. @click.command()
  92. def check_refint(config_folder=None, output=None):
  93. """
  94. Check referential integrity.
  95. This command scans the graph store to verify that all references to
  96. resources within the repository are effectively pointing to existing
  97. resources. For repositories set up with the `referential_integrity` option
  98. (the default), this is a pre-condition for a consistent data set.
  99. If inconsistencies are found, a report is generated in CSV format with the
  100. following columns: `s`, `p`, `o` (respectively the terms of the
  101. triple containing the dangling relationship) and `missing` which
  102. indicates which term is the missing URI (currently always set to `o`).
  103. Note: this check can be run regardless of whether the repository enforces
  104. referential integrity.
  105. """
  106. if config_folder:
  107. env.app_globals = AppGlobals(parse_config(config_dir))
  108. else:
  109. import lakesuperior.env_setup
  110. check_results = admin_api.integrity_check()
  111. click.echo('Integrity check results:')
  112. if len(check_results):
  113. click.echo(click.style('Inconsistencies found!', fg='red', bold=True))
  114. if not output:
  115. output = path.join(getcwd(), 'refint_report-{}.csv'.format(
  116. arrow.utcnow().format('YYYY-MM-DDTHH:mm:ss.S')))
  117. elif not output.endswith('.csv'):
  118. output += '.csv'
  119. with open(output, 'w', newline='') as fh:
  120. writer = csv.writer(fh)
  121. writer.writerow(('s', 'p', 'o', 'missing'))
  122. for trp in check_results:
  123. # ``o`` is always hardcoded for now.
  124. writer.writerow([t.n3() for t in trp[0]] + ['o'])
  125. click.echo('Report generated at {}'.format(output))
  126. else:
  127. click.echo(click.style('Clean. ', fg='green', bold=True)
  128. + 'No inconsistency found. No report generated.')
  129. @click.command()
  130. def cleanup():
  131. """
  132. [STUB] Clean up orphan database items.
  133. """
  134. pass
  135. @click.command()
  136. @click.argument('src')
  137. @click.argument('dest')
  138. @click.option(
  139. '--auth', '-a',
  140. help='Colon-separated credentials for HTTP Basic authentication on the '
  141. 'source repository. E.g. `user:password`.')
  142. @click.option(
  143. '--start', '-s', show_default=True,
  144. help='Starting point for looking for resources in the repository.\n'
  145. 'The default `/` value starts at the root, i.e. migrates the whole '
  146. 'repository.')
  147. @click.option(
  148. '--list-file', '-l', help='Path to a local file containing URIs to be '
  149. 'used as starting points, one per line. Use this alternatively to `-s`. '
  150. 'The URIs can be relative to the repository root (e.g. `/a/b/c`) or fully '
  151. 'qualified (e.g. `https://example.edu/fcrepo/rest/a/b/c`).')
  152. @click.option(
  153. '--zero-binaries', '-z', is_flag=True,
  154. help='If set, binaries are created as zero-byte files in the proper '
  155. 'folder structure rather than having their full content copied.')
  156. @click.option(
  157. '--skip-errors', '-e', is_flag=True,
  158. help='If set, when the application encounters an error while retrieving '
  159. 'a resource from the source repository, it will log the error rather than '
  160. 'quitting. Other exceptions caused by the application will terminate the '
  161. 'process as usual.')
  162. @click_log.simple_verbosity_option(logger)
  163. def migrate(src, dest, auth, start, list_file, zero_binaries, skip_errors):
  164. """
  165. Migrate an LDP repository to Lakesuperior.
  166. This utility creates a fully functional Lakesuperior repository from an
  167. existing repository. The source repo can be Lakesuperior or
  168. another LDP-compatible implementation.
  169. A folder will be created in the location indicated by ``dest``. If the
  170. folder exists already, it will be deleted and recreated. The folder will be
  171. populated with the RDF and binary data directories and a default
  172. configuration directory. The new repository can be immediately started
  173. from this location.
  174. """
  175. logger.info('Migrating {} into a new repository on {}.'.format(
  176. src, dest))
  177. src_auth = tuple(auth.split(':')) if auth else None
  178. entries = admin_api.migrate(
  179. src, dest, src_auth=src_auth, start_pts=start, list_file=list_file,
  180. zero_binaries=zero_binaries, skip_errors=skip_errors)
  181. logger.info('Migrated {} resources.'.format(entries))
  182. logger.info(f'''
  183. Migration complete. A new Lakesuperior environment has been created in
  184. {dest}. To start the new repository, run:
  185. FCREPO_CONFIG_DIR="{dest}/etc" fcrepo
  186. from the directory you launched this script in.
  187. Make sure that the default port is not being used by another repository.
  188. ''')
  189. admin.add_command(bootstrap)
  190. admin.add_command(check_fixity)
  191. admin.add_command(check_refint)
  192. admin.add_command(cleanup)
  193. admin.add_command(migrate)
  194. admin.add_command(stats)
  195. if __name__ == '__main__':
  196. admin()