Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python2.7
- import urllib,urllib2
- import tarfile
- import os, re
- import sys
- import json
- import random
- import shutil
- from BeautifulSoup import BeautifulSoup
- import mysql.connector
- import requests
- from tqdm import tqdm
- def replace_all(text, dic):
- for i, j in dic.iteritems():
- text = text.replace(i, j)
- return text
- def id_gen(length):
- number = '0123456789'
- alpha = 'abcdefghijklmnopqrstuvwxyz'
- id = ''
- for i in range(0,length,2):
- id += random.choice(number)
- id += random.choice(alpha)
- return id
- wp_url = 'http://fr.wordpress.org/'
- domain = sys.argv[1]
- folder = sys.argv[1].replace('.','_')
- folder_root = '/srv/'+folder
- username = folder.replace('_','').replace('-','')
- tmpdir = '/tmp/wp/'
- base_conf = '/srv/staging/example.json'
- themedir = sys.argv[1].split('.')[0]
- dbname = 'demowec_'+themedir
- confsite = json.loads(open(base_conf).read())
- ftppassword = id_gen(32)
- sqlpassword = id_gen(32)
- # check for extraction directories existence
- if not os.path.isdir(folder_root):
- os.makedirs(folder_root)
- else:
- shutil.rmtree(folder_root)
- os.makedirs(folder_root)
- if not os.path.isdir(tmpdir):
- os.makedirs(tmpdir)
- html_data = urllib2.urlopen(wp_url).read()
- soup = BeautifulSoup(html_data)
- latest_wp = soup.findAll('p', { 'class': 'download-tar' } )[0].findAll('a')[0]["href"] #Find the proper tag
- #wp_dl = requests.get(latest_wp).save(tmpdir+'/latest.tar.gz')
- #with open("100K", "wb") as handle:
- # for data in tqdm(wp_dl.iter_content()):
- # handle.write(data)
- urllib.urlretrieve(latest_wp, tmpdir+'/latest.tar.gz')
- wp_dl = tarfile.open(tmpdir+'/latest.tar.gz', 'r:gz')
- wp_dl.extractall(tmpdir)
- wp_dl.close()
- os.rename(tmpdir+'/wordpress', folder_root)
- salt_data = urllib2.urlopen('http://api.wordpress.org/secret-key/1.1/salt/').read()
- cnx = mysql.connector.connect(user='root', password='xxxxxxxxxxxxxxxx', host='127.0.0.1')
- cnx.cursor().execute("create database %s" % dbname )
- cnx.cursor().execute("grant all privileges on %s.* to '%s'@'localhost' identified by '%s'" % (dbname, username, sqlpassword) )
- wp_conf = open(folder_root+'/wp-config-sample.php', 'rt').read()
- unique_str = 'put your unique phrase here'
- while True:
- wp_conf = wp_conf.replace(unique_str, id_gen(64), 1)
- if wp_conf.find(unique_str) < 0:
- break
- wp_conf = wp_conf.replace('votre_nom_de_bdd', dbname)
- wp_conf = wp_conf.replace('votre_utilisateur_de_bdd', username)
- wp_conf = wp_conf.replace('votre_mdp_de_bdd', sqlpassword)
- new_conf_file = open(folder_root+'/wp-config.php', "w")
- new_conf_file.write(wp_conf)
- new_conf_file.close()
- shutil.copytree('/srv/staging/defaulttheme/html5blank/dist', folder_root+'/wp-content/themes/'+themedir)
- shutil.rmtree(folder_root+'/wp-content/themes/twentyfifteen')
- shutil.rmtree(folder_root+'/wp-content/themes/twentysixteen')
- shutil.rmtree(folder_root+'/wp-content/themes/twentyfourteen')
- os.popen('chown -R www-data:www-data '+folder_root)
- #os.popen('wp theme activate '+themedir)
- confsite['root'] = folder_root
- confsite['domains'][0]['domain'] = domain
- confsite['name'] = domain
- confsite['extensions']['ajenti.plugins.vh-pureftpd.pureftpd.PureFTPDExtension']['username'] = username
- confsite['extensions']['ajenti.plugins.vh-pureftpd.pureftpd.PureFTPDExtension']['password'] = ftppassword
- confsite['extensions']['ajenti.plugins.vh-pureftpd.pureftpd.PureFTPDExtension']['path'] = folder_root
- confsite['extensions']['ajenti.plugins.vh-pureftpd.pureftpd.PureFTPDExtension']['created'] = 'false'
- confsite['extensions']['ajenti.plugins.vh-mysql.mysql.MySQLExtension']['users'][0]['name'] = username;
- confsite['extensions']['ajenti.plugins.vh-mysql.mysql.MySQLExtension']['users'][0]['password'] = sqlpassword
- confsite['extensions']['ajenti.plugins.vh-mysql.mysql.MySQLExtension']['databases'][0]['name'] = dbname
- new_aconf_file = open('/srv/staging/%s.json' % username, "w")
- new_aconf_file.write(json.dumps(confsite))
- new_aconf_file.close()
- os.popen('ajenti-ipc v import /srv/staging/'+username+'.json')
- os.popen('ajenti-ipc v apply')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement