def install_blueprint(ag='apt-get'): devs_repo = '/etc/apt/sources.list.d/devstructure.list' with settings( show('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false'): x = open('/etc/lsb-release').readlines() for line in x: if not line.find('DISTRIB_CODENAME') == -1: vers = line.split('=')[1].strip('\n') open(devs_repo,'wt')\ .write("deb http://packages.devstructure.com {0} main".format(vers)) inst_bluep = fab_local("wget -O {0} {1}".format( '/etc/apt/trusted.gpg.d/devstructure.gpg', 'http://packages.devstructure.com/keyring.gpg' )) ## Have to update here to install Blueprint, otherwise would ## save time not updating the cache. apt_upd = fab_local('apt-get update',capture=True) if apt_upd.succeeded: fab_local("{0} {1}".format(ag,'install --no-install-recommends --assume-yes blueprint'))
def validate_os(lsbdata='/etc/lsb-release'): env['shell'] = '/bin/bash -lc' with settings(show('warnings', 'running', 'stdout', 'stderr'), warn_only=True): test = fab_local('[ -f %s ]' % (lsbdata), capture=True) print test.failed if test.succeeded: return False else: fab_local("cat '/etc/lsb-release' ")
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 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 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 sudo_local(command, shell=True, pty=True, combine_stderr=True, user=None, capture=False, warn_only=False): """ sudo/local function based on host """ if env.user == user: return run_local(command, shell=shell, pty=pty, combine_stderr=combine_stderr, capture=capture, warn_only=warn_only) else: if is_local(): return fab_local("sudo %s" % command, capture=capture) else: return fab_sudo(command, shell=shell, pty=pty, combine_stderr=combine_stderr, user=user, warn_only=warn_only)
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 = ''
def patch_os(ag='apt-get'): apt_upd = None with settings( show('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false'): ## Check if package cache is older than one day, if not ## there is no reason to update it again. if time() - os_stat('/var/cache/apt/pkgcache.bin').st_mtime < 86400: fab_puts("Caches are still fresh, will not update.",show_prefix=False) else: apt_upd = fab_local('apt-get update',capture=True) if apt_upd.failed: fab_puts("{0}".format('Aptitude failed to completely update cache.'), show_prefix=False) if apt_upd.succeeded: ## If we successfully updated, let's actually upgrade packages apt_upgr = fab_local("{0} {1}".format(ag,'dist-upgrade --assume-yes'), capture=False)
def local(command, capture=True): """ Run a command locally. :param command: The command to run :param capture: Return the command's output? """ out = fab_local(command, capture) for key in connections.keys(): connections[key].close() del connections[key] return out
def install_deps(ag='apt-get'): reqs = ['apache2', 'libapache2-mod-wsgi', 'libapache2-mod-python', 'memcached', 'python-dev', 'python-cairo-dev', 'python-django', 'python-ldap', 'python-memcache', 'python-pysqlite2', 'sqlite3', 'erlang-os-mon', 'erlang-snmp', 'rabbitmq-server'] with settings( show('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false'): a = fab_local("{0} install --no-install-recommends --assume-yes {1}".format(ag," ".join(reqs))) b = fab_local('pip install django-tagging') if a.succeeded and b.succeeded: fab_puts("Installed Prerequisites for Graphite.", show_prefix=False) else: if a.failed: print a.stderr if b.failed: print b.stderr fab_abort("Failed to Install Pre-requisites, cannot continue.", show_prefix=False)
def startup(): with settings( show('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false'): start_graph = fab_local('/opt/graphite/bin/carbon-cache.py start',capture=True) if start_graph.succeeded: fab_puts("Successfully Started Graphite. Check your browser!",show_prefix=False) else: fab_puts("Failed to Start Graphite. \ Please check Graphite and Apache2 logs.",show_prefix=False)
def create_snapshot(): message = '\"Snapshot before graphite and its dependencies\"' with settings( show('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false'): x = localtime() ## Month needs to always be 2 digits XX, bug. ts = "{0}{1}{2}".format(x.tm_year,x.tm_mon,x.tm_mday) bpcreate = fab_local("blueprint create --sh --message {0} bluep-{1}-001".format(message,ts),capture=True) if bpcreate.succeeded: fab_puts("Blueprint created checkpoint.", show_prefix=False)
def install_rest(pip_cmd='pip install'): with settings( show('running', 'stdout', 'stderr'), warn_only=True, always_use_pty='false'): fab_local("{0} carbon --install-option=\"--prefix={1}\" \ \"--install-option=--install-lib={1}/lib\"".format(pip_cmd,'/opt/graphite')) fab_local("{0} graphite-web --install-option=\"--prefix={1}\" \ \"--install-option=--install-lib={1}/webapp\"".format(pip_cmd,'/opt/graphite')) fab_local("{0} {1}".format(pip_cmd,'whisper'))
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)
#!/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 run(self, *args): print args fab_local("echo MyDebugTask being called.")
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")