init
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import requests
|
||||
import json
|
||||
|
||||
artifactory_url = 'https://art.lmru.tech/artifactory/api/repositories'
|
||||
headers = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
def get_virtual_repo_details(repo_key):
|
||||
virtual_repo_url = f'{artifactory_url}/{repo_key}'
|
||||
response = requests.get(virtual_repo_url, headers=headers)
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
print(f"Error getting details for virtual repository {repo_key}: {response.status_code} {response.text}")
|
||||
return None
|
||||
|
||||
response = requests.get(artifactory_url, headers=headers)
|
||||
if response.status_code == 200:
|
||||
repositories = response.json()
|
||||
for repo in repositories:
|
||||
if repo['type'] == 'VIRTUAL':
|
||||
details = get_virtual_repo_details(repo['key'])
|
||||
if details and 'repositories' in details:
|
||||
repo['repositories'] = details['repositories']
|
||||
|
||||
with open('artifactory_repositories.json', 'w') as file:
|
||||
json.dump(repositories, file, indent=4)
|
||||
print("Repository list successfully retrieved and saved to artifactory_repositories.json")
|
||||
else:
|
||||
print("Error retrieving repository list:", response.status_code, response.text)
|
||||
@@ -0,0 +1,154 @@
|
||||
import requests
|
||||
import json
|
||||
import time
|
||||
import logging
|
||||
import os
|
||||
|
||||
log_dir = '/tmp'
|
||||
if not os.path.exists(log_dir):
|
||||
os.makedirs(log_dir)
|
||||
log_file = os.path.join(log_dir, 'nexus_task_logs.log')
|
||||
|
||||
logging.basicConfig(filename=log_file, level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
logger = logging.getLogger()
|
||||
|
||||
with open('artifactory_repositories.json', 'r') as file:
|
||||
artifactory_repositories = json.load(file)
|
||||
|
||||
headers = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
nexus_url_base = 'https://nexus-test.devops.lmru.tech'
|
||||
nexus_username = 'admin'
|
||||
nexus_password = 'nexus'
|
||||
|
||||
def create_or_update_script(script_name, script_content):
|
||||
script_url = f"{nexus_url_base}/service/rest/v1/script"
|
||||
script_payload = {
|
||||
"name": script_name,
|
||||
"type": "groovy",
|
||||
"content": script_content
|
||||
}
|
||||
|
||||
response = requests.get(f"{script_url}/{script_name}", headers=headers, auth=(nexus_username, nexus_password))
|
||||
if response.status_code == 200:
|
||||
response = requests.put(f"{script_url}/{script_name}", headers=headers, auth=(nexus_username, nexus_password), data=json.dumps(script_payload))
|
||||
else:
|
||||
response = requests.post(script_url, headers=headers, auth=(nexus_username, nexus_password), data=json.dumps(script_payload))
|
||||
|
||||
if response.status_code in [200, 201, 204]:
|
||||
logger.info(f"Script {script_name} successfully created/updated")
|
||||
return True
|
||||
else:
|
||||
logger.error(f"Error creating/updating script {script_name}: {response.status_code} {response.text}")
|
||||
return False
|
||||
|
||||
def run_script(script_name, args):
|
||||
script_run_url = f"{nexus_url_base}/service/rest/v1/script/{script_name}/run"
|
||||
response = requests.post(script_run_url, headers=headers, auth=(nexus_username, nexus_password), data=json.dumps(args))
|
||||
|
||||
if response.status_code == 200:
|
||||
logger.info(f"Script {script_name} successfully executed")
|
||||
return response.json()
|
||||
else:
|
||||
logger.error(f"Error executing script {script_name}: {response.status_code} {response.text}")
|
||||
return None
|
||||
|
||||
def check_task_status(task_id):
|
||||
task_url = f"{nexus_url_base}/service/rest/v1/tasks/{task_id}"
|
||||
while True:
|
||||
response = requests.get(task_url, headers=headers, auth=(nexus_username, nexus_password))
|
||||
if response.status_code == 200:
|
||||
task_info = response.json()
|
||||
logger.debug(f"Raw task_info: {task_info}")
|
||||
status = task_info['currentState']
|
||||
logger.info(f"Task {task_id} status: {status}")
|
||||
if status in ['WAITING', 'FAILED', 'OK']:
|
||||
last_run_result = task_info['lastRunResult']
|
||||
logger.info(f"Task {task_id} last run result: {last_run_result}")
|
||||
return status, last_run_result
|
||||
else:
|
||||
logger.error(f"Error checking status of task {task_id}: {response.status_code} {response.text}")
|
||||
time.sleep(10)
|
||||
|
||||
groovy_script_content = '''
|
||||
import org.sonatype.nexus.scheduling.TaskConfiguration
|
||||
import org.sonatype.nexus.scheduling.TaskInfo
|
||||
import org.sonatype.nexus.scheduling.TaskScheduler
|
||||
|
||||
import groovy.json.JsonOutput
|
||||
import groovy.json.JsonSlurper
|
||||
|
||||
class TaskXO {
|
||||
String typeId
|
||||
Boolean enabled
|
||||
String name
|
||||
String alertEmail
|
||||
Map<String, String> properties
|
||||
}
|
||||
|
||||
TaskXO task = new JsonSlurper().parseText(args)
|
||||
|
||||
TaskScheduler scheduler = container.lookup(TaskScheduler.class.name)
|
||||
|
||||
TaskConfiguration config = scheduler.createTaskConfigurationInstance(task.typeId)
|
||||
config.enabled = task.enabled
|
||||
config.name = task.name
|
||||
config.alertEmail = task.alertEmail
|
||||
task.properties?.each { key, value -> config.setString(key, value) }
|
||||
TaskInfo taskInfo = scheduler.scheduleTask(config, scheduler.scheduleFactory.manual())
|
||||
JsonOutput.toJson(taskInfo)
|
||||
'''
|
||||
|
||||
if create_or_update_script("create_task", groovy_script_content):
|
||||
for repo in artifactory_repositories:
|
||||
logger.info("="*50)
|
||||
repo_name = repo['key']
|
||||
repo_type = repo['type']
|
||||
|
||||
if repo_type != 'LOCAL':
|
||||
continue
|
||||
|
||||
task_name = f"{repo_name}-import"
|
||||
args = {
|
||||
"typeId": "repository.import",
|
||||
"enabled": True,
|
||||
"name": task_name,
|
||||
"alertEmail": None,
|
||||
"properties": {
|
||||
"repositoryName": repo_name,
|
||||
"sourceDir": f"/opt/art-migr/repositories/{repo_name}"
|
||||
}
|
||||
}
|
||||
|
||||
task_info = run_script("create_task", args)
|
||||
|
||||
if task_info:
|
||||
logger.debug(f"Task info: {task_info}")
|
||||
result = json.loads(task_info['result'])
|
||||
if 'id' in result:
|
||||
task_id = result['id']
|
||||
logger.info(f"Task {task_name} successfully created with ID {task_id}")
|
||||
|
||||
task_run_url = f"{nexus_url_base}/service/rest/v1/tasks/{task_id}/run"
|
||||
run_response = requests.post(task_run_url, headers=headers, auth=(nexus_username, nexus_password))
|
||||
|
||||
if run_response.status_code == 204:
|
||||
logger.info(f"Task {task_name} successfully started")
|
||||
status, last_run_result = check_task_status(task_id)
|
||||
if status == 'FAILED':
|
||||
logger.error(f"Task {task_name} failed with result: {last_run_result}")
|
||||
break
|
||||
else:
|
||||
logger.error(f"Error starting task {task_name}: {run_response.status_code} {run_response.text}")
|
||||
break
|
||||
|
||||
time.sleep(5)
|
||||
else:
|
||||
logger.error(f"Task ID not found in the response for task {task_name}")
|
||||
break
|
||||
else:
|
||||
logger.error(f"Failed to create task {task_name}")
|
||||
break
|
||||
@@ -0,0 +1,189 @@
|
||||
import requests
|
||||
import json
|
||||
|
||||
nexus_url_base = 'https://nexus-test.devops.lmru.tech/service/rest/v1/repositories'
|
||||
nexus_username = 'admin'
|
||||
nexus_password = 'nexus'
|
||||
|
||||
with open('artifactory_repositories.json', 'r') as file:
|
||||
artifactory_repositories = json.load(file)
|
||||
|
||||
headers = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
repo_type_mapping = {
|
||||
'LOCAL': 'hosted',
|
||||
'REMOTE': 'proxy',
|
||||
'VIRTUAL': 'group'
|
||||
}
|
||||
|
||||
format_url_mapping = {
|
||||
'maven': 'maven',
|
||||
'npm': 'npm',
|
||||
'nuget': 'nuget',
|
||||
'docker': 'docker',
|
||||
'pypi': 'pypi',
|
||||
'rubygems': 'rubygems',
|
||||
'yum': 'yum',
|
||||
'debian': 'apt',
|
||||
'generic': 'raw',
|
||||
'go': 'go',
|
||||
'gradle': 'gradle',
|
||||
'helm': 'helm',
|
||||
'cocoapods': 'cocoapods'
|
||||
}
|
||||
|
||||
default_attributes = {
|
||||
'maven': {
|
||||
'versionPolicy': 'RELEASE',
|
||||
'layoutPolicy': 'STRICT'
|
||||
},
|
||||
'nugetProxy': {
|
||||
'queryCacheItemMaxAge': 1440,
|
||||
'nugetVersion': 'V3'
|
||||
},
|
||||
'dockerProxy': {
|
||||
'indexType': 'REGISTRY'
|
||||
},
|
||||
'apt': {
|
||||
'flat': False,
|
||||
'distribution': 'default'
|
||||
},
|
||||
'proxyDefaults': {
|
||||
'contentMaxAge': 1440,
|
||||
'metadataMaxAge': 1440
|
||||
}
|
||||
}
|
||||
|
||||
for repo in artifactory_repositories:
|
||||
repo_name = repo['key']
|
||||
repo_type = repo_type_mapping.get(repo['type'])
|
||||
package_type = repo.get('packageType').lower()
|
||||
|
||||
format_url = format_url_mapping.get(package_type)
|
||||
|
||||
if not format_url:
|
||||
print(f"Unknown package type {package_type} for {repo_name}")
|
||||
continue
|
||||
|
||||
nexus_url = f"{nexus_url_base}/{format_url}/{repo_type}"
|
||||
|
||||
if repo_type == 'proxy':
|
||||
config = {
|
||||
"name": repo_name,
|
||||
"online": True,
|
||||
"storage": {
|
||||
"blobStoreName": "nexus-test",
|
||||
"strictContentTypeValidation": True
|
||||
},
|
||||
"proxy": {
|
||||
"remoteUrl": repo['url'],
|
||||
"contentMaxAge": default_attributes['proxyDefaults']['contentMaxAge'],
|
||||
"metadataMaxAge": default_attributes['proxyDefaults']['metadataMaxAge']
|
||||
},
|
||||
"negativeCache": {
|
||||
"enabled": True,
|
||||
"timeToLive": 1440
|
||||
},
|
||||
"httpClient": {
|
||||
"blocked": False,
|
||||
"autoBlock": True
|
||||
},
|
||||
"routingRuleName": None,
|
||||
"format": format_url
|
||||
}
|
||||
|
||||
if format_url == 'docker':
|
||||
config['dockerProxy'] = default_attributes['dockerProxy']
|
||||
config['docker'] = {
|
||||
"v1Enabled": False,
|
||||
"forceBasicAuth": True
|
||||
}
|
||||
|
||||
if format_url == 'apt':
|
||||
config['apt'] = default_attributes['apt']
|
||||
config['apt']['distribution'] = repo.get('distribution', 'default')
|
||||
|
||||
if format_url == 'yum':
|
||||
config['yum'] = {}
|
||||
|
||||
if format_url == 'maven':
|
||||
config["maven"] = default_attributes['maven']
|
||||
|
||||
if format_url == 'nuget':
|
||||
config["nugetProxy"] = default_attributes['nugetProxy']
|
||||
|
||||
elif repo_type == 'hosted':
|
||||
config = {
|
||||
"name": repo_name,
|
||||
"online": True,
|
||||
"storage": {
|
||||
"blobStoreName": "nexus-test",
|
||||
"strictContentTypeValidation": True,
|
||||
"writePolicy": 'ALLOW'
|
||||
},
|
||||
"cleanup": None,
|
||||
"component": {
|
||||
"proprietaryComponents": False
|
||||
},
|
||||
"format": format_url
|
||||
}
|
||||
|
||||
if format_url == 'docker':
|
||||
config['docker'] = {
|
||||
"v1Enabled": False,
|
||||
"forceBasicAuth": True
|
||||
}
|
||||
if format_url == 'maven':
|
||||
config["maven"] = default_attributes['maven']
|
||||
if format_url == 'yum':
|
||||
config['yum'] = {}
|
||||
|
||||
elif repo_type == 'group':
|
||||
config = {
|
||||
"name": repo_name,
|
||||
"online": True,
|
||||
"storage": {
|
||||
"blobStoreName": "nexus-test",
|
||||
"strictContentTypeValidation": True
|
||||
},
|
||||
"group": {
|
||||
"memberNames": repo['repositories']
|
||||
},
|
||||
"format": format_url
|
||||
}
|
||||
|
||||
if format_url == 'docker':
|
||||
config['docker'] = {
|
||||
"v1Enabled": False,
|
||||
"forceBasicAuth": True
|
||||
}
|
||||
if format_url == 'maven':
|
||||
config["maven"] = default_attributes['maven']
|
||||
|
||||
else:
|
||||
print(f"Unknown repository type {repo['type']} for {repo_name}")
|
||||
continue
|
||||
|
||||
|
||||
try:
|
||||
response = requests.post(nexus_url, headers=headers, auth=(nexus_username, nexus_password), data=json.dumps(config))
|
||||
|
||||
if response.status_code == 201:
|
||||
print(f"Repository {repo_name} successfully created")
|
||||
else:
|
||||
try:
|
||||
response_json = response.json()
|
||||
if isinstance(response_json, dict):
|
||||
if "duplicated key" in response_json.get("message", ""):
|
||||
print(f"!!! Repository {repo_name} already exists")
|
||||
else:
|
||||
print(f"Error creating repository {repo_name}: {response.status_code} {response.text}")
|
||||
else:
|
||||
print(f"Error creating repository {repo_name}: {response.status_code} {response.text}")
|
||||
except json.JSONDecodeError:
|
||||
print(f"Error creating repository {repo_name}: {response.status_code} {response.text}")
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Error during request execution: {e}")
|
||||
@@ -0,0 +1,60 @@
|
||||
import requests
|
||||
import json
|
||||
import dns.resolver
|
||||
from urllib.parse import urlparse
|
||||
|
||||
artifactory_url = 'https://art.lmru.tech/artifactory/api/repositories'
|
||||
headers = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
def get_remote_repositories():
|
||||
response = requests.get(artifactory_url, headers=headers)
|
||||
if response.status_code == 200:
|
||||
repositories = response.json()
|
||||
excluded_package_types = ['yum', 'deb', 'rpm', 'apt', 'alpine']
|
||||
remote_repositories = [
|
||||
repo for repo in repositories
|
||||
if repo['type'] == 'REMOTE' and repo.get('packageType', '').lower() not in excluded_package_types
|
||||
]
|
||||
return remote_repositories
|
||||
else:
|
||||
print("Error retrieving repository list:", response.status_code, response.text)
|
||||
return []
|
||||
|
||||
remote_repositories = get_remote_repositories()
|
||||
|
||||
def extract_hostname(url):
|
||||
parsed_url = urlparse(url)
|
||||
return parsed_url.hostname
|
||||
|
||||
def get_ip_addresses(hostname, iterations=20):
|
||||
ip_addresses = set()
|
||||
for _ in range(iterations):
|
||||
try:
|
||||
answers = dns.resolver.resolve(hostname, 'A')
|
||||
for rdata in answers:
|
||||
ip_addresses.add(rdata.to_text())
|
||||
except dns.resolver.NoAnswer:
|
||||
print(f"No A records found for {hostname}")
|
||||
except dns.resolver.NXDOMAIN:
|
||||
print(f"Domain {hostname} does not exist")
|
||||
except dns.exception.Timeout:
|
||||
print(f"Timeout while resolving {hostname}")
|
||||
except Exception as e:
|
||||
print(f"An error occurred while resolving {hostname}: {e}")
|
||||
return ip_addresses
|
||||
|
||||
# Получаем уникальные IP-адреса для каждого хоста
|
||||
hostnames = {extract_hostname(repo['url']) for repo in remote_repositories}
|
||||
hostnames.add('docker.io')
|
||||
unique_ip_addresses = set()
|
||||
for hostname in hostnames:
|
||||
unique_ip_addresses.update(get_ip_addresses(hostname))
|
||||
|
||||
# Сохранение уникальных IP-адресов в файл
|
||||
with open('blocked_ips.txt', 'w') as file:
|
||||
for ip in sorted(unique_ip_addresses):
|
||||
file.write(f"{ip}\n")
|
||||
|
||||
print("Blocked IP addresses list successfully created.")
|
||||
Reference in New Issue
Block a user