[Title] Applied the automatic package server synchronization
authordonghee yang <donghee.yang@samsung.com>
Fri, 17 Aug 2012 07:40:13 +0000 (16:40 +0900)
committerdonghee yang <donghee.yang@samsung.com>
Fri, 17 Aug 2012 07:40:13 +0000 (16:40 +0900)
src/pkg_server/DistSync.rb [new file with mode: 0644]
src/pkg_server/distribution.rb
src/pkg_server/packageServer.rb

diff --git a/src/pkg_server/DistSync.rb b/src/pkg_server/DistSync.rb
new file mode 100644 (file)
index 0000000..d160565
--- /dev/null
@@ -0,0 +1,89 @@
+=begin
+ DistSync.rb
+
+Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+
+Contact:
+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 "thread"
+$LOAD_PATH.unshift File.dirname(__FILE__)
+$LOAD_PATH.unshift File.dirname(File.dirname(__FILE__))+"/common"
+require "packageServer.rb"
+require "Action.rb"
+require "ScheduledActionHandler.rb"
+
+class DistSyncAction < Action
+       
+       def initialize(time, pkgserver, dist_name )
+               super(time, pkgserver.sync_interval)
+
+               @pkgserver = pkgserver
+               @dist_name = dist_name
+       end
+
+
+       def init
+       end
+
+
+       def execute
+       
+               # Start to clean job
+               @pkgserver.log.info "Executing sync action for the #{@dist_name}"
+
+               # update pkg info
+               @pkgserver.reload_dist_package 
+
+               # sync 
+               @pkgserver.sync( @dist_name, false )
+       end     
+end
+
+
+class DistSync
+       attr_accessor :quit
+
+       # init
+       def initialize( server )
+               @server = server
+               @handler = ScheduledActionHandler.new
+       end
+
+       # start thread
+       def start()
+               # scan all sync distribution
+               @server.distribution_list.each do |dist| 
+                       # if dist does not have parent server then skip sync
+                       if dist.server_url.empty? then next end
+
+                       time = Time.now
+                       @server.log.info "Registered sync-action for dist : #{dist.name}"
+                       @handler.register(DistSyncAction.new(time, @server, dist.name))
+               end
+
+               # start handler
+               @handler.start 
+       end
+end
index 5c6b9e29bb192edfd9b389f75e876ff2a3e43230..bbab453da2fd71da9d7f357a4a20cd4c482668b9 100644 (file)
@@ -144,15 +144,23 @@ class Distribution
 
                # copy package list
         for os in @support_os_list 
-                       FileUtils.copy( "#{snapshot_path}/#{PKG_LIST_FILE_PREFIX}#{os}",  \
-                                                  "#{@location}/snapshots/#{name}/#{PKG_LIST_FILE_PREFIX}#{os}" )
+                       FileUtils.copy_file( "#{snapshot_path}/#{PKG_LIST_FILE_PREFIX}#{os}",  
+                               "#{@location}/snapshots/#{name}/#{PKG_LIST_FILE_PREFIX}#{os}" )
                end 
                
                # copy archive package list
-               FileUtils.copy( "#{snapshot_path}/#{ARCHIVE_PKG_FILE}", \
-                                          "#{@location}/snapshots/#{name}/#{ARCHIVE_PKG_FILE}" )
+               FileUtils.copy_file( "#{snapshot_path}/#{ARCHIVE_PKG_FILE}", 
+                               "#{@location}/snapshots/#{name}/#{ARCHIVE_PKG_FILE}" )
+
+               # update snapshot.info file information
+               tmp_file_name = @location + "/temp/." + Utils.create_uniq_name
+               # error check 
+               if File.exist? tmp_file_name then 
+                       raise "snapshot temp file name already exist" 
+               end 
 
-               File.open(@snapshot_info_file_path, "a") do |f|
+               FileUtils.copy_file( @snapshot_info_file_path, tmp_file_name )
+               File.open( tmp_file_name, "a" ) do |f|
                        f.puts "name : #{name}"
                        f.puts "time : #{Time.now.strftime("%Y%m%d%H%M%S")}"
                        if from_cmd then 
@@ -163,16 +171,20 @@ class Distribution
                        f.puts "path : /snapshots/#{name}"
                        f.puts
                end
+               FileUtils.mv( tmp_file_name, @snapshot_info_file_path, :force => true )
                
+               # snapshot is generated
                @log.output( "snapshot is generated : #{@location}/snapshots/#{name}", Log::LV_USER)
                return name
        end  
 
        def sync(force) 
+               pkg_list_update_flag = false
+
                # check distribution's server_url
                if @server_url.empty? then 
                        @log.error("This distribution has not remote server", Log::LV_USER)
-                       return
+                       return false
                end
 
                # generate client class
@@ -196,17 +208,27 @@ class Distribution
                        full_pkg_name_list = server_pkg_name_list + local_pkg_name_list
             
                        full_pkg_name_list.each do |pkg_name| 
-                               sync_package( pkg_name, client, os, force )
+                               ret = sync_package( pkg_name, client, os, force )
+                               if ret then pkg_list_update_flag = true end
                        end
                end
                
-               write_all_pkg_list()
+               if pkg_list_update_flag then
+                       write_all_pkg_list()
+                       return true
+               else
+                       return false
+               end
        end
 
     def sync_archive_pkg
         client = Client.new( @server_url, "#{@location}/source", @log )
 
         download_list = client.archive_pkg_list - @archive_pkg_list
+
+               # if update list is empty then return false
+               if download_list.empty? then return false end 
+
         download_list.each do |pkg|
                        file = client.download_dep_source(pkg)
                if file.nil?
@@ -217,6 +239,8 @@ class Distribution
         end
 
         write_archive_pkg_list()
+
+               return true
     end
 
        def add_os(os)
@@ -600,31 +624,37 @@ class Distribution
                        version_cmp = Utils.compare_version( local_pkg.version, server_pkg.version )
                        if ( version_cmp.eql? 0 ) then
                                # version is same then skip update
-                               return
+                               return false
                        end
+
                        if ( local_pkg.origin.eql? "local" ) then 
                                if not force then
                                        # local_pkg is generated from local and not force mode then skip update
-                                       return
+                                       return false
                                end
                        end 
                                
                        sync_package2( server_pkg, client, os, force )
+                       return true
                # if package exist only server
                elsif ( not server_pkg.nil? ) then
                        sync_package2( server_pkg, client, os, force )
+                       return true
                # if package exist only local
                elsif ( not local_pkg.nil? ) then  
                        # if local pkg is generated from local then skip
                        if local_pkg.origin.eql? "local" then  
-                               next
+                               return false
                        end
                        
                        # package remove
                        @pkg_hash_os[os].delete(pkg_name)
+                       return true
                else
                        raise RuntimeError,"hash merge error!"
                end
+
+               return false
        end 
 
        def sync_package2( pkg, client, os, force ) 
index 29105853f71435963c815c41844af555e6a8a2b5..ca8db5c304bf9781dee93c5de428b1b704d9bd10 100644 (file)
@@ -38,11 +38,14 @@ require "SocketRegisterListener"
 require "client"
 require "utils"
 require "mail"
+require "DistSync"
 
 class PackageServer
        attr_accessor :id, :location, :log, :integrity 
        attr_accessor :finish, :port
        attr_accessor :incoming_path
+       attr_accessor :distribution_list
+       attr_accessor :sync_interval
     
        # initialize
        def initialize (id)
@@ -55,6 +58,7 @@ class PackageServer
                @port = 3333 
                @test_time=0 #test time in mili-seconds
                @lock_file=nil
+               @sync_interval = 3600
                
                update_config_information(id)
 
@@ -103,7 +107,8 @@ class PackageServer
                # create server configure file
                File.open( @config_file_path, "w" ) do |f|
                        f.puts "location : #{@location}"
-                       f.puts "integrity check : YES"
+                       f.puts "integrity check : #{@integrity}"
+                       f.puts "auto sync interval : #{@sync_interval}"
                        f.puts "server_url : #{dist_name} ->  #{server_url}"
                end
                
@@ -187,9 +192,9 @@ class PackageServer
                # move file to package server
         binary_pkg_file_path_list.each do |l|
             if test_flag then 
-                FileUtils.cp( l, "#{distribution.location}/temp/" )
+                FileUtils.copy_file( l, "#{distribution.location}/temp/#{File.basename(l)}" )
             else    
-                FileUtils.cp( l, "#{distribution.location}/binary/" )
+                FileUtils.copy_file( l, "#{distribution.location}/binary/#{File.basename(l)}" )
             end     
         end  
 
@@ -265,11 +270,15 @@ class PackageServer
                end 
 
                @lock_file = Utils.file_lock(distribution.lock_file_name)
-               distribution.sync(mode)
-               distribution.sync_archive_pkg
-               Utils.file_unlock(@lock_file)
+               ret1 = distribution.sync(mode)
+               ret2 = distribution.sync_archive_pkg
+               if ret1 or ret2 then 
+                       distribution.generate_snapshot("", "", false)
+               end
                
                @log.output( "package server [#{@id}]'s distribution [#{dist_name}] has been synchronized.", Log::LV_USER )
+
+               Utils.file_unlock(@lock_file)
        end
 
        def add_distribution( dist_name, server_url, clone )
@@ -434,14 +443,19 @@ class PackageServer
 
        # start server daemon
        def start( port ) 
-               # set job request listener
-               @log.info "Setting listener..."
-
+        @log.info "Package server Start..."
                # set port number. default port is 3333
                @port = port 
 
+               # set job request listener
+        @log.info "Setting listener..."
                listener = SocketRegisterListener.new(self)
                listener.start
+
+        # set auto sync
+        @log.info "Setting auto sync..."
+        autosync = DistSync.new(self)
+        autosync.start
        
                # main loop
                @log.info "Entering main loop..."
@@ -562,6 +576,8 @@ class PackageServer
                                                @location = l.split(" :")[1].strip
                                        elsif l.start_with?( "integrity check :") then 
                                                @integrity = l.split(" :")[1].strip.upcase
+                                       elsif l.start_with?( "auto sync interval :" ) then 
+                                               @sync_interval = l.split(" :")[1].strip.to_i
                                        elsif l.start_with?( "server_url :" ) then
                                                info = l.split(" :")[1].split("->")
                                                @dist_to_server_url[info[0].strip] = info[1].strip 
@@ -621,8 +637,11 @@ class PackageServer
                                @log.info "[#{dist_name}] distribution creation. using local server [#{server_url}]"
                        end
 
-                       distribution.sync(false)
-                       distribution.sync_archive_pkg
+                       ret1 = distribution.sync(false)
+                       ret2 = distribution.sync_archive_pkg
+                       if ret1 or ret2 then 
+                               distribution.generate_snapshot("", "", false)
+                       end
                else
                        @log.info "generate package server do not using remote package server"