This will be called by git mirror service.
Signed-off-by: Zhang Yuhang <zhangyuhang25(a)huawei.com>
---
container/web-backend/web-backend | 8 +++
lib/constants.rb | 3 +
src/lib/web_backend.rb | 107 ++++++++++++++++--------------
3 files changed, 68 insertions(+), 50 deletions(-)
diff --git a/container/web-backend/web-backend b/container/web-backend/web-backend
index 60c7827..07c2f69 100755
--- a/container/web-backend/web-backend
+++ b/container/web-backend/web-backend
@@ -62,3 +62,11 @@ end
get '/get_repos' do
get_repos(params)
end
+
+# POST /update_repos
+# Body: repos hash json
+# Content-Type: "Application/json"
+# Description: update repos by git mirror service
+post '/update_repos' do
+ update_repos(request)
+end
diff --git a/lib/constants.rb b/lib/constants.rb
index 3a4f083..efecaef 100644
--- a/lib/constants.rb
+++ b/lib/constants.rb
@@ -10,3 +10,6 @@ ES_PORT = config['ES_PORT'] || 9200
MAIL_HOST = config['MAIL_HOST'] || '172.17.0.1'
MAIL_PORT = config['MAIL_PORT'] || 11_311
+
+# web-backend service port that makes git mirror service updates "repos" of web-backend service
+WEB_BACKEND_PORT = 32767
diff --git a/src/lib/web_backend.rb b/src/lib/web_backend.rb
index 4544690..6ba18e2 100644
--- a/src/lib/web_backend.rb
+++ b/src/lib/web_backend.rb
@@ -12,7 +12,6 @@ require "#{CCI_SRC}/lib/es_query.rb"
require "#{CCI_SRC}/lib/matrix2.rb"
UPSTREAM_REPOS_PATH = ENV['UPSTREAM_REPOS_PATH'] || '/c/upstream-repos'
-
FIELDS = %w[
upstream_repo
os
@@ -33,6 +32,48 @@ PREFIX_SEARCH_FIELDS = ['tbox_group'].freeze
ES_CLIENT = Elasticsearch::Client.new(url: "http://#{ES_HOST}:#{ES_PORT}")
COMPARE_RECORDS_NUMBER = 50
+# Repos operation class
+class Repos
+ attr_reader :repos, :repos_list
+
+ def initialize
+ @repos = {}
+ @repos_list = []
+
+ Dir["#{UPSTREAM_REPOS_PATH}/*/*/*"].sort.each do |repo_file|
+ git_repo = repo_file[UPSTREAM_REPOS_PATH.size + 1, repo_file.size - 1]
+ urls = YAML.load_file(repo_file)['url'] if File.file? repo_file
+ repo = {
+ git_repo: git_repo,
+ git_url: get_repo_url(git_repo, urls: urls)
+ }
+ @repos_list << repo
+ end
+ end
+
+ def get_repo_url(git_repo, urls: nil)
+ urls ||= @repos[git_repo]['url']
+
+ return if urls.nil? || urls.empty?
+ return urls[1] if urls[1] && urls[1][0, 4] == 'http'
+
+ return urls[0]
+ end
+
+ def repos=(repos)
+ repos_list = []
+ @repos = repos
+ @repos.keys.sort.each do |git_repo|
+ repos_list << {
+ git_repo: git_repo,
+ git_url: get_repo_url(git_repo)
+ }
+ end
+ @repos_list = repos_list
+ end
+end
+
+REPOS = Repos.new
def es_query(query)
ES_CLIENT.search index: 'jobs*', body: query
end
@@ -95,7 +136,7 @@ def compare_candidates_body
OS: [
{ os: 'openeuler', os_version: ['1.0', '20.03'] },
{ os: 'centos', os_version: ['7.6', '7.8', '8.1', 'sid'] },
- { os: 'debian', os_version: ['10', 'sid'] },
+ { os: 'debian', os_version: %w[10 sid] },
{ os: 'archlinux', os_version: ['5.5.0-1'] }
],
os_arch: %w[aarch64 x86_64],
@@ -270,7 +311,7 @@ end
def get_banner(git_repo, branches)
{
repo: git_repo,
- git_url: get_repo(git_repo)[:git_url],
+ git_url: REPOS.get_repo_url(git_repo),
upstream_branch: branches
}
end
@@ -317,62 +358,17 @@ def get_jobs(params)
[200, headers.merge('Access-Control-Allow-Origin' => '*'), JSON.dump(body)]
end
-def get_repo_url(repo_file)
- return unless File.file? repo_file
-
- urls = YAML.load_file(repo_file)['url']
- urls.each do |url|
- return url if url[0, 4] == 'http'
- end
- urls[0]
-end
-
-def get_repo(git_repo, repo_file = nil)
- repo = {
- git_repo: git_repo,
- git_url: nil
- }
- return repo if !git_repo && !repo_file
-
- if !git_repo
- git_repo = repo_file[UPSTREAM_REPOS_PATH.size + 1, repo_file.size - 1]
- repo[:git_repo] = git_repo
- elsif !repo_file
- repo_file = File.join(UPSTREAM_REPOS_PATH, git_repo)
- end
-
- repo[:git_url] = get_repo_url(repo_file)
- repo
-end
-
-def repo_files_list
- Dir["#{UPSTREAM_REPOS_PATH}/*/*/*"].sort
-end
-
-def get_repos_list(repo_files, from, finish, total)
- repos_list = []
-
- total.times do |index|
- next if index < from
- break if index >= finish
-
- repos_list << get_repo(nil, repo_files[index])
- end
- repos_list
-end
-
def get_repos_body(params)
- repo_files = repo_files_list
page_size = get_positive_number(params[:page_size], 20)
page_num = get_positive_number(params[:page_num], 1) - 1
from = page_num * page_size
finish = from + page_size
- total = repo_files.size
+ total = REPOS.repos_list.size
{
total: total,
- repos: get_repos_list(repo_files, from, finish, total)
+ repos: REPOS.repos_list[from, finish]
}
end
@@ -385,3 +381,14 @@ def get_repos(params)
end
[200, headers.merge('Access-Control-Allow-Origin' => '*'), JSON.dump(body)]
end
+
+def update_repos(request)
+ begin
+ request.body.rewind # in case someone already read it
+ REPOS.repos = JSON.parse(request.body.read)
+ rescue StandardError => e
+ warn e.message
+ return [500, headers, 'error']
+ end
+ [200, headers, 'success']
+end
--
2.23.0