Esempio n. 1
0
    def post_pref(self, apt_packages, packages):
        """Post activity after installation of packages"""
        if len(apt_packages):
            if set(SSSVariables.sss_apache).issubset(set(apt_packages)):
                if not (os.path.isfile('/etc/apache2/conf-available/acl.conf')):
                    data = dict(webroot=SSSVariables.sss_webroot)
                    Log.debug(self, 'Writting the Apache configuration to '
                              'file /etc/apache2/conf-available/acl.conf')
                    sss_apache = open('/etc/apache2/conf-available/acl.conf',
                                    encoding='utf-8', mode='w')
                    self.app.render((data), 'acl.mustache',
                                    out=sss_apache)
                    sss_apache.close()

                    # 22222 port setting

                    Log.debug(self, 'Writting the Apache configuration to '
                              'file /etc/apache2/sites-available/'
                              '22222')

                    sss_apache = open('/etc/apache2/sites-available/22222.conf',encoding='utf-8', mode='w')
                    self.app.render((data), '22222.mustache',
                                    out=sss_apache)
                    sss_apache.close()

                    passwd = ''.join([random.choice
                                     (string.ascii_letters + string.digits)
                                     for n in range(6)])

                    try:
                        SSSShellExec.cmd_exec(self, "printf \"SSS:"
                                             "$(openssl passwd -crypt "
                                             "{password} 2> /dev/null)\n\""
                                             "> /etc/apache2/htpasswd-sss "
                                             "2>/dev/null"
                                             .format(password=passwd))
                    except CommandExecutionError as e:
                        Log.error(self, "Failed to save HTTP Auth")

                    # Create Symbolic link for 22222
                    SSSFileUtils.create_symlink(self, ['/etc/apache2/'
                                                      'sites-available/'
                                                      '22222.conf',
                                                      '/etc/apache2/'
                                                      'sites-enabled/'
                                                      '22222.conf'])

                    # Create htdocs folder
                    if not os.path.exists('{0}22222/htdocs'
                                          .format(SSSVariables.sss_webroot)):
                        Log.debug(self, "Creating directory "
                                  "{0}22222/htdocs "
                                  .format(SSSVariables.sss_webroot))
                        os.makedirs('{0}22222/htdocs'
                                    .format(SSSVariables.sss_webroot))

                    if not os.path.exists('/etc/apache2/ssl'):
                        Log.debug(self, "Creating directory "
                                  "/etc/apache2/ssl/")
                        os.makedirs('/etc/apache2/ssl')

                    try:
                        SSSShellExec.cmd_exec(self, "openssl genrsa -out "
                                             "/etc/apache2/ssl/22222.key 2048")
                        SSSShellExec.cmd_exec(self, "openssl req -new -batch  "
                                             "-subj /commonName=127.0.0.1/ "
                                             "-key /etc/apache2/ssl/22222.key "
                                             "-out /etc/apache2/ssl/"
                                             "22222.csr")

                        SSSFileUtils.mvfile(self, "/etc/apache2/ssl/22222.key",
                                           "/etc/apache2/ssl/"
                                           "22222.key.org")

                        SSSShellExec.cmd_exec(self, "openssl rsa -in "
                                             "/etc/apache2/ssl/"
                                             "22222.key.org -out "
                                             "/etc/apache2/ssl/22222.key")

                        SSSShellExec.cmd_exec(self, "openssl x509 -req -days "
                                             "3652 -in /etc/apache2/ssl/"
                                             "22222.csr -signkey "
                                             "/etc/apache2/ssl/22222.key -out "
                                             "/etc/apache2/ssl/22222.crt")

                    except CommandExecutionError as e:
                        Log.error(self, "Failed to generate SSL for 22222")

                    # Apache Configation into GIT
                    SSSGit.add(self,
                              ["/etc/apache2"], msg="Adding Apache into Git")
                    SSSService.restart_service(self, 'apache2')

                    self.msg = (self.msg + ["HTTP Auth User Name: SSS"]
                                + ["HTTP Auth Password : {0}".format(passwd)])

            if set(SSSVariables.sss_php).issubset(set(apt_packages)):
                # Create log directories
                if not os.path.exists('/var/log/php/7.0/'):
                    Log.debug(self, 'Creating directory /var/log/php/7.0/')
                    os.makedirs('/var/log/php/7.0/')

                # TOD : xdebug

                # Parse etc/php5/fpm/php.ini
                config = configparser.ConfigParser()
                Log.debug(self, "configuring php file /etc/php/7.0/fpm/php.ini")
                config.read('/etc/php/7.0/fpm/php.ini')
                config['PHP']['expose_php'] = 'Off'
                config['PHP']['post_max_size'] = '100M'
                config['PHP']['upload_max_filesize'] = '100M'
                config['PHP']['max_execution_time'] = '300'
                config['PHP']['date.timezone'] = SSSVariables.sss_timezone
                with open('/etc/php/7.0/fpm/php.ini',
                          encoding='utf-8', mode='w') as configfile:
                    Log.debug(self, "Writting php configuration into "
                              "/etc/php/7.0/fpm/php.ini")
                    config.write(configfile)

                # Prase /etc/php/7.0/fpm/php-fpm.conf
                config = configparser.ConfigParser()
                Log.debug(self, "configuring php file"
                          "/etc/php/7.0/fpm/php-fpm.conf")
                config.read_file(codecs.open("/etc/php/7.0/fpm/php-fpm.conf",
                                             "r", "utf8"))
                config['global']['error_log'] = '/var/log/php/7.0/fpm.log'
                config.remove_option('global', 'include')
                config['global']['log_level'] = 'notice'
                config['global']['include'] = '/etc/php/7.0/fpm/pool.d/*.conf'
                with codecs.open('/etc/php/7.0/fpm/php-fpm.conf',
                                 encoding='utf-8', mode='w') as configfile:
                    Log.debug(self, "writting php7 configuration into "
                              "/etc/php/7.0/fpm/php-fpm.conf")
                    config.write(configfile)

                # Parse /etc/php/7.0/fpm/pool.d/www.conf
                config = configparser.ConfigParser()
                config.read_file(codecs.open('/etc/php/7.0/fpm/pool.d/www.conf',
                                             "r", "utf8"))
                config['www']['ping.path'] = '/ping'
                config['www']['pm.status_path'] = '/status'
                config['www']['pm.max_requests'] = '500'
                config['www']['pm.max_children'] = '100'
                config['www']['pm.start_servers'] = '20'
                config['www']['pm.min_spare_servers'] = '10'
                config['www']['pm.max_spare_servers'] = '30'
                config['www']['request_terminate_timeout'] = '300'
                config['www']['pm'] = 'ondemand'
                config['www']['listen'] = '127.0.0.1:9000'
                with codecs.open('/etc/php/7.0/fpm/pool.d/www.conf',
                                 encoding='utf-8', mode='w') as configfile:
                    Log.debug(self, "writting PHP5 configuration into "
                              "/etc/php/7.0/fpm/pool.d/www.conf")
                    config.write(configfile)

                #TODO : Debug Config
                #TODO : Disable xdebug

                # PHP and Debug pull configuration
                if not os.path.exists('{0}22222/htdocs/fpm/status/'
                                      .format(SSSVariables.sss_webroot)):
                    Log.debug(self, 'Creating directory '
                              '{0}22222/htdocs/fpm/status/ '
                              .format(SSSVariables.sss_webroot))
                    os.makedirs('{0}22222/htdocs/fpm/status/'
                                .format(SSSVariables.sss_webroot))
                open('{0}22222/htdocs/fpm/status/debug'
                     .format(SSSVariables.sss_webroot),
                     encoding='utf-8', mode='a').close()
                open('{0}22222/htdocs/fpm/status/php'
                     .format(SSSVariables.sss_webroot),
                     encoding='utf-8', mode='a').close()

                # Write info.php
                if not os.path.exists('{0}22222/htdocs/php/'
                                      .format(SSSVariables.sss_webroot)):
                    Log.debug(self, 'Creating directory '
                              '{0}22222/htdocs/php/ '
                              .format(SSSVariables.sss_webroot))
                    os.makedirs('{0}22222/htdocs/php'
                                .format(SSSVariables.sss_webroot))

                with open("{0}22222/htdocs/php/info.php"
                          .format(SSSVariables.sss_webroot),
                          encoding='utf-8', mode='w') as myfile:
                    myfile.write("<?php\nphpinfo();\n?>")

                SSSFileUtils.chown(self, "{0}22222"
                                  .format(SSSVariables.sss_webroot),
                                  SSSVariables.sss_php_user,
                                  SSSVariables.sss_php_user, recursive=True)

                SSSGit.add(self, ["/etc/php/"], msg="Adding PHP into Git")
                SSSService.restart_service(self, 'php7.0-fpm')

            if set(SSSVariables.sss_mysql).issubset(set(apt_packages)):
                if not os.path.isfile("/etc/mysql/my.cnf"):
                    config = ("[mysqld]\nwait_timeout = 30\n"
                              "interactive_timeout=60\nperformance_schema = 0"
                              "\nquery_cache_type = 1")
                    config_file = open("/etc/mysql/my.cnf",
                                       encoding='utf-8', mode='w')
                    config_file.write(config)
                    config_file.close()
                else:
                    try:
                        SSSShellExec.cmd_exec(self, "sed -i \"/#max_conn"
                                             "ections/a wait_timeout = 30 \\n"
                                             "interactive_timeout = 60 \\n"
                                             "performance_schema = 0\\n"
                                             "query_cache_type = 1 \" "
                                             "/etc/mysql/my.cnf")
                    except CommandExecutionError as e:
                        Log.error(self, "Unable to update MySQL file")

                 # Set MySQL Tuning Primer permission
                SSSFileUtils.chmod(self, "/usr/bin/tuning-primer", 0o775)

                SSSGit.add(self, ["/etc/mysql"], msg="Adding MySQL into Git")
                SSSService.reload_service(self, 'mysql')

        if len(packages):
            if any('/tmp/pma.tar.gz' == x[1]
                    for x in packages):
                SSSExtract.extract(self, '/tmp/pma.tar.gz', '/tmp/')
                Log.debug(self, 'Extracting file /tmp/pma.tar.gz to '
                          'location /tmp/')
                if not os.path.exists('{0}22222/htdocs/db'
                                      .format(SSSVariables.sss_webroot)):
                    Log.debug(self, "Creating new  directory "
                              "{0}22222/htdocs/db"
                              .format(SSSVariables.sss_webroot))
                    os.makedirs('{0}22222/htdocs/db'
                                .format(SSSVariables.sss_webroot))
                shutil.move('/tmp/phpmyadmin-STABLE/',
                            '{0}22222/htdocs/db/pma/'
                            .format(SSSVariables.sss_webroot))
                shutil.copyfile('{0}22222/htdocs/db/pma/config.sample.inc.php'
                                .format(SSSVariables.sss_webroot),
                                '{0}22222/htdocs/db/pma/config.inc.php'
                                .format(SSSVariables.sss_webroot))
                Log.debug(self, 'Setting Blowfish Secret Key FOR COOKIE AUTH to  '
                          '{0}22222/htdocs/db/pma/config.inc.php file '
                          .format(SSSVariables.sss_webroot))
                blowfish_key = ''.join([random.choice
                         (string.ascii_letters + string.digits)
                         for n in range(10)])
                SSSFileUtils.searchreplace(self,
                                          '{0}22222/htdocs/db/pma/config.inc.php'
                                          .format(SSSVariables.sss_webroot),
                                          "$cfg[\'blowfish_secret\'] = \'\';","$cfg[\'blowfish_secret\'] = \'{0}\';"
                                          .format(blowfish_key))
                Log.debug(self, 'Setting HOST Server For Mysql to  '
                          '{0}22222/htdocs/db/pma/config.inc.php file '
                          .format(SSSVariables.sss_webroot))
                SSSFileUtils.searchreplace(self,
                                          '{0}22222/htdocs/db/pma/config.inc.php'
                                          .format(SSSVariables.sss_webroot),
                                          "$cfg[\'Servers\'][$i][\'host\'] = \'localhost\';","$cfg[\'Servers\'][$i][\'host\'] = \'{0}\';"
                                          .format(SSSVariables.sss_mysql_host))
                Log.debug(self, 'Setting Privileges of webroot permission to  '
                          '{0}22222/htdocs/db/pma file '
                          .format(SSSVariables.sss_webroot))
                SSSFileUtils.chown(self, '{0}22222'
                                  .format(SSSVariables.sss_webroot),
                                  SSSVariables.sss_php_user,
                                  SSSVariables.sss_php_user,
                recursive=True) 
Esempio n. 2
0
    def post_pref(self, apt_packages, packages):
        """Post activity after installation of packages"""
        if len(apt_packages):
            if set(SSSVariables.sss_apache).issubset(set(apt_packages)):
                if not (os.path.isfile('/etc/apache2/conf-available/acl.conf')
                        ):
                    data = dict(webroot=SSSVariables.sss_webroot)
                    Log.debug(
                        self, 'Writting the Apache configuration to '
                        'file /etc/apache2/conf-available/acl.conf')
                    sss_apache = open('/etc/apache2/conf-available/acl.conf',
                                      encoding='utf-8',
                                      mode='w')
                    self.app.render((data), 'acl.mustache', out=sss_apache)
                    sss_apache.close()

                    # 22222 port setting

                    Log.debug(
                        self, 'Writting the Apache configuration to '
                        'file /etc/apache2/sites-available/'
                        '22222')

                    sss_apache = open(
                        '/etc/apache2/sites-available/22222.conf',
                        encoding='utf-8',
                        mode='w')
                    self.app.render((data), '22222.mustache', out=sss_apache)
                    sss_apache.close()

                    passwd = ''.join([
                        random.choice(string.ascii_letters + string.digits +
                                      string.punctuation) for n in range(10)
                    ])

                    try:
                        SSSShellExec.cmd_exec(
                            self, "printf \"SSS:"
                            "$(openssl passwd -crypt "
                            "{password} 2> /dev/null)\n\""
                            "> /etc/apache2/htpasswd-sss "
                            "2>/dev/null".format(password=passwd))
                    except CommandExecutionError as e:
                        Log.error(self, "Failed to save HTTP Auth")

                    # Create Symbolic link for 22222
                    SSSFileUtils.create_symlink(self, [
                        '/etc/apache2/'
                        'sites-available/'
                        '22222.conf', '/etc/apache2/'
                        'sites-enabled/'
                        '22222.conf'
                    ])

                    # Create htdocs folder
                    if not os.path.exists('{0}22222/htdocs'.format(
                            SSSVariables.sss_webroot)):
                        Log.debug(
                            self, "Creating directory "
                            "{0}22222/htdocs ".format(
                                SSSVariables.sss_webroot))
                        os.makedirs('{0}22222/htdocs'.format(
                            SSSVariables.sss_webroot))

                    if not os.path.exists('/etc/apache2/ssl'):
                        Log.debug(self, "Creating directory "
                                  "/etc/apache2/ssl/")
                        os.makedirs('/etc/apache2/ssl')

                    try:
                        SSSShellExec.cmd_exec(
                            self, "openssl genrsa -out "
                            "/etc/apache2/ssl/22222.key 2048")
                        SSSShellExec.cmd_exec(
                            self, "openssl req -new -batch  "
                            "-subj /commonName=127.0.0.1/ "
                            "-key /etc/apache2/ssl/22222.key "
                            "-out /etc/apache2/ssl/"
                            "22222.csr")

                        SSSFileUtils.mvfile(
                            self, "/etc/apache2/ssl/22222.key",
                            "/etc/apache2/ssl/"
                            "22222.key.org")

                        SSSShellExec.cmd_exec(
                            self, "openssl rsa -in "
                            "/etc/apache2/ssl/"
                            "22222.key.org -out "
                            "/etc/apache2/ssl/22222.key")

                        SSSShellExec.cmd_exec(
                            self, "openssl x509 -req -days "
                            "3652 -in /etc/apache2/ssl/"
                            "22222.csr -signkey "
                            "/etc/apache2/ssl/22222.key -out "
                            "/etc/apache2/ssl/22222.crt")

                    except CommandExecutionError as e:
                        Log.error(self, "Failed to generate SSL for 22222")

                    # Apache Configation into GIT
                    SSSGit.add(self, ["/etc/apache2"],
                               msg="Adding Apache into Git")
                    SSSService.restart_service(self, 'apache2')

                    self.msg = (self.msg + ["HTTP Auth User Name: SSS"] +
                                ["HTTP Auth Password : {0}".format(passwd)])

            #phpmyadmin config
            if set(SSSVariables.sss_pma).issubset(set(apt_packages)):
                php_conf = ("Include /etc/phpmyadmin/apache.conf")
                with open('/etc/apache2/apache2.conf', 'a') as php_conf_file:
                    php_conf_file.write(php_conf)
                """SSSFileUtils.create_symlink(self, ['/etc/phpmyadmin/apache.conf','/etc/apache2/apache2.conf'])"""
                """sss_php = open('/etc/apache2/apache2.conf',encoding='utf-8', mode='a')
                self.app.render((data), 'Include /etc/phpmyadmin/apache.conf',
                                out=sss_php)
                sss_php.close()
                """
            if set(SSSVariables.sss_php).issubset(set(apt_packages)):
                # Create log directories
                if not os.path.exists('/var/log/php/7.0/'):
                    Log.debug(self, 'Creating directory /var/log/php/7.0/')
                    os.makedirs('/var/log/php/7.0/')

                # TOD : xdebug

                # Parse etc/php5/fpm/php.ini
                config = configparser.ConfigParser()
                Log.debug(self,
                          "configuring php file /etc/php/7.0/fpm/php.ini")
                config.read('/etc/php/7.0/fpm/php.ini')
                config['PHP']['expose_php'] = 'Off'
                config['PHP']['post_max_size'] = '100M'
                config['PHP']['upload_max_filesize'] = '100M'
                config['PHP']['max_execution_time'] = '300'
                config['PHP']['date.timezone'] = SSSVariables.sss_timezone
                with open('/etc/php/7.0/fpm/php.ini',
                          encoding='utf-8',
                          mode='w') as configfile:
                    Log.debug(
                        self, "Writting php configuration into "
                        "/etc/php/7.0/fpm/php.ini")
                    config.write(configfile)

                # Prase /etc/php/7.0/fpm/php-fpm.conf
                config = configparser.ConfigParser()
                Log.debug(
                    self, "configuring php file"
                    "/etc/php/7.0/fpm/php-fpm.conf")
                config.read_file(
                    codecs.open("/etc/php/7.0/fpm/php-fpm.conf", "r", "utf8"))
                config['global']['error_log'] = '/var/log/php/7.0/fpm.log'
                config.remove_option('global', 'include')
                config['global']['log_level'] = 'notice'
                config['global']['include'] = '/etc/php/7.0/fpm/pool.d/*.conf'
                with codecs.open('/etc/php/7.0/fpm/php-fpm.conf',
                                 encoding='utf-8',
                                 mode='w') as configfile:
                    Log.debug(
                        self, "writting php7 configuration into "
                        "/etc/php/7.0/fpm/php-fpm.conf")
                    config.write(configfile)

                # Parse /etc/php/7.0/fpm/pool.d/www.conf
                config = configparser.ConfigParser()
                config.read_file(
                    codecs.open('/etc/php/7.0/fpm/pool.d/www.conf', "r",
                                "utf8"))
                config['www']['ping.path'] = '/ping'
                config['www']['pm.status_path'] = '/status'
                config['www']['pm.max_requests'] = '500'
                config['www']['pm.max_children'] = '100'
                config['www']['pm.start_servers'] = '20'
                config['www']['pm.min_spare_servers'] = '10'
                config['www']['pm.max_spare_servers'] = '30'
                config['www']['request_terminate_timeout'] = '300'
                config['www']['pm'] = 'ondemand'
                config['www']['listen'] = '127.0.0.1:9000'
                with codecs.open('/etc/php/7.0/fpm/pool.d/www.conf',
                                 encoding='utf-8',
                                 mode='w') as configfile:
                    Log.debug(
                        self, "writting PHP5 configuration into "
                        "/etc/php/7.0/fpm/pool.d/www.conf")
                    config.write(configfile)

                #TODO : Debug Config
                #TODO : Disable xdebug

                # PHP and Debug pull configuration
                if not os.path.exists('{0}22222/htdocs/fpm/status/'.format(
                        SSSVariables.sss_webroot)):
                    Log.debug(
                        self, 'Creating directory '
                        '{0}22222/htdocs/fpm/status/ '.format(
                            SSSVariables.sss_webroot))
                    os.makedirs('{0}22222/htdocs/fpm/status/'.format(
                        SSSVariables.sss_webroot))
                open('{0}22222/htdocs/fpm/status/debug'.format(
                    SSSVariables.sss_webroot),
                     encoding='utf-8',
                     mode='a').close()
                open('{0}22222/htdocs/fpm/status/php'.format(
                    SSSVariables.sss_webroot),
                     encoding='utf-8',
                     mode='a').close()

                # Write info.php
                if not os.path.exists('{0}22222/htdocs/php/'.format(
                        SSSVariables.sss_webroot)):
                    Log.debug(
                        self, 'Creating directory '
                        '{0}22222/htdocs/php/ '.format(
                            SSSVariables.sss_webroot))
                    os.makedirs('{0}22222/htdocs/php'.format(
                        SSSVariables.sss_webroot))

                with open("{0}22222/htdocs/php/info.php".format(
                        SSSVariables.sss_webroot),
                          encoding='utf-8',
                          mode='w') as myfile:
                    myfile.write("<?php\nphpinfo();\n?>")

                SSSFileUtils.chown(self,
                                   "{0}22222".format(SSSVariables.sss_webroot),
                                   SSSVariables.sss_php_user,
                                   SSSVariables.sss_php_user,
                                   recursive=True)

                SSSGit.add(self, ["/etc/php/"], msg="Adding PHP into Git")
                SSSService.restart_service(self, 'php7.0-fpm')

            if set(SSSVariables.sss_mysql).issubset(set(apt_packages)):
                if not os.path.isfile("/etc/mysql/my.cnf"):
                    config = ("[mysqld]\nwait_timeout = 30\n"
                              "interactive_timeout=60\nperformance_schema = 0"
                              "\nquery_cache_type = 1")
                    config_file = open("/etc/mysql/my.cnf",
                                       encoding='utf-8',
                                       mode='w')
                    config_file.write(config)
                    config_file.close()
                else:
                    try:
                        SSSShellExec.cmd_exec(
                            self, "sed -i \"/#max_conn"
                            "ections/a wait_timeout = 30 \\n"
                            "interactive_timeout = 60 \\n"
                            "performance_schema = 0\\n"
                            "query_cache_type = 1 \" "
                            "/etc/mysql/my.cnf")
                    except CommandExecutionError as e:
                        Log.error(self, "Unable to update MySQL file")

                # Set MySQL Tuning Primer permission
                SSSFileUtils.chmod(self, "/usr/bin/tuning-primer", 0o775)

                SSSGit.add(self, ["/etc/mysql"], msg="Adding MySQL into Git")
                SSSService.reload_service(self, 'mysql')