import re import sys import textwrap import time from functools import partial from urllib.parse import urlparse import requests from django.utils.translation import ugettext_lazy as _ from orchestra.contrib.orchestration import ServiceController from . import ApacheTrafficByHost from .. import settings class WordpressMuController(ServiceController): """ Creates a wordpress site on a WordPress MultiSite installation. You should point it to the database server """ verbose_name = _("Wordpress multisite") model = 'saas.SaaS' default_route_match = "saas.service == 'wordpress'" doc_settings = (settings, ('SAAS_WORDPRESS_ADMIN_PASSWORD', 'SAAS_WORDPRESS_MAIN_URL', 'SAAS_WORDPRESS_VERIFY_SSL') ) VERIFY = settings.SAAS_WORDPRESS_VERIFY_SSL def with_retry(self, method, *args, retries=1, sleep=0.5, **kwargs): for i in range(retries): try: return method(*args, verify=self.VERIFY, **kwargs) except requests.exceptions.ConnectionError: if i >= retries: raise sys.stderr.write("Connection error while {method}{args}, retry {i}/{retries}\n".format( method=method.__name__, args=str(args), i=i, retries=retries)) time.sleep(sleep) def login(self, session): main_url = self.get_main_url() login_url = main_url + '/wp-login.php' login_data = { 'log': 'admin', 'pwd': settings.SAAS_WORDPRESS_ADMIN_PASSWORD, 'redirect_to': '/wp-admin/' } sys.stdout.write("Login URL: %s\n" % login_url) response = self.with_retry(session.post, login_url, data=login_data) if response.url != main_url + '/wp-admin/': raise IOError("Failure login to remote application (%s)" % login_url) def get_main_url(self): main_url = settings.SAAS_WORDPRESS_MAIN_URL return main_url.rstrip('/') def validate_response(self, response): if response.status_code != 200: content = response.content.decode('utf8') errors = re.findall(r'
\n\t(.*)
', content) raise RuntimeError(errors[0] if errors else 'Unknown %i error' % response.status_code) def get_id(self, session, saas): blog_id = saas.data.get('blog_id') search = self.get_main_url() search += '/wp-admin/network/sites.php?s=%s&action=blogs' % saas.name regex = re.compile( '%s' % saas.name ) sys.stdout.write("Search URL: %s\n" % search) response = self.with_retry(session.get, search) content = response.content.decode('utf8') # Get id ids = regex.search(content) if not ids and not blog_id: raise RuntimeError("Blog '%s' not found" % saas.name) if ids: ids = ids.groups() if len(ids) > 1 and not blog_id: raise ValueError("Multiple matches") return blog_id or int(ids[0]), content def create_blog(self, saas, server): if saas.data.get('blog_id'): return session = requests.Session() self.login(session) # Check if blog already exists try: blog_id, content = self.get_id(session, saas) except RuntimeError: url = self.get_main_url() url += '/wp-admin/network/site-new.php' sys.stdout.write("Create URL: %s\n" % url) content = self.with_retry(session.get, url).content.decode('utf8') wpnonce = re.compile('name="_wpnonce_add-blog"\s+value="([^"]*)"') try: wpnonce = wpnonce.search(content).groups()[0] except AttributeError: raise RuntimeError("wpnonce not foud in %s" % content) url += '?action=add-site' data = { 'blog[domain]': saas.name, 'blog[title]': saas.name, 'blog[email]': saas.account.email, '_wpnonce_add-blog': wpnonce, } # Validate response response = self.with_retry(session.post, url, data=data) self.validate_response(response) blog_id = re.compile(r'') content = response.content.decode('utf8') blog_id = blog_id.search(content).groups()[0] sys.stdout.write("Created blog ID: %s\n" % blog_id) saas.data['blog_id'] = int(blog_id) saas.save(update_fields=('data',)) return True else: sys.stdout.write("Retrieved blog ID: %s\n" % blog_id) saas.data['blog_id'] = int(blog_id) saas.save(update_fields=('data',)) def do_action(self, action, session, id, content, saas): url_regex = r"""]*)['"]>""" % action action_url = re.search(url_regex, content).groups()[0].replace("&", '&') sys.stdout.write("%s confirm URL: %s\n" % (action, action_url)) content = self.with_retry(session.get, action_url).content.decode('utf8') wpnonce = re.compile('name="_wpnonce"\s+value="([^"]*)"') try: wpnonce = wpnonce.search(content).groups()[0] except AttributeError: raise RuntimeError(re.search(r'([^<]+)<', content).groups()[0]) data = { 'action': action, 'id': id, '_wpnonce': wpnonce, '_wp_http_referer': '/wp-admin/network/sites.php', } action_url = self.get_main_url() action_url += '/wp-admin/network/sites.php?action=%sblog' % action sys.stdout.write("%s URL: %s\n" % (action, action_url)) response = self.with_retry(session.post, action_url, data=data) self.validate_response(response) def is_active(self, content): return bool( re.findall(r"""