package ru.den.autoinstaller import java.io.File import java.io.IOException import java.nio.charset.Charset import java.nio.file.Files import java.nio.file.Paths class Setup { companion object{ private fun changeApache2Conf(){ val path = Paths.get("/etc/apache2/apache2.conf") val lines = Files.readAllLines(path) var enableScan = false for (i in lines.indices){ if (!enableScan){ if (lines[i].contains("")){ enableScan = true } }else{ if (lines[i].contains("Require all")){ lines[i] = " Require all granted" break } } } Files.write(path, lines, Charset.defaultCharset()) } private fun clearDirectory(dir: File) { for (file in dir.listFiles()) { if (file.isDirectory) clearDirectory(file) file.delete() } } private fun changeConfFirstInstall(domName: String, useHttps: Boolean){ val path1 = "/etc/apache2/sites-available" val path2 = "/etc/apache2/sites-enabled" clearDirectory(File(path1)) clearDirectory(File(path2)) val lines = ArrayList() if (useHttps){ lines.add("") lines.add(" SSLEngine on") lines.add(" ServerName $domName") lines.add(" SSLCertificateFile /etc/ssl/domain_name.crt") lines.add(" SSLCertificateKeyFile /etc/ssl/private.key") lines.add(" SSLCertificateChainFile /etc/ssl/chain.crt") lines.add(" ServerAdmin webmaster@$domName") lines.add(" DocumentRoot /var/www/html") lines.add(" DocumentRoot /var/www/html") lines.add(" DirectoryIndex index.php index.html index.htm") lines.add(" ErrorLog /var/log/apache2/err.log.$domName") lines.add(" ") lines.add(" Options Indexes FollowSymLinks") lines.add(" AllowOverride All") lines.add(" Require all granted") lines.add(" ") lines.add("") } lines.add("") lines.add(" ServerAdmin webmaster@$domName") lines.add(" ServerName $domName") lines.add(" DocumentRoot /var/www/html") lines.add(" DirectoryIndex index.php index.html index.htm") lines.add(" ErrorLog /var/log/apache2/err.log.$domName") lines.add(" ") lines.add(" Options Indexes FollowSymLinks") lines.add(" AllowOverride All") lines.add(" Require all granted") lines.add(" ") lines.add("") Files.write(Paths.get("$path1/$domName.conf"), lines, Charset.defaultCharset()) Files.write(Paths.get("$path2/$domName.conf"), lines, Charset.defaultCharset()) } private fun createHtAccessHttps(useHttps: Boolean, mainPath: String, larPath: String){ val lines = ArrayList() if (useHttps){ lines.add("RewriteEngine On") lines.add("RewriteCond %{SERVER_PORT} !^443\$") lines.add("RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]") } lines.add("") lines.add(" RewriteEngine On") lines.add(" RewriteRule ^(.*)\$ $mainPath/$larPath/public/\$1 [L]") lines.add("") val path = "/var/www/html/.htaccess" File(path).delete() Files.write(Paths.get(path), lines, Charset.defaultCharset()) } private fun createUpdateScript(mainPath: String, larPath: String){ val lines = ArrayList() lines.add("# chmod +x update.sh") lines.add("cd /var/www/html/$mainPath/") lines.add("git reset --hard") lines.add("git pull --all") lines.add("cd $larPath") lines.add("php artisan migrate") lines.add("php artisan storage:link") val path = "/var/www/html/update.sh" File(path).delete() Files.write(Paths.get(path), lines, Charset.defaultCharset()) } @JvmStatic @Throws(IOException::class) fun main(args: Array) { println("запуск установщика ateuco.online - ok.") //com("cmd /c dir %s") if ("Linux" != System.getProperty("os.name")){ println("установщик предназначен только для запуска в линукс Ubuntu - err.") return } println("запуск из Linux - ok.") if (!args.contains("-https")) { println("вы запустили без параметра -https") }else{ println("вы запустили с параметром -https, не забудьте проверить наличие файлов сертификата /etc/ssl/domain_name.crt, /etc/ssl/private.key, /etc/ssl/chain.crt") } if (!File("vps-install.sh").exists()){ println("файл vps-install не найден, работа остановлена") return } val installLines = Files.readAllLines(Paths.get("vps-install.sh")) var mainPath = "" var larPath = "" for (line in installLines){ if (line.startsWith("main_path")){ mainPath = line.trim().replace("main_path=\"","").replace("\";","") } else if (line.startsWith("lar_path")){ larPath = line.trim().replace("lar_path=\"","").replace("\";","") } } if (mainPath.isEmpty() || larPath.isEmpty()){ println("переменные mainPath или larPath не найдены в файле vps-install") return } changeApache2Conf() changeConfFirstInstall(if (args.isNotEmpty() && args[0] != "-https") args[0] else "default.ru", args.contains("-https")) createHtAccessHttps(args.contains("-https"),mainPath,larPath) createUpdateScript(mainPath,larPath) } } }