def _backup_db(site, path): tmpdir = settings.TEMPORARY_PATH (status, remote_tempfile, err) = _remote_ssh( site.platform, 'mktemp %s' % (os.path.join(tmpdir, 'mysqldump.XXXXXXXX'))) if status > 0: logger.error('_backup_db: could not open remote tempfile on host %s' % (site.platform.host, )) return False logger.info('_backup_db: tempfile=%s site=%s' % (remote_tempfile, site)) (status, out, err) = _remote_ssh( site.platform, 'mysqldump -c --quick --single-transaction %s > %s' % (site.database, remote_tempfile)) if status > 0: logger.error( '_backup_db: could not open mysqldump to tempfile on host %s' % (site.platform.host, )) logger.error(out) else: local_tempfile = tempfile.mkdtemp() logger.info('_backup_db: local sql tempfile: %s' % (local_tempfile, )) (s, o, e) = _rsync_pull(site.platform, remote_tempfile, os.path.join(path, site.database + '.sql')) logger.error(o) logger.error(e) _remote_ssh(site.platform, 'rm %s' % (remote_tempfile, )) if s == 0: return True return False
def is_clean(site): """ return true of if there is no trace of the site. this includes symlink, database, sites directory""" clean = True message = '' (status, out, err) = _remote_ssh(site.platform, 'mysql mysql -e "use %s;"' % (site.database, )) logger.info("is_clean: mysql: %d" % (status, )) clean = clean and status (status, out, err) = _remote_ssh(site.platform, '[ -d %s ]' % (site.site_dir(), )) logger.info("is_clean: site_dir: %d" % (status, )) clean = clean and status if site.short_name == 'default': logger.info( "is_clean: default site=%s is clean as it's going to be: %d" % ( site, clean, )) return clean (status, out, err) = _remote_ssh(site.platform, '[ -L %s ]' % (site.site_symlink(), )) logger.info("is_clean: site_symlink: %d" % (status, )) clean = clean and status logger.info("is_clean: site=%s is clean: %d" % ( site, clean, )) return clean
def _db_replace(old_site, new_site): cols = [ ('field_revision_field_link', 'field_link_url'), ('field_revision_body', 'body_value'), ('field_revision_body', 'body_summary'), ('field_data_body', 'body_value'), ('field_data_body', 'body_summary'), ('field_revision_field_link', 'field_link_url'), ('field_data_field_link', 'field_link_url'), ('field_data_field_slide_link', 'field_slide_link_value'), ('field_revision_field_slide_link', 'field_slide_link_value'), ('menu_links', 'link_path'), ('block_custom', 'body'), # ('variable', 'value'), ] for i in cols: (table, col) = i sql1 = "UPDATE %s SET %s = REPLACE(%s, '%s', '%s');" % ( table, col, col, old_site.site_uri, new_site.site_uri) sql2 = "UPDATE %s SET %s = REPLACE(%s, '%s', '%s');" % ( table, col, col, old_site.files_dir, new_site.files_dir) (status, out, err) = _remote_ssh(new_site.platform, 'mysql %s -e "%s"' % (new_site.database, sql1)) (status, out, err) = _remote_ssh(new_site.platform, 'mysql %s -e "%s"' % (new_site.database, sql2)) return True
def _db_replace(old_site, new_site): cols = [ ('field_revision_field_link', 'field_link_url'), ('field_revision_body', 'body_value'), ('field_revision_body', 'body_summary'), ('field_data_body', 'body_value'), ('field_data_body', 'body_summary'), ('field_revision_field_link', 'field_link_url'), ('field_data_field_link', 'field_link_url'), ('field_data_field_slide_link', 'field_slide_link_value'), ('field_revision_field_slide_link', 'field_slide_link_value'), ('menu_links', 'link_path'), ('block_custom', 'body'), # ('variable', 'value'), ] for i in cols: (table, col) = i sql1 = "UPDATE %s SET %s = REPLACE(%s, '%s', '%s');" %( table, col, col, old_site.site_uri, new_site.site_uri ) sql2 = "UPDATE %s SET %s = REPLACE(%s, '%s', '%s');" % ( table, col, col, old_site.files_dir, new_site.files_dir ) (status, out, err) = _remote_ssh(new_site.platform, 'mysql %s -e "%s"' % (new_site.database,sql1) ) (status, out, err) = _remote_ssh(new_site.platform, 'mysql %s -e "%s"' % (new_site.database,sql2) ) return True
def _backup_db(site, path): tmpdir = settings.TEMPORARY_PATH (status, remote_tempfile, err) = _remote_ssh(site.platform, 'mktemp %s' % (os.path.join(tmpdir, 'mysqldump.XXXXXXXX'))) if status > 0: logger.error('_backup_db: could not open remote tempfile on host %s' % (site.platform.host,)) return False logger.info('_backup_db: tempfile=%s site=%s' % (remote_tempfile, site)) (status, out, err) = _remote_ssh(site.platform, 'mysqldump -c --quick --single-transaction %s > %s' % ( site.database, remote_tempfile)) if status > 0: logger.error('_backup_db: could not open mysqldump to tempfile on host %s' % (site.platform.host,)) logger.error(out) else: local_tempfile = tempfile.mkdtemp() logger.info('_backup_db: local sql tempfile: %s' % (local_tempfile,)) (s,o,e) = _rsync_pull(site.platform, remote_tempfile, os.path.join(path,site.database + '.sql')) logger.error(o) logger.error(e) _remote_ssh(site.platform, 'rm %s' % (remote_tempfile,)) if s == 0: return True return False
def _set_site_permissions(site): # dev servers have different permissions (status, out, err) = _remote_ssh(site.platform, 'chmod %d %s;' % ( 666 if site.platform.use == 'dev' else 664, os.path.join( site.site_dir(), 'settings.php' ),) ) (status, out, err) = _remote_ssh(site.platform, 'chmod 775 %s;' % ( site.site_dir(), ) ) #TODO: perhaps do chowning that's currently in _create_site_dirs here? return status == 0
def _create_settings_php(site): settings = tempfile.mkstemp()[1] site.settings_php(settings) #post install this fails since drush chown'd settings.php. _remote_ssh(site.platform, 'chmod 755 %s' % (site.site_dir(), )) (status, out, err) = _rsync_push(site.platform, settings, os.path.join(site.site_dir(), 'settings.php')) return status == 0
def _create_settings_php(site): settings = tempfile.mkstemp()[1] site.settings_php(settings) #post install this fails since drush chown'd settings.php. _remote_ssh(site.platform, 'chmod 755 %s' %( site.site_dir(), ) ) (status, out, err) = _rsync_push(site.platform, settings, os.path.join( site.site_dir(), 'settings.php') ) return status == 0
def _set_site_permissions(site): # dev servers have different permissions (status, out, err) = _remote_ssh( site.platform, 'chmod %d %s;' % ( 666 if site.platform.use == 'dev' else 664, os.path.join(site.site_dir(), 'settings.php'), )) (status, out, err) = _remote_ssh(site.platform, 'chmod 775 %s;' % (site.site_dir(), )) #TODO: perhaps do chowning that's currently in _create_site_dirs here? return status == 0
def check_platform(platform): ok = True #check path (status, out, err) = _remote_ssh(platform, '[ -d %s ]' % (platform.path, )) ok = ok and status #check db/my.cnf (status, out, err) = _remote_ssh(platform, '[ -f %s ]' % ('.my.cnf', )) ok = ok and status #check for drush (status, out, err) = _remote_ssh(platform, 'which drush') ok = ok and status return ok
def check_platform(platform): ok = True #check path (status, out, err) = _remote_ssh(platform, '[ -d %s ]' % (platform.path,)) ok = ok and status #check db/my.cnf (status, out, err) = _remote_ssh(platform, '[ -f %s ]' % ( '.my.cnf',)) ok = ok and status #check for drush (status, out, err) = _remote_ssh(platform, 'which drush') ok = ok and status return ok
def is_clean(site): """ return true of if there is no trace of the site. this includes symlink, database, sites directory""" clean = True message = '' (status, out, err) = _remote_ssh(site.platform, 'mysql mysql -e "use %s;"' % (site.database,)) logger.info("is_clean: mysql: %d" % (status,) ) clean = clean and status (status, out, err) = _remote_ssh(site.platform, '[ -d %s ]' % (site.site_dir(),)) logger.info("is_clean: site_dir: %d" % (status,) ) clean = clean and status if site.short_name == 'default': logger.info("is_clean: default site=%s is clean as it's going to be: %d" % (site, clean,) ) return clean (status, out, err) = _remote_ssh(site.platform, '[ -L %s ]' % (site.site_symlink(),)) logger.info("is_clean: site_symlink: %d" % (status,)) clean = clean and status logger.info("is_clean: site=%s is clean: %d" % (site, clean,) ) return clean
def get_site_du(site): (status, out,err) = _remote_ssh(site.platform, "du -ks %s" % (site.site_dir(),) ) if status == 0: tmp = out.split('\t') kilobytes = int(tmp[0]) locale.setlocale(locale.LC_ALL, '') s = locale.format("%d", kilobytes/1024, grouping=True) + ' megabytes' x = Statistic(site=site, metric='disk_usage', value=s) x.save() return {'disk_usage': s} return {}
def get_site_du(site): (status, out, err) = _remote_ssh(site.platform, "du -ks %s" % (site.site_dir(), )) if status == 0: tmp = out.split('\t') kilobytes = int(tmp[0]) locale.setlocale(locale.LC_ALL, '') s = locale.format("%d", kilobytes / 1024, grouping=True) + ' megabytes' x = Statistic(site=site, metric='disk_usage', value=s) x.save() return {'disk_usage': s} return {}
def reconcile_sites_databases(platform): _ignored_databases = ['information_schema', 'mysql', '_reploy2', '_reploy2_results'] cmd = 'mysql -ss -e "%s"' % ('show databases;',) (status, out,err) = _remote_ssh(platform, cmd) unknown_databases = [] if status == 0: dbs = filter(None, [ (i if i not in _ignored_databases else None) for i in out.split('\n')]) for database in dbs: site = Site.objects.filter(database=database, platform=platform) if len(site) ==0: unknown_databases.append(database) return(True, 'Unaccounted for databases: %s' %(' \n'.join(unknown_databases),)) else: return(False, 'Could not retrieve list of databases.')
def reconcile_sites_dirs(platform): _ignored_files = [ "%s.php" %( platform.host,), 'http_host.pdx.edu.php', 'default'] cmd = "ls %s/sites/" % (platform.path,) (status, out, err) = _remote_ssh(platform, cmd) paths = out.split('\n') sites= Site.objects.filter(platform=platform) for site in sites: if site.files_dir in paths: paths.remove(site.files_dir) for i in _ignored_files: if i in paths: paths.remove(i) return(True, 'Unaccounted for directories: %s' %(' \n'.join(paths),))
def reconcile_sites_dirs(platform): _ignored_files = [ "%s.php" % (platform.host, ), 'http_host.pdx.edu.php', 'default' ] cmd = "ls %s/sites/" % (platform.path, ) (status, out, err) = _remote_ssh(platform, cmd) paths = out.split('\n') sites = Site.objects.filter(platform=platform) for site in sites: if site.files_dir in paths: paths.remove(site.files_dir) for i in _ignored_files: if i in paths: paths.remove(i) return (True, 'Unaccounted for directories: %s' % (' \n'.join(paths), ))
def reconcile_sites_databases(platform): _ignored_databases = [ 'information_schema', 'mysql', '_reploy2', '_reploy2_results' ] cmd = 'mysql -ss -e "%s"' % ('show databases;', ) (status, out, err) = _remote_ssh(platform, cmd) unknown_databases = [] if status == 0: dbs = filter(None, [(i if i not in _ignored_databases else None) for i in out.split('\n')]) for database in dbs: site = Site.objects.filter(database=database, platform=platform) if len(site) == 0: unknown_databases.append(database) return (True, 'Unaccounted for databases: %s' % (' \n'.join(unknown_databases), )) else: return (False, 'Could not retrieve list of databases.')
def wipe_site(site): logger.debug("wipe_site(): site=%s" %(site,) ) if is_clean(site): logger.error("wipe_site(): site=%s is already clean." %(site,)) return (True, "this site is already clean.") # chmod some files that the installer restricts. _remote_ssh(site.platform, 'chmod -R 777 %s' %(site.site_dir(),)) (status, err, out ) = _remote_ssh(site.platform, 'rm -Rf %s' % (site.site_dir(),)) _remote_ssh(site.platform, 'mysql -e "drop database %s;"' % (site.database,)) # default sites don't have symlinks. if not site.short_name == 'default': _remote_ssh(site.platform, 'unlink %s' % (site.site_symlink(),)) #TODO: verify the directory is gone. #TODO: find site.site_dir() -type f for any leftovers. log and return them. return (True, str(err) + str(out))
def wipe_site(site): logger.debug("wipe_site(): site=%s" % (site, )) if is_clean(site): logger.error("wipe_site(): site=%s is already clean." % (site, )) return (True, "this site is already clean.") # chmod some files that the installer restricts. _remote_ssh(site.platform, 'chmod -R 777 %s' % (site.site_dir(), )) (status, err, out) = _remote_ssh(site.platform, 'rm -Rf %s' % (site.site_dir(), )) _remote_ssh(site.platform, 'mysql -e "drop database %s;"' % (site.database, )) # default sites don't have symlinks. if not site.short_name == 'default': _remote_ssh(site.platform, 'unlink %s' % (site.site_symlink(), )) #TODO: verify the directory is gone. #TODO: find site.site_dir() -type f for any leftovers. log and return them. return (True, str(err) + str(out))
def migrate(site, new_platform): """Moves a site to a new platform. Site name, database remain the same.""" if site.platform == new_platform: logger.critical("migrate: trying to migrate ontop of itself.") return (False, "trying to migrate ontop of itself.") backup_result, msg = backup(site) if not backup_result: logger.critical("migrate: backup didn't succeed, bail") return (False, "migrate: backup didn't succeed, bail. reason: " + msg) dest_site = None q = Site.objects.filter(short_name=site.short_name, platform=new_platform) if len(q) == 1: dest_site = q[0] #TODO: Do i want to keep this? wipe_site(dest_site) else: dest_site = copy.deepcopy(site) dest_site.id = None dest_site.platform = new_platform dest_site.save() dest_site.set_flag('unqueried') dest_site.save() if not is_clean(dest_site): logger.critical('migrate: destination site not clean, bail') return (False, "destination site not clean. Wipe destination site") # Find the backup to use tarball_path = _find_backup_file(site) if tarball_path == None: logger.critical('migrate: backup succeeded but now where is it? help.') return (False, "backup succeeded but now where is it? help.") #push the tarball_path into place. _rsync_push(new_platform, tarball_path, settings.TEMPORARY_PATH) # from the tarball, only extract foo.pdx.edu.baz into the sites/ directory # for some reason, these files have a leading ./ which i need to specify when extracting. #also, tarball contains the permenant home of this backup- we need to basename() it tarball = os.path.basename(tarball_path) (status, out, err) = _remote_ssh(new_platform, "tar -zxvf %s -C %s --exclude settings.php ./%s" % (os.path.join(settings.TEMPORARY_PATH,tarball), os.path.join(new_platform.path, 'sites'), 'default' if site.short_name == 'default' else site.platform.host + '.' + site.short_name, ) ) (status, out, err) = _remote_ssh(new_platform, "tar -zxvf %s -C %s ./%s" % (os.path.join(settings.TEMPORARY_PATH,tarball), settings.TEMPORARY_PATH, site.database + '.sql')) #tarball components extracted: now we can remove it. _remote_ssh(new_platform, "rm %s" % (os.path.join(settings.TEMPORARY_PATH,tarball),)) #create and fill database _create_site_database(dest_site) #extract the sql dump out of the tarball locally. _local_cmd(['tar','-zxvf', tarball_path, '-C', settings.TEMPORARY_PATH, './' + site.database + '.sql']) _local_cmd(['mysql','-h', dest_site.platform.database, '-e','\. %s' % ( os.path.join(settings.TEMPORARY_PATH, dest_site.database + '.sql'),), site.database ]) #todo: stage database + replacements first. # this now hangs, so I'm attempting to connect to the databaes directly. # (status, out, err) = _remote_ssh(dest_site.platform, # 'mysql -n -v -v %s < %s' % ( # dest_site.database, # remote_sql_path)) #rename sitedir to the correct thing. _remote_ssh(new_platform, "mv %s %s" % ( os.path.join(new_platform.path,'sites',site.files_dir), dest_site.site_dir() )) #put in a settings.php new_settings_php = tempfile.mkstemp()[1] dest_site.settings_php(new_settings_php) (status, out, err) = _rsync_push(dest_site.platform, new_settings_php, os.path.join(dest_site.site_dir(), 'settings.php')) #cleanup after our tempfile os.remove(new_settings_php) _create_site_dirs(dest_site) _set_site_permissions(dest_site) #search / replace database. status = _db_replace(site, dest_site) return (True, "Still more work to do.")
def _create_site_database(site): (status, out, err) = _remote_ssh(site.platform, 'mysql -e "create database %s;"' % (site.database, )) return status == 0
def _create_site_dirs(site): #link _remote_ssh( site.platform, '/bin/ln -s %s %s' % ( site.platform.path, site.site_symlink(), )) _remote_ssh(site.platform, 'mkdir %s' % (site.site_dir(), )) _remote_ssh(site.platform, 'chown sdtuser:apache %s' % (site.site_dir(), )) _remote_ssh(site.platform, 'chmod 2775 %s' % (site.site_dir(), )) for directory in site.site_files_dir(): _remote_ssh(site.platform, 'mkdir %s' % (directory, )) _remote_ssh(site.platform, 'chown sdtuser:apache %s' % (directory, )) _remote_ssh(site.platform, 'chmod 2775 %s' % (directory, )) return True
def rename(site, new_site, clone=False): """ Renames a site. also renames the database. new_site may be a site or a string with the new short_name. if clone is true, the existing site will not be deleted.""" #TODO: genericify / wrap migrate so that it will handle clone, move & rename. # construct a dest_site dest_site = new_site if new_site.__class__ == str: dest_site = Site() dest_site.short_name = new_site dest_site.user = site.user dest_site.database = _generate_database_name(new_site) dest_site.platform = site.platform dest_site.save() dest_site.set_flag('unqueried') dest_site.set_flag('not installed') dest_site.long_name = site.long_name + ' copy' dest_site.database = _generate_database_name(dest_site.short_name) dest_site.save() backup_result, msg = backup(site) if not backup_result: logger.critical("rename: backup didn't succeed, bail") return (False, "rename: backup didn't succeed, bail. reason: " + msg) tarball_path = _find_backup_file(site) if tarball_path == None: logger.critical('rename: backup succeeded but now where is it? help.') return (False, "backup succeeded but now where is it? help.") #push the tarball_path into place. _rsync_push(dest_site.platform, tarball_path, settings.TEMPORARY_PATH) # from the tarball, only extract foo.pdx.edu.baz into the sites/ directory # for some reason, these files have a leading ./ which i need to specify when extracting. #also, tarball contains the permenant home of this backup- we need to basename() it tarball = os.path.basename(tarball_path) (status, out, err) = _remote_ssh(dest_site.platform, "tar -zxvf %s -C %s ./%s" % (os.path.join(settings.TEMPORARY_PATH,tarball), settings.TEMPORARY_PATH, # was os.path.join(dest_site.platform.path, 'sites'), 'default' if site.short_name == 'default' else site.platform.host + '.' + site.short_name, ) ) (status, out, err) = _remote_ssh(dest_site.platform, "tar -zxvf %s -C %s ./%s" % (os.path.join(settings.TEMPORARY_PATH,tarball), settings.TEMPORARY_PATH, site.database + '.sql')) #tarball components extracted: now we can remove it. _remote_ssh(dest_site.platform, "rm %s" % (os.path.join(settings.TEMPORARY_PATH,tarball),)) #create and fill database #todo: stage database + replacements first. _create_site_database(dest_site) (status, out, err) = _remote_ssh(dest_site.platform, 'mysql %s < %s' % ( dest_site.database, os.path.join(settings.TEMPORARY_PATH, site.database + '.sql') )) #rename sitedir to the correct thing. _remote_ssh(dest_site.platform, "cp -Rp %s %s" % ( os.path.join(settings.TEMPORARY_PATH, site.files_dir), dest_site.site_dir() )) #put in a settings.php new_settings_php = tempfile.mkstemp()[1] dest_site.settings_php(new_settings_php) (status, out, err) = _rsync_push(dest_site.platform, new_settings_php, os.path.join(dest_site.site_dir(), 'settings.php')) #cleanup after our tempfile os.remove(new_settings_php) _create_site_dirs(dest_site) _set_site_permissions(dest_site) #search / replace database. status = _db_replace(site, dest_site) return (True, "Still more work to do.")
def _create_site_dirs(site): #link _remote_ssh(site.platform, '/bin/ln -s %s %s' % ( site.platform.path, site.site_symlink(), ) ) _remote_ssh(site.platform, 'mkdir %s' % (site.site_dir(), )) _remote_ssh(site.platform, 'chown sdtuser:apache %s' % (site.site_dir(), )) _remote_ssh(site.platform, 'chmod 2775 %s' % (site.site_dir(), )) for directory in site.site_files_dir(): _remote_ssh(site.platform, 'mkdir %s' % (directory, )) _remote_ssh(site.platform, 'chown sdtuser:apache %s' % (directory, )) _remote_ssh(site.platform, 'chmod 2775 %s' % (directory, )) return True
def migrate(site, new_platform): """Moves a site to a new platform. Site name, database remain the same.""" if site.platform == new_platform: logger.critical("migrate: trying to migrate ontop of itself.") return (False, "trying to migrate ontop of itself.") backup_result, msg = backup(site) if not backup_result: logger.critical("migrate: backup didn't succeed, bail") return (False, "migrate: backup didn't succeed, bail. reason: " + msg) dest_site = None q = Site.objects.filter(short_name=site.short_name, platform=new_platform) if len(q) == 1: dest_site = q[0] #TODO: Do i want to keep this? wipe_site(dest_site) else: dest_site = copy.deepcopy(site) dest_site.id = None dest_site.platform = new_platform dest_site.save() dest_site.set_flag('unqueried') dest_site.save() if not is_clean(dest_site): logger.critical('migrate: destination site not clean, bail') return (False, "destination site not clean. Wipe destination site") # Find the backup to use tarball_path = _find_backup_file(site) if tarball_path == None: logger.critical('migrate: backup succeeded but now where is it? help.') return (False, "backup succeeded but now where is it? help.") #push the tarball_path into place. _rsync_push(new_platform, tarball_path, settings.TEMPORARY_PATH) # from the tarball, only extract foo.pdx.edu.baz into the sites/ directory # for some reason, these files have a leading ./ which i need to specify when extracting. #also, tarball contains the permenant home of this backup- we need to basename() it tarball = os.path.basename(tarball_path) (status, out, err) = _remote_ssh( new_platform, "tar -zxvf %s -C %s --exclude settings.php ./%s" % ( os.path.join(settings.TEMPORARY_PATH, tarball), os.path.join(new_platform.path, 'sites'), 'default' if site.short_name == 'default' else site.platform.host + '.' + site.short_name, )) (status, out, err) = _remote_ssh( new_platform, "tar -zxvf %s -C %s ./%s" % (os.path.join(settings.TEMPORARY_PATH, tarball), settings.TEMPORARY_PATH, site.database + '.sql')) #tarball components extracted: now we can remove it. _remote_ssh(new_platform, "rm %s" % (os.path.join(settings.TEMPORARY_PATH, tarball), )) #create and fill database _create_site_database(dest_site) #extract the sql dump out of the tarball locally. _local_cmd([ 'tar', '-zxvf', tarball_path, '-C', settings.TEMPORARY_PATH, './' + site.database + '.sql' ]) _local_cmd([ 'mysql', '-h', dest_site.platform.database, '-e', '\. %s' % (os.path.join(settings.TEMPORARY_PATH, dest_site.database + '.sql'), ), site.database ]) #todo: stage database + replacements first. # this now hangs, so I'm attempting to connect to the databaes directly. # (status, out, err) = _remote_ssh(dest_site.platform, # 'mysql -n -v -v %s < %s' % ( # dest_site.database, # remote_sql_path)) #rename sitedir to the correct thing. _remote_ssh( new_platform, "mv %s %s" % (os.path.join( new_platform.path, 'sites', site.files_dir), dest_site.site_dir())) #put in a settings.php new_settings_php = tempfile.mkstemp()[1] dest_site.settings_php(new_settings_php) (status, out, err) = _rsync_push(dest_site.platform, new_settings_php, os.path.join(dest_site.site_dir(), 'settings.php')) #cleanup after our tempfile os.remove(new_settings_php) _create_site_dirs(dest_site) _set_site_permissions(dest_site) #search / replace database. status = _db_replace(site, dest_site) return (True, "Still more work to do.")
def rename(site, new_site, clone=False): """ Renames a site. also renames the database. new_site may be a site or a string with the new short_name. if clone is true, the existing site will not be deleted.""" #TODO: genericify / wrap migrate so that it will handle clone, move & rename. # construct a dest_site dest_site = new_site if new_site.__class__ == str: dest_site = Site() dest_site.short_name = new_site dest_site.user = site.user dest_site.database = _generate_database_name(new_site) dest_site.platform = site.platform dest_site.save() dest_site.set_flag('unqueried') dest_site.set_flag('not installed') dest_site.long_name = site.long_name + ' copy' dest_site.database = _generate_database_name(dest_site.short_name) dest_site.save() backup_result, msg = backup(site) if not backup_result: logger.critical("rename: backup didn't succeed, bail") return (False, "rename: backup didn't succeed, bail. reason: " + msg) tarball_path = _find_backup_file(site) if tarball_path == None: logger.critical('rename: backup succeeded but now where is it? help.') return (False, "backup succeeded but now where is it? help.") #push the tarball_path into place. _rsync_push(dest_site.platform, tarball_path, settings.TEMPORARY_PATH) # from the tarball, only extract foo.pdx.edu.baz into the sites/ directory # for some reason, these files have a leading ./ which i need to specify when extracting. #also, tarball contains the permenant home of this backup- we need to basename() it tarball = os.path.basename(tarball_path) (status, out, err) = _remote_ssh( dest_site.platform, "tar -zxvf %s -C %s ./%s" % ( os.path.join(settings.TEMPORARY_PATH, tarball), settings. TEMPORARY_PATH, # was os.path.join(dest_site.platform.path, 'sites'), 'default' if site.short_name == 'default' else site.platform.host + '.' + site.short_name, )) (status, out, err) = _remote_ssh( dest_site.platform, "tar -zxvf %s -C %s ./%s" % (os.path.join(settings.TEMPORARY_PATH, tarball), settings.TEMPORARY_PATH, site.database + '.sql')) #tarball components extracted: now we can remove it. _remote_ssh(dest_site.platform, "rm %s" % (os.path.join(settings.TEMPORARY_PATH, tarball), )) #create and fill database #todo: stage database + replacements first. _create_site_database(dest_site) (status, out, err) = _remote_ssh( dest_site.platform, 'mysql %s < %s' % (dest_site.database, os.path.join(settings.TEMPORARY_PATH, site.database + '.sql'))) #rename sitedir to the correct thing. _remote_ssh( dest_site.platform, "cp -Rp %s %s" % (os.path.join( settings.TEMPORARY_PATH, site.files_dir), dest_site.site_dir())) #put in a settings.php new_settings_php = tempfile.mkstemp()[1] dest_site.settings_php(new_settings_php) (status, out, err) = _rsync_push(dest_site.platform, new_settings_php, os.path.join(dest_site.site_dir(), 'settings.php')) #cleanup after our tempfile os.remove(new_settings_php) _create_site_dirs(dest_site) _set_site_permissions(dest_site) #search / replace database. status = _db_replace(site, dest_site) return (True, "Still more work to do.")
def _create_site_database(site): (status, out, err) = _remote_ssh(site.platform, 'mysql -e "create database %s;"' % (site.database,)) return status == 0