<s> from django . http import HttpResponse <EOL> from django . core . exceptions import ValidationError <EOL> from django . db import transaction <EOL> from decommission . decommission_utils import BadData , decommission_host <EOL> import MySQLdb <EOL> import simplejson as json <EOL> import reversion <EOL> def decommission_ ( main_blob , load_json = True ) : <EOL> try : <EOL> if load_json : <EOL> json_blob = json . loads ( main_blob ) <EOL> else : <EOL> json_blob = main_blob <EOL> except ValueError , e : <EOL> return None , { '<STR_LIT>' : str ( e ) } <EOL> try : <EOL> systems = json_blob [ '<STR_LIT>' ] <EOL> except ( KeyError , TypeError ) : <EOL> return None , { '<STR_LIT>' : '<STR_LIT>' } <EOL> options = json_blob . get ( '<STR_LIT>' , { } ) <EOL> opts = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True <EOL> } <EOL> opts . update ( options ) <EOL> commit = json_blob . get ( '<STR_LIT>' , False ) <EOL> comment = json_blob . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not isinstance ( systems , list ) : <EOL> return None , { '<STR_LIT>' : '<STR_LIT>' } <EOL> @ transaction . commit_manually <EOL> def do_decommission ( ) : <EOL> messages = [ ] <EOL> if reversion . revision_context_manager . is_active ( ) : <EOL> reversion . set_comment ( comment ) <EOL> try : <EOL> for i , hostname in enumerate ( systems ) : <EOL> messages += decommission_host ( hostname , opts , comment ) <EOL> except BadData , e : <EOL> transaction . rollback ( ) <EOL> return None , { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' . format ( hostname , e . msg ) <EOL> } <EOL> except ValidationError , e : <EOL> transaction . rollback ( ) <EOL> field_errors = '<STR_LIT>' <EOL> if hasattr ( e , '<STR_LIT>' ) : <EOL> for field , errors in e . message_dict . iteritems ( ) : <EOL> field_errors += "<STR_LIT>" . format ( field , '<STR_LIT:U+0020>' . join ( errors ) ) <EOL> else : <EOL> field_errors = '<STR_LIT:U+002CU+0020>' . join ( e . messages ) <EOL> transaction . rollback ( ) <EOL> return None , { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' . format ( hostname , field_errors ) <EOL> } <EOL> except MySQLdb . Warning , e : <EOL> transaction . rollback ( ) <EOL> return None , { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' . format ( hostname , e . message ) <EOL> } <EOL> except Exception , e : <EOL> transaction . rollback ( ) <EOL> return None , { <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( e ) , <EOL> } <EOL> else : <EOL> if commit : <EOL> transaction . commit ( ) <EOL> else : <EOL> transaction . rollback ( ) <EOL> json_blob [ '<STR_LIT>' ] = messages <EOL> return json_blob , None <EOL> return do_decommission ( ) <EOL> def decommission ( request ) : <EOL> raw_data = request . raw_post_data <EOL> if not raw_data : <EOL> return HttpResponse ( json . dumps ( { '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> systems , errors = decommission_ ( raw_data ) <EOL> return HttpResponse ( <EOL> json . dumps ( systems or errors ) , status = <NUM_LIT> if errors else <NUM_LIT:200> <EOL> ) </s>
