Computer >> Hướng Dẫn Máy Tính >  >> Hệ Thống >> Mac

Triển khai Nexus 3 OSS trên Orange Pi 5 dùng Ansible: Hướng dẫn từng bước

Nexus 3 OSS là trình quản lý kho lưu trữ (artifact repository manager) mã nguồn mở xử lý nhiều định dạng như container image, Python PIP, Java jar, và nhiều hơn.

Tại sao cần artifact manager on-premise? Nhiều lý do:

  • Dùng cơ sở hạ tầng riêng: Bạn có thể có code độc quyền cần bảo vệ.
  • Tốc độ tải artifact nhanh hơn: Nếu liên tục tải cùng artifact qua Internet, có thể cache tại vị trí trung tâm, cho nhiều user trên nhiều server nhờ cache.
  • Kiểm soát artifact vào chuỗi build: Tập trung vị trí artifact, đảm bảo được phê duyệt dùng, và xác nhận không chứa mã độc.
  • Phân tách quyền truy cập artifact: Bạn có thể có yêu cầu nghiêm ngặt hơn về ai truy cập artifact nào trong tổ chức.

Bài này hướng dẫn tải, cài, cấu hình phiên bản OSS của Nexus 3 dùng Ansible playbook.

Nexus 3 chạy trên Orange PI 5 với 8 GB RAM, nhưng provisioning này làm được trên máy nào đáp ứng yêu cầu tối thiểu. Phần thiết lập bao gồm đặt proxy cho PyPI.org, cho máy liệt kê trong file inventory.

Cần gì để chạy code hướng dẫn này

  1. Kết nối Internet tải source code Ansible playbook, Nexus, và module PIP
  2. Hai hoặc nhiều máy Linux (tôi dùng Debian, Armbian, Fedora IOT), ít nhất 8 GB RAM. Cụm của tôi mix Raspberry PI 4 và OrangePI 5.
  3. Ansible controller chạy trên máy Fedora, nhưng server nào cũng có thể làm controller. Hướng dẫn cài Ansible dễ làm theo.

Tổ chức Playbook

Tôi chia task thành nhóm và playbook kết quả trông như sau:

[josevnz@dmaf5 Nexus3OnOrangePI]$ tree -N ansible/
ansible/
├── inventories
│   └── home
│       └── hosts.yaml
├── roles
│   ├── clients
│   │   ├── tasks
│   │   │   └── main.yaml
│   │   └── templates
│   │       └── pip.conf.j2
│   └── nexus
│       ├── files
│       │   └── swagger.json
│       ├── tasks
│       │   ├── download.yaml
│       │   ├── install.yaml
│       │   ├── main.yaml
│       │   ├── post_install.yaml
│       │   ├── pre_install.yaml
│       │   ├── repositories.yaml
│       │   ├── third_party.yaml
│       │   └── user.yaml
│       └── templates
│           ├── logrotate.nexus3.j2
│           ├── nexus3.service.j2
│           ├── nexus.rc.j2
│           └── nexus.vmoptions.j2
├── site.yaml
├── vars
│   ├── clients.yaml
│   └── nexus.yaml
└── vault
    ├── nexus_password.enc
    └── README.md
13 directories, 21 files

Giải thích một chút:

  • Hai role: 'nexus' và 'clients'. Role nexus cài phần mềm quản lý artifact, role client cài cài đặt pip trên mọi máy.
  • Vars chứa biến dùng cho mỗi role, tách file để dùng rõ ràng hơn.
  • Có mật khẩu, quản lý bằng tính năng Ansible vault.
  • File 'site.yaml' điều phối thực thi role:
- hosts: all
  tags: clients
  vars_files:
    - vars/clients.yaml
  roles:
    - clients
- hosts: nexus_server
  tags: nexus
  become_user: root
  become: true
  vars_files:
    - vars/nexus.yaml
  roles:
    - nexus

Bây giờ xem môi trường playbook chạy.

Inventory Host (Danh sách máy chủ)

Trường hợp tôi đơn giản — hai nhóm chính: 'clients' và máy chạy Nexus 3 server:

all:
  children:
    nexus_server:
      hosts:
        orangepi5.home:
    home_lab:
      hosts:
        dmaf5.home:
        raspberrypi.home:
        orangepi5.home:

Nhiệm vụ quan trọng tiếp: tải và cấu hình Nexus 3.

Cách cài Nexus 3

File main.yaml mô tả thứ tự và mục đích mỗi task cài cho role Nexus:

# Tasks listed here are related to the remote Nexus 3 server
# Included tasks are called in order
---
- include_tasks: third_party.yaml
- include_tasks: pre_install.yaml
- include_tasks: download.yaml
- include_tasks: install.yaml
- include_tasks: post_install.yaml
- include_tasks: user.yaml
- include_tasks: repositories.yaml

Xem trước "core tasks":

  1. third_party.yaml: Cài OpenJDK8 (Nexus 3 viết bằng Java) và logrotate lo log cũ.
  2. pre_install.yaml: Nhiều việc ở đây, như tạo thư mục cần cho nexus, user non-privileged chạy process.
  3. download.yaml: Tải phiên bản mới Nexus 3 OSS và kiểm tra checksum. Không muốn cài malware từ Internet.

Tiếp theo "customized installation group":

  1. install.yaml: Giải nén phần mềm, chuẩn bị systemd unit tự khởi động, cài JVM settings cho Nexus, triển khai cấu hình logrotate.
  2. post_install.yaml: Phần thú vị — phần mềm cài xong, chạy lần đầu. Đổi mật khẩu mặc định qua REST API, chuyển sang giai đoạn tùy chỉnh.
  3. user.yaml: Chuẩn bị cấp quyền truy cập dịch vụ Nexus cho user cuối. Dùng kết hợp REST-API và code client Ansible:
# https://help.sonatype.com/repomanager3/installation-and-upgrades/post-install-checklist
# https://help.sonatype.com/repomanager3/integrations/rest-and-integration-api
---
- name: Enable anonymous user
  tags: anonymous
  ansible.builtin.uri:
    user: ""
    password: ""
    url: "/v1/security/anonymous"
    method: PUT
    body_format: raw
    status_code: [ 200, 202, 204 ]
    headers:
      Content-Type: application/json
    body: |
      {
        "enabled" : true,
        "userId" : "anonymous",
        "realmName" : "NexusAuthorizingRealm"
      }
    force_basic_auth: true
    return_content: true
    any_errors_fatal: true

- name: Enable Docker security realm
  tags: docker_realm
  ansible.builtin.uri:
    user: ""
    password: ""
    url: "/v1/security/realms/active"
    method: PUT
    body_format: raw
    status_code: [ 200, 202, 204 ]
    headers:
      Content-Type: application/json
    body: |
      [
        "NexusAuthenticatingRealm",
        "NexusAuthorizingRealm",
        "DockerToken"
      ]
    force_basic_auth: true
    return_content: true
    any_errors_fatal: true

Logic dễ theo dõi, dùng phương thức HTTP 'PUT' là thao tác sửa đổi (nghĩa là role và user đã tồn tại). Phát hiện lỗi bằng mã HTTP trả về từ Nexus.

Bước tiếp: chuẩn bị proxy PyPi local. Nhiệm vụ nhiều bước, chi tiết dưới.

Cách thiết lập Proxy PyPI trên Nexus 3

File cuối role Nexus 3 là 'repositories.yaml'. Ở đây thực hiện các bước:

  1. Kiểm tra proxy đã setup chưa (GET hoặc read-only)
  2. Nếu chưa tồn tại, tạo mới (POST với JSON payload chi tiết tạo repository mới)

Lưu ý playbook này không cung cấp tùy chọn cập nhật cài đặt repository. Có thể làm qua REST API, nhưng để bài tập cho người đọc.

Task chuẩn bị proxy PyPi:

# Create proxy for repositories
# https://help.sonatype.com/repomanager3/integrations/rest-and-integration-api
# PyPi: https://pip.pypa.io/en/stable/user_guide/
---
- name: Check if the PyPi proxy exists
  tags: pypi_proxy_exists
  ansible.builtin.uri:
    user: ""
    password: ""
    url: "/v1/repositories/pypi/proxy/python_proxy"
    method: GET
    body_format: raw
    status_code: [ 200, 202, 204, 404 ]
    headers:
      Content-Type: application/json
    force_basic_auth: true
    return_content: true
    any_errors_fatal: true
  register: python_local

- name: Create PyPI proxy
  tags: pypi_proxy_create
  ansible.builtin.uri:
    user: ""
    password: ""
    url: "/v1/repositories/pypi/proxy"
    method: POST
    body_format: raw
    status_code: [ 201 ]
    headers:
      Content-Type: application/json
    body: |
      {
        "name": "python_proxy",
        "online": true,
        "storage": {
          "blobStoreName": "default",
          "strictContentTypeValidation": true
        },
        "proxy": {
          "remoteUrl": "https://pypi.org/",
          "contentMaxAge": -1,
          "metadataMaxAge": 1440
        },
        "negativeCache": {
          "enabled": true,
          "timeToLive": 1440
        },
        "httpClient": {
          "blocked": false,
          "autoBlock": true,
          "connection": {
            "retries": 0,
            "timeout": 60,
            "enableCircularRedirects": false,
            "enableCookies": true,
            "useTrustStore": false
          }
        }
      }
    force_basic_auth: true
    return_content: true
    any_errors_fatal: true
  when: python_local.status == 404

Gần xong. Bây giờ cần báo cho client PyPi dùng Nexus local thay vì PyPi trực tiếp lấy thư viện Python.

Cách thiết lập Client

Role clients đơn giản hơn, chỉ cần triển khai template pip.conf với đủ thông tin ép tìm kiếm trên repository mới:

# Tasks here are meant to be used on our clients user
---
- name: Create installation directory for pip.conf
  tags: pip_basedir
  ansible.builtin.file:
    state: directory
    path: ""
    owner: ""
    group: ""
    mode: "u+rwx,go-rwx"

- name: Copy pip.conf file
  tags: pip_copy
  ansible.builtin.template:
    src: pip.conf.j2
    dest: "/pip.conf"
    owner: ""
    group: ""
    mode: u=rxw,g=r,o=r

File kết quả triển khai trên '~/.config/pip/pip.conf' mọi máy:

# https://pip.pypa.io/en/stable/topics/configuration/
[global]
timeout = 60
[install]
index = https://orangepi5.home:8081/repository/python_proxy/pypi
index-url = https://orangepi5.home:8081/repository/python_proxy/simple/
trusted-host = orangepi5.home

File trên ví dụ bản cuối sau triển khai trên cụm của tôi (của bạn khác với URL resolved).

Đến lúc chạy toàn bộ playbook xem kết quả.

Cách chạy Playbook

Chạy playbook, truyền vài đối số:

  1. Vị trí host inventory
  2. Vị trí file mật khẩu mã hóa và file master chứa master password mở file bảo vệ
  3. Vị trí file playbook chính
cd ansible
ansible-playbook --inventory inventories --extra-vars @vault/nexus_password.enc --vault-password-file $HOME/vault/ansible_vault_pass site.yaml

Kết quả chạy playbook

Cách test proxy PyPI mới

Test proxy mới, cài Python Rich dùng pip và virtual environment.

josevnz@orangepi5:~$ python3 -m venv ~/virtualenv/rich
(rich) josevnz@orangepi5:~$ . ~/virtualenv/rich/bin/activate
(rich) josevnz@orangepi5:~$ pip install rich
Looking in indexes: https://orangepi5.home:8081/repository/python_proxy/simple/
Collecting rich
Downloading https://orangepi5.home:8081/repository/python_proxy/packages/rich/13.3.4/rich-13.3.4-py3-none-any.whl (238 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 238.7/238.7 KB 14.8 MB/s eta 0:00:00
Collecting pygments<3.0.0,>=2.13.0
Downloading https://orangepi5.home:8081/repository/python_proxy/packages/pygments/2.15.0/Pygments-2.15.0-py3-none-any.whl (1.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 23.8 MB/s eta 0:00:00
Collecting markdown-it-py<3.0.0,>=2.2.0
Downloading https://orangepi5.home:8081/repository/python_proxy/packages/markdown-it-py/2.2.0/markdown_it_py-2.2.0-py3-none-any.whl (84 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 84.5/84.5 KB 6.9 MB/s eta 0:00:00
Collecting mdurl~=0.1
Downloading https://orangepi5.home:8081/repository/python_proxy/packages/mdurl/0.1.2/mdurl-0.1.2-py3-none-any.whl (10.0 kB)
Installing collected packages: pygments, mdurl, markdown-it-py, rich
Successfully installed markdown-it-py-2.2.0 mdurl-0.1.2 pygments-2.15.0 rich-13.3.4

Xác nhận cache đã dùng bằng cách thấy artifact mới trên repository:

Thấy artifact PyPi

Xem demo client hoạt động, cài thứ khác:

Demo client cài đặt

Tùy chỉnh thêm dùng REST-API

Mọi cài đặt Nexus cho tải file JSON mô tả API server hỗ trợ. Ví dụ, server orangepi5.home của tôi:

curl --fail --remote-name https://orangepi5.home:8081/service/rest/swagger.json

UI cũng cho thử các endpoint REST API khác để tùy chỉnh cài đặt.

REST API testing

Kết luận

Khuyên dành thời gian đọc sách Nexus 3 để quen tính năng công cụ này.

Cộng đồng đã chuẩn bị installer Debian và RPM, nếu cần setup kiểu đó thay vì Ansible.

Nexus 3 có rất nhiều cài đặt cấu hình. Chúng ta chỉ chạm mặt ở đây. Khi viết bài tôi tìm thấy 'ThoTeam Nexus3-oss repository' với playbook rất đầy đủ và cập nhật, nhưng phức tạp hơn nhiều so với yêu cầu home lab của tôi.

Archiva là artifact manager mã nguồn mở khác, hạn chế chức năng hơn nhưng đơn giản hơn setup.

Có checklist sau cài đặt với vài task tôi không cần cho home lab. Hãy xem để chắc chắn setup hoàn chỉnh.

Học lập trình miễn phí. Chương trình mã nguồn mở của freeCodeCamp đã giúp hơn 40.000 người tìm được việc làm lập trình viên. Bắt đầu ngay