From: donghee yang Date: Mon, 24 Sep 2012 14:37:23 +0000 (+0900) Subject: [Title] Applied the code that save process_id when execute build command X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7bd744638f0db775ae44c11f0fe134aaccea1a80;p=sdk%2Ftools%2Fsdk-build.git [Title] Applied the code that save process_id when execute build command --- diff --git a/src/build_server/BuildJob.rb b/src/build_server/BuildJob.rb index a2b9f42..df3bbc2 100644 --- a/src/build_server/BuildJob.rb +++ b/src/build_server/BuildJob.rb @@ -733,7 +733,7 @@ class BuildJob < CommonJob @log.info( " - Remote Server : #{@remote_server.ip}:#{@remote_server.port}" ) @log.info( " - FTP Server : #{@server.ftp_addr}" ) else - builder = Builder.create( "JB#{@id}", @pkgserver_url, @log.path, + builder = Builder.create( "JB#{@id}", @pkgserver_url, nil, "#{@buildroot_dir}", @server.build_cache_dir ) if builder.nil? @log.error( "Creating job builder failed", Log::LV_USER) @@ -745,10 +745,6 @@ class BuildJob < CommonJob end @log.info( " - Log Path : #{@log.path}" ) - # set log output - builder.log.close - builder.log = @log - # if sub job, install dependent packages of parent-pkgs and not clean use_clean = true local_pkgs = [] @@ -797,10 +793,9 @@ class BuildJob < CommonJob if not compat_ok then # build if @is_remote_job then - result = builder.build(@project.repository, @source_path, @os, - @is_rev_build_check_job, @git_commit, @no_reverse, local_pkgs) + result = builder.build_job(self, local_pkgs) else - result = builder.build(@source_path, @os, use_clean, local_pkgs, false ) + result = builder.build_job(self, use_clean, local_pkgs, false ) end if not result then @log.error( "Building job failed", Log::LV_USER) @@ -834,7 +829,7 @@ class BuildJob < CommonJob @log.info( " - Remote Server : #{@remote_server.ip}:#{@remote_server.port}" ) @log.info( " - FTP Server : #{@server.ftp_addr}" ) else - builder = Builder.create( "JB#{@id}", @pkgserver_url, @log.path, + builder = Builder.create( "JB#{@id}", @pkgserver_url, nil, "#{@buildroot_dir}/#{@os}", @server.build_cache_dir ) if builder.nil? @log.error( "Creating job builder failed", Log::LV_USER) @@ -846,16 +841,11 @@ class BuildJob < CommonJob end @log.info( " - Log Path : #{@log.path}" ) - # set log output - builder.log.close - builder.log = @log - # build if @is_remote_job then - result = builder.build(@project.repository, @source_path, @os, - false, @git_commit, @no_reverse, []) + result = builder.build_job(self, []) else - result = builder.build(@source_path, @os, true, [], false ) + result = builder.build_job(self, true, [], false ) end if not result then @log.error( "Building job failed", Log::LV_USER) @@ -917,7 +907,7 @@ class BuildJob < CommonJob @log.info( " - Remote Server : #{@remote_server.ip}:#{@remote_server.port}" ) @log.info( " - FTP Server : #{@server.ftp_addr}" ) else - builder = Builder.create( "JB#{@id}", @pkgserver_url, @log.path, + builder = Builder.create( "JB#{@id}", @pkgserver_url, nil, "#{@buildroot_dir}/#{@os}", @server.build_cache_dir ) if builder.nil? @log.error( "Creating job builder failed", Log::LV_USER) @@ -929,10 +919,6 @@ class BuildJob < CommonJob end @log.info( " - Log Path : #{@log.path}" ) - # set log output - builder.log.close - builder.log = @log - # get local packages to overwite # they must be composed of packages of pending jobs and its success list local_pkgs=[] @@ -952,10 +938,9 @@ class BuildJob < CommonJob # build if @is_remote_job then - result = builder.build(@project.repository, @source_path, @os, - false, @git_commit, @no_reverse, local_pkgs) + result = builder.build_job(self, local_pkgs) else - result = builder.build(@source_path, @os, true, local_pkgs, false ) + result = builder.build_job(self, true, local_pkgs, false ) end if not result then @log.error( "Building job failed", Log::LV_USER) diff --git a/src/build_server/CommonJob.rb b/src/build_server/CommonJob.rb index 520d2b0..45bcd59 100644 --- a/src/build_server/CommonJob.rb +++ b/src/build_server/CommonJob.rb @@ -36,6 +36,7 @@ class CommonJob attr_accessor :id, :server, :log, :status # initialize + public def initialize(server, id=nil) @id = server.jobmgr.get_new_job_id() @server = server @@ -45,32 +46,41 @@ class CommonJob @status = "JUST_CREATED" @log = nil + + @sub_pid = 0 end + # set parent + public def set_parent_job( parent ) @parent = parent end + # get parent + public def get_parent_job() return @parent end # check this job has a parent job + public def is_sub_job? return (not @parent.nil?) end # get all sub jobs + public def get_sub_jobs return @sub_jobs end # add sub job + public def add_sub_job( job ) @sub_jobs.push job # this will make sub-job to share build-root of parent @@ -79,12 +89,14 @@ class CommonJob # set logger + public def set_logger( logger ) @log = logger end # execute + public def execute(sync=false) # create a thread for job @@ -109,23 +121,42 @@ class CommonJob #terminate + public def terminate() #do noting end #cancel + public def cancel() # do nothing end + # show progress + public def progress # do nothing return "" end + # create process to execute command + public + def execute_command(cmd) + # execute + pid, status = Utils.execute_shell_with_log(cmd, @log.path, false) + @sub_pid = pid + + # wait for finish + pid, status = Process.waitpid2(pid) + @sub_pid = 0 + + # return + return pid, status + end + # # PROTECTED METHODS # @@ -135,4 +166,6 @@ class CommonJob def job_main # do nothing end + + end diff --git a/src/build_server/GitBuildJob.rb b/src/build_server/GitBuildJob.rb index 7a46878..457e379 100644 --- a/src/build_server/GitBuildJob.rb +++ b/src/build_server/GitBuildJob.rb @@ -34,6 +34,8 @@ require "BuildJob.rb" require "utils.rb" +$git_mutex = Mutex.new + class GitBuildJob < BuildJob # initialize @@ -132,7 +134,7 @@ class GitBuildJob < BuildJob end # download source code - @git_commit = @project.get_source_code(@git_repos, @git_branch, @git_commit, @source_path, @log) + @git_commit = get_source_code() if @git_commit.nil? then @status = "ERROR" return false @@ -182,4 +184,102 @@ class GitBuildJob < BuildJob end + # + # PROTECTED/PRIVATE METHODS + # + + protected + def get_source_code() + $git_mutex.synchronize { + get_source_code_internal() + } + end + + + protected + def get_source_code_internal() + # check git directory + git_path = "#{@server.path}/projects/#{@project.name}/cache/git" + cache_path = "#{@server.path}/projects/#{@project.name}/cache" + if not File.exist? cache_path then + FileUtils.mkdir_p cache_path + end + + # check branch name + if File.exist? git_path then + current_branch = git_cmd_return( "branch", git_path).select{|x| x.start_with?("*")}[0].split(" ")[1].strip + if current_branch != @git_branch then + @log.warn( "Branch name is changed.", Log::LV_USER) + FileUtils.rm_rf git_path + end + end + + # git pull operation + if File.exist? git_path and not git_cmd("pull", git_path,@log) then + @log.warn( "Failed on \"git pull\"", Log::LV_USER) + FileUtils.rm_rf git_path + end + + # if no git, clone it + if not File.exist? git_path then + # if "git pull" failed, try to "git clone" + if not git_cmd("clone #{@git_repos} git", cache_path, @log) then + @log.error( "Failed on \"git clone #{@git_repos}\"", Log::LV_USER) + return nil + end + # git checkout + if not git_cmd("checkout #{@git_branch}", git_path, @log) then + @log.error( "Failed on \"git checkout #{@git_branch}\"", Log::LV_USER) + return nil + end + end + + if @git_commit.nil? then + # get git commit-id + commit_id = "" + result_line = git_cmd_return("log -1", git_path) + if result_line != nil then + result_line.each do |l| + if l.start_with?("commit ") then + commit_id = l.split(" ")[1].strip + end + end + end + + @git_commit = commit_id + else + # git reset + if not git_cmd("reset --hard #{@git_commit}", git_path, @log) then + @log.error( "Failed on \"git reset --hard #{@git_commit}\"", Log::LV_USER) + return nil + end + end + + # copy to source path + FileUtils.cp_r(git_path, @source_path) + + return @git_commit + end + + + protected + def git_cmd(cmd, working_dir, log) + build_command = "cd \"#{working_dir}\";#{@server.git_bin_path} #{cmd}" + + pid, status = execute_command( build_command ) + if status.exitstatus != 0 then + return false + else + return true + end + end + + + protected + def git_cmd_return(cmd, working_dir) + build_command = "cd \"#{working_dir}\";#{@server.git_bin_path} #{cmd}" + ret = Utils.execute_shell_return(build_command) + + return ret + end end diff --git a/src/build_server/GitBuildProject.rb b/src/build_server/GitBuildProject.rb index 3ac261c..6436074 100644 --- a/src/build_server/GitBuildProject.rb +++ b/src/build_server/GitBuildProject.rb @@ -27,7 +27,6 @@ Contributors: =end require 'fileutils' -require "thread" $LOAD_PATH.unshift File.dirname(__FILE__) require "CommonProject.rb" require "GitBuildJob.rb" @@ -35,7 +34,6 @@ require "Version.rb" require "PackageManifest.rb" # mutax for git operation -$git_mutex = Mutex.new class GitBuildProject < CommonProject @@ -178,90 +176,4 @@ class GitBuildProject < CommonProject return false end - - # download source code to "source_path" and return its commit-id - def get_source_code( git_repos, git_branch, git_commit, source_path, log ) - $git_mutex.synchronize { - # check git directory - git_path = "#{@server.path}/projects/#{@name}/cache/git" - cache_path = "#{@server.path}/projects/#{@name}/cache" - if not File.exist? cache_path then - FileUtils.mkdir_p cache_path - end - - # check branch name - if File.exist? git_path then - current_branch = git_cmd_return( "branch", git_path).select{|x| x.start_with?("*")}[0].split(" ")[1].strip - if current_branch != git_branch then - log.warn( "Branch name is changed.", Log::LV_USER) - FileUtils.rm_rf git_path - end - end - - # git pull operation - if File.exist? git_path and not git_cmd("pull", git_path,log) then - log.warn( "Failed on \"git pull\"", Log::LV_USER) - FileUtils.rm_rf git_path - end - - # if no git, clone it - if not File.exist? git_path then - # if "git pull" failed, try to "git clone" - if not git_cmd("clone #{git_repos} git", cache_path, log) then - log.error( "Failed on \"git clone #{git_repos}\"", Log::LV_USER) - return nil - end - # git checkout - if not git_cmd("checkout #{git_branch}", git_path, log) then - log.error( "Failed on \"git checkout #{git_branch}\"", Log::LV_USER) - return nil - end - end - - if git_commit.nil? then - # get git commit-id - commit_id = "" - result_line = git_cmd_return("log -1", git_path) - if result_line != nil then - result_line.each do |l| - if l.start_with?("commit ") then - commit_id = l.split(" ")[1].strip - end - end - end - - git_commit = commit_id - else - # git reset - if not git_cmd("reset --hard #{git_commit}", git_path, log) then - log.error( "Failed on \"git reset --hard #{git_commit}\"", Log::LV_USER) - return nil - end - end - - # copy to source path - FileUtils.cp_r(git_path, source_path) - } - - return git_commit - end - - - def git_cmd(cmd, working_dir, log) - build_command = "cd \"#{working_dir}\";#{@server.git_bin_path} #{cmd}" - pid, status = Utils.execute_shell_with_log(build_command,log.path) - if status.exitstatus != 0 then - return false - else - return true - end - end - - - def git_cmd_return(cmd, working_dir) - build_command = "cd \"#{working_dir}\";#{@server.git_bin_path} #{cmd}" - ret = Utils.execute_shell_return(build_command) - - return ret - end end diff --git a/src/build_server/RemoteBuilder.rb b/src/build_server/RemoteBuilder.rb index 0a1ea23..b7d0ff0 100644 --- a/src/build_server/RemoteBuilder.rb +++ b/src/build_server/RemoteBuilder.rb @@ -47,6 +47,28 @@ class RemoteBuilder @ftp_username = ftp_username @ftp_passwd = ftp_passwd @log = Log.new(nil) + @job = nil + end + + + # build_job + def build_job( job, local_pkgs ) + # set job + @job = job + old_log = @log + @log = job.log + + # build + ret = build(@job.project.repository, @job.source_path, @job.os, + @job.is_rev_build_check_job(), @job.git_commit, @job.no_reverse, + local_pkgs ) + + # reset job + @job = nil + @log = old_log + + # return + return ret end diff --git a/src/builder/Builder.rb b/src/builder/Builder.rb index f9eeecb..0159a70 100644 --- a/src/builder/Builder.rb +++ b/src/builder/Builder.rb @@ -48,7 +48,11 @@ class Builder @host_os = Utils::HOST_OS @buildroot_dir = buildroot_dir @cache_dir = cache_dir - @log = Log.new(log_path) + if not log_path.nil? then + @log = Log.new(log_path) + else + @log = Log.new(nil) + end end @@ -140,6 +144,25 @@ class Builder end + # build_job + def build_job( job, clean, local_pkgs, is_local_build ) + # set job + @job = job + old_log = @log + @log = job.log + + # build + ret = build(job.source_path, job.os, clean, local_pkgs, is_local_build) + + # reset job + @job = nil + @log = old_log + + # return + return ret + end + + # build def build( src_path, os, clean, local_pkgs, is_local_build ) @@ -426,16 +449,21 @@ VERSION=\"#{version}\" " f.puts "#{target}" f.puts "echo \"success\"" end - Utils.execute_shell( "chmod +x #{src_path}/.build.sh", @log ) + Utils.execute_shell( "chmod +x #{src_path}/.build.sh" ) build_command = "cd \"#{src_path}\";" + env_def + "./.build.sh" # execute script - pid, status = Utils.execute_shell_with_log( build_command, @log.path ) + status = nil + if not @job.nil? then + pid, status = @job.execute_command( build_command ) + else + pid, status = Utils.execute_shell_with_log( build_command, @log.path ) + end if status.exitstatus != 0 then @log.error( "Failed on build script: \"#{target}\"", Log::LV_USER) return false else - Utils.execute_shell( "rm -rf #{src_path}/.build.sh", @log ) + Utils.execute_shell( "rm -rf #{src_path}/.build.sh" ) return true end end @@ -618,8 +646,14 @@ VERSION=\"#{version}\" " # zip @log.info( "Creating package file ... #{pkg.package_name}_#{pkg.version}_#{os}.zip", Log::LV_USER) - @log.info("cd \"#{install_dir}\"; zip -r -y #{src_path}/#{pkg.package_name}_#{pkg.version}_#{os}.zip *") - Utils.execute_shell_with_log("cd \"#{install_dir}\"; zip -r -y #{src_path}/#{pkg.package_name}_#{pkg.version}_#{os}.zip *", @log.path) + cmd = "cd \"#{install_dir}\"; zip -r -y #{src_path}/#{pkg.package_name}_#{pkg.version}_#{os}.zip *" + @log.info( cmd ) + if not @job.nil? then + @job.execute_command( cmd ) + else + Utils.execute_shell_with_log(cmd, @log.path) + end + if not File.exist? "#{src_path}/#{pkg.package_name}_#{pkg.version}_#{os}.zip" then return false end