diff --git a/app.rb b/app.rb index 10fde23..aee5b95 100644 --- a/app.rb +++ b/app.rb @@ -621,8 +621,16 @@ get "/gitspec/:id/:git_id" do print_error_page(503, "Путь к проектам не существует") else git_info = repo.get_repo_short_info_by_id(params["git_id"].to_i) - @spec_file = db.get_project_repo_spec(params["id"], params["git_id"]) + spec_f = db.get_project_repo_spec(params["id"], params["git_id"]) + if spec_f.nil? + @spec_file = "" + else + @spec_file = spec_f[:spec_name] + end @page_name = "#{prj_info[:projname]} редактирование spec для git проекта #{git_info[:reponame]}" + @proj_name = prj_info[:projname] + @proj_descr = prj_info[:descr] + @git_name = git_info[:reponame] @proj_id = prj_info[:id] @git_id = git_info[:id] git_path = prj.get_project_path_git(prj_info[:id], git_info[:reponame]) @@ -638,6 +646,44 @@ get "/gitspec/:id/:git_id" do end end +post "/gitspec/:id/:git_id" do + prj = ProjectsActions.new(cfg.get_projects_path, db) + if prj.path.nil? + print_error_page(503, "Путь к проектам не существует") + else + repo = GitRepo.new(cfg.get_repo, db) + if repo.path.nil? + print_error_page(503, "Путь к репозиториям не существует") + else + prj_info = prj.get_project(params["id"]) + if prj_info.nil? + print_error_page(503, "Путь к проектам не существует") + else + git_info = repo.get_repo_short_info_by_id(params["git_id"].to_i) + unless params["cancel"].nil? + redirect "/prjedit/#{params["id"]}" + else + unless params["save"].nil? + save_spec = "" + if params["useCustom"].nil? + save_spec = params["filelst"].strip + else + save_spec = params["speccustom"].strip + end + if save_spec != "" + db.save_project_repo_spec(prj_info[:id], git_info[:id], save_spec) + end + redirect "/prjedit/#{params["id"]}" + else + db.delete_project_repo_spec(prj_info[:id], git_info[:id]) + redirect "/prjedit/#{params["id"]}" + end + end + end + end + end +end + not_found do status 404 @page_name = "Кто-то потерялся" diff --git a/classes/db.rb b/classes/db.rb index ffd959a..fd58bd8 100644 --- a/classes/db.rb +++ b/classes/db.rb @@ -21,6 +21,9 @@ end class ProjectsReposSpec < Sequel::Model(:projects_repos_spec) end +class ProjectsProjects < Sequel::Model(:projects_projects) +end + class DBase attr :error, :last_id @@ -166,4 +169,23 @@ class DBase 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 + + 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 end diff --git a/classes/gitinfo.rb b/classes/gitinfo.rb index b3173ff..e95f859 100644 --- a/classes/gitinfo.rb +++ b/classes/gitinfo.rb @@ -24,28 +24,25 @@ class GitRepo def create_git(project_name, description) @error = nil ret_val = 0 - File.open("locks/gitcreate", "r") do |f| - f.flock(File::LOCK_EX) - fname = File.expand_path("#{project_name}.git", @path) - if File.exist?(fname) - @error = "Репозиторий с таким именем уже существует: #{project_name}" - ret_val = 1 - else - Dir.mkdir(fname) - Rugged::Repository.init_at(fname, :bare) - repo = Rugged::Repository.new(fname) - created = false - if repo.bare? - @error = @db.creategit(project_name, description) - if @error.nil? - created = true - end - else - @error = "Репозиторий почему-то не пустой" - end - unless created - FileUtils.rm_rf(fname, secure: true) + fname = File.expand_path("#{project_name}.git", @path) + if File.exist?(fname) + @error = "Репозиторий с таким именем уже существует: #{project_name}" + ret_val = 1 + else + Dir.mkdir(fname) + Rugged::Repository.init_at(fname, :bare) + repo = Rugged::Repository.new(fname) + created = false + if repo.bare? + @error = @db.creategit(project_name, description) + if @error.nil? + created = true end + else + @error = "Репозиторий почему-то не пустой" + end + unless created + FileUtils.rm_rf(fname, secure: true) end end ret_val @@ -58,20 +55,17 @@ class GitRepo def getrepos repos_data = [] - File.open("locks/gitcreate", "r") do |f| - f.flock(File::LOCK_SH) - repos_files = Dir[File.join(@path, "*.git")] - repos_files.each do |fl| - repo_name = File.basename(fl, ".git") - db_info = @db.get_repo_info_by_name(repo_name) - unless db_info.nil? + repos_files = Dir[File.join(@path, "*.git")] + repos_files.each do |fl| + repo_name = File.basename(fl, ".git") + db_info = @db.get_repo_info_by_name(repo_name) + unless db_info.nil? + repos_data << { :reponame => db_info.reponame, :descr => db_info.descr, :public => db_info.public, :id => db_info.id } + else + result = create_git_db_only(repo_name) + if result.nil? + db_info = @db.get_repo_info_by_name(repo_name) repos_data << { :reponame => db_info.reponame, :descr => db_info.descr, :public => db_info.public, :id => db_info.id } - else - result = create_git_db_only(repo_name) - if result.nil? - db_info = @db.get_repo_info_by_name(repo_name) - repos_data << { :reponame => db_info.reponame, :descr => db_info.descr, :public => db_info.public, :id => db_info.id } - end end end end @@ -84,53 +78,50 @@ class GitRepo @error = nil repos_data = { :full => 0 } git_path = File.join(@path, reponame + ".git") - File.open("locks/gitcreate", "r") do |f| - f.flock(File::LOCK_SH) - if File.exist?(git_path) - repo = Rugged::Repository.new(git_path) - db_info = @db.get_repo_info_by_name(reponame) - unless db_info.nil? + if File.exist?(git_path) + repo = Rugged::Repository.new(git_path) + db_info = @db.get_repo_info_by_name(reponame) + unless db_info.nil? + db_info = db_info.first + repos_data = { :reponame => db_info.reponame, :descr => db_info.descr, :public => db_info.public, :full => 1 } + else + result = create_git_db_only(reponame) + if result.nil? + db_info = @db.get_repo_info_by_name(reponame) db_info = db_info.first repos_data = { :reponame => db_info.reponame, :descr => db_info.descr, :public => db_info.public, :full => 1 } + end + end + if repos_data[:full] == 1 + info[:info] = repos_data + if repo.empty? + info[:commits] = [] + info[:branches] = [] + info[:tags] = [] else - result = create_git_db_only(reponame) - if result.nil? - db_info = @db.get_repo_info_by_name(reponame) - db_info = db_info.first - repos_data = { :reponame => db_info.reponame, :descr => db_info.descr, :public => db_info.public, :full => 1 } + ref = repo.head + unless branch.nil? + ref_name = File.join("refs/heads/", branch) + ref = repo.references[ref_name] end - end - if repos_data[:full] == 1 - info[:info] = repos_data - if repo.empty? - info[:commits] = [] - info[:branches] = [] - info[:tags] = [] - else - ref = repo.head - unless branch.nil? - ref_name = File.join("refs/heads/", branch) - ref = repo.references[ref_name] - end - commits = [] - unless ref.nil? - walker = Rugged::Walker.new(repo) - walker.sorting(Rugged::SORT_DATE) - walker.push(ref.target) - commits = walker.map do |commit| - { :message => commit.message, :author => commit.author, :time => commit.time, :sha => commit.oid } - end.first(10) - end - info[:commits] = commits - info[:branches] = repo.branches.each_name(:local).sort - info[:tags] = repo.tags.map { |tag| tag.name } + commits = [] + unless ref.nil? + walker = Rugged::Walker.new(repo) + walker.sorting(Rugged::SORT_DATE) + walker.push(ref.target) + commits = walker.map do |commit| + { :message => commit.message, :author => commit.author, :time => commit.time, :sha => commit.oid } + end.first(10) end - else - @error = result + info[:commits] = commits + info[:branches] = repo.branches.each_name(:local).sort + info[:tags] = repo.tags.map { |tag| tag.name } end else - @error = "Репозиторий отсутсвует" + @error = result end + else + @error = "Репозиторий отсутсвует" end info[:error] = @error info @@ -138,12 +129,9 @@ class GitRepo def delete_repo(reponame) git_path = File.expand_path(reponame + ".git", @path) - File.open("locks/gitcreate", "r") do |f| - f.flock(File::LOCK_EX) - if File.exist?(git_path) - FileUtils.rm_rf(git_path, secure: true) - @db.delete_repo_by_name(reponame) - end + if File.exist?(git_path) + FileUtils.rm_rf(git_path, secure: true) + @db.delete_repo_by_name(reponame) end end @@ -155,23 +143,20 @@ class GitRepo @error = nil repo_info = @db.get_repo_info_by_id(id) unless repo_info.nil? - File.open("locks/gitcreate", "r") do |f| - f.flock(File::LOCK_SH) - git_path = File.join(@path, repo_info[:reponame] + ".git") - if File.exist?(git_path) - repo = Rugged::Repository.new(git_path) - if repo.empty? - @error = "Репозиторий пустой, нельзя добавить в проект пустой репозиторий" - else - if File.exist?(path) - FileUtils.rm_rf(path, secure: true) - end - Dir.mkdir(path) - Rugged::Repository.clone_at(repo.path, path) - end + git_path = File.join(@path, repo_info[:reponame] + ".git") + if File.exist?(git_path) + repo = Rugged::Repository.new(git_path) + if repo.empty? + @error = "Репозиторий пустой, нельзя добавить в проект пустой репозиторий" else - @error = "Файла репозитория не существует" + if File.exist?(path) + FileUtils.rm_rf(path, secure: true) + end + Dir.mkdir(path) + Rugged::Repository.clone_at(repo.path, path) end + else + @error = "Файла репозитория не существует" end else @error = "Нет информации о репозитории" @@ -182,12 +167,9 @@ class GitRepo def is_repos_sync(repo_name, prj_repo_path) res = false repo = Rugged::Repository.new(prj_repo_path) - File.open("locks/gitcreate", "r") do |f| - f.flock(File::LOCK_SH) - git_path = File.join(@path, repo_name + ".git") - bare_repo = Rugged::Repository.new(git_path) - res = (bare_repo.head.target.tree.oid == repo.head.target.tree.oid) - end + git_path = File.join(@path, repo_name + ".git") + bare_repo = Rugged::Repository.new(git_path) + res = (bare_repo.head.target.tree.oid == repo.head.target.tree.oid) res end end diff --git a/classes/projects.rb b/classes/projects.rb index 3e0437f..b733f6e 100644 --- a/classes/projects.rb +++ b/classes/projects.rb @@ -1,5 +1,6 @@ require "fileutils" require_relative "db" +require_relative "repomanage" PROJECTS_STRUCTURE = { :REPO => "repo", @@ -30,21 +31,11 @@ class ProjectsActions end def get_projects - prj = [] - File.open("locks/prjcreate", "r") do |f| - f.flock(File::LOCK_SH) - prj = @db.proj_list - end - prj + @db.proj_list end def get_project(id) - prj = nil - File.open("locks/prjcreate", "r") do |f| - f.flock(File::LOCK_SH) - prj = @db.proj(id.to_i) - end - prj + @db.proj(id.to_i) end def get_project_path(id) @@ -77,71 +68,109 @@ class ProjectsActions File.join(proj_path, PROJECTS_STRUCTURE[:SRC], gitname) end + def generate_linked_repos(id, proj_path, proj_name) + linked_prj = [] + proj_repo_path = File.join(proj_path, PROJECTS_STRUCTURE[:REPO]) + proj_repo = <<~PRJ_CFG + [#{proj_name}-repository] + name=Project repository #{proj_name} + baseurl=file://#{proj_repo_path} + enabled=1 + priority=80 + skip_if_unavailable=True + PRJ_CFG + linked_prj << proj_repo + unless id.nil? + link_prj_list = @db.get_projects_links(id) + unless link_prj_list.nil? + link_prj_list.each do |item| + internal_repo = ProjectsActions.new(@path, @db) + internal_path = internal_repo.get_project_path(item[:proj_id_repository]) + internal_repo_path = File.join(internal_path, PROJECTS_STRUCTURE[:REPO]) + internal_proj_info = internal_repo.get_project(item[:proj_id_repository]) + proj_repo = <<~PRJ_CFG + [#{internal_proj_info[:projname]}-repository] + name=Project repository #{internal_proj_info[:projname]} + baseurl=file://#{internal_repo_path} + enabled=1 + skip_if_unavailable=True + PRJ_CFG + linked_prj << proj_repo + end + end + end + File.open(prj_incl_path, "w") { |f| f << linked_prj.join("\n\n\n") } + end + + def generate_config(id, configuration_path, proj_path, proj_name) + proj_conf_path = File.join(proj_path, PROJECTS_STRUCTURE[:CONFIGS], "#{proj_name}.cfg") + conf_path = File.join(proj_path, PROJECTS_STRUCTURE[:CONFIGS]) + prj_incl_path = File.join(conf_path, "repos_include.tpl") + proj_config = <<~PRJ_CFG + include("#{configuration}") + include("#{prj_incl_path}") + config_opts['plugin_conf']['ccache_enable'] = True + config_opts['plugin_conf']['ccache_opts']['max_cache_size'] = '4G' + config_opts['plugin_conf']['ccache_opts']['hashdir'] = True + config_opts['plugin_conf']['ccache_opts']['debug'] = False + config_opts['plugin_conf']['ccache_opts']['show_stats'] = True + config_opts['plugin_conf']['package_state_enable'] = True + config_opts['plugin_conf']['procenv_enable'] = True + config_opts['plugin_conf']['root_cache_enable'] = True + config_opts['plugin_conf']['showrc_enable'] = True + config_opts['plugin_conf']['yum_cache_enable'] = True + PRJ_CFG + File.open(proj_conf_path, "w") { |f| f << proj_config } + generate_linked_repos(id, proj_path, proj_name) + end + def create_project(name, description, configuration) @error = nil ret_val = 0 project_name = sanitize_rcptname(name) - File.open("locks/prjcreate", "r") do |f| - f.flock(File::LOCK_EX) - fname = File.expand_path("#{project_name}.prj", @path) - if File.exist?(fname) - @error = "Проект с таким именем уже существует: #{project_name}" - ret_val = 1 - else - created = false - begin - Dir.mkdir(fname) - PROJECTS_STRUCTURE.each_pair do |key, value| - new_path = File.join(fname, value) - Dir.mkdir(new_path) - end - if File.exist?(configuration) - conf_path = File.join(fname, PROJECTS_STRUCTURE[:CONFIGS], "#{project_name}.cfg") - proj_config = <<~PRJ_CFG - include("#{configuration}") - config_opts['plugin_conf']['ccache_enable'] = True - config_opts['plugin_conf']['ccache_opts']['max_cache_size'] = '4G' - config_opts['plugin_conf']['ccache_opts']['hashdir'] = True - config_opts['plugin_conf']['ccache_opts']['debug'] = False - config_opts['plugin_conf']['ccache_opts']['show_stats'] = True - config_opts['plugin_conf']['package_state_enable'] = True - config_opts['plugin_conf']['procenv_enable'] = True - config_opts['plugin_conf']['root_cache_enable'] = True - config_opts['plugin_conf']['showrc_enable'] = True - config_opts['plugin_conf']['yum_cache_enable'] = True - PRJ_CFG - File.open(conf_path, "w") { |f| f << proj_config } - @error = @db.proj_create(project_name, description) - if @error.nil? - created = true - end - else - ret_val = 1 - @error = "Конфигурация #{configuration} не существует" + fname = File.expand_path("#{project_name}.prj", @path) + if File.exist?(fname) + @error = "Проект с таким именем уже существует: #{project_name}" + ret_val = 1 + else + created = false + begin + Dir.mkdir(fname) + PROJECTS_STRUCTURE.each_pair do |key, value| + new_path = File.join(fname, value) + Dir.mkdir(new_path) + end + if File.exist?(configuration) + generate_config(nil, configuration, fname, project_name) + @error = @db.proj_create(project_name, description) + if @error.nil? + created = true end - rescue => e + repo_path = File.join(fname, PROJECTS_STRUCTURE[:REPO]) + repoman = RepoManager.new(repo_path) + repoman.create_repo + else ret_val = 1 - @error = e.message - end - unless created - FileUtils.rm_rf(fname, secure: true) + @error = "Конфигурация #{configuration} не существует" end + rescue => e + ret_val = 1 + @error = e.message + end + unless created + FileUtils.rm_rf(fname, secure: true) end end ret_val end def get_project_gits(id, repo) - res = [] - File.open("locks/prjcreate", "r") do |f| - f.flock(File::LOCK_EX) - res = @db.get_gits_for_projects(id) - res_sync = res.map do |item| - prj_p = get_project_path(id) - path = File.join(prj_p, PROJECTS_STRUCTURE[:SRC], item[:reponame]) - item[:is_repo_synced] = repo.is_repos_sync(item[:reponame], path) - item - end + res = @db.get_gits_for_projects(id) + res_sync = res.map do |item| + prj_p = get_project_path(id) + path = File.join(prj_p, PROJECTS_STRUCTURE[:SRC], item[:reponame]) + item[:is_repo_synced] = repo.is_repos_sync(item[:reponame], path) + item end res end diff --git a/classes/repomanage.rb b/classes/repomanage.rb new file mode 100644 index 0000000..43c905a --- /dev/null +++ b/classes/repomanage.rb @@ -0,0 +1,14 @@ +class RepoManager + attr :path, :error + + def initialize(path) + @error = nil + unless File.exist? (path) + Dir.mkdir(path) + end + @path = path + end + + def create_repo + end +end diff --git a/views/prjprj.erb b/views/prjprj.erb new file mode 100644 index 0000000..e6dedd2 --- /dev/null +++ b/views/prjprj.erb @@ -0,0 +1,49 @@ +<%= erb :header %> + +