From 79bd71226c2c08096e2965b38f3a846697d6139e Mon Sep 17 00:00:00 2001 From: "donghyuk.yang" Date: Tue, 12 Feb 2013 16:00:34 +0900 Subject: [PATCH] [Title] Do not access remote server when working on local --- pkg-cli | 35 ++++------- src/pkg_server/client.rb | 119 ++++++++++++++++++++++------------- src/pkg_server/downloader.rb | 12 +++- 3 files changed, 96 insertions(+), 70 deletions(-) diff --git a/pkg-cli b/pkg-cli index f2d2e8b..eff2d9d 100755 --- a/pkg-cli +++ b/pkg-cli @@ -66,49 +66,36 @@ when "update" then client = Client.new( option[:url], nil, nil ) #client.update() when "clean" then - client = Client.new( nil, option[:loc], nil ) + client = Client.new( nil, option[:loc], nil, false ) client.clean(option[:f]) when "download" then client = Client.new( option[:url], option[:loc], nil ) - #if not option[:url].nil? then - # client.update() - #end file_loc = client.download( option[:pkg], option[:os], option[:t] ) when "install" then client = Client.new( option[:url], option[:loc], nil ) - #if not option[:url].nil? then - # client.update() - #end client.install( option[:pkg], option[:os], option[:t], option[:f] ) when "install-file" then - client = Client.new( option[:url], option[:loc], nil ) - client.install_local_pkg( option[:pkg], option[:t], option[:f] ) + if option[:t] then + client = Client.new( option[:url], option[:loc], nil ) + client.install_local_pkg( option[:pkg], option[:t], option[:f] ) + else + client = Client.new( nil, option[:loc], nil, false ) + client.install_local_pkg( option[:pkg], option[:t], option[:f] ) + end when "uninstall" then - client = Client.new( nil, option[:loc], nil ) + client = Client.new( nil, option[:loc], nil, false ) client.uninstall( option[:pkg], option[:t] ) when "upgrade" then client = Client.new( option[:url], option[:loc], nil ) - #if not option[:url].nil? then - # client.update() - #end client.upgrade( option[:os], option[:t] ) when "check-upgrade" then client = Client.new( option[:url], option[:loc], nil ) - #if not option[:url].nil? then - # client.update() - #end client.check_upgrade( option[:os] ) when "show-rpkg" then client = Client.new( option[:url], nil, nil ) - #if not option[:url].nil? then - # client.update() - #end puts client.show_pkg_info( option[:pkg], option[:os] ) when "list-rpkg" then client = Client.new( option[:url], nil, nil ) - #if not option[:url].nil? then - # client.update() - #end result = client.show_pkg_list( option[:os] ) if not result.nil? and not result.empty? then result.each do |i| @@ -119,10 +106,10 @@ when "list-rpkg" then end end when "show-lpkg" then - client = Client.new( nil, option[:loc], nil ) + client = Client.new( nil, option[:loc], nil, false ) puts client.show_installed_pkg_info( option[:pkg] ) when "list-lpkg" then - client = Client.new( nil, option[:loc], nil ) + client = Client.new( nil, option[:loc], nil, false ) result = client.show_installed_pkg_list() if not result.nil? and not result.empty? then result.each do |i| diff --git a/src/pkg_server/client.rb b/src/pkg_server/client.rb index 87d7ca6..c30e0cc 100644 --- a/src/pkg_server/client.rb +++ b/src/pkg_server/client.rb @@ -56,7 +56,7 @@ class Client CONFIG_PATH = "#{PackageServerConfig::CONFIG_ROOT}/client" PACKAGE_INFO_DIR = ".info" DEFAULT_INSTALL_DIR = "#{Utils::HOME}/build_root" - DEFAULT_SERVER_ADDR = "http://172.21.17.55/dibs/unstable" + #DEFAULT_SERVER_ADDR = "http://172.21.17.55/dibs/unstable" OS_INFO_FILE = "os_info" ARCHIVE_PKG_LIST_FILE = "archive_pkg_list" @@ -67,7 +67,7 @@ class Client # create "remote package hash" and "installed package hash" # @server_addr = server address (can be included distribution, snapshot) # @location = client location (download and install file to this location) - def initialize(server_addr, location, logger) + def initialize(server_addr, location, logger, access_svr = true) # set log if logger.nil? or logger.class.to_s.eql? "String" then @@ -78,56 +78,77 @@ class Client # create directory if not File.exist? CONFIG_PATH then FileUtils.mkdir_p "#{CONFIG_PATH}" end - - # set default server address, location - if server_addr.nil? then server_addr = get_default_server_addr() end + # set location if location.nil? then location = get_default_inst_dir() end - # chop server address, if end with "/" - if server_addr.strip.end_with? "/" then server_addr = server_addr.chop end - + @location = location @snapshot_path = nil @snapshot_url = false - - if is_snapshot_url(server_addr) then - @snapshot_url = true - @server_addr, @snapshot_path = split_addr_and_snapshot(server_addr) - else - @server_addr = server_addr - end - - @location = location @pkg_hash_os = {} @installed_pkg_hash_loc = {} @archive_pkg_list = [] @all_dep_list = [] - @is_server_remote = Utils.is_url_remote(server_addr) @support_os_list = [] - @config_dist_path = CONFIG_PATH + "/" + get_flat_serveraddr - @download_path = @config_dist_path + "/downloads" - @tmp_path = @config_dist_path + "/tmp" - @snapshots_path = @config_dist_path + "/snapshots" + @server_addr = nil - # create directory - if not File.exist? @config_dist_path then FileUtils.mkdir_p "#{@config_dist_path}" end - if not File.exist? @download_path then FileUtils.mkdir_p "#{@download_path}" end - if not File.exist? @snapshots_path then FileUtils.mkdir_p "#{@snapshots_path}" end - if not File.exist? @tmp_path then FileUtils.mkdir_p "#{@tmp_path}" end + + # if client should access remote server, set server information + if access_svr then + # if server address is nil, get a previous server address + if server_addr.nil? then + server_addr = get_default_server_addr() + if not server_addr.nil? then + @log.info "Get default server address from config file [#{server_addr}]" + end + end + + if not server_addr.nil? then + # chop server address, if end with "/" + if server_addr.strip.end_with? "/" then server_addr = server_addr.chop end + + if is_snapshot_url(server_addr) then + @snapshot_url = true + @server_addr, @snapshot_path = split_addr_and_snapshot(server_addr) + else + @server_addr = server_addr + end + @is_server_remote = Utils.is_url_remote(server_addr) + @config_dist_path = CONFIG_PATH + "/" + get_flat_serveraddr + @download_path = @config_dist_path + "/downloads" + @tmp_path = @config_dist_path + "/tmp" + @snapshots_path = @config_dist_path + "/snapshots" + + # create directory + if not File.exist? @config_dist_path then FileUtils.mkdir_p "#{@config_dist_path}" end + if not File.exist? @download_path then FileUtils.mkdir_p "#{@download_path}" end + if not File.exist? @snapshots_path then FileUtils.mkdir_p "#{@snapshots_path}" end + if not File.exist? @tmp_path then FileUtils.mkdir_p "#{@tmp_path}" end + end + + # read remote pkg list, and hash list + @log.info "Update remote package list and supported os list.." + if update() then + @log.info "Initialize - #{server_addr}, #{location}" + else + @log.error "Failed to update remote package list." + end + end # read installed pkg list, and create hash if not File.exist? @location then FileUtils.mkdir_p "#{@location}" end @log.info "Update local package list.. [#{@location}]" read_installed_pkg_list() - - # read remote pkg list, and hash list - @log.info "Update remote package list and supported os list.." - update() - @log.info "Initialize - #{server_addr}, #{location}" end public # update package list from server def update() + + if @server_addr.nil? then + @log.warn "Server address is null" + return false + end + if not @snapshot_url then $get_snapshot_mutex.synchronize do @snapshot_path = get_lastest_snapshot(@is_server_remote) @@ -135,7 +156,7 @@ class Client end @log.info "The lastest snapshot : #{@snapshot_path}" if @snapshot_path.nil? then - @log.warn "Failed to get the lastest package list" + @log.warn "Failed to get the lastest package list." @snapshot_path = "" end @@ -144,12 +165,12 @@ class Client @log.info "Snapshot information is already cached [#{get_pkglist_path()}]" exists_snapshot = true else - @log.info "Snapshot information is not cached" + @log.info "Snapshot information is not cached." end list_path = get_pkglist_path() if list_path.nil? then - @log.error "Failed to get package list path" + @log.error "Failed to get package list path." return false end @@ -189,7 +210,11 @@ class Client end $update_mutex.synchronize do - create_default_config(@server_addr) + if not create_default_config(@server_addr) then + @log.warn "Config file can not be created." + else + @log.info "Config file is created." + end @log.info "Update package list from \"#{@server_addr}\".. OK" end @@ -793,11 +818,13 @@ class Client filepath = "#{CONFIG_PATH}/config" server_addr = nil - if not File.exist? filepath then create_default_config(nil) end - if not File.exist? filepath then - @log.error "There is no default server address in #{filepath}" - return nil - end + if not File.exist? filepath then return nil end + + #if not File.exist? filepath then create_default_config(nil) end + #if not File.exist? filepath then + # @log.error "There is no default server address in #{filepath}" + # return nil + #end File.open filepath, "r" do |f| f.each_line do |l| @@ -808,7 +835,7 @@ class Client end end - if server_addr.nil? then create_default_config(DEFAULT_SERVER_ADDR) end + #if server_addr.nil? then create_default_config(DEFAULT_SERVER_ADDR) end return server_addr end @@ -821,8 +848,12 @@ class Client private # create default config file (Utils::HOME/.build_tools/client/config) def create_default_config(server_addr) + if server_addr.nil? then + return false + end + filepath = "#{CONFIG_PATH}/config" - if server_addr.nil? then server_addr = DEFAULT_SERVER_ADDR end + #if server_addr.nil? then server_addr = DEFAULT_SERVER_ADDR end if File.exist? filepath then FileUtils.rm_f(filepath) @@ -833,6 +864,8 @@ class Client File.open(filepath, "a+") do |file| file.puts "DEFAULT_SERVER_ADDR : #{server_addr}" end + + return true end public diff --git a/src/pkg_server/downloader.rb b/src/pkg_server/downloader.rb index b5dd1ec..fafefc4 100644 --- a/src/pkg_server/downloader.rb +++ b/src/pkg_server/downloader.rb @@ -45,9 +45,15 @@ class FileDownLoader logger.info "Downloading #{url}" if is_remote then - pid,status = Utils.execute_shell_with_log( "wget #{url} -O #{fullpath} -nv --tries=3", logger.path ) - ret = (not status.nil? and status.exitstatus != 0) ? false : true - #ret = Utils.execute_shell( "wget #{url} -O #{fullpath} -q") + if logger.path.nil? or logger.path.empty? then + ret = system "wget #{url} -O #{fullpath} -nv --tries=3" + puts ret + else + pid,status = Utils.execute_shell_with_log( "wget #{url} -O #{fullpath} -nv --tries=3", logger.path ) + if not status.nil? then + ret = (status.exitstatus != 0) ? false : true + end + end else if not File.exist? url then logger.error "\"#{url}\" file does not exist" -- 2.34.1