@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)
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 = []
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)
@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)
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)
@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)
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=[]
# 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)
attr_accessor :id, :server, :log, :status
# initialize
+ public
def initialize(server, id=nil)
@id = server.jobmgr.get_new_job_id()
@server = server
@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
# set logger
+ public
def set_logger( logger )
@log = logger
end
# execute
+ public
def execute(sync=false)
# create a thread for job
#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
#
def job_main
# do nothing
end
+
+
end
require "utils.rb"
+$git_mutex = Mutex.new
+
class GitBuildJob < BuildJob
# initialize
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
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
=end
require 'fileutils'
-require "thread"
$LOAD_PATH.unshift File.dirname(__FILE__)
require "CommonProject.rb"
require "GitBuildJob.rb"
require "PackageManifest.rb"
# mutax for git operation
-$git_mutex = Mutex.new
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
@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
@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
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 )
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
# 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