lsup_admin.py 7.2 KB

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