0
0

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.

HTTPでリクエスト受けてコマンド実行->レスポンスで結果を返す

0
Posted at

こちらの記事で紹介したツールの機能紹介です

普段ドキュメントはいっさい信用しない(実機に勝る情報ソースはない、ゆえにドキュメント作成もしない)インフラエンジニアの私ですが、よくプログラマの方から、インフラの設定がどうなっているか、ログがどこにあるかなどの質問を受け、その都度サーバにログインして確認したりしています

何度も使うようなコマンドはコード化して、個人用のリポジトリで管理しているのですが、もう聞かれるたびにコマンド一発打つのさえ面倒になってきて**「HTTPでリクエスト受けてコマンド実行->レスポンスで結果を返す」**みたいなことできないかと考えて機能を実装してみました

endpoint.js
const Submarine = require('Submarine');

const Tutrial = class extends Submarine {
  query(){
    return {
      hostname: 'hostname -s',
      ip_addrs: String.raw`
        ip -o -f inet a \
          |awk '{print $4}'
      `,
      logs: String.raw`
        ls -lha /var/log/syslog* \
          | head -30
      `,
      tail: String.raw`
        sudo tail -6 /var/log/syslog
      `
    };
  }


}


const tut=new Tutrial({
  conn: 'ssh',
  host: server,
});

const endpoint=tut.communicate({
  pathname: '/logs/syslog'
});

こんな感じで定義しておいて

$ curl localhost:30117/logs/syslog

queryの戻り値に定義した4つのコマンドの実行結果をJSON形式で取得できます。コマンド結果が複数行にわたるときは、結果は配列で返されます。他のクラスをnewしてcommunicate関数に別のpathnameを渡して呼び出せば、複数のエンドポイントを作ることもできます。

ここでは私の検証環境にあわせてsyslogの情報を取得していますが、なんのログでもステータスでも、こうして公開しておけば「好きなときに勝手にたたいてね」と言うことができます

ログくらいいくらでもツールあるんだからブラウザで見られるようにしとけよ、と思うんですが、本格的にログ基盤を作るとなると結構な時間もコストもかかってしまうので、ありもので済ませようということで

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?