initial commit

This commit is contained in:
2026-04-12 23:24:13 +08:00
commit 7592af1c3a
55 changed files with 5051 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
---
# roles/common/tasks/main.yml
# 基础系统配置: 安装依赖包、创建 git 用户、配置防火墙、创建目录结构
- name: 安装基础依赖包
dnf:
name:
- git
- wget
- curl
state: present
- name: 创建 git 用户组
group:
name: git
system: yes
state: present
- name: 创建 git 系统用户
user:
name: git
group: git
system: yes
shell: /bin/bash
home: /home/git
create_home: yes
comment: "Git Version Control"
- name: 创建 Gitea 数据目录
file:
path: "{{ item.path }}"
state: directory
owner: "{{ item.owner }}"
group: "{{ item.group }}"
mode: "{{ item.mode }}"
loop:
- { path: /var/lib/gitea, owner: git, group: git, mode: "0750" }
- { path: /var/lib/gitea/custom, owner: git, group: git, mode: "0750" }
- { path: /var/lib/gitea/data, owner: git, group: git, mode: "0750" }
- { path: /var/lib/gitea/log, owner: git, group: git, mode: "0750" }
- name: 创建 Gitea 配置目录
file:
path: /etc/gitea
state: directory
owner: root
group: git
mode: "0770"
- name: 确保 firewalld 正在运行
systemd:
name: firewalld
state: started
enabled: yes
- name: 开放 Gitea HTTP 端口
firewalld:
port: "{{ gitea_http_port }}/tcp"
permanent: yes
immediate: yes
state: enabled
+9
View File
@@ -0,0 +1,9 @@
---
- name: 重载 systemd
systemd:
daemon_reload: yes
- name: 重启 Gitea
systemd:
name: gitea
state: restarted
+100
View File
@@ -0,0 +1,100 @@
---
# roles/gitea/tasks/configure.yml
# 配置 Gitea: 生成密钥、app.ini、systemd 服务、创建管理员
- name: 生成 SECRET_KEY
command: /usr/local/bin/gitea generate secret SECRET_KEY
register: gitea_secret_key_result
changed_when: false
when: gitea_secret_key is not defined or gitea_secret_key == ""
- name: 保存 SECRET_KEY
set_fact:
gitea_secret_key: "{{ gitea_secret_key_result.stdout }}"
when: gitea_secret_key is not defined or gitea_secret_key == ""
- name: 生成 INTERNAL_TOKEN
command: /usr/local/bin/gitea generate secret INTERNAL_TOKEN
register: gitea_internal_token_result
changed_when: false
when: gitea_internal_token is not defined or gitea_internal_token == ""
- name: 保存 INTERNAL_TOKEN
set_fact:
gitea_internal_token: "{{ gitea_internal_token_result.stdout }}"
when: gitea_internal_token is not defined or gitea_internal_token == ""
- name: 生成 LFS_JWT_SECRET
command: /usr/local/bin/gitea generate secret LFS_JWT_SECRET
register: gitea_lfs_jwt_result
changed_when: false
when: gitea_lfs_jwt_secret is not defined or gitea_lfs_jwt_secret == ""
- name: 保存 LFS_JWT_SECRET
set_fact:
gitea_lfs_jwt_secret: "{{ gitea_lfs_jwt_result.stdout }}"
when: gitea_lfs_jwt_secret is not defined or gitea_lfs_jwt_secret == ""
- name: 部署 Gitea 配置文件
template:
src: app.ini.j2
dest: /etc/gitea/app.ini
owner: root
group: git
mode: "0640"
notify: 重启 Gitea
- name: 部署 systemd 服务文件
template:
src: gitea.service.j2
dest: /etc/systemd/system/gitea.service
owner: root
group: root
mode: "0644"
notify:
- 重载 systemd
- 重启 Gitea
- name: 启用并启动 Gitea 服务
systemd:
name: gitea
state: started
enabled: yes
- name: 等待 Gitea 就绪
uri:
url: "http://127.0.0.1:{{ gitea_http_port }}/api/v1/version"
method: GET
status_code: 200
register: gitea_health
until: gitea_health.status == 200
retries: 30
delay: 2
- name: 显示 Gitea 版本
debug:
msg: "Gitea {{ gitea_health.json.version }} 已启动"
- name: 创建管理员账号
command: >
/usr/local/bin/gitea admin user create
--username {{ gitea_admin_user }}
--password {{ gitea_admin_password }}
--email {{ gitea_admin_email }}
--admin
--config /etc/gitea/app.ini
become: true
become_user: git
environment:
GITEA_WORK_DIR: /var/lib/gitea
register: admin_create
changed_when: "'has been successfully created' in admin_create.stdout"
failed_when: >
admin_create.rc != 0 and
'already exists' not in admin_create.stderr
no_log: true
- name: 收紧配置目录权限
file:
path: /etc/gitea
mode: "0750"
+50
View File
@@ -0,0 +1,50 @@
---
# roles/gitea/tasks/install.yml
# 下载并安装 Gitea 二进制文件
- name: 获取 Gitea 最新版本信息
uri:
url: https://dl.gitea.com/gitea/version.json
return_content: yes
register: gitea_version_json
until: gitea_version_json.status == 200
retries: 3
delay: 2
when: gitea_version == ""
- name: 解析最新版本号
set_fact:
gitea_resolved_version: "{{ (gitea_version_json.content | from_json).latest.version }}"
when: gitea_version == ""
- name: 使用指定版本号
set_fact:
gitea_resolved_version: "{{ gitea_version }}"
when: gitea_version != ""
- name: 显示将要安装的 Gitea 版本
debug:
msg: "将安装 Gitea {{ gitea_resolved_version }}"
- name: 检查 Gitea 是否已安装
stat:
path: /usr/local/bin/gitea
register: gitea_binary
- name: 获取已安装的 Gitea 版本
command: /usr/local/bin/gitea --version
register: gitea_installed_version
changed_when: false
failed_when: false
when: gitea_binary.stat.exists
- name: 下载 Gitea 二进制文件
get_url:
url: "https://dl.gitea.com/gitea/{{ gitea_resolved_version }}/gitea-{{ gitea_resolved_version }}-linux-amd64"
dest: /usr/local/bin/gitea
mode: "0755"
force: yes
when: >
not gitea_binary.stat.exists or
gitea_resolved_version not in (gitea_installed_version.stdout | default(''))
notify: 重启 Gitea
+8
View File
@@ -0,0 +1,8 @@
---
# roles/gitea/tasks/main.yml
- name: 安装 Gitea
import_tasks: install.yml
- name: 配置 Gitea
import_tasks: configure.yml
+75
View File
@@ -0,0 +1,75 @@
; Gitea Configuration
; Managed by Ansible - DO NOT EDIT MANUALLY
APP_NAME = {{ gitea_app_name | default('同济大学高级语言程序设计答疑平台') }}
RUN_USER = git
RUN_MODE = prod
WORK_PATH = /var/lib/gitea
[database]
DB_TYPE = postgres
HOST = 127.0.0.1:5432
NAME = {{ gitea_db_name }}
USER = {{ gitea_db_user }}
PASSWD = {{ gitea_db_password }}
SSL_MODE = disable
LOG_SQL = false
[server]
PROTOCOL = http
DOMAIN = {{ gitea_domain }}
HTTP_ADDR = {{ gitea_http_addr | default('0.0.0.0') }}
HTTP_PORT = {{ gitea_http_port }}
ROOT_URL = http://{{ gitea_domain }}:{{ gitea_http_port }}/
APP_DATA_PATH = /var/lib/gitea/data
LFS_START_SERVER = true
LFS_JWT_SECRET = {{ gitea_lfs_jwt_secret }}
[repository]
ROOT = /var/lib/gitea/data/gitea-repositories
DEFAULT_BRANCH = main
DEFAULT_REPO_UNITS = repo.code,repo.issues
DISABLE_STARS = true
[security]
INSTALL_LOCK = true
SECRET_KEY = {{ gitea_secret_key }}
INTERNAL_TOKEN = {{ gitea_internal_token }}
MIN_PASSWORD_LENGTH = 8
[service]
DISABLE_REGISTRATION = true
REQUIRE_SIGNIN_VIEW = false
ENABLE_NOTIFY_MAIL = false
DEFAULT_KEEP_EMAIL_PRIVATE = true
NO_REPLY_ADDRESS = noreply.{{ gitea_domain }}
[mailer]
ENABLED = false
[session]
PROVIDER = file
PROVIDER_CONFIG = /var/lib/gitea/data/sessions
[log]
ROOT_PATH = /var/lib/gitea/log
MODE = file
LEVEL = info
[webhook]
ALLOWED_HOST_LIST = {{ gitea_webhook_allowed_hosts | default('*') }}
SKIP_TLS_VERIFY = {{ gitea_webhook_skip_tls | default('false') }}
[api]
ENABLE_SWAGGER = true
MAX_RESPONSE_ITEMS = 100
[indexer]
ISSUE_INDEXER_PATH = /var/lib/gitea/data/indexers/issues.bleve
[picture]
DISABLE_GRAVATAR = true
[openid]
ENABLE_OPENID_SIGNIN = false
ENABLE_OPENID_SIGNUP = false
@@ -0,0 +1,28 @@
[Unit]
Description=Gitea (Git with a cup of tea)
After=syslog.target
After=network.target
After=postgresql-{{ postgresql_version }}.service
[Service]
Type=simple
User=git
Group=git
WorkingDirectory=/var/lib/gitea
ExecStart=/usr/local/bin/gitea web --config /etc/gitea/app.ini
Restart=always
RestartSec=5s
Environment=USER=git HOME=/home/git GITEA_WORK_DIR=/var/lib/gitea
# Resource limits
LimitMEMLOCK=infinity
LimitNOFILE=65535
# Sandboxing
ProtectSystem=full
PrivateDevices=yes
PrivateTmp=yes
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target
@@ -0,0 +1,5 @@
---
- name: 重启 PostgreSQL
systemd:
name: "postgresql-{{ postgresql_version }}"
state: restarted
+73
View File
@@ -0,0 +1,73 @@
---
# roles/postgresql/tasks/main.yml
# PostgreSQL 安装与配置
- name: 禁用系统自带的 postgresql 模块
command: dnf module disable postgresql -y
register: pg_module_disable
changed_when: "'Nothing to do' not in pg_module_disable.stdout"
failed_when: false
- name: 安装 PostgreSQL 官方仓库 RPM
dnf:
name: "https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm"
state: present
disable_gpg_check: yes
- name: 安装 PostgreSQL {{ postgresql_version }}
dnf:
name:
- postgresql{{ postgresql_version }}-server
- postgresql{{ postgresql_version }}
- python3-psycopg2
state: present
- name: 检查 PostgreSQL 数据目录是否已初始化
stat:
path: "/var/lib/pgsql/{{ postgresql_version }}/data/PG_VERSION"
register: pg_data_dir
- name: 初始化 PostgreSQL 数据库集群
command: "/usr/pgsql-{{ postgresql_version }}/bin/postgresql-{{ postgresql_version }}-setup initdb"
when: not pg_data_dir.stat.exists
- name: 部署 pg_hba.conf
template:
src: pg_hba.conf.j2
dest: "/var/lib/pgsql/{{ postgresql_version }}/data/pg_hba.conf"
owner: postgres
group: postgres
mode: "0600"
notify: 重启 PostgreSQL
- name: 启用并启动 PostgreSQL 服务
systemd:
name: "postgresql-{{ postgresql_version }}"
state: started
enabled: yes
- name: 等待 PostgreSQL 就绪
wait_for:
port: 5432
delay: 2
timeout: 30
- name: 创建 Gitea 数据库用户
become: true
become_user: postgres
community.postgresql.postgresql_user:
name: "{{ gitea_db_user }}"
password: "{{ gitea_db_password }}"
state: present
no_log: true
- name: 创建 Gitea 数据库
become: true
become_user: postgres
community.postgresql.postgresql_db:
name: "{{ gitea_db_name }}"
owner: "{{ gitea_db_user }}"
encoding: UTF-8
lc_collate: en_US.UTF-8
lc_ctype: en_US.UTF-8
state: present
@@ -0,0 +1,15 @@
# PostgreSQL Client Authentication Configuration File
# ===================================================
# Managed by Ansible - DO NOT EDIT MANUALLY
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all postgres peer
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5