lsup_admin.py 7.4 KB

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