def makedirs(path, isfile=False): """Make directories in path""" if isfile: path = os.path.dirname(path) if not exists(path): with hide('commands'): env.doit('mkdir -p %s' % path)
def ls(d): """Get a directory listing for directory d.""" if env.settings == 'loc': return [path(d, f) for f in env.doit("ls -1 %s" % d, capture=True).splitlines()] else: return [path(d, f) for f in env.doit("ls -1 %s" % d).splitlines()]
def dump_data(self, file_path): """ Dump data from database to file. """ with hide('commands'): if env.settings == 'loc': result = self.cmd('-e "SHOW TABLES;" {0.name}', capture=True) else: result = self.cmd('-e "SHOW TABLES;" {0.name}') c = 'mysqldump -h {0.host} -u {0.user}' if env.password: c += ' -p"{0.password}" ' # Ignore django administrative tables for line in result.splitlines(): if line.startswith('+'): continue name = line.strip('| ') if name.startswith('Tables_in_'): continue if name.startswith('auth_') or name.startswith('django_'): c += ' --ignore-table={0.name}.' + name env.doit((c + ' {0.name} > ' + file_path).format(self))
def dump_data(self, file_path): """ Dump data from database to file. """ with hide('commands'): if env.settings == 'loc': result = self.cmd('-e "SHOW TABLES;" {0.name}', capture=True) else: result = self.cmd('-e "SHOW TABLES;" {0.name}') c = 'mysqldump -h {0.host} -u {0.user}' if env.password: c += ' -p"{0.password}" ' # Ignore django administrative tables for line in result.splitlines(): if line.startswith('+'): continue name = line.strip('| ') if name.startswith('Tables_in_'): continue if name.startswith('auth_') or name.startswith('django_'): c += ' --ignore-table={0.name}.'+name env.doit((c+' {0.name} > '+file_path).format(self))
def clean(path): """Delete contents of path""" path = os.path.abspath(path) puts('clean: %s' % path) if exists(path): with hide('commands'): if env.settings == 'loc': result = local('file -b %s' % path, capture=True) else: result = run('file -b %s' % path) if result == 'directory': for item in ls(path): env.doit('rm -rf %s' % item) else: env.doit('rm -rf %s' % path)
def destroy(self): """ Remove the database and user. """ if self.user_exists(): notice('Dropping database user "{0.user}"'.format(self)) self.cmd('-c "' \ 'DROP OWNED BY {0.user};' \ 'DROP USER {0.user};" {0.name}') else: notice('Database user "{0.user}" does not exist'.format(self)) if self.db_exists(self.name): notice('Dropping database "{0.name}"'.format(self)) env.doit('dropdb -h {0.host} -U {0.root_user} {0.name}'.format(self)) else: notice('Database "{0.name}" does not exist'.format(self))
def cmd(self, cmd, prefix='', **kwargs): """ Send command to mysql. """ s = prefix+' mysql -h {0.host} -u {0.root_user} ' if self.root_password: s += ' -p"{0.root_password}" ' s += cmd return env.doit(s.format(self) % env, **kwargs)
def cmd(self, cmd, prefix='', **kwargs): """ Send command to mysql. """ s = prefix + ' mysql -h {0.host} -u {0.root_user} ' if self.root_password: s += ' -p"{0.root_password}" ' s += cmd return env.doit(s.format(self) % env, **kwargs)
def cmd(self, cmd, user='', prefix='', **kwargs): """ Send command to psql. """ if user: s = prefix+' psql -h {0.host} -U '+user+' '+cmd else: s = prefix+' psql -h {0.host} -U {0.root_user} '+cmd return env.doit((s.format(self)) % env)
def setup(): """Create the project database and user.""" created_db = False # Create the template database with hide('warnings'), settings(warn_only=True): result = _psql('-l | grep "template_postgis "') if result.failed: notice('Creating template database template_postgis') env.doit('createdb -h %(host)s -U %(postgis_user)s template_postgis' % env) _psql('-f %(postgis_root)s/postgis.sql template_postgis') _psql('-f %(postgis_root)s/spatial_ref_sys.sql template_postgis') else: notice('Template database template_postgis already exists') # Create the project database with hide('warnings'), settings(warn_only=True): result = _psql('-l | grep "%(project_name)s "') if result.failed: notice('Creating database %(project_name)s from template' % env) env.doit('createdb -h %(host)s -U %(postgis_user)s -T template_postgis %(project_name)s' % env) created_db = True else: notice('Database %(project_name)s already exists' % env) # Create the database user with hide('warnings'), settings(warn_only=True): result = _psql('-c "SELECT rolname FROM pg_roles" %(project_name)s | grep "%(db_user)s"') if result.failed: notice('Creating database user %(db_user)s' % env) _psql('-c "' \ 'CREATE USER %(db_user)s;' \ 'GRANT ALL PRIVILEGES ON DATABASE %(project_name)s to %(db_user)s;' \ 'ALTER TABLE geometry_columns OWNER TO %(db_user)s;' \ 'ALTER TABLE spatial_ref_sys OWNER TO %(db_user)s;' \ '" %(project_name)s') elif created_db: _psql('-c "' \ 'ALTER TABLE geometry_columns OWNER TO %(db_user)s;' \ 'ALTER TABLE spatial_ref_sys OWNER TO %(db_user)s;' \ '" %(project_name)s') else: notice('Database user %(db_user)s already exists' % env)
def coverage(): """[localhost] Mostra o relatório de testes do código fonte """ env.doit("coverage -e") env.doit("coverage run -m unittest discover -b tests") env.doit( "coverage report --omit=fabfile/code.py,fabfile/utils.py -m fabfile/*.py" )
def setup(self): """ Create the project database and user. """ created_db = False # Create the template database if self.db_exists('template_postgis'): notice('Template database template_postgis already exists') else: notice('Creating template database template_postgis') env.doit('createdb -h %{0.host} -U {0.root_user} template_postgis'.format(self)) self.cmd('-f {0.postgis_root}/postgis.sql template_postgis') self.cmd('-f {0.postgis_root}/spatial_ref_sys.sql template_postgis') # Create the project database if self.db_exists(self.name): notice('Database "{0.name}" exists on host {0.host}'.format(self)) else: notice('Creating database {0.name} from template'.format(self)) env.doit('createdb -h {0.host} -U {0.root_user} -T template_postgis {0.name}'.format(self)) created_db = True # Create the database user if self.user_exists(): notice('Database user "{0.user}" exists on host {0.host}'.format(self)) else: notice('Creating db user "{0.user}"'.format(self)) self.cmd('-c "' \ 'CREATE USER {0.user};' \ 'GRANT ALL PRIVILEGES ON DATABASE {0.name} to {0.user};' \ '" {0.name}') if created_db: self.cmd('-c "' \ 'ALTER TABLE geometry_columns OWNER TO {0.user};' \ 'ALTER TABLE spatial_ref_sys OWNER TO {0.user};' \ '" {0.name}')
def destroy(): """Remove the database and user.""" warn('This will delete the %(project_name)s db and %(db_user)s user ' \ 'for %(settings)s on %(host)s.') msg = 'Destroy %(project_name)s database and %(db_user)s user for ' \ '%(settings)s deployment? (y/n) ' if not confirm(msg % env): abort('Cancelling') with hide('warnings'): with settings(warn_only=True): result = _psql('-l | grep "%(project_name)s "') if result.failed: notice('Database %(project_name)s does not exist' % env) return # Drop database user with settings(warn_only=True): result = _psql( '-c "SELECT rolname FROM pg_roles" %(project_name)s' \ ' | grep "%(db_user)s"') if result.failed: notice('Database user %(db_user)s does not exist' % env) else: notice('Dropping user %(db_user)s' % env) _psql('-c "' \ 'DROP OWNED BY %(db_user)s;' \ 'DROP USER %(db_user)s;' \ '" %(project_name)s') # Drop project database with settings(warn_only=True): result = _psql('-l | grep "%(project_name)s "') if result.failed: notice('Database %(project_name)s does not exist' % env) else: notice('Dropping database %(project_name)s' % env) env.doit('dropdb -h %(host)s -U %(postgis_user)s %(project_name)s' % env)
def setup(self): """ Create the project database and user. """ created_db = False # Create the template database if self.db_exists('template_postgis'): notice('Template database template_postgis already exists') else: notice('Creating template database template_postgis') env.doit('createdb -h {0.host} -U {0.root_user} template_postgis'.format(self)) self.cmd('-f {0.postgis_root}/postgis.sql template_postgis') self.cmd('-f {0.postgis_root}/spatial_ref_sys.sql template_postgis') # Create the project database if self.db_exists(self.name): notice('Database "{0.name}" exists on host {0.host}'.format(self)) else: notice('Creating database {0.name} from template'.format(self)) env.doit('createdb -h {0.host} -U {0.root_user} -T template_postgis {0.name}'.format(self)) created_db = True # Create the database user if self.user_exists(): notice('Database user "{0.user}" exists on host {0.host}'.format(self)) else: notice('Creating db user "{0.user}"'.format(self)) self.cmd('-c "' \ 'CREATE USER {0.user};' \ 'GRANT ALL PRIVILEGES ON DATABASE {0.name} to {0.user};' \ '" {0.name}') if created_db: self.cmd('-c "' \ 'ALTER TABLE geometry_columns OWNER TO {0.user};' \ 'ALTER TABLE spatial_ref_sys OWNER TO {0.user};' \ '" {0.name}')
def a2restart(graceful='y'): """Restart apache. Set graceful=n for immediate restart (default = y).""" if do(graceful): env.doit('sudo /usr/sbin/apache2ctl graceful') else: env.doit('sudo /usr/sbin/apache2ctl restart')
def a2stop(graceful='y'): """Stop apache. Set graceful=n for immediate stop (default = y).""" if do(graceful): env.doit('sudo /usr/sbin/apache2ctl graceful-stop') else: env.doit('sudo /usr/sbin/apache2ctl stop')
def a2start(): """Start apache. Uses init.d instead of apachectl for fabric.""" env.doit('sudo /etc/init.d/apache2 start')
def checkout(): """Pull the latest code on remote servers.""" env.doit('cd %(project_path)s; git pull' % env)
def _mysql(cmd, prefix=''): c = ' mysql -h %(db_host)s -u %(db_user)s ' if env.db_password: c += '-p"%(db_password)s" ' return env.doit((prefix+c+cmd) % env)
def run_in_ve(command): """Execute the command inside the virtualenv.""" with prefix('. %s' % env.activate_path): env.doit(command)
def _psql(cmd, host='', user='', prefix=''): c = ' psql -h '+(host or env.host)+' -U '+(user or env.postgis_user)+ ' ' return env.doit((prefix+c+cmd) % env)
def test(): """[localhost] Executa testes no código fonte """ env.doit("python -m unittest discover -b tests")
def cmd(self, cmd, prefix=''): """ Send command to mongo. """ s = prefix + ' mongo -host {0.host} ' + cmd return env.doit((s.format(self)) % env)