Coverage for curator/repomgrcli.py: 99%

1"""es_repo_mgr CLI"""

2 import sys

3 import re

4 import logging

5 import click

6 import elasticsearch7

7 from .defaults import settings

8 from .exceptions import *

9 from .config_utils import process_config

10 from .utils import *

11 from ._version import __version__

12

13

14 def delete_callback(ctx, param, value):

15 """Callback if delete called"""

16 if not value:

17 ctx.abort()

18

19 def show_repos(client):

20 """Show all repositories"""

21 for repository in sorted(get_repository(client, '_all').keys()):

22 print('{0}'.format(repository))

23 sys.exit(0)

24

25@click.command(short_help='Filesystem Repository')

26@click.option('--repository', required=True, type=str, help='Repository name')

27@click.option(

28 '--location',

29 required=True,

30 type=str,

31 help=(

32 'Shared file-system location. '

33 'Must match remote path, & be accessible to all master & data nodes'

34 )

35)

36@click.option(

37 '--compression',

38 type=bool,

39 default=True,

40 show_default=True,

41 help='Enable/Disable metadata compression.'

42)

43@click.option('--chunk_size', type=str, help='Chunk size, e.g. 1g, 10m, 5k. [unbounded]')

44@click.option(

45 '--max_restore_bytes_per_sec',

46 type=str, default='20mb',

47 show_default=True,

48 help='Throttles per node restore rate (per second).'

49)

50@click.option(

51 '--max_snapshot_bytes_per_sec',

52 type=str, default='20mb',

53 show_default=True,

54 help='Throttles per node snapshot rate (per second).'

55)

56@click.option(

57 '--skip_repo_fs_check',

58 type=bool,

59 default=False,

60 show_default=True,

61 help='Skip repository verification after creation'

62)

63@click.pass_context

64 def fs(

65 ctx, repository, location, compression, chunk_size, max_restore_bytes_per_sec,

66 max_snapshot_bytes_per_sec, skip_repo_fs_check

67 ):

68 """

69 Create a filesystem repository.

70 """

71 logger = logging.getLogger('curator.repomgrcli.fs')

72 client = get_client(**ctx.obj['client_args'])

73 try:

74 create_repository(client, repo_type='fs', **ctx.params)

75 except FailedExecution as err:

76 logger.critical(err)

77 sys.exit(1)

78

79

80@click.command(short_help='S3 Repository')

81@click.option('--repository', required=True, type=str, help='Repository name')

82@click.option('--bucket', required=True, type=str, help='S3 bucket name')

83@click.option('--region', type=str, help='S3 region. [US Standard]')

84@click.option('--base_path', type=str, help='S3 base path. [root]')

85@click.option('--access_key', type=str, help='S3 access key. [value of cloud.aws.access_key]')

86@click.option('--secret_key', type=str, help='S3 secret key. [value of cloud.aws.secret_key]')

87@click.option(

88 '--compression',

89 type=bool,

90 default=True,

91 show_default=True,

92 help='Enable/Disable metadata compression.'

93)

94@click.option(

95 '--chunk_size',

96 type=str,

97 help='Chunk size, e.g. 1g, 10m, 5k. [unbounded]'

98)

99@click.option(

100 '--max_restore_bytes_per_sec',

101 type=str,

102 default='20mb',

103 show_default=True,

104 help='Throttles per node restore rate (per second).'

105)

106@click.option(

107 '--max_snapshot_bytes_per_sec',

108 type=str,

109 default='20mb',

110 show_default=True,

111 help='Throttles per node snapshot rate (per second).'

112)

113@click.option(

114 '--skip_repo_fs_check',

115 type=bool,

116 default=False,

117 show_default=True,

118 help='Skip repository verification after creation'

119)

120@click.pass_context

121 def s3(

122 ctx, repository, bucket, region, base_path, access_key, secret_key, compression,

123 chunk_size, max_restore_bytes_per_sec, max_snapshot_bytes_per_sec, skip_repo_fs_check

124 ):

125 """

126 Create an S3 repository.

127 """

128 logger = logging.getLogger('curator.repomgrcli.s3')

129 client = get_client(**ctx.obj['client_args'])

130 try:

131 create_repository(client, repo_type='s3', **ctx.params)

132 except FailedExecution as err:

133 logger.critical(err)

134 sys.exit(1)

135

136

137@click.group()

138@click.option(

139 '--config',

140 help="Path to configuration file. Default: ~/.curator/curator.yml",

141 type=click.Path(exists=True), default=settings.config_file()

142)

143@click.pass_context

144 def repo_mgr_cli(ctx, config):

145 """Repository manager for Elasticsearch Curator."""

146 ctx.obj = {}

147 ctx.obj['client_args'] = process_config(config)

148 logger = logging.getLogger(__name__)

149 logger.debug('Client and logging options validated.')

150

151@repo_mgr_cli.group('create')

152@click.pass_context

153 def _create(ctx):

154 """Create an Elasticsearch repository"""

155 _create.add_command(fs)

156 _create.add_command(s3)

157

158@repo_mgr_cli.command('show')

159@click.pass_context

160 def show(ctx):

161 """Show all repositories"""

162 client = get_client(**ctx.obj['client_args'])

163 show_repos(client)

164

165@repo_mgr_cli.command('delete')

166@click.option('--repository', required=True, help='Repository name', type=str)

167@click.option(

168 '--yes', is_flag=True, callback=delete_callback, expose_value=False,

169 prompt='Are you sure you want to delete the repository?'

170)

171@click.pass_context

172 def _delete(ctx, repository):

173 """Delete an Elasticsearch repository"""

174 logger = logging.getLogger('curator.repomgrcli._delete')

175 client = get_client(**ctx.obj['client_args'])

176 try:

177 logger.info('Deleting repository {0}...'.format(repository))

178 client.snapshot.delete_repository(repository=repository)

179 except elasticsearch7.NotFoundError:

180 logger.error(

181 'Unable to delete repository: {0} Not Found.'.format(repository))

182 sys.exit(1)