lsup_admin.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import click
  2. import click_log
  3. import json
  4. import logging
  5. import os
  6. import sys
  7. from lakesuperior.api import admin as admin_api
  8. from lakesuperior.config_parser import config
  9. from lakesuperior.env import env
  10. from lakesuperior.store.ldp_rs.lmdb_store import TxnManager
  11. logger = logging.getLogger(__name__)
  12. click_log.basic_config(logger)
  13. @click.group()
  14. def admin():
  15. pass
  16. @click.command()
  17. def bootstrap():
  18. """
  19. Bootstrap binary and graph stores.
  20. This script will parse configuration files and initialize a filesystem and
  21. triplestore with an empty FCREPO repository.
  22. It is used in test suites and on a first run.
  23. Additional scaffolding files may be parsed to create initial contents.
  24. """
  25. import lakesuperior.env_setup
  26. rdfly = env.app_globals.rdfly
  27. nonrdfly = env.app_globals.nonrdfly
  28. click.echo(
  29. click.style(
  30. 'WARNING: This operation will WIPE ALL YOUR DATA.\n',
  31. bold=True, fg='red')
  32. + 'Are you sure? (Please type `yes` to continue) > ', nl=False)
  33. choice = input().lower()
  34. if choice != 'yes':
  35. click.echo('Aborting.')
  36. sys.exit(1)
  37. click.echo('Initializing graph store at {}'.format(rdfly.store.path))
  38. with TxnManager(env.app_globals.rdf_store, write=True) as txn:
  39. rdfly.bootstrap()
  40. rdfly.store.close()
  41. click.echo('Graph store initialized.')
  42. click.echo('Initializing binary store at {}'.format(nonrdfly.root))
  43. nonrdfly.bootstrap()
  44. click.echo('Binary store initialized.')
  45. click.echo('Repository successfully set up. Go to town.')
  46. @click.command()
  47. @click.option(
  48. '--human', '-h', is_flag=True, flag_value=True,
  49. help='Print a human-readable string. By default, JSON is printed.')
  50. def stats(human=False):
  51. """
  52. Print repository statistics.
  53. @param human (bool) Whether to output the data in human-readable
  54. format.
  55. """
  56. stat_data = admin_api.stats()
  57. if human:
  58. click.echo(
  59. 'This option is not supported yet. Sorry.\nUse the `/admin/stats`'
  60. ' endpoint in the web UI for a pretty printout.')
  61. else:
  62. click.echo(json.dumps(stat_data))
  63. @click.command()
  64. def check_fixity(uid):
  65. """
  66. [STUB] Check fixity of a resource.
  67. """
  68. pass
  69. @click.option(
  70. '--config-folder', '-c', default=None, help='Alternative configuration '
  71. 'folder to look up. If not set, the location set in the environment or '
  72. 'the default configuration is used.')
  73. @click.command()
  74. def check_refint(config_folder=None):
  75. """
  76. Check referential integrity.
  77. This command scans the graph store to verify that all references to
  78. resources within the repository are effectively pointing to existing
  79. resources. For repositories set up with the `referential_integrity` option
  80. (the default), this is a pre-condition for a consistent data set.
  81. Note: this check is run regardless of whether the repository enforces
  82. referential integrity.
  83. """
  84. check_results = admin_api.integrity_check(config_folder)
  85. click.echo('Integrity check results:')
  86. if len(check_results):
  87. click.echo(click.style('Inconsistencies found!', fg='red', bold=True))
  88. click.echo('Missing object in the following triples:')
  89. for trp in check_results:
  90. click.echo(' '.join([str(t) for t in trp[0]]))
  91. else:
  92. click.echo(click.style('Clean. ', fg='green', bold=True)
  93. + 'No inconsistency found.')
  94. @click.command()
  95. def cleanup():
  96. """
  97. [STUB] Clean up orphan database items.
  98. """
  99. pass
  100. @click.command()
  101. @click.argument('src')
  102. @click.argument('dest')
  103. @click.option(
  104. '--start', '-s', show_default=True,
  105. help='Starting point for looking for resources in the repository.\n'
  106. 'The default `/` value starts at the root, i.e. migrates the whole '
  107. 'repository.')
  108. @click.option(
  109. '--list-file', '-l', help='Path to a local file containing URIs to be '
  110. 'used as starting points, one per line. Use this alternatively to `-s`. '
  111. 'The URIs can be relative to the repository root (e.g. `/a/b/c`) or fully '
  112. 'qualified (e.g. `https://example.edu/fcrepo/rest/a/b/c`).')
  113. @click.option(
  114. '--zero-binaries', '-z', is_flag=True,
  115. help='If set, binaries are created as zero-byte files in the proper '
  116. 'folder structure rather than having their full content copied.')
  117. @click.option(
  118. '--skip-errors', '-e', is_flag=True,
  119. help='If set, when the application encounters an error while retrieving '
  120. 'a resource from the source repository, it will log the error rather than '
  121. 'quitting. Other exceptions caused by the application will terminate the '
  122. 'process as usual.')
  123. @click_log.simple_verbosity_option(logger)
  124. def migrate(src, dest, start, list_file, zero_binaries, skip_errors):
  125. """
  126. Migrate an LDP repository to LAKEsuperior.
  127. This utility creates a fully functional LAKEshore repository from an
  128. existing repository. The source repo can be LAKEsuperior or
  129. another LDP-compatible implementation.
  130. A folder will be created in the location indicated by ``dest``. If the
  131. folder exists already, it will be deleted and recreated. The folder will be
  132. populated with the RDF and binary data directories and a default
  133. configuration directory. The new repository can be immediately started
  134. from this location.
  135. """
  136. logger.info('Migrating {} into a new repository on {}.'.format(
  137. src, dest))
  138. entries = admin_api.migrate(
  139. src, dest, start_pts=start, list_file=list_file,
  140. zero_binaries=zero_binaries, skip_errors=skip_errors)
  141. logger.info('Migrated {} resources.'.format(entries))
  142. logger.info("""Migration complete. To start the new repository, from the
  143. directory you launched this script run:
  144. FCREPO_CONFIG_DIR="{}/etc" ./fcrepo
  145. Make sure that the default port is not being used by another repository.
  146. """.format(dest))
  147. admin.add_command(bootstrap)
  148. admin.add_command(check_fixity)
  149. admin.add_command(check_refint)
  150. admin.add_command(cleanup)
  151. admin.add_command(migrate)
  152. admin.add_command(stats)
  153. if __name__ == '__main__':
  154. admin()