From 29dd6a38b71370141292515632b6f058d90fad9c Mon Sep 17 00:00:00 2001 From: Haitao Pan Date: Wed, 20 May 2026 16:27:54 +0800 Subject: [PATCH] feat: deploy modern IT history ebook --- deploy_modern_it_history.yml | 8 ++ .../modern_it_history/defaults/main.yml | 10 ++ .../modern_it_history/handlers/main.yml | 9 ++ roles/vhosts/modern_it_history/tasks/main.yml | 110 ++++++++++++++++++ .../templates/ebook.svc.plus.caddy.j2 | 23 ++++ .../modern-it-history-update.service.j2 | 8 ++ .../templates/modern-it-history-update.sh.j2 | 23 ++++ .../modern-it-history-update.timer.j2 | 10 ++ 8 files changed, 201 insertions(+) create mode 100644 deploy_modern_it_history.yml create mode 100644 roles/vhosts/modern_it_history/defaults/main.yml create mode 100644 roles/vhosts/modern_it_history/handlers/main.yml create mode 100644 roles/vhosts/modern_it_history/tasks/main.yml create mode 100644 roles/vhosts/modern_it_history/templates/ebook.svc.plus.caddy.j2 create mode 100644 roles/vhosts/modern_it_history/templates/modern-it-history-update.service.j2 create mode 100644 roles/vhosts/modern_it_history/templates/modern-it-history-update.sh.j2 create mode 100644 roles/vhosts/modern_it_history/templates/modern-it-history-update.timer.j2 diff --git a/deploy_modern_it_history.yml b/deploy_modern_it_history.yml new file mode 100644 index 0000000..1b578fc --- /dev/null +++ b/deploy_modern_it_history.yml @@ -0,0 +1,8 @@ +--- +- name: Deploy Modern IT History Docusaurus ebook + hosts: "{{ modern_it_history_target_host | default('jp_xhttp_contabo_host') }}" + gather_facts: true + become: true + roles: + - roles/vhosts/nodejs + - roles/vhosts/modern_it_history diff --git a/roles/vhosts/modern_it_history/defaults/main.yml b/roles/vhosts/modern_it_history/defaults/main.yml new file mode 100644 index 0000000..42d5ced --- /dev/null +++ b/roles/vhosts/modern_it_history/defaults/main.yml @@ -0,0 +1,10 @@ +--- +modern_it_history_domain: ebook.svc.plus +modern_it_history_repo: https://github.com/haitaopanhq/modern-it-infrastructure-evolution.git +modern_it_history_branch: main +modern_it_history_root: /opt/modern-it-history +modern_it_history_repo_dir: "{{ modern_it_history_root }}/repo" +modern_it_history_current_dir: "{{ modern_it_history_root }}/current" +modern_it_history_build_dir: "{{ modern_it_history_repo_dir }}/build" +modern_it_history_update_interval: 5min +modern_it_history_node_env: production diff --git a/roles/vhosts/modern_it_history/handlers/main.yml b/roles/vhosts/modern_it_history/handlers/main.yml new file mode 100644 index 0000000..709c517 --- /dev/null +++ b/roles/vhosts/modern_it_history/handlers/main.yml @@ -0,0 +1,9 @@ +--- +- name: Reload systemd + ansible.builtin.systemd: + daemon_reload: true + +- name: Reload caddy + ansible.builtin.systemd: + name: caddy + state: reloaded diff --git a/roles/vhosts/modern_it_history/tasks/main.yml b/roles/vhosts/modern_it_history/tasks/main.yml new file mode 100644 index 0000000..dc772bd --- /dev/null +++ b/roles/vhosts/modern_it_history/tasks/main.yml @@ -0,0 +1,110 @@ +--- +- name: Install site build prerequisites + ansible.builtin.apt: + name: + - git + - rsync + - caddy + state: present + update_cache: true + +- name: Ensure Modern IT History directories exist + ansible.builtin.file: + path: "{{ item }}" + state: directory + owner: root + group: root + mode: "0755" + loop: + - "{{ modern_it_history_root }}" + - "{{ modern_it_history_current_dir }}" + +- name: Clone or update Modern IT History repository + ansible.builtin.git: + repo: "{{ modern_it_history_repo }}" + dest: "{{ modern_it_history_repo_dir }}" + version: "{{ modern_it_history_branch }}" + force: true + update: true + +- name: Install Docusaurus dependencies + ansible.builtin.command: npm ci + args: + chdir: "{{ modern_it_history_repo_dir }}" + environment: + NODE_ENV: "{{ modern_it_history_node_env }}" + +- name: Build Docusaurus static site + ansible.builtin.command: npm run build + args: + chdir: "{{ modern_it_history_repo_dir }}" + environment: + NODE_ENV: "{{ modern_it_history_node_env }}" + +- name: Publish Docusaurus build to current static root + ansible.builtin.command: > + rsync -a --delete {{ modern_it_history_build_dir }}/ {{ modern_it_history_current_dir }}/ + +- name: Install GitHub auto-update script + ansible.builtin.template: + src: modern-it-history-update.sh.j2 + dest: /usr/local/bin/modern-it-history-update + owner: root + group: root + mode: "0755" + +- name: Install Modern IT History update service + ansible.builtin.template: + src: modern-it-history-update.service.j2 + dest: /etc/systemd/system/modern-it-history-update.service + owner: root + group: root + mode: "0644" + notify: Reload systemd + +- name: Install Modern IT History update timer + ansible.builtin.template: + src: modern-it-history-update.timer.j2 + dest: /etc/systemd/system/modern-it-history-update.timer + owner: root + group: root + mode: "0644" + notify: Reload systemd + +- name: Install ebook Caddy site + ansible.builtin.template: + src: ebook.svc.plus.caddy.j2 + dest: /etc/caddy/sites-enabled/{{ modern_it_history_domain }}.caddy + owner: root + group: root + mode: "0644" + notify: Reload caddy + +- name: Validate Caddy config + ansible.builtin.command: caddy validate --config /etc/caddy/Caddyfile + changed_when: false + +- name: Enable GitHub auto-update timer + ansible.builtin.systemd: + name: modern-it-history-update.timer + enabled: true + state: started + daemon_reload: true + +- name: Ensure Caddy is enabled and running + ansible.builtin.systemd: + name: caddy + enabled: true + state: started + +- name: Validate local ebook endpoint + ansible.builtin.uri: + url: "http://127.0.0.1/" + headers: + Host: "{{ modern_it_history_domain }}" + status_code: 200 + register: modern_it_history_local_check + +- name: Show local validation status + ansible.builtin.debug: + msg: "ebook local validation status={{ modern_it_history_local_check.status }}" diff --git a/roles/vhosts/modern_it_history/templates/ebook.svc.plus.caddy.j2 b/roles/vhosts/modern_it_history/templates/ebook.svc.plus.caddy.j2 new file mode 100644 index 0000000..8766b2c --- /dev/null +++ b/roles/vhosts/modern_it_history/templates/ebook.svc.plus.caddy.j2 @@ -0,0 +1,23 @@ +{{ modern_it_history_domain }} { + root * {{ modern_it_history_current_dir }} + encode zstd gzip + try_files {path} {path}/ /index.html + file_server + + header { + Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" + X-Content-Type-Options "nosniff" + Referrer-Policy "strict-origin-when-cross-origin" + Permissions-Policy "geolocation=(), microphone=(), camera=()" + } + + @static { + path *.js *.css *.png *.jpg *.jpeg *.gif *.svg *.webp *.ico *.woff *.woff2 + } + header @static Cache-Control "public, max-age=31536000, immutable" + + @html { + path *.html / + } + header @html Cache-Control "public, max-age=300" +} diff --git a/roles/vhosts/modern_it_history/templates/modern-it-history-update.service.j2 b/roles/vhosts/modern_it_history/templates/modern-it-history-update.service.j2 new file mode 100644 index 0000000..3f294ac --- /dev/null +++ b/roles/vhosts/modern_it_history/templates/modern-it-history-update.service.j2 @@ -0,0 +1,8 @@ +[Unit] +Description=Update Modern IT History Docusaurus ebook from GitHub +Wants=network-online.target +After=network-online.target + +[Service] +Type=oneshot +ExecStart=/usr/local/bin/modern-it-history-update diff --git a/roles/vhosts/modern_it_history/templates/modern-it-history-update.sh.j2 b/roles/vhosts/modern_it_history/templates/modern-it-history-update.sh.j2 new file mode 100644 index 0000000..5bc5ad5 --- /dev/null +++ b/roles/vhosts/modern_it_history/templates/modern-it-history-update.sh.j2 @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +set -euo pipefail + +repo_dir="{{ modern_it_history_repo_dir }}" +current_dir="{{ modern_it_history_current_dir }}" +build_dir="{{ modern_it_history_build_dir }}" +branch="{{ modern_it_history_branch }}" + +cd "$repo_dir" +before="$(git rev-parse HEAD 2>/dev/null || true)" +git fetch origin "$branch" +git reset --hard "origin/$branch" +after="$(git rev-parse HEAD)" + +if [[ "$before" == "$after" && -d "$current_dir" && -n "$(find "$current_dir" -mindepth 1 -maxdepth 1 -print -quit)" ]]; then + exit 0 +fi + +npm ci +npm run build +rsync -a --delete "$build_dir"/ "$current_dir"/ +caddy validate --config /etc/caddy/Caddyfile +systemctl reload caddy diff --git a/roles/vhosts/modern_it_history/templates/modern-it-history-update.timer.j2 b/roles/vhosts/modern_it_history/templates/modern-it-history-update.timer.j2 new file mode 100644 index 0000000..28586d5 --- /dev/null +++ b/roles/vhosts/modern_it_history/templates/modern-it-history-update.timer.j2 @@ -0,0 +1,10 @@ +[Unit] +Description=Run Modern IT History GitHub auto-update + +[Timer] +OnBootSec=2min +OnUnitActiveSec={{ modern_it_history_update_interval }} +Unit=modern-it-history-update.service + +[Install] +WantedBy=timers.target