<s> from __future__ import absolute_import <EOL> from __future__ import division <EOL> from __future__ import print_function <EOL> '''<STR_LIT>''' <EOL> from datetime import datetime <EOL> import os <EOL> import copy <EOL> from pprint import pprint <EOL> CREATE_USER = """<STR_LIT>""" <EOL> CREATE_DB = """<STR_LIT>""" <EOL> BACKUP = """<STR_LIT>""" <EOL> RESTORE = """<STR_LIT>""" <EOL> DROP_DB = '''<STR_LIT>''' <EOL> HAS_PG_RUNNING = '''<STR_LIT>''' <EOL> def run ( host , script , * args , ** kwargs ) : <EOL> if host in [ '<STR_LIT:localhost>' , '<STR_LIT:127.0.0.1>' ] : <EOL> kw = copy . deepcopy ( kwargs ) <EOL> kw [ '<STR_LIT>' ] = True <EOL> return __salt__ [ '<STR_LIT>' ] ( script , * args , ** kw ) <EOL> else : <EOL> return __salt__ [ '<STR_LIT>' ] ( host , script , * args , ** kwargs ) <EOL> def test_pg ( db_host = None , cfg = '<STR_LIT>' ) : <EOL> if not db_host : <EOL> data = __salt__ [ '<STR_LIT>' ] ( cfg ) <EOL> db = data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT:default>' ] <EOL> db_host = db [ '<STR_LIT>' ] <EOL> ret = run ( db_host , HAS_PG_RUNNING ) <EOL> if ret [ '<STR_LIT>' ] : <EOL> pprint ( ret ) <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def setup_database ( db_host = None , <EOL> db_user = None , <EOL> db_pass = None , <EOL> db_name = None , <EOL> drop = False , <EOL> cfg = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> data = __salt__ [ '<STR_LIT>' ] ( cfg ) <EOL> db = data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT:default>' ] <EOL> if not db_host : <EOL> db_host = db [ '<STR_LIT>' ] <EOL> if not db_user : <EOL> db_user = db [ '<STR_LIT>' ] <EOL> if not db_pass : <EOL> db_pass = db [ '<STR_LIT>' ] <EOL> if not db_name : <EOL> db_name = db [ '<STR_LIT>' ] <EOL> script = CREATE_USER <EOL> if drop : <EOL> script += DROP_DB <EOL> script += CREATE_DB <EOL> script = script . format ( ** locals ( ) ) <EOL> ret = run ( host = db_host , script = script ) <EOL> if ret [ '<STR_LIT>' ] : <EOL> pprint ( ret ) <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def backup_database ( db_host = None , db_name = None , cfg = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> data = __salt__ [ '<STR_LIT>' ] ( cfg ) <EOL> db = data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT:default>' ] <EOL> if not db_host : <EOL> db_host = db [ '<STR_LIT>' ] <EOL> if not db_name : <EOL> db_name = db [ '<STR_LIT>' ] <EOL> dump_filename = '<STR_LIT>' . format ( <EOL> db_name , <EOL> datetime . now ( ) . strftime ( '<STR_LIT>' ) ) <EOL> script = BACKUP . format ( ** locals ( ) ) <EOL> script += "<STR_LIT>" <EOL> ret = run ( host = db_host , script = script ) <EOL> if ret [ '<STR_LIT>' ] : <EOL> pprint ( ret ) <EOL> raise Exception ( '<STR_LIT>' ) <EOL> return dump_filename <EOL> def restore_from ( db_host = None , <EOL> db_user = None , <EOL> db_pass = None , <EOL> db_name = None , <EOL> media = None , <EOL> dump = None , <EOL> orig_db_host = None , <EOL> orig_db_name = None , <EOL> orig_media = None , <EOL> orig_media_host = None , <EOL> skip_setup = False , <EOL> skip_db = False , <EOL> skip_media = False , <EOL> dev = None , <EOL> drop = True , <EOL> post_hook = None , <EOL> cfg = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> data = __salt__ [ '<STR_LIT>' ] ( cfg ) <EOL> if dev is None : <EOL> if data [ '<STR_LIT>' ] in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> dev = True <EOL> if post_hook and post_hook not in __salt__ : <EOL> raise Exception ( '<STR_LIT>' . format ( post_hook ) ) <EOL> if not orig_media : <EOL> orig_media = data [ '<STR_LIT:data>' ] . get ( '<STR_LIT>' ) <EOL> if not media : <EOL> media = data [ '<STR_LIT:data>' ] . get ( '<STR_LIT>' ) <EOL> if not orig_db_name : <EOL> orig_db_name = data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] <EOL> if not orig_media_host : <EOL> orig_media_host = data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] <EOL> if not orig_db_host : <EOL> orig_db_host = data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] <EOL> db = data [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT:default>' ] <EOL> if not db_host : <EOL> db_host = db [ '<STR_LIT>' ] <EOL> if not db_user : <EOL> db_user = db [ '<STR_LIT>' ] <EOL> if not db_pass : <EOL> db_pass = db [ '<STR_LIT>' ] <EOL> if not db_name : <EOL> db_name = db [ '<STR_LIT>' ] <EOL> if not skip_db : <EOL> dodump = backup_database ( db_host = db_host , db_name = db_name , cfg = cfg ) <EOL> if not dump : <EOL> try : <EOL> dump = backup_database ( db_host = orig_db_host , <EOL> db_name = orig_db_name , cfg = cfg ) <EOL> script = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( ** locals ( ) ) <EOL> ret = __salt__ [ '<STR_LIT>' ] ( script ) <EOL> if ret [ '<STR_LIT>' ] : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> finally : <EOL> ormret = run ( <EOL> host = orig_db_host , <EOL> script = "<STR_LIT>" . format ( ** locals ( ) ) ) <EOL> if not skip_setup : <EOL> setup_database ( db_host , <EOL> db_user = db_user , <EOL> db_pass = db_pass , <EOL> db_name = db_name , <EOL> drop = drop , <EOL> cfg = cfg ) <EOL> script = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( ** locals ( ) ) <EOL> ret = __salt__ [ '<STR_LIT>' ] ( script ) <EOL> if ret [ '<STR_LIT>' ] : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> try : <EOL> script = RESTORE . format ( ** locals ( ) ) <EOL> ret = run ( <EOL> host = db_host , script = script ) <EOL> if ret [ '<STR_LIT>' ] : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> finally : <EOL> rmret = run ( <EOL> host = db_host , <EOL> script = "<STR_LIT>" . format ( ** locals ( ) ) ) <EOL> if media and orig_media and not skip_media : <EOL> script = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( ** locals ( ) ) <EOL> ret = __salt__ [ '<STR_LIT>' ] ( script , use_vt = True ) <EOL> if ret [ '<STR_LIT>' ] : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if post_hook : <EOL> __salt__ [ post_hook ] ( <EOL> db_host = db_host , <EOL> db_user = db_user , <EOL> db_pass = db_pass , <EOL> db_name = db_name , <EOL> media = media , <EOL> dump = dump , <EOL> orig_db_host = orig_db_host , <EOL> orig_db_name = orig_db_name , <EOL> orig_media = orig_media , <EOL> orig_media_host = orig_media_host , <EOL> skip_setup = skip_setup , <EOL> skip_db = skip_db , <EOL> skip_media = skip_media , <EOL> dev = dev , <EOL> drop = drop , <EOL> post_hook = post_hook , <EOL> cfg = cfg , <EOL> data = data ) </s>
