254.1.1
by David Goetz
changing /usr/bin/python to /usr/bin/env python |
1 |
#!/usr/bin/env python
|
140.3.1
by gholt
Incorporated Swauth into Swift as an optional DevAuth replacement. |
2 |
# Copyright (c) 2010 OpenStack, LLC.
|
3 |
#
|
|
4 |
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 |
# you may not use this file except in compliance with the License.
|
|
6 |
# You may obtain a copy of the License at
|
|
7 |
#
|
|
8 |
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9 |
#
|
|
10 |
# Unless required by applicable law or agreed to in writing, software
|
|
11 |
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
13 |
# implied.
|
|
14 |
# See the License for the specific language governing permissions and
|
|
15 |
# limitations under the License.
|
|
16 |
||
140.3.14
by gholt
Merge from trunk (i18n) |
17 |
import gettext |
140.3.1
by gholt
Incorporated Swauth into Swift as an optional DevAuth replacement. |
18 |
from optparse import OptionParser |
19 |
from os.path import basename |
|
20 |
from sys import argv, exit |
|
21 |
||
22 |
from swift.common.bufferedhttp import http_connect_raw as http_connect |
|
173.6.1
by Michael Barton
ipv6 support |
23 |
from swift.common.utils import urlparse |
140.3.1
by gholt
Incorporated Swauth into Swift as an optional DevAuth replacement. |
24 |
|
25 |
||
26 |
if __name__ == '__main__': |
|
140.3.14
by gholt
Merge from trunk (i18n) |
27 |
gettext.install('swift', unicode=1) |
140.3.1
by gholt
Incorporated Swauth into Swift as an optional DevAuth replacement. |
28 |
parser = OptionParser(usage='Usage: %prog [options]') |
29 |
parser.add_option('-A', '--admin-url', dest='admin_url', |
|
30 |
default='http://127.0.0.1:8080/auth/', help='The URL to the auth ' |
|
31 |
'subsystem (default: http://127.0.0.1:8080/auth/') |
|
32 |
parser.add_option('-U', '--admin-user', dest='admin_user', |
|
33 |
default='.super_admin', help='The user with admin rights to add users ' |
|
34 |
'(default: .super_admin).') |
|
35 |
parser.add_option('-K', '--admin-key', dest='admin_key', |
|
36 |
help='The key for the user with admin rights to add users.') |
|
37 |
args = argv[1:] |
|
38 |
if not args: |
|
39 |
args.append('-h') |
|
40 |
(options, args) = parser.parse_args(args) |
|
41 |
if args: |
|
42 |
parser.parse_args(['-h']) |
|
43 |
parsed = urlparse(options.admin_url) |
|
44 |
if parsed.scheme not in ('http', 'https'): |
|
45 |
raise Exception('Cannot handle protocol scheme %s for url %s' % |
|
46 |
(parsed.scheme, repr(options.admin_url))) |
|
240.1.1
by gholt
Fix for incorrect use of urlparse results in swauth bins |
47 |
parsed_path = parsed.path |
48 |
if not parsed_path: |
|
49 |
parsed_path = '/' |
|
50 |
elif parsed_path[-1] != '/': |
|
51 |
parsed_path += '/' |
|
52 |
path = '%sv2/.prep' % parsed_path |
|
140.3.1
by gholt
Incorporated Swauth into Swift as an optional DevAuth replacement. |
53 |
headers = {'X-Auth-Admin-User': options.admin_user, |
54 |
'X-Auth-Admin-Key': options.admin_key} |
|
55 |
conn = http_connect(parsed.hostname, parsed.port, 'POST', path, headers, |
|
56 |
ssl=(parsed.scheme == 'https')) |
|
57 |
resp = conn.getresponse() |
|
58 |
if resp.status // 100 != 2: |
|
278.1.1
by Greg Lange
swauth scripts exit with non zero exit codes on failure now |
59 |
exit('Auth subsystem prep failed: %s %s' % (resp.status, resp.reason)) |