22distutils commands for riak-python-client
33"""
44
5- __all__ = ['create_bucket_types' , 'setup_security' , 'preconfig_security ' ,
6- 'setup_tests ' ]
5+ __all__ = ['create_bucket_types' , 'setup_security' , 'enable_security ' ,
6+ 'disable_security' , 'preconfigure' , 'configure ' ]
77
88from distutils import log
99from distutils .core import Command
@@ -153,7 +153,32 @@ def _btype_command(self, *args):
153153 return cmd
154154
155155
156- class setup_security (Command ):
156+ class security_commands :
157+ def check_security_command (self , * args ):
158+ cmd = self ._security_command (* args )
159+ return self .check_output (cmd )
160+
161+ def run_security_command (self , * args ):
162+ self .spawn (self ._security_command (* args ))
163+
164+ def _security_command (self , * args ):
165+ cmd = [self .riak_admin , "security" ]
166+ if isinstance (args , tuple ):
167+ for elem in args :
168+ cmd .extend (elem )
169+ else :
170+ cmd .extend (args )
171+ return cmd
172+
173+ def check_output (self , * args , ** kwargs ):
174+ if self .dry_run :
175+ log .info (' ' .join (args ))
176+ return bytearray ()
177+ else :
178+ return check_output (* args , ** kwargs )
179+
180+
181+ class setup_security (Command , security_commands ):
157182 """
158183 Sets up security for testing. By default this will create:
159184
@@ -187,7 +212,6 @@ class setup_security(Command):
187212 ]
188213
189214 _commands = [
190- "enable" ,
191215 "add-user $USERNAME password=$PASSWORD" ,
192216 "add-source $USERNAME 127.0.0.1/32 password" ,
193217 "add-user $CERTUSER password=$CERTPASS" ,
@@ -243,12 +267,6 @@ def run(self):
243267 for perm in self ._grants :
244268 self ._apply_grant (perm , self ._grants [perm ])
245269
246- def check_output (self , * args , ** kwargs ):
247- if self .dry_run :
248- log .info (' ' .join (args ))
249- return bytearray ()
250- else :
251- return check_output (* args , ** kwargs )
252270
253271 def _check_available (self ):
254272 try :
@@ -269,33 +287,61 @@ def _apply_grant(self, perm, targets):
269287 .format (perm , target , self .certuser ))
270288 self .run_security_command (cmd )
271289
272- def check_security_command (self , * args ):
273- cmd = self ._security_command (* args )
274- return self .check_output (cmd )
275290
276- def run_security_command (self , * args ):
277- self .spawn (self ._security_command (* args ))
291+ class enable_security (Command ,security_commands ):
292+ """
293+ Actually turn on security.
294+ """
295+ description = "turn on security within Riak"
278296
279- def _security_command (self , * args ):
280- cmd = [self .riak_admin , "security" ]
281- if isinstance (args , tuple ):
282- for elem in args :
283- cmd .extend (elem )
284- else :
285- cmd .extend (args )
286- return cmd
297+ user_options = [
298+ ('riak-admin=' , None , 'path to the riak-admin script' ),
299+ ]
300+
301+ def initialize_options (self ):
302+ self .riak_admin = None
303+
304+ def finalize_options (self ):
305+ if self .riak_admin is None :
306+ raise DistutilsOptionError ("riak-admin option not set" )
307+
308+ def run (self ):
309+ cmd = "enable"
310+ self .run_security_command (tuple (cmd .split (' ' )))
287311
288312
289- class preconfig_security (Command ):
313+ class disable_security (Command ,security_commands ):
314+ """
315+ Actually turn off security.
316+ """
317+ description = "turn off security within Riak"
318+
319+ user_options = [
320+ ('riak-admin=' , None , 'path to the riak-admin script' ),
321+ ]
322+
323+ def initialize_options (self ):
324+ self .riak_admin = None
325+
326+ def finalize_options (self ):
327+ if self .riak_admin is None :
328+ raise DistutilsOptionError ("riak-admin option not set" )
329+
330+ def run (self ):
331+ cmd = "disable"
332+ self .run_security_command (tuple (cmd .split (' ' )))
333+
334+
335+ class preconfigure (Command ):
290336 """
291337 Sets up security configuration.
292338
293339 * Update these lines in riak.conf
294340 * storage_backend = leveldb
295341 * search = on
296342 * listener.protobuf.internal = 127.0.0.1:8087
297- * listener.https .internal = 127.0.0.1:8098
298- * ## listener.http .internal = 127.0.0.1:8098
343+ * listener.http .internal = 127.0.0.1:8098
344+ * listener.https .internal = 127.0.0.1:8099
299345 * ssl.certfile = $pwd/tests/resources/server.crt
300346 * ssl.keyfile = $pwd/tests/resources/server.key
301347 * ssl.cacertfile = $pwd/tests/resources/ca.crt
@@ -314,6 +360,7 @@ def initialize_options(self):
314360 self .riak_conf = None
315361 self .host = "127.0.0.1"
316362 self .pb_port = "8087"
363+ self .http_port = "8098"
317364 self .https_port = "8099"
318365
319366 def finalize_options (self ):
@@ -326,6 +373,7 @@ def run(self):
326373 self ._update_riak_conf ()
327374
328375 def _update_riak_conf (self ):
376+ http_host = self .host + ':' + self .http_port
329377 https_host = self .host + ':' + self .https_port
330378 pb_host = self .host + ':' + self .pb_port
331379 self ._backup_file (self .riak_conf )
@@ -347,6 +395,9 @@ def _update_riak_conf(self):
347395 r'ssl.cacertfile = ' + self .cert_dir +
348396 '/ca.crt' ,
349397 conf , flags = re .MULTILINE )
398+ conf = re .sub (r'^#*\s*listener.http.internal\s+=\s+\S+' ,
399+ r'listener.http.internal = ' + http_host ,
400+ conf , flags = re .MULTILINE )
350401 conf = re .sub (r'^#*\s*listener.https.internal\s+=\s+\S+' ,
351402 r'listener.https.internal = ' + https_host ,
352403 conf , flags = re .MULTILINE )
@@ -365,7 +416,7 @@ def _backup_file(self, name):
365416 log .info ("Cannot backup missing file {!r}" .format (name ))
366417
367418
368- class setup_tests (Command ):
419+ class configure (Command ):
369420 """
370421 Sets up security configuration.
371422
0 commit comments