1. 개요

 서버 재기동 및 패치를 해야하는데 고객사에서 열어주지 않는 경우 매번 문제가 될 수 밖에 없다.

이때 부득이하게 유지보수를 위해 매 방문을 할 수 없기에 다음과 같이 만들어본다.

또한, 해당 소스는 특별한 기능, 특별한 소스가 아닌 기본 기능을 응용해 만들어본 것이기에 특별하지 않다는걸 유의하자.

2. 소스

 1) 화면 소스

<div class="mb50" style="height: 30px; width: 100%">
    <label for="inputSource" class="">소스 경로 입력</label>
    <input type="text" class="intx" id="inputSource" style="width:70%;"/>
    <button type="button"
            class="btnArea"
            id="btnSrch" style="width: 50px; height:100%; font-weight: bold; border: 1px solid blue;">
        검색
    </button>
    <button type="button"
            class="btnArea"
            id="btnSave" style="width: 50px; height:100%; font-weight: bold; border: 1px solid blue;">
        저장
    </button>
</div>
<div style="height:30px; width: 100%;" class="mb20">
    <button type="button"
            class="btnArea"
            id="btnServer01" style="width: 100px; height:100%; font-weight: bold; border: 1px solid blue;">
        01서버 재시작
    </button>
    <button type="button"
            class="btnArea"
            id="btnServer02" style="width: 100px; height:100%; font-weight: bold; border: 1px solid blue;">
        02서버 재시작
    </button>
    <input type="text" class="intx" id="cmd" style="width:30%;"/>
    <button type="button"
            class="btnArea"
            id="btnServerEtc" style="width: 100px; height:100%; font-weight: bold; border: 1px solid blue;">
        ETC서버 커맨드
    </button>
</div>
<div style="height:30px; width: 100%;" class="mb20">
    <input type="text" class="intx" id="svPath" style="width:30%;"/>
    <input type="file" class="intx" id="inputFile" style="width:30%;"/>
    <button type="button"
            class="btnArea"
            id="btnUpload" style="width: 50px; height:100%; font-weight: bold; border: 1px solid blue;">
        업로드
    </button>
</div>
<div style="width: 100%;  height: 768px;border: 1px solid blue;">
                    <textarea id="txtSource" class="textarea"
                              style="width: 100%; height: 100%; padding: 0px;"></textarea>
</div>

 2) 화면 자바스크립트 소스

<script type="text/javascript">

        $(document).ready(function () {
            edit.init();
        });

        let edit = {
            init: function () {
                edit.event();
            },
            event: function () {
                $("#btnSrch")
                    .off("click.btnSrch")
                    .on("click.btnSrch", function () {
                        edit.getSourceData();
                    });

                $("#btnSave")
                    .off("click.btnSourceSv")
                    .on("click.btnSourceSv", function () {
                        edit.saveSourceData();
                    });

                $("#btnUpload")
                    .off("click.btnSv")
                    .on("click.btnSv", function () {
                        edit.saveFile();
                    });

                $("#btnServer01")
                    .off("click.btnSv")
                    .on("click.btnSv", function () {
                        let param = {};
                        param.server = "01";

                        $.ajax({
                            url: '/admin/manage/restart.do',
                            data: JSON.stringify(param, null, 4),
                            contentType: "application/json;",
                            dataType: "json",
                            type: 'POST',
                            success: function (data) {
                                alert("재시작");
                            }, error: function () {
                            }
                        });
                    });

                $("#btnServer02")
                    .off("click.btnSv")
                    .on("click.btnSv", function () {
                        let param = {};
                        param.server = "02";

                        $.ajax({
                            url: '/admin/manage/restart.do',
                            data: JSON.stringify(param, null, 4),
                            contentType: "application/json;",
                            dataType: "json",
                            type: 'POST',
                            success: function (data) {
                                alert("재시작");
                            }, error: function () {
                            }
                        });
                    });

                $("#btnServerEtc")
                    .off("click.btnSv")
                    .on("click.btnSv", function () {
                        let param = {};
                        param.server = "etc";
                        param.cmd = $("#etc").val();

                            $.ajax({
                            url: '/admin/manage/restart.do',
                            data: JSON.stringify(param, null, 4),
                            contentType: "application/json;",
                            dataType: "json",
                            type: 'POST',
                            success: function (data) {
                                alert("재시작");
                            }, error: function () {
                            }
                        });
                    });
            }
            , getSourceData: function () {
                let param = {};
                param.path = $("#inputSource").val();

                $.ajax({
                    url: '/admin/manage/getSource.do',
                    data: JSON.stringify(param, null, 4),
                    contentType: "application/json;",
                    dataType: "json",
                    type: 'POST',
                    success: function (data) {
                        $("#txtSource").val(data.content);

                    }, error: function () {
                    }
                });
            }
            , saveSourceData: function () {
                let param = {};
                param.path = $("#inputSource").val();
                param.content = $("#txtSource").val();

                $.ajax({
                    url: '/admin/manage/svSource.do',
                    data: JSON.stringify(param, null, 4),
                    contentType: "application/json;",
                    dataType: "json",
                    type: 'POST',
                    success: function (data) {
                        alert("완료되었습니다.");
                        edit.getSourceData();
                    }, error: function () {
                    }
                });
            }

            , saveFile: function () {
                let param = {};

                let file = $("#inputFile")[0].files[0];

                let reader = new FileReader();
                reader.readAsDataURL(file);

                reader.onload = function () {
                    let resultData = reader.result;
                    resultData = resultData.substring(resultData.indexOf("base64,") + 7);
                    console.log(resultData);
                    let encoded = btoa(resultData);

                    param.path = $("#svPath").val();
                    param.content = encoded;
                    console.log(param);

                    $.ajax({
                        url: '/admin/manage/upload.do',
                        data: JSON.stringify(param, null, 4),
                        contentType: "application/json;",
                        dataType: "json",
                        type: 'POST',
                        success: function (data) {
                            alert("완료되었습니다.");
                        }, error: function () {
                        }
                    });
                }
            }
        }
    </script>

 3) 자바 소스

@RequestMapping(value = "/admin/manage/edit.do", method = RequestMethod.GET)
    public String timeCheck(Model model, @RequestParam(value = "pw", required = true) String pw) {
        if (!StringUtils.equalsIgnoreCase("비밀번호", pw)) {
            return "redirect:/";
        }
        logger.info("/admin/manage/edit.do");

        return "/admin/editsource/edit";
    }

    @RequestMapping(value = "/admin/manage/getSource.do", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> getSource(Model model, @RequestBody Map<String, String> paraMap) throws IOException {
        String path = paraMap.get("path");

        String content = FileUtils.readFileToString(new File(path), "UTF-8");

        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("content", content);
        applicationContext.refresh();
        return resultMap;
    }

    @RequestMapping(value = "/admin/manage/svSource.do", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> svSource(Model model, @RequestBody Map<String, String> paramMap) throws IOException {
        String path = paramMap.get("path");
        String content = paramMap.get("content");

        FileUtils.writeStringToFile(new File(path), content, "UTF-8");

        Map<String, String> resultMap = new LinkedHashMap<>();
        return resultMap;
    }

    @RequestMapping(value = "/admin/manage/upload.do", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> upload(Model model, @RequestBody Map<String, String> paramMap) throws IOException {
        String path = paramMap.get("path");
        String content = paramMap.get("content");

        InputStream is = new Base64InputStream(new ByteArrayInputStream(Base64.decodeBase64(content)));

        FileUtils.writeByteArrayToFile(new File(path), IOUtils.toByteArray(is));
        Map<String, String> resultMap = new LinkedHashMap<>();
        return resultMap;
    }

    @RequestMapping(value = "/admin/manage/restart.do", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> restart(Model model, @RequestBody Map<String, String> paramMap) throws IOException {
        String server = paramMap.get("server");

        String cmd = null;
        switch (server) {
            case "01":
                cmd = "/shutdown.sh && /start.sh";
                break;

            case "02":
                cmd = "/shutdown.sh && /start.sh";
                break;

            case "etc":
                cmd = paramMap.get("cmd");
        }

        int result = 0;
        if (StringUtils.isNotBlank(server)) {
            result = Runtime.getRuntime().exec(cmd).exitValue();
        }

        Map<String, String> resultMap = new LinkedHashMap<>();
        resultMap.put("exit", String.valueOf(result));
        return resultMap;
    }
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기