2025-02-17 17:21:56 +03:00
require " sequel "
cfg_internal = IniConfig . new ( )
2025-03-16 00:12:37 +03:00
$DDB = Sequel . connect ( cfg_internal . get_db )
2025-02-17 17:21:56 +03:00
class Repos < Sequel :: Model ( :repos )
end
2025-02-20 23:56:47 +03:00
class Recips < Sequel :: Model ( :recips )
end
class RepocRecips < Sequel :: Model ( :repos_recips )
end
2025-02-23 23:59:45 +03:00
class Projects < Sequel :: Model ( :projects )
end
class ReposProjects < Sequel :: Model ( :repos_projects )
end
2025-03-08 00:13:31 +03:00
class ProjectsReposSpec < Sequel :: Model ( :projects_repos_spec )
end
2025-03-08 23:57:56 +03:00
class ProjectsProjects < Sequel :: Model ( :projects_projects )
end
2025-03-12 23:44:22 +03:00
class BuildTask < Sequel :: Model ( :buildtask )
end
2025-03-15 00:01:18 +03:00
class Rpms < Sequel :: Model ( :rpms )
end
class BuildRpms < Sequel :: Model ( :build_rpm )
end
2025-02-17 17:21:56 +03:00
class DBase
2025-03-14 00:06:42 +03:00
attr :error , :last_id , :cfg
2025-02-17 17:21:56 +03:00
2025-03-14 00:06:42 +03:00
def initialize ( cfg )
@cfg = cfg
end
2025-03-15 00:01:18 +03:00
def creategit ( project_name , description )
2025-02-17 17:21:56 +03:00
@error = nil
2025-02-22 23:28:36 +03:00
data = Repos . where ( reponame : project_name ) . first
if data . nil?
2025-02-17 17:21:56 +03:00
id = Repos . insert ( reponame : project_name , descr : description , public : 1 )
@last_id = id
else
@error = " Данный репозиторий уже существует "
end
@error
end
def get_repo_info_by_name ( repo_name )
2025-03-08 00:13:31 +03:00
Repos . where ( reponame : repo_name ) . first
2025-02-17 17:21:56 +03:00
end
2025-02-20 23:56:47 +03:00
2025-02-28 00:13:04 +03:00
def get_repo_info_by_id ( id )
Repos [ id ]
end
2025-02-20 23:56:47 +03:00
def get_recips ( )
Recips . order ( :id ) . map do | item |
{ :fname = > item [ :filepath ] , :descr = > item [ :descr ] , :id = > item [ :id ] }
end
end
2025-02-22 00:11:05 +03:00
def delete_repo_by_name ( repo_name )
rep_id = Repos . where ( reponame : repo_name ) . first
unless rep_id [ :id ] . nil?
id = rep_id [ :id ]
RepocRecips . where ( repo_id : id ) . delete
2025-02-28 00:13:04 +03:00
ReposProjects . where ( repo_id : id ) . delete
2025-03-23 17:27:22 +03:00
Repos . where ( reponame : repo_name ) . delete
2025-02-22 00:11:05 +03:00
end
end
2025-02-22 23:28:36 +03:00
def createrecip ( filepath , description , codedata , gitlist )
error_data = nil
filepath_san = sanitize_rcptname ( filepath )
is_data = Recips . where ( filepath : filepath_san ) . first
if codedata . nil? || codedata . strip == " "
error_data
else
if is_data . nil?
id = Recips . insert ( filepath : filepath_san , descr : description , content : codedata )
@last_id = id
if ! gitlist . nil? && gitlist . length > 0
gitlist . each do | item |
data = Repos . where ( id : item . to_i ) . first
unless data . nil?
RepocRecips . insert ( repo_id : data [ :id ] , recip_id : id )
end
end
end
error_data
else
" Рецепт с таким именем #{ filepath_san } уже существует "
end
end
end
def updaterecip ( id , filepath , description , codedata , gitlist )
error_data = nil
filepath_san = sanitize_rcptname ( filepath )
is_data = Recips . where ( filepath : filepath_san ) . first
if codedata . nil? || codedata . strip == " "
error_data
else
unless is_data . nil?
Recips . where ( id : id . to_i ) . update ( filepath : filepath_san , descr : description , content : codedata )
RepocRecips . where ( recip_id : id . to_i ) . delete
if ! gitlist . nil? && gitlist . length > 0
gitlist . each do | item |
data = Repos . where ( id : item . to_i ) . first
unless data . nil?
RepocRecips . insert ( repo_id : data [ :id ] , recip_id : id )
end
end
end
error_data
else
" Рецепт с таким именем #{ filepath_san } не существует "
end
end
end
def get_rcp_info_by_id ( rcpi_id )
info = Recips [ rcpi_id . to_i ]
gits = RepocRecips . where ( recip_id : info [ :id ] )
info [ :repos_list ] = gits . map { | item | item [ :repo_id ] . to_s }
info
end
def delete_rcp ( id )
RepocRecips . where ( recip_id : id . to_i ) . delete
Recips . where ( id : id . to_i ) . delete
end
2025-02-23 23:59:45 +03:00
def proj_list
Projects . order ( :id ) . all
end
2025-02-24 23:54:28 +03:00
2025-02-26 23:55:39 +03:00
def proj ( id )
Projects [ id ]
end
2025-11-07 23:31:30 +03:00
def proj_create ( proj_name , proj_descr , nopublic , tmpbld )
2025-02-24 23:54:28 +03:00
@error = nil
2025-02-25 23:57:44 +03:00
data = Projects . where ( projname : proj_name ) . first
2025-02-24 23:54:28 +03:00
if data . nil?
2025-03-21 00:02:18 +03:00
public_proj = 1
unless nopublic . nil?
public_proj = 0
end
2025-11-07 23:31:30 +03:00
tmpbld_proj = 1
if tmpbld . nil?
tmpbld_proj = 0
end
id = Projects . insert ( projname : proj_name , descr : proj_descr , public : public_proj , tmpstpbuild : tmpbld_proj )
2025-02-24 23:54:28 +03:00
@last_id = id
else
@error = " Данный проект уже существует "
end
@error
end
2025-02-26 23:55:39 +03:00
def get_gits_for_projects ( id )
result = [ ]
git_list = ReposProjects . where ( proj_id : id . to_i ) . all
unless git_list . nil?
result = git_list . map do | item |
Repos [ item [ :repo_id ] ]
end . reject do | item |
item . nil?
end
end
result
end
2025-02-28 00:13:04 +03:00
def save_git_project ( prj_id , git_id )
result = ReposProjects . where ( proj_id : prj_id , repo_id : git_id ) . first
if result . nil?
id = ReposProjects . insert ( proj_id : prj_id , repo_id : git_id )
@last_id = id
end
end
2025-03-08 00:13:31 +03:00
def get_project_repo_spec ( prj_id , git_id )
ProjectsReposSpec . where ( proj_id : prj_id . to_i , repo_id : git_id . to_i ) . first
end
2025-03-08 23:57:56 +03:00
def save_project_repo_spec ( prj_id , git_id , spec )
rep = ProjectsReposSpec . where ( proj_id : prj_id . to_i , repo_id : git_id . to_i ) . first
if rep . nil?
id = ProjectsReposSpec . insert ( proj_id : prj_id . to_i , repo_id : git_id . to_i , spec_name : spec )
@last_id = id
else
ProjectsReposSpec . where ( proj_id : prj_id . to_i , repo_id : git_id . to_i ) . update ( spec_name : spec )
@last_id = nil
end
end
def delete_project_repo_spec ( prj_id , git_id )
ProjectsReposSpec . where ( proj_id : prj_id . to_i , repo_id : git_id . to_i ) . delete
end
def get_projects_links ( prj_id )
ProjectsProjects . where ( proj_id : prj_id . to_i ) . all
end
2025-03-09 23:50:10 +03:00
def delete_linked_projects ( prj_id )
ProjectsProjects . where ( proj_id : prj_id . to_i ) . delete
end
def delete_linked_projects_with_id ( prj_id , prj_id_link )
ProjectsProjects . where ( proj_id : prj_id . to_i , proj_id_repository : prj_id_link . to_i ) . delete
end
def save_linked_projects ( prj_id , prj_id_link )
item = ProjectsProjects . where ( proj_id : prj_id . to_i , proj_id_repository : prj_id_link . to_i ) . first
if item . nil?
id = ProjectsProjects . insert ( proj_id : prj_id . to_i , proj_id_repository : prj_id_link . to_i )
@last_id = id
end
end
2025-03-11 23:54:19 +03:00
def get_git_recips ( git_id )
res = [ ]
recip = RepocRecips . where ( repo_id : git_id . to_i ) . all
unless recip . nil?
res = recip . map do | item |
rcp_info = Recips [ item [ :recip_id ] ]
rcp_info
end
end
res
end
2025-03-12 23:44:22 +03:00
#result = 0 (in progress), 1 (stopped - error), 2 (stopped - success)
def create_build_task ( prj_id , git_id , proj_path )
2025-03-21 23:15:46 +03:00
id = BuildTask . insert ( repo_id : git_id . to_i , proj_id : prj_id . to_i , logpath : " " , errlogpath : " " , result : 0 )
2025-03-12 23:44:22 +03:00
@last_id = id
2025-03-15 00:01:18 +03:00
BuildTask . where ( id : id ) . update ( logpath : File . join ( proj_path , " #{ id } " ) , errlogpath : File . join ( proj_path , " #{ id } " , " process.log " ) )
2025-03-12 23:44:22 +03:00
end
def update_build_task_status ( build_id , status )
BuildTask . where ( id : build_id . to_i ) . update ( result : status . to_i )
end
2025-11-15 22:54:56 +03:00
def get_build_task_status ( build_id )
BuildTask . where ( id : build_id . to_i ) . first
end
2025-03-12 23:44:22 +03:00
def update_build_task_error_log ( build_id , path )
BuildTask . where ( id : build_id . to_i ) . update ( errlogpath : path )
end
def get_build_task_process_log ( build_id )
BuildTask . where ( id : build_id . to_i ) . first
end
2025-03-14 00:06:42 +03:00
def before_fork ( )
Sequel :: DATABASES . each ( & :disconnect )
end
def after_fork ( )
2025-03-16 00:12:37 +03:00
$DDB = Sequel . connect ( @cfg . get_db )
2025-03-14 00:06:42 +03:00
end
2025-03-15 00:01:18 +03:00
2025-03-21 00:02:18 +03:00
def save_rpm ( build_id , path_to_rpm , rpm_name , git_id , sha256 )
id = Rpms . insert ( savepath : path_to_rpm , rpmname : rpm_name , sign : 0 , signpath : " " , repo_id : git_id . to_i , filehash : sha256 )
2025-03-15 00:01:18 +03:00
@last_id = id
BuildRpms . insert ( build_id : build_id . to_i , rpm_id : id )
end
2025-03-16 00:12:37 +03:00
def get_gits_rpms ( )
2025-03-20 00:37:19 +03:00
$DDB [ " select t1.id, t1.reponame, count(*) as packages from repos as t1 join rpms as t2 on t2.repo_id = t1.id group by t1.id, t1.reponame order by t1.id desc " ] . all
2025-03-16 00:12:37 +03:00
end
def get_rpms_for_git ( git_id )
2025-03-20 00:37:19 +03:00
$DDB [ " select t2.id as rpmid, t2.rpmname, t1.reponame as repoid, t4.id as builid, t4.proj_id as prjid, t4.create_at from repos as t1 join rpms as t2 on t2.repo_id = t1.id join build_rpm as t3 on t3.rpm_id = t2.id join buildtask as t4 on t4.id = t3.build_id where t1.id = ? and t2.savepath not like '%.src.rpm' order by t4.id desc, t2.rpmname " , git_id . to_i ] . all
2025-03-16 00:12:37 +03:00
end
2025-03-16 18:08:13 +03:00
def get_rpm_info ( rpm_id )
Rpms [ rpm_id . to_i ]
end
def get_rpm_build ( rpm_id )
bld_info = BuildRpms . where ( rpm_id : rpm_id . to_i ) . first
if bld_info . nil?
nil
else
bld_info [ :build_id ]
end
end
def get_rpm_srpms ( rpm_id )
bld_info = BuildRpms . where ( rpm_id : rpm_id . to_i ) . first
if bld_info . nil?
nil
else
bld_info [ :build_id ]
result = BuildRpms . where ( build_id : bld_info [ :build_id ] . to_i )
if result . nil?
nil
else
result . each do | item |
rpm_p = Rpms [ item [ :rpm_id ] . to_i ]
unless rpm_p . nil?
if rpm_p [ :savepath ] =~ / \ .src \ .rpm$ /
return rpm_p
end
end
end
end
nil
end
end
2025-03-17 00:36:18 +03:00
def get_builds ( )
2025-12-03 23:47:03 +03:00
$DDB [ " select t1.id as buildid, t1.create_at as createat, t1.result as state, case when buildstart is null then 0 when buildstop is null then 0 else Cast((JulianDay(buildstop) - JulianDay(buildstart))*24*60*60 As Integer) end as timeproc, t2.reponame as reponame, t2.id as gitid, t3.id as projid, t3.projname as prjname, count(*) as pkgcnt from buildtask as t1 join repos as t2 on t1.repo_id = t2.id join projects as t3 on t1.proj_id = t3.id left join build_rpm as t4 on t4.build_id = t1.id group by buildid, createat, state, reponame, projid, prjname, gitid order by t1.id desc " ] . all
2025-03-17 00:36:18 +03:00
end
def get_build_info ( build_id )
BuildTask [ build_id . to_i ]
end
def get_rpms_for_build ( build_id )
bld_info = BuildRpms . where ( build_id : build_id . to_i )
bld_info . map do | item |
result = Rpms [ item [ :rpm_id ] ]
result [ :fname ] = File . basename ( result [ :savepath ] )
result
end
end
def get_rpms ( )
Rpms . order ( :id ) . all
end
2025-03-17 23:33:47 +03:00
def get_builds_for_project ( prj_id )
2025-12-03 23:47:03 +03:00
$DDB [ " select t1.id as buildid, t1.create_at as createat, t1.result as state, case when buildstart is null then 0 when buildstop is null then 0 else Cast((JulianDay(buildstop) - JulianDay(buildstart))*24*60*60 As Integer) end as timeproc,t2.reponame as reponame, t2.id as gitid, t3.id as projid, t3.projname as prjname, count(*) as pkgcnt from buildtask as t1 join repos as t2 on t1.repo_id = t2.id join projects as t3 on t1.proj_id = t3.id left join build_rpm as t4 on t4.build_id = t1.id where t1.proj_id = ? group by buildid, createat, state, reponame, projid, prjname, gitid order by t1.id desc " , prj_id . to_i ] . all
2025-03-17 23:33:47 +03:00
end
def get_builds_for_project_git ( prj_id , git_id )
2025-12-03 23:47:03 +03:00
$DDB [ " select t1.id as buildid, t1.create_at as createat, t1.result as state, case when buildstart is null then 0 when buildstop is null then 0 else Cast((JulianDay(buildstop) - JulianDay(buildstart))*24*60*60 As Integer) end as timeproc ,t2.reponame as reponame, t2.id as gitid, t3.id as projid, t3.projname as prjname, count(*) as pkgcnt from buildtask as t1 join repos as t2 on t1.repo_id = t2.id join projects as t3 on t1.proj_id = t3.id left join build_rpm as t4 on t4.build_id = t1.id where t1.proj_id = ? and t1.repo_id = ? group by buildid, createat, state, reponame, projid, prjname, gitid order by t1.id desc " , prj_id . to_i , git_id . to_i ] . all
2025-03-17 23:33:47 +03:00
end
def delete_git_from_project ( prj_id , git_id )
ReposProjects . where ( proj_id : prj_id . to_i , repo_id : git_id . to_i ) . delete
ProjectsReposSpec . where ( proj_id : prj_id . to_i , repo_id : git_id . to_i ) . delete
end
2025-03-18 00:10:53 +03:00
def delete_project ( prj_id )
2025-12-25 23:32:53 +03:00
result = ProjectsProjects . where ( proj_id_repository : prj_id . to_i )
count = 0
result . each do | item |
count = count + 1
end
return 1 if count > 0
2025-03-18 00:10:53 +03:00
ReposProjects . where ( proj_id : prj_id . to_i ) . delete
ProjectsReposSpec . where ( proj_id : prj_id . to_i ) . delete
builds = BuildTask . where ( proj_id : prj_id . to_i )
2025-03-21 00:02:18 +03:00
builds . each do | item |
2025-03-18 00:10:53 +03:00
rpms = BuildRpms . where ( build_id : item [ :id ] )
2025-12-25 23:32:53 +03:00
rpms . each do | rpm |
rpm_id_t = rpm [ :rpm_id ]
BuildRpms . where ( build_id : item [ :id ] , rpm_id : rpm_id_t ) . delete
Rpms . where ( id : rpm_id_t ) . delete
end
2025-03-18 00:10:53 +03:00
end
BuildTask . where ( proj_id : prj_id . to_i ) . delete
2025-12-25 23:32:53 +03:00
ProjectsProjects . where ( proj_id : prj_id . to_i ) . delete
2025-03-21 00:02:18 +03:00
Projects . where ( id : prj_id . to_i ) . delete
2025-12-25 23:32:53 +03:00
0
2025-03-18 00:10:53 +03:00
end
def projects_with_current_as_link ( prj_id )
ProjectsProjects . where ( proj_id_repository : prj_id . to_i ) . all
end
2025-03-21 23:15:46 +03:00
def get_rpm_info_by_hash ( hash )
Rpms . where ( filehash : hash ) . first
end
def update_rpm_sign ( rpm_id , sign_path )
Rpms . where ( id : rpm_id . to_i ) . update ( sign : 1 , signpath : sign_path )
end
def set_project_address ( prj_id , address )
2025-03-23 20:29:43 +03:00
Projects . where ( id : prj_id . to_i ) . update ( remote_address : address )
2025-03-21 23:15:46 +03:00
end
2025-03-23 17:27:22 +03:00
def get_gits ( )
Repos . all
end
def delete_git_by_id ( id )
Repos . where ( id : id . to_i ) . delete
end
2025-11-15 22:54:56 +03:00
def cancel_hang_builds ( )
BuildTask . where ( result : [ 0 , 3 ] ) . update ( result : 4 )
end
2025-12-03 23:47:03 +03:00
def update_build_task_begin_time ( build_id )
BuildTask . where ( id : build_id . to_i ) . update ( buildstart : DateTime . now )
end
def update_build_task_end_time ( build_id )
BuildTask . where ( id : build_id . to_i ) . update ( buildstop : DateTime . now )
end
2025-02-17 17:21:56 +03:00
end