def update_remote(hg_revision='tip'): """update remote repository""" change_owner() group_writable_permissions() # get a list of all of the django projects in this rel_django_project_dirs = [] hg_root = fabhelp.get_hg_root() for root, dirs, files in os.walk(hg_root): d = os.path.join(hg_root, root) try: dirs.remove('.hg') # ignore mercurial repositories except ValueError: pass # .hg is not in dirs if fabhelp.is_django_project(d): rel_django_project_dirs.append(os.path.relpath(d, hg_root)) dirs[:] = [] # no need to decend any further... save time! # get the remote directory from the apache file fabhelp.progress("updating remote repository") remote_repo_dir = get_remote_repo_dir() with cd(remote_repo_dir): with fabric_settings(warn_only = True): run("hg pull") # run database2fixture for each django repository in this repository for rel_django_project_dir in rel_django_project_dirs: with cd(rel_django_project_dir): if fabhelp.is_dbvcs_installed(): run("./manage.py database2fixture") run("hg update -r %s"%hg_revision) remote_project_dir = get_remote_project_dir() with cd(remote_project_dir): run("./manage.py setup_virtualenv") # add new packages when necessary group_writable_permissions()
def add_dsa_app(app_name=None): """add an app from the DjangoApps collection of repositories""" # make sure project name is provided if app_name is None: app_name = prompt("Please enter an app name: ") # remember some directories cur_dir, bin_dir, src_dir = fabhelp.get_cur_bin_src_directories() # make sure we are in the root of the django project if not fabhelp.is_django_project(cur_dir, in_root_dir=True): abort("This command must be run from root of django project") # abort if local changes to .hgignore or .hgsub hg_root = fabhelp.get_hg_root() hg_modified_files = [ os.path.join(hg_root, ".hgsub"), os.path.join(cur_dir, "conf", "settings_modules.txt"), ] fabhelp.abort_if_local_changes(hg_modified_files) # clone the subrepository in the right place clone_subrepository( "ssh://poisson//srv/hg/DjangoApps/%s"%app_name, os.path.join(cur_dir, "apps", app_name), ) # create the settings file fabhelp.progress("creating the conf/%s.py settings for project"%app_name) from django.conf import settings as django_settings from django.template.loader import render_to_string try: django_settings.configure( DEBUG=True, TEMPLATE_DEBUG=True, TEMPLATE_DIRS=( os.path.join(cur_dir, 'templates'), ), ) except RuntimeError: pass # this is being run from the setup_dsa_app django command # --- hopefully f = open(os.path.join(cur_dir, "conf", "%s.py"%app_name), 'w') f.write(render_to_string(".settings_template.py", { "app_name": app_name, "is_dbvcs_installed": fabhelp.is_dbvcs_installed(), })) f.close() # add the settings to the settings_modules.txt file fabhelp.progress("add the settings file to conf/settings_modules.txt") f = open(os.path.join(cur_dir, "conf", "settings_modules.txt"), 'a') f.write(""" # automatically including %s conf/%s.py """%(app_name, app_name)) f.close() # add the media soft links fabhelp.progress("soft links for media") with lcd(".media"): local("ln -s ../apps/%s/media/%s %s"%(app_name, app_name, app_name)) with lcd(".static"): local("ln -s ../apps/%s/static/%s %s"%(app_name, app_name, app_name)) # synchronize the database and dump the database fabhelp.progress("syncdb and database2fixture (as necessary)!") local("./manage.py syncdb --noinput") if fabhelp.is_dbvcs_installed(): local("./manage.py database2fixture --all") # add these fixtures to the repository. make sure to change into # the local directory first in case the dbvcs data directory is a # subrepository if fabhelp.is_dbvcs_installed(): result = local("./manage.py listfixtures apps.%s" % app_name, capture=True) for filename in result.split(): d, f = os.path.split(filename) with lcd(d): local("hg add %s"%f) # run hg add fabhelp.progress("add everything to the mercurial repository") local("hg add %s"%' '.join([ os.path.join(cur_dir, "conf", "%s.py"%app_name), os.path.join(cur_dir, ".media", app_name), os.path.join(cur_dir, ".static", app_name), ]))
def render_apache_configuration(production_root_url, local_master_django_project_dir=''): """setup apache configuration files for BOTH staging and production. this does NOT manipulate apache configuration on remote servers. it is only intended torender the apache templates """ msg = "setup apache configuration files for BOTH staging and production" fabhelp.progress(msg) # if the production root url has a suburl, the # local_master_django_project_dir must be specified!!! WARNING: do not # remove this condition --- it is used very strongly below to # assume when things need to be written to the staging apache conf if production_root_url.suburl and not local_master_django_project_dir: msg = "if production_root_url is a suburl django project,\n" msg += "you must specify the --parent_project option\n" raise TypeError(msg) # make sure that master_conf_filename exists if it is specified if local_master_django_project_dir: local_master_django_project_dir = \ os.path.abspath(local_master_django_project_dir) if not fabhelp.is_django_project(local_master_django_project_dir): msg="--parent_project must point to a django project root.\n" msg+="'%s' is not a django project root" % ( local_master_django_project_dir, ) abort(msg) # also need to make sure that local_master_django_project_dir # is actually hosting the same domain, otherwise could cause # problems! filename = os.path.join(local_master_django_project_dir, "conf", "noether", "apache", "noether.conf") try: f = open(filename) except IOError: msg = "can not find parent project apache conf in %s\n"%filename msg += "have you run setup_staging in %s yet?" % ( local_master_django_project_dir, ) abort(msg) s = f.read() f.close() result = re.search(r"<VirtualHost (?P<domain>[\w\.]+):\d+>", s) if result is None: msg = "apache configuration file (%s)\n"%filename msg += "does not appear to be the root VirtualHost" abort(msg) master_domain = result.groupdict()["domain"] if production_root_url.domain!=master_domain: msg = "domain of parent_project (%s)\n" % master_domain msg += "does not match specified domain (%s)"%( production_root_url.domain, ) abort(msg) # get the project name --- remember, this command is always run # from within the django project project_dir = local('pwd', capture=True) project_name = os.path.basename(project_dir) # setup django from django.conf import settings as django_settings from django.template.loader import render_to_string try: django_settings.configure( DEBUG=True, TEMPLATE_DEBUG=True, TEMPLATE_DIRS=( os.path.join(project_dir, 'common', 'templates'), ), ) except RuntimeError: pass # this is being run from the setup_{staging,production} # django command --- hopefully # set things up for both staging and production for server in ("poisson", "noether",): # determing the template to use suburl = production_root_url.suburl if server=="poisson" or local_master_django_project_dir: conf_template = os.path.join("apache", "suburl.conf") if server=="poisson": suburl = '/' + production_root_url.subdomain + suburl else: conf_template = os.path.join("apache", "root.conf") # write the configuration template for this server conf_filename = os.path.join(project_dir, "conf", server, "apache", "%s.conf"%server) local("mkdir -p %s"%os.path.dirname(conf_filename)) hg_root = fabhelp.get_hg_root() f = open(conf_filename, 'w') f.write(render_to_string( conf_template, { "subdomain": production_root_url.subdomain, "domain": production_root_url.domain, "suburl": suburl, "server": server, "hg_path_to_django_project": os.path.join( os.path.basename(hg_root), os.path.relpath(project_dir, hg_root)), })) f.close() local("hg add %s"%os.path.relpath(conf_filename, project_dir)) # include this new apache file as necessary in the appropriate # root url apache configuration if local_master_django_project_dir: remote_django_project_dir = get_remote_project_dir() root_apache_filename = os.path.join( local_master_django_project_dir, "conf", server, "apache", "%s.conf"%server) # XXXX TODO: this is probably not enough to simply place the # suburls directly before the ####XXXX line. we probably need # to order things in a smart way depending on what the suburls # are: the most restrictive first like /foo/bar before /foo. apache_file = open(root_apache_filename,'r') apache_conf = apache_file.read() apache_file.close() include_directive = "Include %s"%os.path.join( remote_django_project_dir, "conf", server, "apache", "%s.conf"%server ).replace('/', r'\/') if include_directive not in apache_conf: local(r"sed 's/####XXXX/%s\n####XXXX/' %s > kk"%( include_directive, root_apache_filename, )) local("mv kk %s"%root_apache_filename) with settings(warn_only=True): result = local("hg ci -m 'auto-added apache configuration setup'", capture=True) if result.failed: warn("apache configuration already added")