#!/usr/bin/env ruby # -*- coding: utf-8 -*- require 'digest/md5' require 'fileutils' require 'rack' require 'uri' require 'sekkaconfig' require 'sekka/sekkaversion' DICTDIR = File.expand_path( "~/.sekka-server" ) DICTURL = "https://raw.github.com/kiyoka/sekka/master/public_dict/" + SekkaVersion.dictVersion PIDFILE = DICTDIR + "/pid" DICTTYPE = if ENV.has_key?( 'SEKKA_AZIK' ) "A" else "N" end TC_OPTS = "#xmsiz=256m" TC_FILE = DICTDIR + "/SEKKA-JISYO.SMALL.#{DICTTYPE}.tch" + TC_OPTS TSVFILE = DICTDIR + "/SEKKA-JISYO.SMALL.#{DICTTYPE}.tsv" SUMFILE = DICTDIR + "/SEKKA-JISYO.SMALL.#{DICTTYPE}.md5" URLURL = DICTURL + "/SEKKA-JISYO.SMALL.#{DICTTYPE}.url" SUMURL = DICTURL + "/SEKKA-JISYO.SMALL.#{DICTTYPE}.md5" TC_FILE_LIST = [ DICTDIR + "/SEKKA-JISYO.CUSTOM.tch" + TC_OPTS, DICTDIR + "/SEKKA-JISYO.LARGE.#{DICTTYPE}.tch" + TC_OPTS, DICTDIR + "/SEKKA-JISYO.SMALL.#{DICTTYPE}.tch" + TC_OPTS ] MEMCACHED = "localhost:11211" # memcahced def getSekkaDbInfo( env ) if env.has_key?( 'SEKKA_DB' ) m = env['SEKKA_DB'].match( /^redis:(.*)$/ ) if m dictSource = if 0 == m[1].size dictSource = "localhost" else dictSource = m[1] end [ :redis, dictSource ] elsif m = ENV['SEKKA_DB'].match( /^tokyo[a-z]+$/ ) [ :tokyocabinet, nil ] else raise RuntimeError, "Error: env var SEKKA_DB requires [redis:hostname] or [redis:] or [tokyocabinet]" end else [ :tokyocabinet, nil ] end end def checkJisyoIsInstalled( dictType, dictSource ) key = "SEKKA:VERSION" STDERR.printf( "Info: Checking SEKKA jisyo on #{dictType} server...\n" ) begin result = case dictType when :tokyocabinet require 'tokyocabinet' require 'sekka/kvs' tc = Kvs.new( :tokyocabinet ) tc.open( dictSource ) ret = tc.get( key ) tc.close() ret when :redis begin require 'redis' require 'sekka/kvs' redis = Kvs.new( :redis ) redis.open( dictSource ) ret = redis.get( key ) redis.close() ret rescue => e STDERR.printf( "Error: sekka-server can't connect to redis(port=%s)...\n", dictSource ) exit 1 end end end end def main if not File.directory?( DICTDIR ) Dir.mkdir( DICTDIR ) STDERR.printf( "Info: created directory [%s]\n", DICTDIR ) end # sekka-server自身のpidを書きこむ(デーモン化したときの停止用) open( PIDFILE, "w" ) {|f| f.printf( "%d\n", Process.pid ) } # 環境変数からHTTPプロキシサーバーの情報を取得する proxyHost = nil proxyPort = nil if ENV.key?( 'http_proxy' ) uri = URI.parse ENV[ 'http_proxy' ] proxyPort = uri.port proxyHost = uri.host end if not File.exist?( TSVFILE ) STDERR.printf( "Info: Downloading SEKKA-JISYO\n" ) # 辞書をダウンロードする cmd = sprintf( "curl %s", URLURL ) STDERR.printf( "Command : %s\n", cmd ) tsvurl = open( "|" + cmd ) { |f| f.read } STDERR.printf( " download URL of tsv file : %s\n", tsvurl ); cmd = sprintf( "curl -o %s %s", TSVFILE, tsvurl ) STDERR.printf( "Command : %s\n", cmd ) system( cmd ) cmd = sprintf( "curl -o %s %s", SUMFILE, SUMURL ) STDERR.printf( "Command : %s\n", cmd ) system( cmd ) # チェックサムを確認する downloadSum = "" open( TSVFILE ) { |f| dataBody = f.read downloadSum = Digest::MD5.hexdigest( dataBody ) } open( SUMFILE ) { |f| correctSum = f.readline.chomp.split[0] STDERR.printf( " downloaded file's MD5 : %s\n", downloadSum ) STDERR.printf( " correct MD5 : %s\n", correctSum ) if downloadSum == correctSum STDERR.printf( "Info: downloaded file [%s] verify OK.\n", TSVFILE ) else STDERR.printf( "Error: downloaded file [%s] verify NG.\n", TSVFILE ) File.unlink( TSVFILE ) exit( 1 ) end } end # 環境変数から、DBの接続先情報を取得する。 ( dictType, dictSource ) = getSekkaDbInfo( ENV ) case dictType when :tokyocabinet # .tchファイルが存在するか調べる list = TC_FILE_LIST.select { |name| File.exist?( name ) } # redisサーバーに辞書が投入済みか確認する ok = checkJisyoIsInstalled( dictType, TC_FILE ) if 0 == list.size and (not ok) # tsvファイルをuploadする STDERR.printf( "Info: Uploading...\n" ) cmd = sprintf( "sekka-jisyo restore %s %s", TSVFILE, TC_FILE ) STDERR.printf( "Command : %s\n", cmd ) system( cmd ) end STDERR.printf( "Info: [OK]\n" ) # 存在する.tchファイルの中で一番最初のファイルを使う。 list = TC_FILE_LIST.select { |name| File.exist?( name ) } dictSource = list[0] when :redis # redisサーバーに辞書が投入済みか確認する ok = checkJisyoIsInstalled( dictType, dictSource ) unless ok # tsvファイルをuploadする。 STDERR.printf( "Info: Uploading...\n" ) cmd = sprintf( "sekka-jisyo restore %s %s:%s", TSVFILE, dictType, dictSource ) STDERR.printf( "Command : %s\n", cmd ) system( cmd ) end STDERR.printf( "Info: [OK]\n" ) else raise RuntimeError, sprintf( "Error: unknown dictType [%s]", dictType ) end # 設定項目をConfigオブジェクトに代入 SekkaServer::Config.setup( dictType, dictSource, MEMCACHED, 12929, proxyHost, proxyPort ) # rackに渡すための sekka.ru のインストールパスを求める。 vendordir = File.expand_path(File.dirname(__FILE__) + "/../lib") if RbConfig::CONFIG[ 'vendordir' ] if File.exists? RbConfig::CONFIG[ 'vendordir' ] + "/sekka.ru" vendordir = RbConfig::CONFIG[ 'vendordir' ] end end # print "vendordir = " + vendordir + "\n" # サーバー起動 Rack::Server.start( :environment => "development", :pid => nil, :Port => SekkaServer::Config.listenPort, :Host => "0.0.0.0", :AccessLog => [], :config => vendordir + "/sekka.ru" ) end main()