def run(self): with settings(show('warnings', 'running', 'stdout', 'stderr'), warn_only=True): fab_lcd(dir_p) result = fab_local('ls -l', capture=True) print 'Current Environment :: %s' % (env) print result
def actual_task(dir_p='/',env='lab'): print '%s' % ('Testing, testing...') print dir_p with settings(show('warnings', 'running', 'stdout', 'stderr'), warn_only=True): for a in dir_p: print a with fab_lcd(a): result = fab_local('ls -l', capture=True) print 'Current Environment :: %s' % (env) #print result result = ''
#!/usr/bin/env python ## Switching into a directory and executing 'ls' #from fabric.api import cd as fab_cd from fabric.api import lcd as fab_lcd from fabric.api import local as fab_local from __future__ import with_statement with fab_lcd('/tmp'): files = [ a for a in fab_local('ls',capture=True).split('\n') ] ## All things we can import from fabric.api # ['abort', 'cd', 'env', 'fastprint', 'get', 'hide', 'hosts', 'lcd', # 'local', 'open_shell', 'output', 'path', 'prefix', 'prompt', # 'put', 'puts', 'reboot', 'require', 'roles', 'run', 'runs_once', # 'settings', 'show', 'sudo', 'task', 'warn', 'with_settings'] ## All things we can import from fabric.context_managers # ['_change_cwd', '_set_output', '_setenv', 'cd', 'char_buffered', # 'contextmanager', 'env', 'hide', 'lcd', 'nested', 'output', 'path', # 'prefix', 'settings', 'show', 'sys', 'termios', 'tty', 'win32'] hosts_entry = {'host_a':'10.10.1.101','host_b':'10.10.1.102'} with fab_lcd('/tmp'): for k,v in hosts_entry.iteritems(): print '%s\t\t %s' % (k, v) #for k,v in izip(hosts_entry.keys(),hosts_entry.values()): #print '%s %-10s' % (k, v) ## Template to write entries into hosts files
def configure(): graph_conf_path='/opt/graphite/conf' graph_stor_path = '/opt/graphite/storage' apache_avail = '/etc/apache2/sites-available' apache_enab = '/etc/apache2/sites-enabled' apache_src_conf = '/root/default-graphite' apache_dst_conf = "{0}/{1}".format(apache_avail,'default-graphite') apache_enab_conf = "{0}/{1}".format(apache_enab,'default-graphite') stor_sch_conf = "{0}/{1}".format(graph_conf_path,'storage-schemas.conf') with settings( show('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false'): for file in ['carbon.conf','storage-schemas.conf','graphite.wsgi']: src = "{0}/{1}.example".format(graph_conf_path,file) dst = "{0}/{1}".format(graph_conf_path,file) ## Copy from example, template to real file copyfile(src,dst) ## Generate default-graphite apache config file, based on ## template at top of this file. make_apache_conf = Template(apache_base_templ) ## Write template into the new config file ## /etc/apache2/sites-available/default-graphite try: open(apache_dst_conf,'wt').write( make_apache_conf.substitute(port=80,wsgi_sockd='/etc/httpd/wsgi/') ) fab_puts("Wrote apache config for Graphite WebApp.",show_prefix=False) except IOError as e: fab_abort("Error {0} Failed to open file {1}".format(e.errno,e.filename)) try: open(stor_sch_conf,'at').write(stor_base_templ) fab_puts("Updated storage schema config with brickstor elements.",show_prefix=False) except IOError as e: fab_abort("Error {0} Failed to open file {1}".format(e.errno,e.filename)) try: os_remove('/etc/apache2/sites-enabled/000-default') except OSError as e: print "Warning: {0} {1}".format(e.filename,e.args) ## Create necessary directories for Apache for dir in ['/etc/httpd','/etc/httpd/wsgi']: try: os_mkdir(dir,0755) fab_puts("Created directory: {0}".format(dir),show_prefix=False) except OSError as e: print "Warning: {0} {1}".format(e.filename,e.args) try: os_symlink(apache_dst_conf, apache_enab_conf) fab_puts("Created symbolic link for {0}".format(apache_dst_conf),show_prefix=False) except OSError as e: print "Warning: {0} {1}".format(e.filename,e.args) with fab_lcd('/opt/graphite/webapp/graphite/'): fab_local('python manage.py syncdb') ## This should really use python os module, will fix later. fab_local("chown -R {0} {1}".format('www-data:www-data',graph_stor_path)) ## Copy local_settings.py.example config into real config file src = '/opt/graphite/webapp/graphite/local_settings.py.example' dst = '/opt/graphite/webapp/graphite/local_settings.py' copyfile(src,dst) ## Reload Apache config after all the changes fab_local("/etc/init.d/apache2 reload")