1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

node.js経由でアプリケーションを操作する

Posted at

ここでJail環境を作成した際に
どうしても、rootユーザーでの実行が必要なコマンド(具体的にはプロセスの起動・停止)など
を許可する必要が生じまして、node.js経由で実行できるようにしました。

処理内容


  • クライアントはhttp経由でコマンド実行
  • サーバは127.0.0.1からしか処理をしか受け付けない
  • 実行したいコマンドはfuncsの要素として定義
  • cmdにコマンドを、actionに引数を指定するようにする
  • 実行した処理はログに記録
スクリプト本体

/opt/app_manage/index.js
var express = require('express');
var app = express();
var execSync = require('child_process').execSync;
var log4js = require('log4js');

log4js.configure({
    appenders: [
        {
            "type": "file",
            "category": "action",
            "filename": "/var/log/app_manage/action.log",
            "pattern": "-yyyy-MM-dd"
        },
    ]
});

var loggerAction = log4js.getLogger('action');

funcs = {
        "tomcat" : function(action) {
                        var cmd = "/etc/init.d/tomcat " + action
                        var result = execSync(cmd);
                        return result ;
        },
        "apache" : function(action) {
                        var cmd = "/etc/init.d/httpd " + action
                        var result = execSync(cmd);
                        return result ;
        }
}

app.get('/:user/:target/:action', function (req, res) {
  var user = req.params.user ;
  var target = req.params.target ;
  var action = req.params.action ;
  var logmsg = "USER: " + user + " TARGET: " +  target + " ACTION: " + action ;
  loggerAction.info(logmsg);
  var msg = funcs[target](action);
  res.send(msg + "\n");
});

var server = app.listen(8888,'localhost',function () {
  var host = server.address().address;
  var port = server.address().port;
  console.log('Example app listening at http://%s:%s', host, port);
});
SysVinitスクリプトの作成
$ initd-forever -a /opt/app_manage/index.js -n app_manage
スクリプト編集
# vi app_manage
------------------------------------------
#
# initd a node app
# Based on a script posted by https://gist.github.com/jinze at https://gist.github.com/3748766
#

export PATH=/usr/local/nodejs/bin:$PATH
------------------------------------------
自動起動の設定
# chmod +x app_manage 
# mv app_manage /etc/init.d/
# chkconfig --add app_manage
# chkconfig app_manage on
# chkconfig app_manage --list
app_manage      0:off   1:off   2:on    3:on    4:on    5:on    6:off
クライアントからの実行方法
apacheを停止する場合
curl http://127.0.0.1:8888/`whoami`/apache/stop
apacheを起動する場合
curl http://127.0.0.1:8888/`whoami`/apache/start
tomcatを停止する場合
curl http://127.0.0.1:8888/`whoami`/tomcat/stop
tomcatを起動する場合
curl http://127.0.0.1:8888/`whoami`/tomcat/start
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?