def deploy_db(rollback=False): """ Deploy a sqlite database from development """ env.deployment_root = '/home/%s/%s/'% (env.user,root_domain()) db_name = env.deployment_root + '/'.join(['database',project_name()+'.db']) if not rollback: db_dir = env.deployment_root+'database' if env.DEFAULT_DATABASE_ENGINE=='django.db.backends.sqlite3' and not exists(db_name): if env.verbosity: print env.host,"DEPLOYING DEFAULT SQLITE DATABASE to",db_name if not os.path.exists(env.DEFAULT_DATABASE_NAME) or not env.DEFAULT_DATABASE_NAME: print "ERROR: the database does not exist. Run python manage.py syncdb to create your database first." sys.exit(1) run('mkdir -p '+db_dir) put(env.DEFAULT_DATABASE_NAME,db_name) sudo("chown -R %s:www-data %s"% (env.user,db_dir)) sudo("chmod -R ug+w "+db_dir) elif rollback and env.DEFAULT_DATABASE_ENGINE=='django.db.backends.sqlite3': if env.INTERACTIVE: delete = confirm('DELETE the database on the host?',default=False) if delete: run('rm -f '+db_name) return
def stage_local_files(self): #a dest_path_postfix is a /url/postfix/ from a django media setting #we need to create the full postfix directory on top of the deploy_root (eg STATIC_ROOT+ADMIN_MEDIA_PREFIX) #so locally we create a tmp staging directory to rsync from staging_dir = '%s_staging_dir'% self.deploy_type #ensure this is run only once per deploy_type if hasattr(env,staging_dir): return env[staging_dir] env[staging_dir] = s = tempfile.mkdtemp() #for cleanup later env.woventempdirs = env.woventempdirs + [s] shutil.copytree(self.local_path,os.path.join(s,self.last_postfix)) #render settings files and replace if self.deploy_type == 'project': context = { 'user':env.user, 'project_name':project_name(), 'project_fullname':project_fullname(), 'root_domain':root_domain(), } for d in env.DOMAINS: context['domain']=d template_file = d.replace('.','_') + '.py' template_path = os.path.join(s,'project',project_name(),'sitesettings',template_file) f = open(template_path,"r") t = f.read() f.close() t = Template(t) rendered = t.render(Context(context)) f = open(template_path,"w+") f.write(rendered) f.close() return s
def __init__(self,version=''): super(Project,self).__init__(version) self.deploy_type = self.__class__.__name__.lower() self.deploy_root = env.deployment_root+'/'.join(['env',self.fullname,self.deploy_type,'']) #try to exclude a few common things in the project directory self.rsync_exclude = ['*.pyc','*.log','.*','/build','/dist','/media','/app*','/www','/public','/templates'] self.versioned = True self.local_path = './' self.local_settings_dir = os.path.join('./',project_name(),'sitesettings') #the name of a settings.py setting self.setting ='' self.dest_path_postfix = '' if not env.DOMAINS: env.DOMAINS = [root_domain()]
def make_local_sitesettings(self,overwrite=False): if not os.path.exists(self.local_settings_dir) or overwrite: if overwrite: shutil.rmtree(self.local_settings_dir,ignore_errors=True) os.mkdir(self.local_settings_dir) f = open(self.local_settings_dir+'/__init__.py',"w") f.close() site_id = 0 for domain in env.DOMAINS: site_id+=1 settings_file_path = os.path.join(self.local_settings_dir,domain.replace('.','_')+'.py') if not os.path.exists(settings_file_path): output ="""#Import global project settings from %s.settings import * #Override global settings with site/host local settings #template tags will be substituted on project deployment #Customize and add any other local site settings as required DEBUG = False TEMPLATE_DEBUG = DEBUG DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 'NAME': '/home/{{ user }}/{{ root_domain }}/database/{{ project_name }}.db', # Or path to database file if using sqlite3. 'USER': '', # Not used with sqlite3. 'PASSWORD': '', # Not used with sqlite3. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '', # Set to empty string for default. Not used with sqlite3. } } #Amend this if required SITE_ID = %s #Normally you won't need to amend these settings MEDIA_ROOT = '/home/{{ user }}/{{ root_domain }}/public/' STATIC_ROOT = '/home/{{ user }}/{{ root_domain }}/env/{{ project_fullname }}/static/' TEMPLATE_DIRS = ('/home/{{ user }}/{{ root_domain }}/env/{{ project_fullname }}/templates/', '/home/{{ user }}/{{ root_domain }}/env/{{ project_fullname }}/templates/{{ domain }}',) """% (project_name(), site_id) f = open(settings_file_path,"w+") f.writelines(output) f.close() return