def site_migrate(request): form = Migrate(request.POST if request.POST else None) if form.is_valid(): #what sites: l = request.GET['ids'].split(',') site_ids = [int(i) for i in l] sites = Site.objects.filter(pk__in=site_ids) platform = Platform.objects.get(pk=request.POST['new_platform']) for site in sites: ctask = migrate.delay(site, platform) event = Event(task_id=ctask.task_id, site=site, user=request.user, event='migrate') event.save() messages.add_message( request, messages.INFO, "The migration of the site %s has been queued: %s" % (site, ctask.task_id)) # this needs to redirect or something. return redirect(urlresolvers.reverse('admin:deploy_site_changelist')) data = { 'user': request.user, 'form': form, } return render_to_response('migrate.html', data)
def site_clone(request): l = request.GET['ids'].split(',') site_id = [int(i) for i in l][0] site = get_object_or_404(Site, pk=site_id) default_name = "%s_copy" % (site.short_name, ) form = Clone(request.POST if request.POST else None, initial={'new_name': default_name}) if form.is_valid(): #what sites: do_clone = form.cleaned_data['clone'] ctask = rename.delay(site, form.cleaned_data['new_name'], do_clone) event = Event(task_id=ctask.task_id, site=site, user=request.user, event='migrate') event.save() messages.add_message( request, messages.INFO, "The clone of the site %s has been queued: %s" % (site, ctask.task_id)) return redirect(urlresolvers.reverse('admin:deploy_site_changelist')) data = { 'user': request.user, 'form': form, } return render_to_response('clone.html', data)
def site_clone(request): l = request.GET["ids"].split(",") site_id = [int(i) for i in l][0] site = get_object_or_404(Site, pk=site_id) default_name = "%s_copy" % (site.short_name,) form = Clone(request.POST if request.POST else None, initial={"new_name": default_name}) if form.is_valid(): # what sites: do_clone = form.cleaned_data["clone"] ctask = rename.delay(site, form.cleaned_data["new_name"], do_clone) event = Event(task_id=ctask.task_id, site=site, user=request.user, event="migrate") event.save() messages.add_message( request, messages.INFO, "The clone of the site %s has been queued: %s" % (site, ctask.task_id) ) return redirect(urlresolvers.reverse("admin:deploy_site_changelist")) data = {"user": request.user, "form": form} return render_to_response("clone.html", data)
def site_manage(request, sid): site = get_object_or_404(Site, pk=sid) events = Event.objects.filter(site=site).order_by("date") callbacks = Event.objects.filter(site=site, event="status").order_by("date") # this is gross and I would really like to be rid of it. op = request.POST.get("submit", None) form_instance = request.POST if request.method == "POST" else None forms = { "clone": Clone(form_instance if op == "clone" else None), "migrate": Migrate(form_instance if op == "migrate" else None), "drush": Drush(form_instance if op == "drush" else None), } ctask = None if not op == None: if op == "enable": ctask = enable.delay(site) elif op == "disable": ctask = disable.delay(site) elif op == "cache": ctask = cacheclear.delay(site) elif op == "varnish": ctask = varnishclear.delay(site) elif op == "verify": ctask = verify.delay(site) elif op == "migrate": if forms["migrate"].is_valid(): new_platform = Platform.objects.get(pk=forms["migrate"].cleaned_data["new_platform"]) if new_platform: ctask = migrate.delay(site, new_platform) elif op == "clone" or op == "rename": if forms["clone"].is_valid(): new_name = forms["clone"].cleaned_data["new_name"] ctask = rename.delay(site, new_name, op == "clone") if not ctask == None: # record the task as an event, that way it will display in the task log. event = Event(task_id=ctask.task_id, site=site, user=request.user, event=op) event.save() messages.add_message( request, messages.INFO, "The %s of the site %s has been queued as '%s'." % (op, site, ctask.task_id) ) return redirect(urlresolvers.reverse("deploy.views.site_manage", args=(sid,))) data = {"events": events, "user": request.user, "site": site, "callbacks": callbacks, "forms": forms} return render_to_response("site-manage.html", data)
def drush(site, cmd): (status, out, err) = _remote_drush(site, cmd) logger.debug("drush: %s" % (out, )) logger.debug("drush: %s" % (err, )) event = Event( site=site, event='drush', #user=request.user, task_id=drush.request.id, status=status == 0, message="%s" % (out, ), # + "\n\n" + err, ) event.save() return (status == 0, "%s\n%s" % (str(out), str(err)))
def drush(site, cmd): (status, out, err) = _remote_drush(site, cmd) logger.debug("drush: %s" %(out,)) logger.debug("drush: %s" %(err,)) event = Event( site=site, event='drush', #user=request.user, task_id=drush.request.id, status= status==0, message = "%s" %( out, ), # + "\n\n" + err, ) event.save() return (status==0, "%s\n%s" %(str(out), str(err)))
def backup(site): """Returns a path to a backup or false if it doesn't succeed.""" path = tempfile.mkdtemp(prefix='sdt',dir=settings.TEMPORARY_PATH) logger.info('backup: local temporary_path=%s' % (path,)) status = 1 cacheclear(site); db = _backup_db(site,path) fs = _backup_files(site,path) if not (db and fs): logger.info('backup: _backup_db was %s' % (str(db),) ) logger.info('backup: _backup_fs was %s' % (str(fs),) ) shutil.rmtree(path) if not backup.request.called_directly: event = Event(site=site, event='backup', #user=request.user, task_id=backup.request.id, status=False, message = '', ) event.save() return (False, "Backup didn't complete.") site_name = site.platform.host + '.' + site.short_name friendly_backup_path = os.path.join(settings.BACKUP_PATH, site_name + '-' + datetime.datetime.now().strftime('%Y%m%d.%H%M%S') + '.tgz') logger.info('backup: destination_path=%s' %(friendly_backup_path,)) cmd = ['tar','-C', path, '-cpzf', friendly_backup_path, '.'] logger.info('backup: command is: %s' % (cmd,)) process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output,stderr = process.communicate() status = process.poll() try: #remove temporary directory shutil.rmtree(path) except: logger.critical("backup: oops; can't delete: %s" % (path,)) if not backup.request.called_directly: event = Event(site=site, event='backup', #user=request.user, task_id=backup.request.id, status=status==0, message = _out, ) event.save() return ( status==0, "backup is %s" %(friendly_backup_path,))
def site_migrate(request): form = Migrate(request.POST if request.POST else None) if form.is_valid(): # what sites: l = request.GET["ids"].split(",") site_ids = [int(i) for i in l] sites = Site.objects.filter(pk__in=site_ids) platform = Platform.objects.get(pk=request.POST["new_platform"]) for site in sites: ctask = migrate.delay(site, platform) event = Event(task_id=ctask.task_id, site=site, user=request.user, event="migrate") event.save() messages.add_message( request, messages.INFO, "The migration of the site %s has been queued: %s" % (site, ctask.task_id) ) # this needs to redirect or something. return redirect(urlresolvers.reverse("admin:deploy_site_changelist")) data = {"user": request.user, "form": form} return render_to_response("migrate.html", data)
def site_offline(self, request, queryset): for site in queryset: ctask = disable.delay(site) event = Event( task_id=ctask.task_id, site=site, event="offline") event.save() messages.add_message(request, messages.INFO, "%s has been submitted to be entered into maintenance: %s" % ( site, ctask.task_id) )
def site_manage(request, sid): site = get_object_or_404(Site, pk=sid) events = Event.objects.filter(site=site).order_by('date') callbacks = Event.objects.filter(site=site, event='status').order_by('date') # this is gross and I would really like to be rid of it. op = request.POST.get('submit', None) form_instance = request.POST if request.method == 'POST' else None forms = { 'clone': Clone(form_instance if op == 'clone' else None), 'migrate': Migrate(form_instance if op == 'migrate' else None), 'drush': Drush(form_instance if op == 'drush' else None), } ctask = None if not op == None: if op == 'enable': ctask = enable.delay(site) elif op == 'disable': ctask = disable.delay(site) elif op == 'cache': ctask = cacheclear.delay(site) elif op == 'varnish': ctask = varnishclear.delay(site) elif op == 'verify': ctask = verify.delay(site) elif op == 'migrate': if forms['migrate'].is_valid(): new_platform = Platform.objects.get( pk=forms['migrate'].cleaned_data['new_platform']) if new_platform: ctask = migrate.delay(site, new_platform) elif op == 'clone' or op == 'rename': if forms['clone'].is_valid(): new_name = forms['clone'].cleaned_data['new_name'] ctask = rename.delay(site, new_name, op == 'clone') if not ctask == None: #record the task as an event, that way it will display in the task log. event = Event(task_id=ctask.task_id, site=site, user=request.user, event=op) event.save() messages.add_message( request, messages.INFO, "The %s of the site %s has been queued as '%s'." % (op, site, ctask.task_id)) return redirect( urlresolvers.reverse('deploy.views.site_manage', args=(sid, ))) data = { 'events': events, 'user': request.user, 'site': site, 'callbacks': callbacks, 'forms': forms } return render_to_response('site-manage.html', data)
def backup(site): """Returns a path to a backup or false if it doesn't succeed.""" path = tempfile.mkdtemp(prefix='sdt', dir=settings.TEMPORARY_PATH) logger.info('backup: local temporary_path=%s' % (path, )) status = 1 cacheclear(site) db = _backup_db(site, path) fs = _backup_files(site, path) if not (db and fs): logger.info('backup: _backup_db was %s' % (str(db), )) logger.info('backup: _backup_fs was %s' % (str(fs), )) shutil.rmtree(path) if not backup.request.called_directly: event = Event( site=site, event='backup', #user=request.user, task_id=backup.request.id, status=False, message='', ) event.save() return (False, "Backup didn't complete.") site_name = site.platform.host + '.' + site.short_name friendly_backup_path = os.path.join( settings.BACKUP_PATH, site_name + '-' + datetime.datetime.now().strftime('%Y%m%d.%H%M%S') + '.tgz') logger.info('backup: destination_path=%s' % (friendly_backup_path, )) cmd = ['tar', '-C', path, '-cpzf', friendly_backup_path, '.'] logger.info('backup: command is: %s' % (cmd, )) process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output, stderr = process.communicate() status = process.poll() try: #remove temporary directory shutil.rmtree(path) except: logger.critical("backup: oops; can't delete: %s" % (path, )) if not backup.request.called_directly: event = Event( site=site, event='backup', #user=request.user, task_id=backup.request.id, status=status == 0, message=_out, ) event.save() return (status == 0, "backup is %s" % (friendly_backup_path, ))
def verify(site): _out = '' (status, out, err) = _remote_drush(site, "vget maintenance_mode") _out += out site.unset_flag('error') site.unset_flag('unqueried') (status, out, err) = _remote_drush(site, "vget site_name") _out += out if status == 0: site.long_name = parse_vget('site_name', out) (status, extra, err) = _remote_drush(site, "vget site_name_extra") _out += extra if status == 0: site.long_name += ": " + parse_vget('site_name_extra', extra) site.set_flag('ok') site.unset_flag('unqueried') site.unset_flag('not installed') site.save() else: site.unset_flag('ok') site.save() if status == 0: x = parse_vget('maintenance_mode', out) if x == 1: site.set_flag('maintenance') else: site.unset_flag('maintenance') site.save() else: site.unset_flag('maintenance') site.save() (status, out, err) = _remote_drush(site, "vget site_mail") _out += out if status == 0: site.contact_email = parse_vget('site_mail', out) site.save() else: site.set_flag('error') site.save() #TODO: possibly could need to flush varnish first before checking. status = _check_site(site) if status == 500 or status > 500: site.set_flag('error') if status == 404: site.set_flag('not installed') if not status == 200: site.unset_flag('ok') site.save() else: (status, out, err) = _remote_drush(site, "status") if status == 0: db = parse_status('Database name', out) logger.error( 'verify: updating database from %s to %s for site %s.' % (site.database, db, str(site))) site.database = db site.save() # if we're this far, profile the site. asynchronously. get_site_status.delay(site) #create a log entry. event = Event( site=site, event='verify', #user=request.user, task_id=verify.request.id, status='', message=_out, ) event.save() return (True, "This command completed sucessfully.")
def verify(site): _out = '' (status, out, err) = _remote_drush(site, "vget maintenance_mode") _out += out site.unset_flag('error') site.unset_flag('unqueried') (status, out, err) = _remote_drush(site, "vget site_name") _out += out if status == 0: site.long_name = parse_vget('site_name', out) (status, extra, err) = _remote_drush(site, "vget site_name_extra") _out += extra if status == 0: site.long_name += ": " + parse_vget('site_name_extra', extra) site.set_flag('ok') site.unset_flag('unqueried') site.unset_flag('not installed') site.save() else: site.unset_flag('ok') site.save() if status == 0: x = parse_vget('maintenance_mode', out) if x == 1: site.set_flag('maintenance') else: site.unset_flag('maintenance') site.save() else: site.unset_flag('maintenance') site.save() (status, out, err) = _remote_drush(site, "vget site_mail") _out += out if status == 0: site.contact_email = parse_vget('site_mail', out) site.save() else: site.set_flag('error') site.save() #TODO: possibly could need to flush varnish first before checking. status = _check_site(site) if status == 500 or status > 500: site.set_flag('error') if status == 404: site.set_flag('not installed') if not status == 200: site.unset_flag('ok') site.save() else: (status, out, err) = _remote_drush(site, "status") if status == 0: db = parse_status('Database name', out) logger.error('verify: updating database from %s to %s for site %s.' %( site.database, db, str(site))) site.database = db site.save() # if we're this far, profile the site. asynchronously. get_site_status.delay(site) #create a log entry. event = Event( site=site, event='verify', #user=request.user, task_id=verify.request.id, status='', message = _out, ) event.save() return (True, "This command completed sucessfully.")