##// END OF EJS Templates
artifacts: added CLI script for adding really big artifacts.
marcink -
r4005:804e6e03 default
parent child Browse files
Show More
@@ -0,0 +1,107 b''
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2016-2019 RhodeCode GmbH
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
21 import sys
22 import logging
23
24 import click
25
26 from rhodecode.lib.pyramid_utils import bootstrap
27 from rhodecode.model.db import Session, User, Repository
28 from rhodecode.model.user import UserModel
29 from rhodecode.apps.file_store import utils as store_utils
30
31 log = logging.getLogger(__name__)
32
33
34 @click.command()
35 @click.argument('ini_path', type=click.Path(exists=True))
36 @click.option(
37 '--filename',
38 required=True,
39 help='Filename for artifact.')
40 @click.option(
41 '--file-path',
42 required=True,
43 type=click.Path(exists=True, dir_okay=False, readable=True),
44 help='Path to a file to be added as artifact')
45 @click.option(
46 '--repo-id',
47 required=True,
48 type=int,
49 help='ID of repository to add this artifact to.')
50 @click.option(
51 '--user-id',
52 default=None,
53 type=int,
54 help='User ID for creator of artifact. '
55 'Default would be first super admin.')
56 @click.option(
57 '--description',
58 default=None,
59 type=str,
60 help='Add description to this artifact')
61 def main(ini_path, filename, file_path, repo_id, user_id, description):
62 return command(ini_path, filename, file_path, repo_id, user_id, description)
63
64
65 def command(ini_path, filename, file_path, repo_id, user_id, description):
66 with bootstrap(ini_path, env={'RC_CMD_SETUP_RC': '1'}) as env:
67 try:
68 from rc_ee.api.views.store_api import _store_file
69 except ImportError:
70 click.secho('ERROR: Unable to import store_api. '
71 'store_api is only available in EE edition of RhodeCode',
72 fg='red')
73 sys.exit(-1)
74
75 request = env['request']
76
77 repo = Repository.get(repo_id)
78 if not repo:
79 click.secho('ERROR: Unable to find repository with id `{}`'.format(repo_id),
80 fg='red')
81 sys.exit(-1)
82
83 # if we don't give user, or it's "DEFAULT" user we pick super-admin
84 if user_id is not None or user_id == 1:
85 db_user = User.get(user_id)
86 else:
87 db_user = User.get_first_super_admin()
88
89 if not db_user:
90 click.secho('ERROR: Unable to find user with id/username `{}`'.format(user_id),
91 fg='red')
92 sys.exit(-1)
93
94 auth_user = db_user.AuthUser(ip_addr='127.0.0.1')
95
96 storage = store_utils.get_file_storage(request.registry.settings)
97
98 with open(file_path, 'rb') as f:
99 click.secho('Adding new artifact from path: `{}`'.format(file_path),
100 fg='green')
101
102 file_data = _store_file(
103 storage, auth_user, filename, content=None, check_acl=True,
104 file_obj=f, description=description,
105 scope_repo_id=repo.repo_id)
106 click.secho('File Data: {}'.format(file_data),
107 fg='green')
@@ -169,6 +169,7 b' setup('
169 169 'rc-setup-app=rhodecode.lib.rc_commands.setup_rc:main',
170 170 'rc-upgrade-db=rhodecode.lib.rc_commands.upgrade_db:main',
171 171 'rc-ishell=rhodecode.lib.rc_commands.ishell:main',
172 'rc-add-artifact=rhodecode.lib.rc_commands.add_artifact:main',
172 173 'rc-ssh-wrapper=rhodecode.apps.ssh_support.lib.ssh_wrapper:main',
173 174 ],
174 175 'beaker.backends': [
General Comments 0
You need to be logged in to leave comments. Login now