2015-03-23 15:36:51 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.orchestration import ServiceController
|
2015-03-23 15:36:51 +00:00
|
|
|
|
2015-09-20 12:28:22 +00:00
|
|
|
from . import ApacheTrafficByHost
|
2015-03-23 15:36:51 +00:00
|
|
|
from .. import settings
|
|
|
|
|
|
|
|
|
|
|
|
class WordpressMuBackend(ServiceController):
|
2015-04-24 11:39:20 +00:00
|
|
|
"""
|
|
|
|
Creates a wordpress site on a WordPress MultiSite installation.
|
|
|
|
"""
|
2015-03-23 15:36:51 +00:00
|
|
|
verbose_name = _("Wordpress multisite")
|
2015-06-09 11:16:36 +00:00
|
|
|
model = 'saas.SaaS'
|
|
|
|
default_route_match = "saas.service == 'wordpress'"
|
2015-04-24 11:39:20 +00:00
|
|
|
doc_settings = (settings,
|
2015-09-29 12:35:22 +00:00
|
|
|
('SAAS_WORDPRESS_ADMIN_PASSWORD', 'SAAS_WORDPRESS_MAIN_URL')
|
2015-04-24 11:39:20 +00:00
|
|
|
)
|
2015-03-23 15:36:51 +00:00
|
|
|
|
|
|
|
def login(self, session):
|
2015-09-29 12:35:22 +00:00
|
|
|
main_url = self.get_main_url()
|
|
|
|
login_url = main_url + '/wp-login.php'
|
2015-03-23 15:36:51 +00:00
|
|
|
login_data = {
|
|
|
|
'log': 'admin',
|
2015-04-24 11:39:20 +00:00
|
|
|
'pwd': settings.SAAS_WORDPRESS_ADMIN_PASSWORD,
|
2015-03-23 15:36:51 +00:00
|
|
|
'redirect_to': '/wp-admin/'
|
|
|
|
}
|
|
|
|
response = session.post(login_url, data=login_data)
|
2015-09-29 12:35:22 +00:00
|
|
|
if response.url != main_url + '/wp-admin/':
|
2015-03-23 15:36:51 +00:00
|
|
|
raise IOError("Failure login to remote application")
|
|
|
|
|
2015-09-29 12:35:22 +00:00
|
|
|
def get_main_url(self):
|
|
|
|
main_url = settings.SAAS_WORDPRESS_MAIN_URL
|
|
|
|
return main_url.rstrip('/')
|
2015-03-23 15:36:51 +00:00
|
|
|
|
|
|
|
def validate_response(self, response):
|
|
|
|
if response.status_code != 200:
|
2015-04-20 14:23:10 +00:00
|
|
|
errors = re.findall(r'<body id="error-page">\n\t<p>(.*)</p></body>', response.content.decode('utf8'))
|
2015-03-23 15:36:51 +00:00
|
|
|
raise RuntimeError(errors[0] if errors else 'Unknown %i error' % response.status_code)
|
|
|
|
|
2015-09-10 10:34:07 +00:00
|
|
|
def get_id(self, session, saas):
|
2015-09-29 12:35:22 +00:00
|
|
|
search = self.get_main_url()
|
2015-09-10 10:34:07 +00:00
|
|
|
search += '/wp-admin/network/sites.php?s=%s&action=blogs' % saas.name
|
2015-03-23 15:36:51 +00:00
|
|
|
regex = re.compile(
|
|
|
|
'<a href="http://[\.\-\w]+/wp-admin/network/site-info\.php\?id=([0-9]+)"\s+'
|
2015-09-10 10:34:07 +00:00
|
|
|
'class="edit">%s</a>' % saas.name
|
2015-03-23 15:36:51 +00:00
|
|
|
)
|
2015-04-20 14:23:10 +00:00
|
|
|
content = session.get(search).content.decode('utf8')
|
2015-03-23 15:36:51 +00:00
|
|
|
# Get id
|
|
|
|
ids = regex.search(content)
|
|
|
|
if not ids:
|
2015-09-10 10:34:07 +00:00
|
|
|
raise RuntimeError("Blog '%s' not found" % saas.name)
|
2015-03-23 15:36:51 +00:00
|
|
|
ids = ids.groups()
|
|
|
|
if len(ids) > 1:
|
|
|
|
raise ValueError("Multiple matches")
|
|
|
|
# Get wpnonce
|
|
|
|
wpnonce = re.search(r'<span class="delete">(.*)</span>', content).groups()[0]
|
|
|
|
wpnonce = re.search(r'_wpnonce=([^"]*)"', wpnonce).groups()[0]
|
|
|
|
return int(ids[0]), wpnonce
|
|
|
|
|
2015-09-10 10:34:07 +00:00
|
|
|
def create_blog(self, saas, server):
|
2015-03-23 15:36:51 +00:00
|
|
|
session = requests.Session()
|
|
|
|
self.login(session)
|
|
|
|
|
|
|
|
# Check if blog already exists
|
|
|
|
try:
|
2015-09-10 10:34:07 +00:00
|
|
|
self.get_id(session, saas)
|
2015-03-23 15:36:51 +00:00
|
|
|
except RuntimeError:
|
2015-09-29 12:35:22 +00:00
|
|
|
url = self.get_main_url()
|
2015-03-23 15:36:51 +00:00
|
|
|
url += '/wp-admin/network/site-new.php'
|
2015-04-20 14:23:10 +00:00
|
|
|
content = session.get(url).content.decode('utf8')
|
2015-03-23 15:36:51 +00:00
|
|
|
|
|
|
|
wpnonce = re.compile('name="_wpnonce_add-blog"\s+value="([^"]*)"')
|
|
|
|
wpnonce = wpnonce.search(content).groups()[0]
|
|
|
|
|
|
|
|
url += '?action=add-site'
|
|
|
|
data = {
|
2015-09-10 10:34:07 +00:00
|
|
|
'blog[domain]': saas.name,
|
|
|
|
'blog[title]': saas.name,
|
|
|
|
'blog[email]': saas.account.email,
|
2015-03-23 15:36:51 +00:00
|
|
|
'_wpnonce_add-blog': wpnonce,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Validate response
|
|
|
|
response = session.post(url, data=data)
|
|
|
|
self.validate_response(response)
|
|
|
|
|
2015-09-10 10:34:07 +00:00
|
|
|
def delete_blog(self, saas, server):
|
2015-03-23 15:36:51 +00:00
|
|
|
session = requests.Session()
|
|
|
|
self.login(session)
|
|
|
|
|
|
|
|
try:
|
2015-09-10 10:34:07 +00:00
|
|
|
id, wpnonce = self.get_id(session, saas)
|
2015-03-23 15:36:51 +00:00
|
|
|
except RuntimeError:
|
|
|
|
pass
|
|
|
|
else:
|
2015-09-29 12:35:22 +00:00
|
|
|
delete = self.get_main_url()
|
2015-03-23 15:36:51 +00:00
|
|
|
delete += '/wp-admin/network/sites.php?action=confirm&action2=deleteblog'
|
|
|
|
delete += '&id=%d&_wpnonce=%s' % (id, wpnonce)
|
|
|
|
|
2015-04-20 14:23:10 +00:00
|
|
|
content = session.get(delete).content.decode('utf8')
|
2015-03-23 15:36:51 +00:00
|
|
|
wpnonce = re.compile('name="_wpnonce"\s+value="([^"]*)"')
|
|
|
|
wpnonce = wpnonce.search(content).groups()[0]
|
|
|
|
data = {
|
|
|
|
'action': 'deleteblog',
|
|
|
|
'id': id,
|
|
|
|
'_wpnonce': wpnonce,
|
|
|
|
'_wp_http_referer': '/wp-admin/network/sites.php',
|
|
|
|
}
|
2015-09-29 12:35:22 +00:00
|
|
|
delete = self.get_main_url()
|
2015-03-23 15:36:51 +00:00
|
|
|
delete += '/wp-admin/network/sites.php?action=deleteblog'
|
|
|
|
response = session.post(delete, data=data)
|
|
|
|
self.validate_response(response)
|
|
|
|
|
2015-09-10 10:34:07 +00:00
|
|
|
def save(self, saas):
|
|
|
|
self.append(self.create_blog, saas)
|
2015-03-23 15:36:51 +00:00
|
|
|
|
2015-09-10 10:34:07 +00:00
|
|
|
def delete(self, saas):
|
|
|
|
self.append(self.delete_blog, saas)
|
2015-09-16 12:15:05 +00:00
|
|
|
|
|
|
|
|
2015-09-20 12:28:22 +00:00
|
|
|
class WordpressMuTraffic(ApacheTrafficByHost):
|
|
|
|
__doc__ = ApacheTrafficByHost.__doc__
|
2015-09-16 12:15:05 +00:00
|
|
|
verbose_name = _("Wordpress MU Traffic")
|
|
|
|
default_route_match = "saas.service == 'wordpress'"
|
|
|
|
doc_settings = (settings,
|
|
|
|
('SAAS_TRAFFIC_IGNORE_HOSTS', 'SAAS_WORDPRESS_LOG_PATH')
|
|
|
|
)
|
2015-09-17 11:21:35 +00:00
|
|
|
log_path = settings.SAAS_WORDPRESS_LOG_PATH
|