遷移背景:
從nexus 3.33 升級到 nexus 3.64 過程中,私服 npm-hosted 無法上傳。由于這個 npm-hosted 和 npm-proxy 放的同一個 blob存儲,無法單獨拆除去,所以采用遷移的方式
遷移思路:
down下來 npm-hosted 倉庫,然后 批量上傳
技術(shù)棧:
python shell 正則文章來源:http://www.zghlxwxcb.cn/news/detail-811327.html
down倉庫的python文件:
import os
import re
import requests
from urllib.parse import unquote
def decode_urls(url_list):
decoded_urls = [unquote(url) for url in url_list]
return decoded_urls
def download_url(url, save_dir):
response = requests.get(url)
# 檢查響應(yīng)狀態(tài)碼
if response.status_code == 200:
# 獲取URL的基本路徑
base_url = '/'.join(url.split('/')[:-1])
# 解析HTML內(nèi)容
html_content = response.text
# 搜索所有鏈接
links = find_links(html_content)
# 遍歷鏈接
for link in links:
file_url = base_url +"/"+ link
# 檢查鏈接是否為目錄
if link.endswith('/'):
# 創(chuàng)建本地目錄
save_subdir = os.path.join(save_dir, link)
os.makedirs(save_subdir, exist_ok=True)
# 遞歸下載子目錄
download_url(file_url, save_subdir)
else:
# 下載文件
save_file = link.split("/")[-1]
download_file(link, save_dir+save_file)
else:
print(f"Failed to download URL: {url}")
def find_links(html_content):
# 使用正則表達式或HTML解析庫解析HTML內(nèi)容,提取所有鏈接
# 例如,可以使用正則表達式 r'<a\s+href=[\'"](.*?)[\'"]\s*>' 來提取鏈接
# 返回一個包含所有鏈接的列表
# 使用正則表達式匹配鏈接
pattern = r'<a\s+href=[\'"](.*?)[\'"]\s*>'
matches = re.findall(pattern, html_content)
matches = decode_urls(matches)
if '../' in matches:
matches.remove('../')
print(matches)
# 返回匹配到的鏈接列表
return matches
def download_file(url, save_path):
response = requests.get(url, stream=True)
# 檢查響應(yīng)狀態(tài)碼
if response.status_code == 200:
with open(save_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
else:
print(f"Failed to download file: {url}")
# 指定下載URL和保存目錄
url = "https://mirrors.xinyunkeji.com/service/rest/repository/browse/npm-test-hosted/"
save_dir = '/opt/npm/download'
# 創(chuàng)建保存目錄(如果不存在)
os.makedirs(save_dir, exist_ok=True)
# 開始下載
download_url(url, save_dir)
批量上傳新倉庫shell文件:
這個curl語句是從api接口里面,模擬上傳一個文件,然后再下方獲取的curl命令文章來源地址http://www.zghlxwxcb.cn/news/detail-811327.html
#!/bin/bash
#需要上傳到的倉庫url
url='https://mirrors.xinyunkeji.com/service/rest/v1/components?repository=npm-test-hosted2'
#使用python下載的倉庫目錄
directory='/opt/npm/download'
#nexus有上傳權(quán)限的賬戶密碼
username='test'
password='mimaya'
for file in $(find $directory -name "*.tgz"); do
echo "準備上傳${file}文件"
curl -X POST $url \
-H 'accept: application/json' \
-H 'NX-ANTI-CSRF-TOKEN: 0.05104117117544127' \
-H 'X-Nexus-UI: true' \
-F "npm.asset=@$file;type=application/x-compressed" \
-u "$username:$password"
done
到了這里,關(guān)于nexus3 npm-hosted倉庫遷移的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!