This repository has been archived by the owner on Aug 27, 2023. It is now read-only.
/
scripts.py
246 lines (195 loc) · 7.27 KB
/
scripts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
""" Commandline scripts """
import gzip
import transaction
import json
import sys
import argparse
import getpass
from base64 import b64encode
from jinja2 import Template
from pkg_resources import resource_string
from pyramid.paster import bootstrap
import os
from pypicloud.access import pwd_context
def gen_password():
""" Generate a salted password """
print _gen_password()
def _gen_password():
""" Prompt user for a password twice for safety """
while True:
password = getpass.getpass()
verify = getpass.getpass()
if password == verify:
return pwd_context.encrypt(password)
else:
print "Passwords do not match!"
NO_DEFAULT = object()
def prompt(msg, default=NO_DEFAULT, validate=None):
""" Prompt user for input """
while True:
response = raw_input(msg + ' ').strip()
if not response:
if default is NO_DEFAULT:
continue
return default
if validate is None or validate(response):
return response
def prompt_option(text, choices, default=NO_DEFAULT):
""" Prompt the user to choose one of a list of options """
while True:
for i, msg in enumerate(choices):
print "[%d] %s" % (i + 1, msg)
response = prompt(text, default=default)
try:
idx = int(response) - 1
return choices[idx]
except (ValueError, IndexError):
print "Invalid choice\n"
def promptyn(msg, default=None):
""" Display a blocking prompt until the user confirms """
while True:
yes = "Y" if default else "y"
if default or default is None:
no = "n"
else:
no = "N"
confirm = prompt("%s [%s/%s]" % (msg, yes, no), '').lower()
if confirm in ('y', 'yes'):
return True
elif confirm in ('n', 'no'):
return False
elif len(confirm) == 0 and default is not None:
return default
def make_config(argv=None):
""" Create a server config file """
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(description=make_config.__doc__)
group = parser.add_mutually_exclusive_group()
group.add_argument('-d', action='store_true',
help="Create config file for development")
group.add_argument('-t', action='store_true',
help="Create config file for testing")
group.add_argument('-p', action='store_true',
help="Create config file for production")
parser.add_argument('outfile', nargs='?', default="config.ini",
help="Name of output file (default %(default)s)")
args = parser.parse_args(argv)
if os.path.exists(args.outfile):
msg = "'%s' already exists. Overwrite?" % args.outfile
if not promptyn(msg, False):
return
if args.d:
env = 'dev'
elif args.t:
env = 'test'
elif args.p:
env = 'prod'
else:
env = prompt_option("What is this config file for?",
['dev', 'test', 'prod'])
data = {
'env': env,
}
data['reload_templates'] = env == 'dev'
storage = prompt_option("Where do you want to store your packages?",
['s3', 'filesystem'])
if storage == 'filesystem':
storage = 'file'
data['storage'] = storage
if storage == 's3':
if 'AWS_ACCESS_KEY_ID' in os.environ:
data['access_key'] = os.environ['AWS_ACCESS_KEY_ID']
else:
data['access_key'] = prompt("AWS access key id?")
if 'AWS_SECRET_ACCESS_KEY' in os.environ:
data['secret_key'] = os.environ['AWS_SECRET_ACCESS_KEY']
else:
data['secret_key'] = prompt("AWS secret access key?")
def bucket_validate(name):
""" Check for valid bucket name """
if '.' in name:
print "Bucket names cannot contain '.'"
return False
return True
data['s3_bucket'] = prompt("S3 bucket name?", validate=bucket_validate)
data['encrypt_key'] = b64encode(os.urandom(32))
data['validate_key'] = b64encode(os.urandom(32))
data['admin'] = prompt("Admin username?")
data['password'] = _gen_password()
data['session_secure'] = env == 'prod'
data['env'] = env
if env == 'dev' or env == 'test':
data['wsgi'] = 'waitress'
elif env == 'prod':
if hasattr(sys, 'real_prefix'):
data['venv'] = sys.prefix
data['wsgi'] = 'uwsgi'
tmpl_str = resource_string('pypicloud', 'templates/config.ini.jinja2')
template = Template(tmpl_str)
with open(args.outfile, 'w') as ofile:
ofile.write(template.render(**data))
print "Config file written to '%s'" % args.outfile
def migrate_packages(argv=None):
"""
Migrate packages from one storage backend to another
Create two config.ini files that are configured to use different storage
backends. All packages will be migrated from the storage backend in the
first to the storage backend in the second.
ex: pypicloud-migrate-packages file_config.ini s3_config.ini
"""
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(
description=migrate_packages.__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('config_from',
help="Name of config file to migrate from")
parser.add_argument('config_to',
help="Name of config file to migrate to")
args = parser.parse_args(argv)
old_env = bootstrap(args.config_from)
old_storage = old_env['request'].db.storage
all_packages = old_storage.list()
new_env = bootstrap(args.config_to)
new_storage = new_env['request'].db.storage
for package in all_packages:
print "Migrating %s" % package
with old_storage.open(package) as data:
new_storage.upload(package, data)
def export_access(argv=None):
""" Dump the access control data to a universal format """
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(description=export_access.__doc__)
parser.add_argument('config', help="Name of config file")
parser.add_argument('-o', help="Name of output file")
args = parser.parse_args(argv)
env = bootstrap(args.config)
access = env['request'].access
data = access.dump()
if args.o:
with gzip.open(args.o, 'w') as ofile:
json.dump(data, ofile)
else:
print json.dumps(data, indent=2)
def import_access(argv=None):
""" Load the access control data from a dump file or stdin """
if argv is None:
argv = sys.argv[1:]
parser = argparse.ArgumentParser(description=import_access.__doc__)
parser.add_argument('config', help="Name of config file")
parser.add_argument('-i', help="Name of input file")
args = parser.parse_args(argv)
if args.i:
with gzip.open(args.i, 'r') as ifile:
data = json.load(ifile)
else:
print "Reading data from stdin..."
data = json.load(sys.stdin)
env = bootstrap(args.config)
access = env['request'].access
result = access.load(data)
transaction.commit()
if result is not None:
print result