def show_zvol_more_info(nmc_cmd='/usr/bin/nmc -c', zvol_path=''): with settings( hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell='/usr/bin/bash --noprofile -lc', user='******', ): if zvol_path == '': ## Assuming that we are geting list for all ZVOLs result = fab_run('%s "%s"' % (nmc_cmd, 'show zvol -v')) ## If we succeed we just want to return result if result.succeeded: print result return True else: return False ## Selecting individual zvol by name, assuming zvol_p is not empty elif not zvol_path == '': result = fab_run('%s "%s %s"' % (nmc_cmd, 'show zvol -v', zvol_p)) ## If we succeed we just want to return result if result.succeeded: print result return True else: return False
def send_blueprint_script(src_path,target_path): with settings(show('warnings', 'running', 'stdout', 'stderr'), warn_only=False, shell='/bin/bash -lc', user='******', ): ## try/except wrapped check to validate existance of local file try: with open(src_path, 'rt') as f: f.readline() except IOError as e: print "I/O error [{0}] {1}: {2}".format(e.errno, e.strerror, e.filename) sys_exit(1) test_exist = fab_run('ls -l %s' % (target_path)) if test_exist.succeeded: replace_yesno = fab_prompt( 'File <{0}> already Exists. OK to Replace? [yes/no]'.format(target_path), default='no') if debug == True: print 'You said [{0}], exiting.'.format(replace_yesno) if 'yes' in replace_yesno.lower(): replace_yesno = True else: replace_yesno = False #ch = lambda x: 'yes' if x == True else 'no' sys_exit(0) test = fab_put(src_path,target_path,use_sudo=False, mirror_local_mode=False, mode=None) else: test = fab_run('[ -f %s ]' % (lsbdata))
def show_zvol_more_info (nmc_cmd='/usr/bin/nmc -c', zvol_path=''): with settings( hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell='/usr/bin/bash --noprofile -lc', user='******',): if zvol_path == '': ## Assuming that we are geting list for all ZVOLs result = fab_run('%s "%s"' % (nmc_cmd, 'show zvol -v')) ## If we succeed we just want to return result if result.succeeded: print result return True else: return False ## Selecting individual zvol by name, assuming zvol_p is not empty elif not zvol_path == '': result = fab_run('%s "%s %s"' % (nmc_cmd, 'show zvol -v',zvol_p)) ## If we succeed we just want to return result if result.succeeded: print result return True else: return False
def test(): """ Runs tests with nose """ settings = get_normalized_settings() engine = get_db_engine(settings) Base.metadata.create_all(bind=engine) fab_run("nosetests -vs")
def build_docs(): """ Builds Sphinx Docs html version, docs/build/html """ fab_run( 'sphinx-build -a %s/docs/source %s/docs/build/html' % ( PROJECT_ROOT, PROJECT_ROOT ) )
def validate_os(lsbdata): with settings(show('warnings', 'running', 'stdout', 'stderr'), warn_only=True, shell='/bin/bash -lc'): if debug == True: test = fab_local('[ -f %s ]' % (lsbdata), capture=True) else: test = fab_run('[ -f %s ]' % (lsbdata)) if test.succeeded: if debug == True: lsb_conts = fab_local("cat %s " % (lsbdata), capture=True) else: lsb_conts = fab_run("cat %s " % (lsbdata)) ## Convert string to a StringIO object, to ease converting ## to a dictionary ## Seems like extra work, but this is meant ## to ease future improvements to this code lsbIO = cStringIO.StringIO(lsb_conts) ## Build dictionary with dict. comprehension ## Stripping '\r' is only required when execuing remotely lsbdata_d = { k: v for k, v in [ a.strip('\n').strip('\r').split('=') for a in lsbIO.readlines() ] } if debug: print '<Debug Enabled> Flag Raised after building lsbdata_d Dict' if lsbdata_d['DISTRIB_ID'].lower() == 'ubuntu': if debug == True: result = fab_local( 'echo This is where we would execute our deploy.') if result.succeeded: #print 'Succeeded!' return True else: return False else: result = fab_run( 'echo This is where we would execute our deploy.', shell=True) if result.succeeded: #print 'Succeeded!' return True else: return False else: return False else: return False
def list_snapshot(nmc_cmd='/usr/bin/nmc -c', grep_cmd='/usr/bin/grep -v', dataset=''): with settings( hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell='/usr/bin/bash --noprofile -lc', user='******', ): if dataset == '': ## Assuming that we are geting list for all ZVOLs result = fab_run( '%s "%s"|%s %s' % (nmc_cmd, 'show snapshot -r -Screation', grep_cmd, '^NAME')) ## If we succeed we just want to return result if result.succeeded: ''' This may seem odd, but using cstringio seems to actually work very well in this particular instance ''' import cStringIO fi_obj = cStringIO.StringIO(result).readlines() final_snapshots = [] write_line(char='x') print '>> Task Succeeded: [%s] >> Total Snapshot Count: [%d]' % ( 'list_snapshot', len(fi_obj)) write_line(char='x') for line in fi_obj: for end in ['\n', '\r']: line = line.replace(end, '') line = line.split(' ') print line[0] final_snapshots.append(line[0]) #print final_snapshots return True else: return False ## Selecting individual zvol by name, assuming zvol_p is not empty elif not zvol_p == '': result = fab_run('%s "%s %s"' % (nmc_cmd, 'show zvol -v', zvol_p)) ## If we succeed we just want to return result if result.succeeded: print result return True else: return False
def list_zvol (zfs_cmd='/usr/sbin/zfs', tr_cmd='tr',nmc_cmd='nmc -c'): with settings( hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell='/usr/bin/bash --noprofile -lc', user='******',): result = fab_run('%s %s | %s %s' % (zfs_cmd, 'list -t volume -H -oname,used,avail,refer', tr_cmd, '"\t" " "')) if result.succeeded: import cStringIO fi_obj = cStringIO.StringIO(result).readlines() final_zvols = [] print '%s' % ('') write_line(char='x') print '>> Task Succeeded: [%s] >> Total ZVOL Count: [%d]' % ('show_zvol',len(fi_obj)) write_line(char='x') print '%s' % ('') for line in fi_obj: for end in ['\n','\r']: line = line.replace(end,'') line = line.split(' ') final_zvols.append(line) ofields = (line[0],line[1],line[2],line[3]) print 'Name: {0[0]:s}\nUsed: {0[1]:10s}Avail: {0[2]:10s} Refer: {0[3]:10s}'.format(ofields) write_line() return True else: return False
def do(cmd, capture=False, *args, **kwargs): """ Runs command locally or remotely depending on whether a remote host has been specified. """ if env.host_string or env.host or env.hosts: with cd(env.remote_path): if env.get('sudo', False): cmd = 'sudo %s' % cmd return fab_run(cmd, *args, **kwargs) else: # project root path is the default working directory path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) if env['lcwd']: # lcd has already been invoked. If it's with a relative path, let's # make that relative to the project root if not env['lcwd'].startswith('/'): path = '%s/%s' % (path, env['lcwd']) else: # Honour the current lcd contact if it's an absolute path path = env['lcwd'] with lcd(path): # Add shell envs for name, val in env.shell_env.iteritems(): cmd = 'export {name}="{val}" && {cmd}'.format(name=name, val=val, cmd=cmd) # Add command prefixes to local commands for prefix in env.command_prefixes: cmd = '{prefix} && {cmd}'.format(prefix=prefix, cmd=cmd) return local(cmd, *args, capture=capture, **kwargs)
def list_zvol_test (zfs_cmd='/usr/sbin/zfs', tr_cmd='tr',nmc_cmd='nmc -c'): with settings( hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell='/usr/bin/bash --noprofile -lc', user='******',): result = fab_run('%s %s | %s %s' % (zfs_cmd, 'list -t volume -H -oname,used,avail,refer', tr_cmd, '"\t" " "')) if result.succeeded: final_zvols = organize_dataset_list(result) #print final_zvols print '%s' % ('') write_line(char='x') print '>> Task Succeeded: [%s] >> Total ZVOL Count: [%d]' % ('show_zvol',len(final_zvols)) write_line(char='x') for line in final_zvols: ofields = (line[0],line[1],line[2],line[3]) print 'Name: {0[0]:s}\nUsed: {0[1]:10s}Avail: {0[2]:10s} Refer: {0[3]:10s}'.format(ofields) return True else: return False
def run(cmd): """Runs a nicely niced run command, or prints it if DEBUG is on.""" cmd = 'nice -n 19 %s' % cmd if DEBUG == False: return fab_run(cmd) else: print "run: %s" % cmd
def list_zvol_test(zfs_cmd='/usr/sbin/zfs', tr_cmd='tr', nmc_cmd='nmc -c'): with settings( hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell='/usr/bin/bash --noprofile -lc', user='******', ): result = fab_run('%s %s | %s %s' % (zfs_cmd, 'list -t volume -H -oname,used,avail,refer', tr_cmd, '"\t" " "')) if result.succeeded: final_zvols = organize_dataset_list(result) #print final_zvols print '%s' % ('') write_line(char='x') print '>> Task Succeeded: [%s] >> Total ZVOL Count: [%d]' % ( 'show_zvol', len(final_zvols)) write_line(char='x') for line in final_zvols: ofields = (line[0], line[1], line[2], line[3]) print 'Name: {0[0]:s}\nUsed: {0[1]:10s}Avail: {0[2]:10s} Refer: {0[3]:10s}'.format( ofields) return True else: return False
def list_zvol(zfs_cmd='/usr/sbin/zfs', tr_cmd='tr', nmc_cmd='nmc -c'): with settings( hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell='/usr/bin/bash --noprofile -lc', user='******', ): result = fab_run('%s %s | %s %s' % (zfs_cmd, 'list -t volume -H -oname,used,avail,refer', tr_cmd, '"\t" " "')) if result.succeeded: import cStringIO fi_obj = cStringIO.StringIO(result).readlines() final_zvols = [] print '%s' % ('') write_line(char='x') print '>> Task Succeeded: [%s] >> Total ZVOL Count: [%d]' % ( 'show_zvol', len(fi_obj)) write_line(char='x') print '%s' % ('') for line in fi_obj: for end in ['\n', '\r']: line = line.replace(end, '') line = line.split(' ') final_zvols.append(line) ofields = (line[0], line[1], line[2], line[3]) print 'Name: {0[0]:s}\nUsed: {0[1]:10s}Avail: {0[2]:10s} Refer: {0[3]:10s}'.format( ofields) write_line() return True else: return False
def validate_os(lsbdata): with settings(show('warnings', 'running', 'stdout', 'stderr'), warn_only=True,shell='/bin/bash -lc'): if debug == True: test = fab_local('[ -f %s ]' % (lsbdata), capture=True) else: test = fab_run('[ -f %s ]' % (lsbdata)) if test.succeeded: if debug == True: lsb_conts = fab_local("cat %s " % (lsbdata), capture=True) else: lsb_conts = fab_run("cat %s " % (lsbdata)) ## Convert string to a StringIO object, to ease converting ## to a dictionary ## Seems like extra work, but this is meant ## to ease future improvements to this code lsbIO = cStringIO.StringIO(lsb_conts) ## Build dictionary with dict. comprehension ## Stripping '\r' is only required when execuing remotely lsbdata_d = { k:v for k,v in [ a.strip('\n').strip('\r').split('=') for a in lsbIO.readlines() ] } if debug: print '<Debug Enabled> Flag Raised after building lsbdata_d Dict' if lsbdata_d['DISTRIB_ID'].lower() == 'ubuntu': if debug == True: result = fab_local('echo This is where we would execute our deploy.') if result.succeeded: #print 'Succeeded!' return True else: return False else: result = fab_run('echo This is where we would execute our deploy.',shell=True) if result.succeeded: #print 'Succeeded!' return True else: return False else: return False else: return False
def list_snapshot (nmc_cmd='/usr/bin/nmc -c', grep_cmd='/usr/bin/grep -v', dataset=''): with settings( hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell='/usr/bin/bash --noprofile -lc', user='******',): if dataset == '': ## Assuming that we are geting list for all ZVOLs result = fab_run('%s "%s"|%s %s' % (nmc_cmd, 'show snapshot -r -Screation', grep_cmd, '^NAME')) ## If we succeed we just want to return result if result.succeeded: ''' This may seem odd, but using cstringio seems to actually work very well in this particular instance ''' import cStringIO fi_obj = cStringIO.StringIO(result).readlines() final_snapshots = [] write_line(char='x') print '>> Task Succeeded: [%s] >> Total Snapshot Count: [%d]' % ('list_snapshot',len(fi_obj)) write_line(char='x') for line in fi_obj: for end in ['\n','\r']: line = line.replace(end,'') line = line.split(' ') print line[0] final_snapshots.append(line[0]) #print final_snapshots return True else: return False ## Selecting individual zvol by name, assuming zvol_p is not empty elif not zvol_p == '': result = fab_run('%s "%s %s"' % (nmc_cmd, 'show zvol -v',zvol_p)) ## If we succeed we just want to return result if result.succeeded: print result return True else: return False
def do(*args, **kwargs): """ Runs command locally or remotely depending on whether a remote host has been specified. """ if env.host_string: with settings(cd(config.remote_path)): return fab_run(*args, **kwargs) else: return local(*args, **kwargs)
def show_volume_details(cmd='/usr/sbin/zpool status -v'): with settings(hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell='/usr/bin/bash --noprofile -lc', user='******'): zpools = get_volume() for i in zpools: print i capture = fab_run('%s %s' % (cmd, i)) print capture
def show_volume_details (cmd='/usr/sbin/zpool status -v'): with settings( hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell='/usr/bin/bash --noprofile -lc', user='******'): zpools = get_volume() for i in zpools: print i capture = fab_run('%s %s' % (cmd, i)) print capture
def send_blueprint_script(src_path, target_path): with settings( show('warnings', 'running', 'stdout', 'stderr'), warn_only=False, shell='/bin/bash -lc', user='******', ): ## try/except wrapped check to validate existance of local file try: with open(src_path, 'rt') as f: f.readline() except IOError as e: print "I/O error [{0}] {1}: {2}".format(e.errno, e.strerror, e.filename) sys_exit(1) test_exist = fab_run('ls -l %s' % (target_path)) if test_exist.succeeded: replace_yesno = fab_prompt( 'File <{0}> already Exists. OK to Replace? [yes/no]'.format( target_path), default='no') if debug == True: print 'You said [{0}], exiting.'.format(replace_yesno) if 'yes' in replace_yesno.lower(): replace_yesno = True else: replace_yesno = False #ch = lambda x: 'yes' if x == True else 'no' sys_exit(0) test = fab_put(src_path, target_path, use_sudo=False, mirror_local_mode=False, mode=None) else: test = fab_run('[ -f %s ]' % (lsbdata))
def get_volume(zpool_cmd='/usr/sbin/zpool list -H -oname', vol=''): with settings(hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell=default_shell, user='******'): ## If there is no value for 'val', result will be a list, ## because we assume more than one volume/pool on system if not vol: result = fab_run('%s' % (zpool_cmd)).split('\n') else: result = fab_run('%s %s' % (zpool_cmd, vol)) if result.succeeded: #print result return result result = [i.strip('\r') for i in result] #print result return result else: return False
def get_volume (zpool_cmd='/usr/sbin/zpool list -H -oname',vol=''): with settings( hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell=default_shell, user='******'): ## If there is no value for 'val', result will be a list, ## because we assume more than one volume/pool on system if not vol: result = fab_run('%s' % (zpool_cmd)).split('\n') else: result = fab_run('%s %s' % (zpool_cmd, vol)) if result.succeeded: #print result return result result = [i.strip('\r') for i in result] #print result return result else: return False
def show_space_usage(zfs_cmd='/usr/sbin/zfs', tr_cmd='/usr/bin/tr', nmc_cmd='/usr/bin/nmc -c', tr='tr \'[[:blank:]]\' \' \'', dataset=''): with settings( hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell=default_shell, user='******', ): ''' Testing to see if dataset is name of pool only, or name of pool including child dataset ''' if not dataset == dataset.split('/'): ## is '/' in the name validate = get_volume(vol=dataset.split('/')[0]) else: validate = get_volume(vol=dataset) ''' Next, we need to confirm that pool name is valid and exists on the target SAN ''' if validate == False: fab_abort( 'Invalid Volume Name {0}, Please check name.'.format(dataset)) print 'FAILED!!!' else: result = fab_run('%s %s|%s' % (zfs_spa_cmd, dataset, tr)) if result.succeeded: final_space_usage = organize_dataset_list(result) #print final_zvols print '%s' % ('') write_line(char='x') print '>> Task Succeeded: [%s] >> Total Dataset Count: [%d]' % \ ('show_zvol',len(final_space_usage)) write_line(char='x') for line in final_space_usage: ofields = (line[0], line[1], line[2], line[3], line[4], line[5], line[6]) print show_space_usage_fmt.format(ofields) return True else: return False
def show_space_usage (zfs_cmd='/usr/sbin/zfs', tr_cmd='/usr/bin/tr', nmc_cmd='/usr/bin/nmc -c', tr = 'tr \'[[:blank:]]\' \' \'', dataset=''): with settings( hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell=default_shell, user='******',): ''' Testing to see if dataset is name of pool only, or name of pool including child dataset ''' if not dataset == dataset.split('/'): ## is '/' in the name validate = get_volume(vol=dataset.split('/')[0]) else: validate = get_volume(vol=dataset) ''' Next, we need to confirm that pool name is valid and exists on the target SAN ''' if validate == False: fab_abort('Invalid Volume Name {0}, Please check name.'.format(dataset)) print 'FAILED!!!' else: result = fab_run('%s %s|%s' % (zfs_spa_cmd, dataset, tr)) if result.succeeded: final_space_usage = organize_dataset_list(result) #print final_zvols print '%s' % ('') write_line(char='x') print '>> Task Succeeded: [%s] >> Total Dataset Count: [%d]' % \ ('show_zvol',len(final_space_usage)) write_line(char='x') for line in final_space_usage: ofields = (line[0],line[1],line[2],line[3], line[4],line[5],line[6]) print show_space_usage_fmt.format(ofields) return True else: return False
def init_db(dump=None): """Initialize ecomap_db database. If you set the dump configuration like fab init_db:from_dump, you will receive the data from dump. Otherwise you will create a new db without data. """ fab_run('sudo -u postgres psql -f init_ecomap_db.sql;' 'export PYTHONPATH=":/ecomap"') if dump == 'from_dump': path_to_dal = '/ecomap/api/dal' fab_run( 'sudo -u postgres psql ecomap_db < %s/dumps/ecomap_db_dump.sql;' % path_to_dal) else: Base.metadata.create_all(get_db_engine(settings)) fab_run('sudo -u postgres psql -f /ecomap/api/dal/view.sql;')
def do(cmd, capture=False, *args, **kwargs): """ Runs command locally or remotely depending on whether a remote host has been specified. """ if env.host_string: with cd(env.remote_path): return fab_run(cmd, *args, **kwargs) else: # project root path is the default working directory path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) if env['lcwd']: # lcd has already been invoked. If it's with a relative path, let's # make that relative to the project root if not env['lcwd'].startswith('/'): path = '%s/%s' % (path, env['lcwd']) else: # Honour the current lcd contact if it's an absolute path path = env['lcwd'] with lcd(path): return local(cmd, *args, capture=capture, **kwargs)
def get_checkpoints(nmc_cmd='nmc -c', domain="whatever.com"): with settings(hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell='/usr/bin/bash --noprofile -lc', user='******'): #fab_local('echo Env: {0} and Dom: {1}'.format('moo','yaya.com')) capture = fab_run('%s %s' % (nmc_cmd, '\"show appliance checkpoint\"')) if capture.succeeded: for line in capture.split('\n'): pattern = re.compile(r'^rootfs', re.IGNORECASE) match = re.match(pattern, line) ## Match the heading of available checkpoints list, ## and decorate with '^' if match and match.group() == match.group().upper(): print '%s' % ('^' * 80) print line print '%s' % ('^' * 80) elif match: print line else: pass
def get_checkpoints(nmc_cmd='nmc -c', domain="whatever.com"): with settings( hide('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false', shell='/usr/bin/bash --noprofile -lc', user='******'): #fab_local('echo Env: {0} and Dom: {1}'.format('moo','yaya.com')) capture = fab_run('%s %s' % (nmc_cmd, '\"show appliance checkpoint\"')) if capture.succeeded: for line in capture.split('\n'): pattern = re.compile(r'^rootfs', re.IGNORECASE) match = re.match(pattern, line) ## Match the heading of available checkpoints list, ## and decorate with '^' if match and match.group() == match.group().upper(): print '%s' % ('^' * 80) print line print '%s' % ('^' * 80) elif match: print line else: pass
def run(): """ Runs default App """ fab_run(os.path.join(PROJECT_ROOT, 'app.py'))
def test(): """ Runs tests with nose """ fab_run('nosetests -vs')
def clean_pyc(): """ Removes *.pyc files """ fab_run('find . -name "*.pyc" -delete')
def populate_db(problems_count): """Populate database with fake data.""" fab_run('export PYTHONPATH=":/ecomap"') fab_run('python factories.py %s' % problems_count)
def runtests(): """Runs runtests.py with buffer and failfast options.""" fab_run('python -m api.v1_0.tests.runtests --buffer --failfast')
def build_docs(): """ Builds Sphinx Docs html version, docs/build/html """ fab_run('sphinx-build -a %s/docs/source %s/docs/build/html' % (PROJECT_ROOT, PROJECT_ROOT))
def run_local(command, shell=True, pty=True, combine_stderr=True, capture=False, warn_only=False): """ run/local function based on host """ if is_local(): return fab_local(command, capture=capture) else: return fab_run(command, shell=shell, pty=pty, combine_stderr=combine_stderr, warn_only=warn_only)
def upgrade(): """ Upgrades reqs.pip dependencies """ reqs = os.path.join(PROJECT_ROOT, "reqs.pip") fab_run("pip install -r %s --upgrade" % reqs)
def run(): """ Runs default App """ fab_run(os.path.join(PROJECT_ROOT, "app.py"))
def upgrade(): """ Upgrades reqs.pip dependencies """ reqs = os.path.join(PROJECT_ROOT, 'reqs.pip') fab_run('pip install -r %s --upgrade' % reqs)