雑記

インフラのことだったりプログラムのことだったりどうでもいいこと書いたり。要は雑記。

nginxのluaモジュールを使ってみる

インストール

インストールの方法としてopenrestyを使う方法と各ライブラリをひとつずつ用意して通常のnginxに組み込む形があった。 openrestyを使うことが強く推奨とのことですが、今回はnginxに自分で組み込む形にします。 (公式が配布していないnginxを使うということに心理的に抵抗があっただけ。openresty自体はlua-nginx-moduleのメンテナさんが作ったものらしいので、まぁ大丈夫だろうけど)

事前に用意が必要なのが以下

  • LuaJIT
  • ngx_devel_kit
  • ngx_lua

LuaJIT

# cd /usr/local/src
# git clone http://luajit.org/git/luajit-2.0.git
# cd luajit-2.0
# make
# make install

ngx_devel_kit

# cd /usr/local/src
# git clone https://github.com/simpl/ngx_devel_kit.git

ngx_lua

# cd /usr/local/src
# git clone https://github.com/openresty/lua-nginx-module.git

あとはnginxのconfigureオプションに以下を追加すればよいだけ。他のオプションは環境に応じて付与する感じ。

--with-ld-opt=-Wl,-E,-rpath,/usr/local/lib
--add-module=/usr/local/src/ngx_devel_kit
--add-module=/usr/local/src/lua-nginx-module

実際の設定

サブドメインによってproxy先を変更してみる

問い合わせ先はファイルで設定(問い合わせ先をredisとかにすることも可能)

server {
    listen       80;
    server_name  ~^(?<subdomain>[^\.]+).example.com;

    # /etc/nginx/route/settings/$subdomain に記載されているhost:portに対してproxyする
    location / {
        # この環境ではlocalにdnsmasqを立てたので、127.0.0.1を指定。実環境では普通にdnsサーバを指定する。
        resolver 127.0.0.1 valid=30s;

        # 先にproxy_toを定義しておかないと、シンタックスチェックが通らない
        set $proxy_to "";
        # luaの部分
        rewrite_by_lua '
          # サブドメインを引数に内部リダイレクト
          res = ngx.location.capture("/settings/" .. ngx.var.subdomain)
          # proxy先の設定ファイルが存在すれば、proxy_to変数に読みこんだ結果をセット
          # 存在しなかったら404で終了
          if res.status == ngx.HTTP_OK then
              ngx.var.proxy_to = string.gsub(res.body, "\\n", "")
          else
              ngx.exit(ngx.HTTP_NOT_FOUND)
          end
        ';

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;

        proxy_pass http://$proxy_to;
        proxy_redirect ~^(http://[^:]+):\d+(/.+)$ $1$2;
    }

    # proxyy先設定ファイルを各区人
    location ~ ^/settings {
        internal;
        root /etc/nginx/route;
    }
}

proxy先の設定ファイルは以下のフォーマット /etc/nginx/route/settings/hoge

localhost:8080

上記の場合、「hoge.example.com」にアクセスした場合、proxy先はlocalhost:8080となる。

ちなみになんでRedisじゃなくてファイルにしているかというと、Redis落ちるとサイトにアクセスできなくなるということであり、そうなるとRedisもちゃんと管理しなきゃになり、カジュアルにこの機能を使おうとするとちょっとおおげさかなと思ってでした。

唯一懸念だったのがパフォーマンスだったけど、サイボウズさんですらほぼ影響はなかったとのことなので気にしなくてよさげですしね(もちろん本番に反映するときはちゃんと自分でも検証しますが)

参考

Rubyで特定のディレクトリ以下のファイルを一括requireする

タイトル通り、特定のディレクトリ以下にあるファイルを一括でrequireする方法

ディレクトリ構成

├── libs
│   ├── hoge.rb
│   └── piyo.rb
└── run.rb

上記のlibs以下を読み込んでみる。

コード

module Test
  class Hoge
    def output()
      puts 'hoge'
    end
  end
end
  • piyo.rb
module Test
  class Piyo
    def output()
      puts 'piyo'
    end
  end
end
  • run.rb
#!/usr/bin/env ruby
basedir = File.dirname __FILE__
$: << File.expand_path(basedir + "/libs")

Dir::glob(basedir + '/libs/*.rb').each do |f|
  require File::basename f, '.rb'
end

hogehoge = Test::Hoge.new()
piyopiyo = Test::Piyo.new()

hogehoge.output
piyopiyo.output

結果

% ruby run.rb
hoge
piyo

実際使うときは例外もちゃんと考える必要あり。

Ansibleでリトライ処理をする

例えばAnsibleでTomcatを起動したとき、確認のためにhttpでアクセスして確認したいとします。 curlでローカルにアクセス、失敗したら数秒後に再度アクセスして確認という処理をしたかったのですが、 Ansibleでそんなのないよなーって思って探してみたらありましたよ。

Do-Until Loops

http://docs.ansible.com/playbooks_loops.html#do-until-loops

untilがloopから抜ける条件

retriesがリトライ回数

delayがスリープの秒数

ですね

サンプル

さっきの仕様を実現するとしたら以下ですね。

- shell: curl -o /dev/null -w %{http_code} http://localhost:8080/ 2> /dev/null
  register: result
  until: result.stdout.find('200') != -1
  retries: 6
  delay: 20

ステータスコードが200以外だったら20秒後に再度リトライします。 6回繰り返して全て失敗したらエラーです。

意外と利用シーンは多めかもですね。活用したいと思います。

AMIにaws-sdk for rubyをインストールしようとしたらエラーがでた

タイトル通りなんだけど、その時の対応メモ。

そもそもの依存関係のパッケージインストール

$ sudo yum install libxml2-devel
$ sudo yum install libxslt-devel

aws-sdkをインストール

aws-sdkをインストール

$ sudo gem i aws-sdk

ただ、依存関係にあるパッケージのnokogiriのインストールに失敗してこける

Building native extensions.  This could take a while...
ERROR:  Error installing aws-sdk:
  ERROR: Failed to build gem native extension.

    /usr/bin/ruby2.0 extconf.rb
checking if the C compiler accepts ... yes
Building nokogiri using packaged libraries.
checking for gzdopen() in -lz... yes
checking for iconv... yes
************************************************************************
IMPORTANT NOTICE:

Building Nokogiri with a packaged version of libxml2-2.9.2
with the following patches applied:
  - 0001-Revert-Missing-initialization-for-the-catalog-module.patch
  - 0002-Fix-missing-entities-after-CVE-2014-3660-fix.patch

Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:

    gem install nokogiri -- --use-system-libraries
        [--with-xml2-config=/path/to/xml2-config]
        [--with-xslt-config=/path/to/xslt-config]

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

Note, however, that nokogiri is not fully compatible with arbitrary
versions of libxml2 provided by OS/package vendors.
************************************************************************
Extracting libxml2-2.9.2.tar.gz into tmp/x86_64-redhat-linux-gnu/ports/libxml2/2.9.2... OK
Running patch with /usr/local/share/ruby/gems/2.0/gems/nokogiri-1.6.6.2/ports/patches/libxml2/0001-Revert-Missing-initialization-for-the-catalog-module.patch...
Running 'patch' for libxml2 2.9.2... ERROR, review '/usr/local/share/ruby/gems/2.0/gems/nokogiri-1.6.6.2/ext/nokogiri/tmp/x86_64-redhat-linux-gnu/ports/libxml2/2.9.2/patch.log' to see what happened.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
  --with-opt-dir
  --without-opt-dir
  --with-opt-include
  --without-opt-include=${opt-dir}/include
  --with-opt-lib
  --without-opt-lib=${opt-dir}/lib64
  --with-make-prog
  --without-make-prog
  --srcdir=.
  --curdir
  --ruby=/usr/bin/ruby2.0
  --help
  --clean
  --use-system-libraries
  --enable-static
  --disable-static
  --with-zlib-dir
  --without-zlib-dir
  --with-zlib-include
  --without-zlib-include=${zlib-dir}/include
  --with-zlib-lib
  --without-zlib-lib=${zlib-dir}/lib
  --enable-cross-build
  --disable-cross-build
/usr/local/share/ruby/gems/2.0/gems/mini_portile-0.6.2/lib/mini_portile.rb:279:in `block in execute': Failed to complete patch task (RuntimeError)
  from /usr/local/share/ruby/gems/2.0/gems/mini_portile-0.6.2/lib/mini_portile.rb:271:in `chdir'
  from /usr/local/share/ruby/gems/2.0/gems/mini_portile-0.6.2/lib/mini_portile.rb:271:in `execute'
  from extconf.rb:311:in `block in patch'
  from extconf.rb:308:in `each'
  from extconf.rb:308:in `patch'
  from /usr/local/share/ruby/gems/2.0/gems/mini_portile-0.6.2/lib/mini_portile.rb:108:in `cook'
  from extconf.rb:278:in `block in process_recipe'
  from extconf.rb:177:in `tap'
  from extconf.rb:177:in `process_recipe'
  from extconf.rb:475:in `<main>'


Gem files will remain installed in /usr/local/share/ruby/gems/2.0/gems/nokogiri-1.6.6.2 for inspection.
Results logged to /usr/local/share/ruby/gems/2.0/gems/nokogiri-1.6.6.2/ext/nokogiri/gem_make.out

libxml2のバージョンが合わないからっぽい。 とりあえず指示通り、nokogiriを単体で--use-system-librariesをつけてインストールしたらいけた

$ sudo gem i nokogiri -- --use-system-libraries

その後、再度aws-sdkのインストールを実行したら通っていけました。

OpsCenterをNginxでproxyする

はまったのでメモ

普通にproxyすると

こんな設定で普通にproxyすると、各ノードの状態を取得することができません。

    location / {
        proxy_pass http://localhost:8888;
    }

/tcp/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/xhrstream?nocache=0.9999999999999999といったリクエストがタイムアウトします。

どうするか?

proxy_buffering offを設定します。

    location / {
        proxy_pass http://localhost:8888;
        proxy_buffering off;
    }

これで通信が通ります。

proxy_bufferingって?

要はproxy先のレスポンスをnginxでキャッシュせず、クライアントに即座に返すってことかな。