简易的系统初始化页面

由于要做负载均衡的系统部署,然后,希望用docker的方式将整个程序打包,系统本身是建立在wordpress和部分插件之上的,所以,这导致了必须要先安装wordpress,然后再初始化数据库,安装插件,做好基本的插件配置,然后再才启用自己些的插件,去做初始化配置,这太繁琐了,所以需要一个极简的安装页面,一键初始化所有操作。于是……

/index.php

if(!file_exists('init.api.lock')) {header('Location: /install.php');die();}

/install.php

<?php
// var_dump($_SERVER);
if(!empty($_POST) && !file_exists('init.api.lock')){
    $_POST['r'] = isset($_POST['r']) ? $_POST['r'] : '';
    $last_status = false;
    $last_message = '';
    switch ($_POST['r']) {
        case 'connect':
            $mysqli = new mysqli($_POST['host'],$_POST['user'],$_POST['pass'],$_POST['name']);
            if (mysqli_connect_errno()) {
                $last_message = "Connect failed: ".mysqli_connect_error();
            } else {
                $last_message = "Connect success";
                $last_status = true;
            }
            break;
        
        default:
            break;
    }
    // $sql = file_get_contents('mysqldump.sql');
    var_dump($_POST);
    $mysqli = new mysqli($_POST['host'],$_POST['user'],$_POST['pass'],$_POST['name']);
    if (mysqli_connect_errno()) { /* check connection */
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    } else {
        echo 'Connect success';
    }
    
    /* execute multi query */
    // if ($mysqli->multi_query($sql)) {
    //     echo "success";
    // } else {
    //   echo "error";
    // }
    // file_put_contents('','init.api.lock');
    die(json_encode(['status'=>$last_status,'message'=>$last_message]));
} else {
    // $host='dbhost:3306';$name='api_www';$user='api_user';$pass='api_pass';
    $host='';$name='';$user='';$pass='';
    ?>
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <style type="text/css">body,html{height:100%}body{display:-ms-flexbox;display:-webkit-box;display:flex;-ms-flex-align:center;-ms-flex-pack:center;-webkit-box-align:center;align-items:center;-webkit-box-pack:center;justify-content:center;padding-top:40px;padding-bottom:40px;background-color:#f5f5f5}.form-signin{width:100%;max-width:330px;padding:15px;margin:0 auto}.form-signin .form-control{position:relative;box-sizing:border-box;height:auto;padding:10px;font-size:16px}.form-signin .form-control:focus{z-index:2}.progress{margin: 5px 0;}</style>
    <title>接口管理平台</title>
  </head>
  <body class="text-center">
    <form class="form-signin" id="init">
        <h1 class="h3 mb-3 font-weight-normal">接口平台初始化</h1>
        <input type="text" id="dbhost" class="form-control" placeholder="数据库主机(192.168.1.1:3306)" required autofocus value="<?= $host?>">
        <input type="text" id="dbname" class="form-control" placeholder="数据库的名称" required value="<?= $name?>">
        <input type="text" id="dbuser" class="form-control" placeholder="数据库用户名" required value="<?= $user?>">
        <input type="password" id="dbpass" class="form-control" placeholder="数据库密码" required value="<?= $pass?>">
        <div class="progress">
          <div class="progress-bar progress-bar-striped" role="progressbar" style="width: 0%" aria-valuenow="1" aria-valuemin="0" aria-valuemax="100"></div>
        </div>
        <button class="btn btn-lg btn-primary btn-block" type="submit">开始初始化</button>
        <p class="mt-5 mb-3 text-muted">&copy; <?= date('Y')?></p>
    </form>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#init").submit(function(){
                let host = $('#dbhost').val();
                let name = $('#dbname').val();
                let user = $('#dbuser').val();
                let pass = $('#dbpass').val();
                if(host && name && user && pass) {
                    console.info('ls:',host,name,user,pass);
                    $.post("/install.php",{host:host,name:name,user:user,pass:pass},function(result){
                        console.info(result);
                        console.info(result.status);
                        console.info(JSON.stringify(result));
                        return false;
                    });
                    $(".progress-bar").attr('style','width: 10%');
                    return false;
                } else {
                    return false;
                }
            });
        });
    </script>
  </body>
</html><?php
}
?>

其实我还想到了另外一种方案,所有的配置都配置在环境变量中,index负责初始化操作,这样就没有操作的界面了。如果部署的要求更偏技术化,那么就可以连初始化的操作都免了,不需要本页面。

<?php
// 
// define('SHORTINIT', true);
// require('wp-load.php');
// WP_Filesystem();
// global $wp_filesystem;
// global $wpdb;
// echo 'Current Path is '. __DIR__;
// $file =  __DIR__."/sql/my_insert_query.sql";
// if(!is_readable($file)) {
//     echo 'File not found or not readable '.$file;
// }     
// // $table_name = $wpdb->prefix . "tableName";

// // $sql_insert = "INSERT INTO $table_name (id, something) ";
// // $sql_insert .= $wp_filesystem->get_contents( $file );
// // $rows_affected = $wpdb->query( $sql_insert );
    
// $tables = $wpdb->get_results("show tables", ARRAY_A);
// var_dump($tables);

// file_put_contents('','init.api.lock');