Advertisement
Guest User

Untitled

a guest
May 25th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.13 KB | None | 0 0
  1. #!/usr/bin/python2.7
  2. import urllib,urllib2
  3. import tarfile
  4. import os, re
  5. import sys
  6. import json
  7. import random
  8. import shutil
  9. from BeautifulSoup import BeautifulSoup
  10. import mysql.connector
  11. import requests
  12. from tqdm import tqdm
  13.  
  14. def replace_all(text, dic):
  15.     for i, j in dic.iteritems():
  16.         text = text.replace(i, j)
  17.     return text
  18.    
  19. def id_gen(length):
  20.     number = '0123456789'
  21.     alpha = 'abcdefghijklmnopqrstuvwxyz'
  22.     id = ''
  23.     for i in range(0,length,2):
  24.         id += random.choice(number)
  25.         id += random.choice(alpha)
  26.     return id
  27.      
  28. wp_url = 'http://fr.wordpress.org/'
  29. domain = sys.argv[1]
  30. folder = sys.argv[1].replace('.','_')
  31. folder_root = '/srv/'+folder
  32. username = folder.replace('_','').replace('-','')
  33. tmpdir = '/tmp/wp/'
  34. base_conf = '/srv/staging/example.json'
  35. themedir = sys.argv[1].split('.')[0]
  36. dbname = 'demowec_'+themedir
  37.  
  38. confsite = json.loads(open(base_conf).read())
  39.                
  40. ftppassword = id_gen(32)
  41. sqlpassword = id_gen(32)
  42.  
  43.  
  44. # check for extraction directories existence
  45.  
  46. if not os.path.isdir(folder_root):
  47.     os.makedirs(folder_root)
  48. else:
  49.     shutil.rmtree(folder_root)
  50.     os.makedirs(folder_root)
  51.    
  52. if not os.path.isdir(tmpdir):
  53.     os.makedirs(tmpdir)
  54.    
  55.    
  56.  
  57. html_data = urllib2.urlopen(wp_url).read()
  58. soup = BeautifulSoup(html_data)
  59. latest_wp = soup.findAll('p', { 'class': 'download-tar' } )[0].findAll('a')[0]["href"] #Find the proper tag
  60.  
  61. #wp_dl = requests.get(latest_wp).save(tmpdir+'/latest.tar.gz')
  62.  
  63. #with open("100K", "wb") as handle:
  64. #    for data in tqdm(wp_dl.iter_content()):
  65. #        handle.write(data)
  66.  
  67. urllib.urlretrieve(latest_wp, tmpdir+'/latest.tar.gz')
  68.  
  69. wp_dl = tarfile.open(tmpdir+'/latest.tar.gz', 'r:gz')
  70. wp_dl.extractall(tmpdir)
  71. wp_dl.close()
  72. os.rename(tmpdir+'/wordpress', folder_root)
  73.  
  74.  
  75. salt_data = urllib2.urlopen('http://api.wordpress.org/secret-key/1.1/salt/').read()
  76.  
  77.  
  78.  
  79. cnx = mysql.connector.connect(user='root', password='xxxxxxxxxxxxxxxx', host='127.0.0.1')
  80. cnx.cursor().execute("create database %s" % dbname )
  81. cnx.cursor().execute("grant all privileges on %s.* to '%s'@'localhost' identified by '%s'" % (dbname, username, sqlpassword) )
  82.  
  83.  
  84. wp_conf = open(folder_root+'/wp-config-sample.php', 'rt').read()
  85.  
  86. unique_str = 'put your unique phrase here'
  87.  
  88. while True:    
  89.     wp_conf = wp_conf.replace(unique_str, id_gen(64), 1)
  90.     if wp_conf.find(unique_str) < 0:
  91.         break
  92.  
  93. wp_conf = wp_conf.replace('votre_nom_de_bdd', dbname)
  94. wp_conf = wp_conf.replace('votre_utilisateur_de_bdd', username)
  95. wp_conf = wp_conf.replace('votre_mdp_de_bdd', sqlpassword)
  96.  
  97. new_conf_file = open(folder_root+'/wp-config.php', "w")
  98. new_conf_file.write(wp_conf)
  99. new_conf_file.close()
  100.  
  101. shutil.copytree('/srv/staging/defaulttheme/html5blank/dist', folder_root+'/wp-content/themes/'+themedir)
  102. shutil.rmtree(folder_root+'/wp-content/themes/twentyfifteen')
  103. shutil.rmtree(folder_root+'/wp-content/themes/twentysixteen')
  104. shutil.rmtree(folder_root+'/wp-content/themes/twentyfourteen')
  105.  
  106. os.popen('chown -R www-data:www-data '+folder_root)
  107.  
  108. #os.popen('wp theme activate '+themedir)
  109.  
  110. confsite['root'] = folder_root
  111. confsite['domains'][0]['domain'] = domain
  112. confsite['name'] = domain
  113. confsite['extensions']['ajenti.plugins.vh-pureftpd.pureftpd.PureFTPDExtension']['username'] = username
  114. confsite['extensions']['ajenti.plugins.vh-pureftpd.pureftpd.PureFTPDExtension']['password'] = ftppassword
  115. confsite['extensions']['ajenti.plugins.vh-pureftpd.pureftpd.PureFTPDExtension']['path'] = folder_root
  116. confsite['extensions']['ajenti.plugins.vh-pureftpd.pureftpd.PureFTPDExtension']['created'] = 'false'
  117.  
  118. confsite['extensions']['ajenti.plugins.vh-mysql.mysql.MySQLExtension']['users'][0]['name'] = username;
  119. confsite['extensions']['ajenti.plugins.vh-mysql.mysql.MySQLExtension']['users'][0]['password'] = sqlpassword
  120. confsite['extensions']['ajenti.plugins.vh-mysql.mysql.MySQLExtension']['databases'][0]['name'] = dbname
  121.  
  122. new_aconf_file = open('/srv/staging/%s.json' % username, "w")
  123. new_aconf_file.write(json.dumps(confsite))
  124. new_aconf_file.close()
  125.  
  126. os.popen('ajenti-ipc v import /srv/staging/'+username+'.json')
  127. os.popen('ajenti-ipc v apply')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement