[Title] // Add web-admin command(web-svr)
authordev.sungmin.kim <dev.sungmin.kim@samsung.com>
Fri, 8 Mar 2013 08:52:18 +0000 (17:52 +0900)
committerdev.sungmin.kim <dev.sungmin.kim@samsung.com>
Fri, 8 Mar 2013 08:52:18 +0000 (17:52 +0900)
[Redmine#]  // #7682

package/build.linux
package/build.macos
package/build.windows
web-svr [new file with mode: 0755]

index 8264341c43caf734fe4f11199ce4407f5135294a..4e6e1fd578d91b5d2c6bf8c278ab89ad54d2dd0e 100755 (executable)
@@ -22,5 +22,6 @@ install()
        cp -rf $SRCDIR/src $BIN_DIR/
        cp -rf $SRCDIR/dibs-web $BIN_DIR/
        cp -f $SRCDIR/upgrade $BIN_DIR/
+       cp -f $SRCDIR/web-svr $BIN_DIR/
        echo $VERSION > $BIN_DIR/VERSION
 }
index 8264341c43caf734fe4f11199ce4407f5135294a..d878892915969f6871cf9085fd8a18e877602cf4 100755 (executable)
@@ -21,6 +21,7 @@ install()
        cp -f $SRCDIR/build-* $BIN_DIR/
        cp -rf $SRCDIR/src $BIN_DIR/
        cp -rf $SRCDIR/dibs-web $BIN_DIR/
+       cp -f $SRCDIR/web-svr $BIN_DIR/
        cp -f $SRCDIR/upgrade $BIN_DIR/
        echo $VERSION > $BIN_DIR/VERSION
 }
index 8264341c43caf734fe4f11199ce4407f5135294a..d878892915969f6871cf9085fd8a18e877602cf4 100755 (executable)
@@ -21,6 +21,7 @@ install()
        cp -f $SRCDIR/build-* $BIN_DIR/
        cp -rf $SRCDIR/src $BIN_DIR/
        cp -rf $SRCDIR/dibs-web $BIN_DIR/
+       cp -f $SRCDIR/web-svr $BIN_DIR/
        cp -f $SRCDIR/upgrade $BIN_DIR/
        echo $VERSION > $BIN_DIR/VERSION
 }
diff --git a/web-svr b/web-svr
new file mode 100755 (executable)
index 0000000..abb900a
--- /dev/null
+++ b/web-svr
@@ -0,0 +1,364 @@
+#!/usr/bin/ruby
+=begin
+
+ web-svr
+
+Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+
+Contact:
+Sungmin Kim <dev.sungmin.kim@samsung.com>
+Taejun Ha <taejun.ha@samsung.com>
+Jiil Hyoun <jiil.hyoun@samsung.com>
+Donghyuk Yang <donghyuk.yang@samsung.com>
+DongHee Yang <donghee.yang@samsung.com>
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+Contributors:
+- S-Core Co., Ltd
+=end
+
+require 'fileutils'
+require 'optparse'
+require 'yaml'
+$LOAD_PATH.unshift File.dirname(__FILE__)+"/src/common"
+$LOAD_PATH.unshift File.dirname(__FILE__)+"/src/build_server"
+require "log.rb"
+require 'utils'
+require 'db_utils'
+require "BuildServer"
+require "BuildServerController"
+
+class OptParse
+       #Sub command usage
+       START = "dibs-web start -n <server name> [-p <port number>] [-m <DB mode>] [-d]"
+       STOP = "dibs-web stop -n <server name>"
+       CREATE = "dibs-web create -n <server name> [-e <engine name] [-p <port number>] [-m <DB mode>] [-i]"
+
+       def self.option_error_check( options )
+               case options[:cmd]
+                       when "start" then
+                               if options[:name].nil? then
+                                       raise ArgumentError, "Usage: " + START
+                               end
+                       when "stop" then
+                               if options[:name].nil? then
+                                       raise ArgumentError, "Usage: " + STOP
+                               end
+                       when "create" then
+                               if options[:name].nil? then
+                                       raise ArgumentError, "Usage: " + CREATE
+                               end
+                       else
+                               raise ArgumentError, "Input is incorrect : #{options[:cmd]}"
+           end
+       end
+       
+       def self.option_parse
+           options = {}
+           banner = "DIBS web service command-line tool." + "\n" \
+               + "\n" + "Usage: dibs-web <SUBCOMMAND> [OPTS] or dibs-web (-h|-v)" + "\n" \
+               + "\n" + "SubCommands:" + "\n" \
+               + "\t" + "start         Start web server." + "\n" \
+               + "\t" + "stop          Stop web server." + "\n" \
+               + "\t" + "create        Create web server configuration." + "\n" \
+               + "\n" + "Subcommand usage:" + "\n" \
+               + "\t" + START+ "\n" \
+               + "\t" + STOP+ "\n" \
+               + "\t" + CREATE + "\n" \
+               + "\n" + "Options:" + "\n"
+       
+           optparse = OptionParser.new(nil, 32, ' '*8) do|opts|
+                       opts.banner = banner
+       
+                       options[:locate] = File.dirname(__FILE__)
+               opts.on( '-n', '--name <server name>', 'build server name' ) do|name|
+                   options[:name] = name
+               end
+       
+               options[:engine] = "WEBRick" 
+               opts.on( '-e', '--engine <engine name>', 'web server engine : WEBrick | Passenger' ) do|engine|
+                   options[:engine] = engine
+               end
+       
+               options[:port] = 3000
+                       opts.on( '-p', '--port <port number>', 'default server port' ) do|port|
+                   options[:port] = port
+               end
+       
+               options[:mode] = "production"
+                       opts.on( '-m', '--mode <mode>', 'DB mode : production | test | development' ) do|mode|
+                   options[:mode] = mode.strip
+               end
+       
+               options[:port] = "3000" 
+                       opts.on( '-p', '--port <port>', 'server port number: 3000' ) do|port|
+                   options[:port] = port.strip.to_i
+               end
+       
+                       opts.on( '-i', '--import', 'import configuration from build-server' ) do|import|
+                   options[:import] = true
+               end
+
+                       opts.on( '-d', '--daemon', 'daemon process' ) do|import|
+                   options[:daemon] = true
+               end
+       
+                       opts.on( '-h', '--help', 'display this information' ) do
+                               opts.help.split("\n").each {|op| puts op if not op.include? "--noreverse"}
+                               exit
+               end
+               end
+
+               cmd = ARGV[0]
+
+               if cmd.eql? "create" or cmd.eql? "start" or cmd.eql? "stop" or
+                       cmd =~ /(-v)|(--version)/ or cmd =~ /(help)|(-h)|(--help)/ then
+
+                       if cmd.eql? "help" then
+                               ARGV[0] = "-h"
+                       end
+
+                       options[:cmd] = ARGV[0]
+               else
+                       raise ArgumentError, "Usage: build-cli <SUBCOMMAND> [OPTS] or build-cli -h"
+               end
+       
+           optparse.parse!
+       
+           option_error_check options
+       
+           return options
+       end
+end
+
+class ControlWebServer
+       CONFIG_ROOT = "#{Utils::HOME}/.build_tools/web_server"
+       BUILD_SERVER_CONFIG_ROOT = "#{Utils::HOME}/.build_tools/build_server"
+       SERVER_ROOT =  File.dirname(__FILE__)+"/dibs-web"
+       SERVER_CONFIG =  File.dirname(__FILE__)+"/dibs-web/config/database.yml"
+
+       def self.check_config_file(svr_name)
+               if File.exist? "#{CONFIG_ROOT}/#{svr_name}/server.yml"
+                       return true
+               else
+                       return false
+               end
+       end
+
+       def self.get_server_config(svr_name)
+               config = YAML.load_file("#{CONFIG_ROOT}/#{svr_name}/server.yml")
+       end
+       
+       def self.get_build_server_config(svr_name)
+               config_file = "#{ControlWebServer::BUILD_SERVER_CONFIG_ROOT}/#{svr_name}/server.cfg"
+               config = {}
+
+               #check build-server config-file
+               if not File.exist? config_file
+                       raise RuntimeError, "Creating server failed. The build server is not exist"
+               end
+
+               #parse build-server config-file
+               File.open( config_file, "r" ) do |f|
+                       f.each_line do |l|
+                               if l.strip.start_with?("#") then next end
+                               idx = l.index("=") + 1
+                               length = l.length - idx
+
+                               if l.start_with?("DB_DSN=")
+                                       case l[idx,length].strip
+                                       when /^SQLite3:(.*)/i then
+                                               if $1.strip.empty? then db_dsn = "SQLite3:#{BuildServer::CONFIG_ROOT}/#{id}/server.db"
+                                               else config[:db_dsn] = "SQLite3:#{$1.strip}"
+                                               end
+                                       when /^Mysql:(.*)/i then
+                                               config[:db_dsn] = "Mysql:#{$1}"
+                                       else
+                                               config[:db_dsn] = "SQLite3:#{BuildServer::CONFIG_ROOT}/#{id}/server.db"
+                                       end
+                               elsif l.start_with?("DB_USER=")
+                                       config[:db_user] = l[idx,length].strip if not l[idx,length].strip.empty?
+                               elsif l.start_with?("DB_PASSWORD=")
+                                       config[:db_passwd] = l[idx,length].strip if not l[idx,length].strip.empty?
+                               else
+                                       next
+                               end
+                       end
+               end
+
+               #parse dsn
+               db = DBUtils.dsn_parser(config[:db_dsn])
+
+               config[:database] = db[:database]
+               config[:db_name] = db[:name]
+               config[:db_host] = db[:host]
+               config[:db_port] = db[:port]
+               config[:db_socket] = db[:socket]
+               config[:db_flag] = db[:flag]
+
+               return config
+       end
+end
+
+#option parsing
+begin
+       option = OptParse.option_parse
+rescue => e
+       puts e.message
+       exit 0
+end
+
+# main
+begin
+       test = {}
+       production = {}
+       development = {}
+
+       case option[:cmd]
+               when "create"
+                       # check configuration directory
+                       if not File.exist? ControlWebServer::CONFIG_ROOT
+                               puts "Creating new server configuration... #{ControlWebServer::CONFIG_ROOT}"
+                               FileUtils.mkdir_p "#{ControlWebServer::CONFIG_ROOT}"
+                       end
+
+                       # check config-file
+                       if ControlWebServer.check_config_file(option[:name])
+                               raise RuntimeError, "Creating server failed. The server id is already exist"
+                       else
+                               FileUtils.mkdir_p "#{ControlWebServer::CONFIG_ROOT}/#{option[:name]}"
+                       end
+
+                       # check build-server config-file
+                       if option[:import]
+                               db = ControlWebServer.get_build_server_config(option[:name])
+
+                               if db[:database].upcase == "Mysql".upcase
+                                       db[:adapter] = "mysql2"
+                                       db[:encoding] = "utf8"
+                                       db[:pool] = 5
+                                       db[:timeout] = 5000
+                                       case option[:mode]
+                                               when "production"
+                                                       production = db
+                                               when "test"
+                                                       test = db
+                                               when "development"
+                                                       development = db
+                                       else
+                                               raise RuntimeError, "The mode \"#{option[:mode]}\" is invalid!"
+                                       end
+                               end
+                       end
+
+                       # write config-file
+                       File.open( "#{ControlWebServer::CONFIG_ROOT}/#{option[:name]}/server.yml", "w" ) do |f|
+                               f.puts "server: "
+                               f.puts "   engine: #{option[:engine]}"
+                               f.puts "   port: #{option[:port]}"
+                               f.puts "   db_mode: #{option[:mode]}" 
+                               f.puts "database: "
+                               f.puts " test:"
+                               f.puts "   adapter: #{test[:adapter]}"
+                               f.puts "   encoding: #{test[:encoding]}"
+                               f.puts "   host: #{test[:db_host]}"
+                               f.puts "   port: #{test[:db_port]}"
+                               f.puts "   database: #{test[:db_name]}"
+                               f.puts "   username: #{test[:db_user]}"
+                               f.puts "   password: #{test[:db_passwd]}"
+                               f.puts "   pool: #{test[:pool]}"
+                               f.puts "   timeout: #{test[:timeout]}"
+                               f.puts "" 
+                               f.puts " production: "
+                               f.puts "   adapter: #{production[:adapter]}"
+                               f.puts "   encoding: #{production[:encoding]}"
+                               f.puts "   host: #{production[:db_host]}"
+                               f.puts "   port: #{production[:db_port]}"
+                               f.puts "   database: #{production[:db_name]}"
+                               f.puts "   username: #{production[:db_user]}"
+                               f.puts "   password: #{production[:db_passwd]}"
+                               f.puts "   pool: #{production[:pool]}"
+                               f.puts "   timeout: #{production[:timeout]}"
+                               f.puts "" 
+                               f.puts " development: "
+                               f.puts "   adapter: #{development[:adapter]}"
+                               f.puts "   encoding: #{development[:encoding]}"
+                               f.puts "   host: #{development[:db_host]}"
+                               f.puts "   port: #{development[:db_port]}"
+                               f.puts "   database: #{development[:db_name]}"
+                               f.puts "   username: #{development[:db_user]}"
+                               f.puts "   password: #{development[:db_passwd]}"
+                               f.puts "   pool: #{development[:pool]}"
+                               f.puts "   timeout: #{development[:timeout]}"
+                       end
+               when "start"
+                       # check server config
+                       if not ControlWebServer.check_config_file(option[:name])
+                               raise RuntimeError, "The server \"#{option[:name]}\" does not exist!"
+                       end
+
+                       # parsing config
+                       config = ControlWebServer.get_server_config(option[:name])
+                       server = config['server']
+                       database = config['database']
+
+                       # copy config about database
+                       File.open("#{ControlWebServer::SERVER_CONFIG}", 'w') do |f|
+                               f.write(YAML.dump(database))
+                       end
+                       
+                       # run web-server
+                       if option[:daemon]
+                               daemon = "-d"
+                       end
+
+                       case server['engine'].upcase
+                               when 'WEBRick'.upcase
+                                       cmd = "rails server -p #{server['port']} #{daemon} -e #{server['db_mode']}"
+                               when 'Passenger'.upcase
+                                       cmd = "passenger start -p #{server['port']} #{daemon} -e #{server['db_mode']}"
+                       else
+                               raise RuntimeError, "The engine is not supported. #{server['engine']}"
+                       end
+                        Dir.chdir(ControlWebServer::SERVER_ROOT)
+
+                        system(cmd)
+
+               when "stop"
+                       # check server config
+                       if not ControlWebServer.check_config_file(option[:name])
+                               raise RuntimeError, "The server \"#{option[:name]}\" does not exist!"
+                       end
+
+                       # parsing config
+                       config = ControlWebServer.get_server_config(option[:name])
+                       server = config['server']
+
+                       # stop web-server
+                       case server['engine'].upcase
+                               when 'WEBRick'.upcase
+                                       puts "WEBRick stopping is not support. Try kill process"
+                               when 'Passenger'.upcase
+                                       cmd = "passenger stop --pid-file #{ControlWebServer::SERVER_ROOT}/tmp/pids/passenger.#{server['port']}.pid"
+                                       system(cmd)
+                       else
+                               raise RuntimeError, "The engine is not supported. #{server['engine']}"
+                       end
+       else
+               raise RuntimeError, "input option incorrect : #{option[:cmd]}"
+       end
+rescue => e
+       puts e.message
+end
+