diff options
| -rw-r--r-- | CLAUDE.md | 50 | ||||
| -rw-r--r-- | Deploy.md | 93 | ||||
| -rwxr-xr-x | git/install.sh | 15 | ||||
| -rw-r--r-- | server/authorized_keys | 3 | ||||
| -rw-r--r-- | server/requirements.md | 18 |
5 files changed, 105 insertions, 74 deletions
diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..28206a6 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,50 @@ +# hetzner-infra + +Hetzner VPS 上で動くサービス群のインフラ管理リポジトリ。 + +## ディレクトリ構成 + +``` +caddy/ リバースプロキシ(Caddyfile, docker-compose.yml, deploy.sh) +git/ ベアリポジトリ・フック管理(repos.txt, hooks/, install.sh) +claude/ Claude Code セッション管理(sessions.txt, systemd unit, sync.sh) +server/ サーバー共通設定(authorized_keys, requirements.md) +``` + +## デプロイの仕組み + +- `git push origin master` → post-receive フック → `caddy/deploy.sh` だけ実行 +- フック・リポジトリの変更は push 後に `bash /app/infra/git/install.sh` を手動実行 +- Claude セッションの変更は push 後に `bash /app/infra/claude/sync.sh` を手動実行 + +## 管理対象リポジトリ + +`git/repos.txt` に定義。`git/hooks/<repo>/post-receive` がデプロイ内容。 + +| リポジトリ | work tree | +|---|---| +| hetzner-infra | /app/infra | +| tokyo-livehouse-events | /app/tokyo-livehouse-events | +| whois-band | /app/whois-band | + +## よく使うコマンド + +```bash +# Caddy 設定変更 +vi caddy/Caddyfile && git push origin master + +# フック・リポジトリ定義変更 +vi git/hooks/.../post-receive # または git/repos.txt +git push origin master +ssh root@localhost 'bash /app/infra/git/install.sh' + +# Claude セッション変更 +vi claude/sessions.txt +git push origin master +ssh root@localhost 'bash /app/infra/claude/sync.sh' + +# SSH 鍵追加 +vi server/authorized_keys +git push origin master +ssh root@localhost 'bash /app/infra/git/install.sh' +``` @@ -1,101 +1,46 @@ # Deploy -Hetzner Cloud + Docker + Caddy (HTTPS) の共有インフラ。 +## 初回サーバーセットアップ -## ディレクトリ構成 +```bash +# 1. Docker + hetzner-infra ブートストラップ +scp git/server-setup.sh root@<server-ip>:~/ +ssh root@<server-ip> 'bash server-setup.sh' -| ディレクトリ | 管理対象 | -|---|---| -| `caddy/` | リバースプロキシ(Caddyfile, docker-compose.yml, deploy.sh) | -| `git/` | ベアリポジトリ・フック(repos.txt, hooks/\*/post-receive, install.sh) | -| `claude/` | Claude Code セッション(sessions.txt, systemd unit, sync.sh) | +# 2. 最初の push(/app/infra に展開される) +git push origin master -## リバースプロキシ(caddy/) +# 3. 残りのリポジトリ作成・全フックインストール・SSH鍵適用 +ssh root@<server-ip> 'bash /app/infra/git/install.sh' +``` -git push 時に post-receive フックが `caddy/deploy.sh` を実行する。 +## 通常デプロイ ```bash -git push origin master +git push origin master # Caddy 設定・コンテナ更新 ``` -Caddyfile 変更時も同様。 - -## git リポジトリ管理(git/) - -`git/repos.txt` と `git/hooks/` で全リポジトリのベアリポジトリとフックを管理する。 -git push では**自動展開しない**。変更後は手動で実行する。 +## フック / リポジトリ定義を変更した場合 ```bash git push origin master ssh root@localhost 'bash /app/infra/git/install.sh' ``` -| ファイル | 役割 | -|---|---| -| `git/repos.txt` | `name:work-tree` 形式でリポジトリを定義 | -| `git/hooks/<repo>/post-receive` | 各リポジトリのフック内容 | -| `git/install.sh` | ベアリポジトリ作成 + フック展開 | - -## Claude Code セッション管理(claude/) - -`claude/sessions.txt` で管理し、手動で同期する。 +## Claude Code セッションを変更した場合 ```bash git push origin master ssh root@localhost 'bash /app/infra/claude/sync.sh' ``` -| ファイル | 役割 | -|---|---| -| `claude/sessions.txt` | `user:repo` 形式でセッションを定義 | -| `claude/systemd/claude-code@.service` | サービステンプレート | -| `claude/sync.sh` | user service を同期 | - -### サービス確認 - -```bash -systemctl --user status 'claude-code@*.service' -tmux ls -``` +## SSH 鍵を追加した場合 -## リポジトリとデプロイ先 +`server/authorized_keys` を編集して push 後に `git/install.sh` を実行。 -| リポジトリ | bare repo | work tree | -|---|---|---| -| hetzner-infra | /var/git/hetzner-infra.git | /app/infra | -| tokyo-livehouse-events | /var/git/tokyo-livehouse-events.git | /app/tokyo-livehouse-events | -| whois-band | /var/git/whois-band.git | /app/whois-band | - -## Docker - -``` -web (external network) - ├── caddy-caddy-1 (80/443) - ├── tokyo-app (golive.yyamashita.com → :3000) - └── whois-app (whoisband.yyamashita.com → :3000) -``` - -- **サーバー**: Hetzner CX22 (Helsinki, Ubuntu 24.04) -- **SSH**: `ssh root@localhost` - -## 新アプリ追加手順 - -1. `git/repos.txt` に `<name>:<work-tree>` を追記 -2. `git/hooks/<name>/post-receive` を作成 -3. `caddy/Caddyfile` にホスト追加 -4. `git push origin master` → `ssh root@localhost 'bash /app/infra/git/install.sh'` -5. 新アプリ側: `git remote add hetzner root@localhost:/var/git/<name>.git` → `git push hetzner master` - -## 初回サーバーセットアップ(再構築時) +## サーバー確認 ```bash -# 1. hetzner-infra をブートストラップ -scp git/server-setup.sh root@<server-ip>:~/ -ssh root@<server-ip> 'bash server-setup.sh' - -# 2. 最初の push(bootstrap hook で /app/infra に展開される) -git push origin master - -# 3. 正式フックをインストール + 他リポジトリ作成 -ssh root@<server-ip> 'bash /app/infra/git/install.sh' +ssh root@localhost 'docker ps' +ssh root@localhost 'docker logs caddy-caddy-1 -f' ``` diff --git a/git/install.sh b/git/install.sh index 12f561c..d4522ea 100755 --- a/git/install.sh +++ b/git/install.sh @@ -7,12 +7,27 @@ set -euo pipefail APP_DIR="$(cd "$(dirname "$0")/.." && pwd)" REPOS_FILE="$APP_DIR/git/repos.txt" HOOKS_SRC="$APP_DIR/git/hooks" +AUTH_KEYS_SRC="$APP_DIR/server/authorized_keys" if [[ "$(id -u)" -ne 0 ]]; then echo "ERROR: root として実行してください" >&2 exit 1 fi +echo "=== SSH authorized_keys ===" +if [[ -f "$AUTH_KEYS_SRC" ]]; then + mkdir -p /root/.ssh + chmod 700 /root/.ssh + if diff -q "$AUTH_KEYS_SRC" /root/.ssh/authorized_keys >/dev/null 2>&1; then + echo " unchanged" + else + cp "$AUTH_KEYS_SRC" /root/.ssh/authorized_keys + chmod 600 /root/.ssh/authorized_keys + echo " installed" + fi +fi + +echo "" echo "=== Repositories ===" while IFS=: read -r repo_name work_tree; do [[ "$repo_name" =~ ^#.*$ || -z "$repo_name" ]] && continue diff --git a/server/authorized_keys b/server/authorized_keys new file mode 100644 index 0000000..fa01484 --- /dev/null +++ b/server/authorized_keys @@ -0,0 +1,3 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIL38Bz/ffc14WRi9g3CnLgzLluHwhCPdjRAXxET5t/+Y yyamashita@ubuntu-4gb-hel1-1 +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAWtunt4nZ3MVeom6TbtCFVtEzsiSicnZ1Ea6YX6QLOc +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA4ER14o2du4bd6shr8qBkGunRjQr85elyGIb34/nVmz u0_a188@localhost diff --git a/server/requirements.md b/server/requirements.md new file mode 100644 index 0000000..8be8bd0 --- /dev/null +++ b/server/requirements.md @@ -0,0 +1,18 @@ +# Requirements + +## Server (Hetzner VPS / Ubuntu 24.04) + +| ツール | 用途 | インストール方法 | +|---|---|---| +| Docker Engine + Compose | コンテナ実行 | `curl -fsSL https://get.docker.com \| sh` | +| git | bare リポジトリ | `apt install git` | +| tmux | Claude Code セッション | `apt install tmux` | +| claude CLI | Claude Code | [claude.ai/code](https://claude.ai/code) | +| bash, curl | スクリプト実行 | 標準搭載 | + +## Local + +| ツール | 用途 | +|---|---| +| git | push でデプロイ | +| ssh | サーバー操作 | |
