Advertisement
yacel100

fabfile.py final completo

Sep 9th, 2017
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.01 KB | None | 0 0
  1. from fabric.api import env
  2. import sys
  3. from ilogue.fexpect import expect, expecting, run , sudo
  4.  
  5. def instalar_pxp():
  6.  
  7.     question = raw_input("La conexion se realizara por un proxy? (s/n) : ")
  8.     if question == 's' :
  9.         question = raw_input("Ingrese la cadena de conexion del servidor proxy  (proxyuser:proxypwd@server:port o server:port) : ")
  10.         proxy = question
  11.     else :
  12.         proxy = ""
  13.            
  14.     run("yum -y install wget")
  15.        
  16.     version = run("grep -o release.. /etc/redhat-release")
  17.    
  18.     if(version == 'release 7'):
  19.         # postgres de  rpm de postgres 9.5#
  20.         run("wget http://yum.postgresql.org/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-2.noarch.rpm")
  21.     else:
  22.         # postgres de  rpm de postgres 9.5#
  23.         run("wget http://yum.postgresql.org/9.5/redhat/rhel-6-x86_64/pgdg-redhat95-9.5-2.noarch.rpm")
  24.  
  25. # configuraicon de archivos de centos-base.repo agregando una linea #
  26.     s = open("/etc/yum.repos.d/CentOS-Base.repo",'a')
  27.     s.write("exclude=postgresql*\n\n")
  28.     s.close()
  29.  
  30.     if(version == 'release 7'):
  31.         run("rpm -Uvh --replacepkgs pgdg-centos95-9.5-2.noarch.rpm")
  32.     else:
  33.         run("rpm -Uvh --replacepkgs pgdg-redhat95-9.5-2.noarch.rpm")
  34.    
  35. # instalacion de postgres y la primera corrida #
  36.     S_pgsql="service postgresql-9.5"
  37.     I_pgsql="postgresql95"
  38.     sudo("yum -y install postgresql95-server postgresql95-docs postgresql95-contrib postgresql95-plperl postgresql95-plpython postgresql95-pltcl postgresql95-test rhdb-utils gcc-objc postgresql95-devel ")
  39.     if(version == 'release 7'):
  40.        
  41.         run("/usr/pgsql-9.5/bin/postgresql95-setup initdb")
  42.         run("systemctl start postgresql-9.5")
  43.         run("systemctl enable postgresql-9.5")
  44.     else:
  45.        
  46.         run("service postgresql-9.5 initdb")
  47.         run("service postgresql-9.5 start")
  48.         run("chkconfig postgresql-9.5 on")
  49.  
  50. # instalacion del php y apache mas la primera corrida #
  51.  
  52.  
  53.     sudo("yum -y install httpd php  mod_ssl mod_auth_pgsql  php-pear php-bcmath  php-mbstring php-cli php-ldap php-pdo php-pgsql php-gd")
  54.    
  55.     if(version == 'release 7'):
  56.         run("systemctl start httpd")
  57.         run("systemctl enable httpd")
  58.     else:
  59.         run("service httpd start")
  60.         run("chkconfig httpd on")
  61.  
  62. #Creacion de archivos para bitacoras
  63.     archi = open("/usr/local/lib/phx.c",'w')
  64.     archi.write('#include "postgres.h"\n')
  65.     archi.write('#include <string.h>\n')
  66.     archi.write('#include "fmgr.h"\n')
  67.     archi.write('#include "utils/geo_decls.h"\n')
  68.     archi.write('#include <stdio.h>\n')
  69.    
  70.     archi.write('#ifdef PG_MODULE_MAGIC\n')
  71.     archi.write('PG_MODULE_MAGIC;\n')
  72.     archi.write('#endif\n')
  73.     archi.write('/* by value */\n')
  74.     archi.write('PG_FUNCTION_INFO_V1(monitor_phx);\n')
  75.    
  76.     archi.write('Datum\n')
  77.     archi.write('monitor_phx(PG_FUNCTION_ARGS)\n')
  78.     archi.write('{\n')
  79.     archi.write('    int32   arg = PG_GETARG_INT32(0);\n')
  80.     archi.write('    system("sudo /usr/local/lib/./phxbd.sh");\n')
  81.     archi.write('        PG_RETURN_INT32(arg);\n')
  82.     archi.write('}')
  83.     archi.close()
  84.    
  85.     run("gcc -I /usr/local/include -I /usr/pgsql-9.5/include/server/ -fpic -c /usr/local/lib/phx.c")
  86.     run("gcc -I /usr/local/include -I /usr/pgsql-9.5/include/server/ -shared -o /usr/local/lib/phx.so phx.o")
  87.    
  88.  
  89.     run("chown root.postgres /usr/local/lib/phx.so")
  90.     run("chmod 750 /usr/local/lib/phx.so")
  91.    
  92.     archi = open("/usr/local/lib/phxbd.sh",'w')
  93.     archi.write('!/bin/bash\n')
  94.     archi.write('top -b -n 1 | grep -e postgres -e httpd | awk \'{print $1","$12","$2","$9","$10","$5""""}\' > /tmp/procesos.csv\n')
  95.     archi.write('chown root.postgres /tmp/procesos.csv\n')
  96.     archi.write('chmod 740 /tmp/procesos.csv')
  97.    
  98.     sudo("chown root.postgres /usr/local/lib/phxbd.sh")
  99.     sudo("sudo chmod 700 /usr/local/lib/phxbd.sh")
  100.    
  101.     f = open("/etc/sudoers",'r')
  102.     chain = f.read()
  103.     chain = chain.replace("Defaults    requiretty","#Defaults    requiretty")
  104.     chain = chain.replace("root    ALL=(ALL)       ALL","root    ALL=(ALL)       ALL\n postgres        ALL=NOPASSWD: /usr/local/lib/phxbd.sh")
  105.     f.close()
  106.    
  107.     f = open("/etc/sudoers",'w')
  108.     f.write(chain)
  109.     f.close()
  110.    
  111.    
  112.    
  113. #Instalacion de mcrypt para servicios rest
  114.     if(version == 'release 7'):
  115.         run("yum install epel-release")
  116.     else:
  117.         run("wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm")
  118.         run("wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm")
  119.         sudo("rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm")
  120.    
  121.    
  122.     run("yum -y update")
  123.     run("yum -y install php-mcrypt*")
  124.  
  125. # cambio de los archivos pg_hba y postgres.config#
  126.    
  127.     archi=open("/var/lib/pgsql/9.5/data/pg_hba.conf",'w')
  128.        
  129.     archi.write("# TYPE  DATABASE        USER            ADDRESS                 METHOD\n\n")
  130.     archi.write("# 'local' is for Unix domain socket connections only\n")
  131.     archi.write("local   all        postgres,dbkerp_conexion                  trust\n")
  132.     archi.write("local   all             all                                     md5\n")
  133.     archi.write("# IPv4 local connections:\n")
  134.     archi.write("host    all             all             127.0.0.1/32            md5\n")
  135.     archi.write("host    all             all             192.168.0.0/16          md5\n")
  136.     archi.write("# IPv6 local connections:\n")
  137.     archi.write("host    all             all             ::1/128                 md5\n")
  138.     archi.close()
  139.  
  140.    
  141.     f = open("/var/lib/pgsql/9.5/data/postgresql.conf",'r')
  142.    
  143.     chain = f.read()
  144.     chain = chain.replace("pg_catalog.english","pg_catalog.spanish")
  145.     chain = chain.replace("log_destination = 'stderr'","log_destination = 'csvlog'")
  146.     chain = chain.replace("log_filename = 'postgresql-%a.log'","log_filename = 'postgresql-%Y-%m-%d.log'")
  147.     chain = chain.replace("log_truncate_on_rotation = on","log_truncate_on_rotation = off")
  148.     chain = chain.replace("#log_error_verbosity = default","log_error_verbosity = verbose")
  149.     chain = chain.replace("#log_statement = 'none'","log_statement = 'mod'")
  150.     chain = chain.replace("iso, mdy","iso, dmy")
  151.     f.close()
  152.    
  153.    
  154.     otro = open("/var/lib/pgsql/9.5/data/postgresql.conf",'w')
  155.    
  156.        
  157.     otro.write(chain)
  158.     otro.close()
  159.    
  160.     s = open("/var/lib/pgsql/9.5/data/postgresql.conf",'a')
  161.    
  162.     s.write("listen_addresses = '*'\n")
  163.     s.write("bytea_output = 'escape'\n")
  164.     s.close()
  165.    
  166.    
  167.     db_pass = "postgres"
  168.     sudo('psql -c "ALTER USER postgres WITH ENCRYPTED PASSWORD E\'%s\'"' % (db_pass), user='postgres')
  169.     sudo('psql -c "CREATE DATABASE dbkerp WITH ENCODING=\'UTF-8\';"', user='postgres')
  170.     sudo('psql -c "CREATE USER dbkerp_conexion WITH PASSWORD \'dbkerp_conexion\';"', user='postgres')
  171.     sudo('psql -c "ALTER ROLE dbkerp_conexion SUPERUSER;"', user='postgres')
  172.     sudo('psql -c "CREATE USER dbkerp_admin WITH PASSWORD \'a1a69c4e834c5aa6cce8c6eceee84295\';"', user='postgres')
  173.     sudo('psql -c "ALTER ROLE dbkerp_admin SUPERUSER;"', user='postgres')
  174.    
  175.     if(version == 'release 7'):
  176.         run('systemctl restart postgresql-9.5')
  177.     else:
  178.         run('service postgresql-9.5 restart')
  179.  
  180. # instalacion de git para poder bajar el repositoriio pxp y moviendo a la carpeta /var/www/html/kerp/#
  181.     sudo("yum -y install git-core")
  182.     run("mkdir /var/www/html/kerp")
  183.     run("mkdir /var/www/html/kerp/pxp")
  184.        
  185.     #Si existe proxy se configura github para el proxy
  186.     if (proxy != ""):
  187.         run("git config --global http.proxy http://" + proxy)
  188.         run("git config --global https.proxy https://" + proxy)
  189.        
  190.     run("git clone https://github.com/kplian/pxp.git /var/www/html/kerp/pxp")
  191.     run("chown -R apache.apache /var/www/html/kerp/")
  192.     run("chmod 700 -R /var/www/html/kerp/")
  193.  
  194. # haciendo una copia de datosgenerales.samples.php y modificando archivo#
  195.     f = open("/var/www/html/kerp/pxp/lib/DatosGenerales.sample.php")
  196.     g = open("/var/www/html/kerp/pxp/lib/DatosGenerales.php","w")
  197.     linea = f.readline()
  198.     while linea != "":
  199.         g.write(linea)
  200.         linea = f.readline()
  201.  
  202.     g.close()
  203.     f.close()
  204.     #TODO    VOLVER VARIABLE LA CARPETA PRINCIPAL KERP
  205.     f = open("/var/www/html/kerp/pxp/lib/DatosGenerales.php",'r')
  206.     chain = f.read()
  207.     chain = chain.replace("/web/lib/lib_control/","/kerp/pxp/lib/lib_control/")
  208.     chain = chain.replace("/kerp-boa/","/kerp/")
  209.    
  210.    
  211.     chain = chain.replace("/var/lib/pgsql/9.1/data/pg_log/","/var/lib/pgsql/9.5/data/pg_log/")
  212.    
  213.  
  214.     f.close()
  215.     otro = open("/var/www/html/kerp/pxp/lib/DatosGenerales.php",'w')
  216.     otro.write(chain)
  217.     otro.close()
  218.  
  219.  
  220.     run("ln -s /var/www/html/kerp/pxp/lib /var/www/html/kerp/lib")
  221.     run("ln -s /var/www/html/kerp/pxp/index.php /var/www/html/kerp/index.php")
  222.     run("ln -s /var/www/html/kerp/pxp/sis_generador /var/www/html/kerp/sis_generador")
  223.     run("ln -s /var/www/html/kerp/pxp/sis_organigrama /var/www/html/kerp/sis_organigrama")
  224.     run("ln -s /var/www/html/kerp/pxp/sis_parametros /var/www/html/kerp/sis_parametros")
  225.     run("ln -s /var/www/html/kerp/pxp/sis_seguridad /var/www/html/kerp/sis_seguridad")
  226.     run("ln -s /var/www/html/kerp/pxp/sis_workflow /var/www/html/kerp/sis_workflow")
  227.    
  228.  
  229.    
  230.     archi=open('/var/www/html/kerp/sistemas.txt','w')
  231.     archi.close()
  232.    
  233.    
  234.     run("mkdir /var/www/html/kerp/reportes_generados")
  235.    
  236.     sudo("setfacl -R -m u:apache:wrx /var/www/html/kerp/reportes_generados")
  237.    
  238. #   sudo("yum -y install rpm-build")
  239.    
  240.     sudo("setfacl -R -m u:postgres:wrx /var/www/html")
  241.    
  242.     sudo("chcon -Rv --type=httpd_sys_rw_content_t /var/www/html/kerp/")
  243.     sudo("setsebool -P httpd_can_network_connect_db=1")
  244.    
  245.     sudo("setsebool -P httpd_can_network_connect 1")
  246.  
  247. # iptables
  248.     if(version == 'release 6'):
  249.         run("iptables --flush")
  250.         run("iptables -P INPUT ACCEPT")
  251.         run("iptables -P OUTPUT ACCEPT")
  252.         run("iptables -P FORWARD ACCEPT")
  253.         #Interfaz local aceptar
  254.         run("iptables -A INPUT -i lo -j ACCEPT")
  255.         #Comunicaciones establecidas aceptar
  256.         run("iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT")
  257.         #Ping Aceptar
  258.         run("iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT")
  259.         #Ssh Aceptar
  260.         run("iptables -A INPUT -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT")
  261.         #http y https aceptar
  262.         run("iptables -A INPUT -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT")
  263.         run("iptables -A INPUT -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT")
  264.         #websocket
  265.         run("iptables -A INPUT -p tcp --dport 8010 -m state --state NEW,ESTABLISHED -j ACCEPT")
  266.        
  267.         #postgres  aceptar
  268.         run("iptables -A INPUT -p tcp --dport 5432 -m state --state NEW,ESTABLISHED -j ACCEPT")
  269.         run("iptables -P INPUT DROP")
  270.         run("service iptables save")
  271.         run("service iptables restart")
  272.     else:
  273.         run("firewall-cmd --permanent --add-port=22/tcp")
  274.             run("firewall-cmd --permanent --add-port=80/tcp")
  275.             run("firewall-cmd --permanent --add-port=5432/tcp")
  276.         run("firewall-cmd --permanent --add-port=8010/tcp")
  277.         run("firewall-cmd --reload")
  278.    
  279.    
  280.     sudo("php /var/www/html/kerp/lib/ratchet/pxp-Server.php > /dev/null 2>&1 &")
  281.    
  282.     prompts = []
  283.     prompts += expect('Ingrese una opcion.*','1')
  284.     prompts += expect('Ingrese el nombre de la BD.*','dbkerp') 
  285.     prompts += expect('Desea obtener un backup de la BD.*','NO')
  286.     prompts += expect('los datos de prueba.*','n') 
  287.    
  288.     with expecting(prompts):
  289.         sudo("/var/www/html/kerp/pxp/utilidades/restaurar_bd/./restaurar_todo.py" , user="postgres")
  290.        
  291. def prueba_local():
  292.     prompts = []
  293.     prompts += expect('is your.*','Jaime')
  294.     prompts += expect('you at stack.*','si')           
  295.    
  296.     with expecting(prompts):
  297.         run("python /root/prueba_local.py")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement