#!/usr/bin/env bash # # setup-code-server.sh # - code-server 미설치 시 자동 설치 # - 최초 1회 실행해 ~/.config/code-server/config.yaml 생성 # - config.yaml을 0.0.0.0: + 지정 비밀번호로 갱신 # - 기존에 떠있는 code-server(수동/비-systemd) 프로세스 정리 # - systemd 미사용: nohup으로 백그라운드 실행 # # 환경변수: # PORT=8080 # 바인드 포트 (기본 8080) # PASSWORD= # 비밀번호(무인 실행용) # SKIP_CONFIRM=0/1 # 비밀번호 확인 입력 생략 # set -euo pipefail PORT="${PORT:-8080}" CONFIG_DIR="${HOME}/.config/code-server" CONFIG_FILE="${CONFIG_DIR}/config.yaml" LOG_FILE="${HOME}/code-server.log" say() { echo -e "$@"; } die() { echo -e "❌ $@" >&2; exit 1; } # 0) 필수 도구 준비 (curl/timeout/pgrep 등) if ! command -v curl >/dev/null 2>&1 || ! command -v timeout >/dev/null 2>&1; then say "📦 필요 패키지 설치 중 (curl, coreutils, procps 등)..." if command -v apt >/dev/null 2>&1; then apt update -y >/dev/null 2>&1 || true apt install -y curl ca-certificates coreutils procps >/dev/null 2>&1 else die "apt 환경이 아닙니다. curl/timeout/pgrep가 필요합니다." fi fi # 1) code-server 설치 확인 및 자동 설치 if ! command -v code-server >/dev/null 2>&1; then say "📦 code-server 미설치 상태 → 설치 진행..." bash <(curl -fsSL https://code-server.dev/install.sh) command -v code-server >/dev/null 2>&1 || die "code-server 설치 실패" say "✅ code-server 설치 완료" else say "✅ code-server 이미 설치됨" fi # 2) config.yaml 생성 (없으면 최초 1회 3~5초 실행) if [ ! -f "${CONFIG_FILE}" ]; then say "📝 config.yaml 이 없어 최초 1회 실행으로 생성합니다..." mkdir -p "${CONFIG_DIR}" timeout 5s code-server >/dev/null 2>&1 || true [ -f "${CONFIG_FILE}" ] || die "config.yaml 생성 실패" say "✅ 기본 config.yaml 생성됨: ${CONFIG_FILE}" else say "ℹ️ 기존 config.yaml 감지: ${CONFIG_FILE}" fi # 3) 비밀번호 입력/확정 if [ "${PASSWORD-}" = "" ]; then read -rsp "🔐 code-server 접속 비밀번호 입력: " PASS; echo if [ "${SKIP_CONFIRM-0}" != "1" ]; then read -rsp "🔐 비밀번호 확인 입력: " PASS2; echo [ "$PASS" = "$PASS2" ] || die "비밀번호 불일치" fi else PASS="$PASSWORD" fi [ -n "$PASS" ] || die "비밀번호는 비어 있을 수 없습니다." # 4) 기존 파일 백업 후 config.yaml 갱신 ts="$(date +%Y%m%d%H%M%S)" if [ -f "${CONFIG_FILE}" ]; then cp -a "${CONFIG_FILE}" "${CONFIG_FILE}.bak.${ts}" say "🗂 백업 생성: ${CONFIG_FILE}.bak.${ts}" fi cat > "${CONFIG_FILE}" </dev/null 2>&1; then if systemctl is-active --quiet "code-server@${USER}"; then say "⏹ systemd 서비스(code-server@${USER}) 중지" systemctl stop "code-server@${USER}" || true fi if [ "$EUID" -eq 0 ] && systemctl is-active --quiet "code-server@root"; then say "⏹ systemd 서비스(code-server@root}) 중지" systemctl stop "code-server@root" || true fi fi # 6) 수동/기존 실행 프로세스 정리 (부모/자식 순서 종료) say "🧹 기존 code-server 수동 프로세스 정리..." # 부모 엔트리(메인/entry) TERM pids="$(pgrep -f "/usr/lib/code-server/lib/node /usr/lib/code-server($|/out/node/entry)" || true)" if [ -n "${pids}" ]; then for p in $pids; do pkill -TERM -P "$p" 2>/dev/null || true kill -TERM "$p" 2>/dev/null || true done sleep 2 fi # 남아있으면 KILL pids="$(pgrep -f "/usr/lib/code-server/lib/node /usr/lib/code-server($|/out/node/entry)" || true)" [ -n "${pids}" ] && kill -9 $pids 2>/dev/null || true # 보조 호스트/터미널 프로세스 잔여물 정리(있어도 없어도 무방) pkill -f "vscode/out/bootstrap-fork --type=ptyHost" 2>/dev/null || true pkill -f "vscode/out/bootstrap-fork --type=extensionHost" 2>/dev/null || true pkill -f "shellIntegration-bash.sh" 2>/dev/null || true # 7) 비-systemd 백그라운드 실행 say "🚀 code-server 일반 실행(nohup 백그라운드) 시작..." nohup code-server > "${LOG_FILE}" 2>&1 & pid=$! disown || true sleep 1 say "✅ 실행됨 (PID: ${pid})" say "📄 로그 보기: tail -f ${LOG_FILE}" say "🌐 접속 URL: http://<서버IP>:${PORT}" say "🔑 비밀번호: (방금 설정한 값)" say "🔒 보안 권장: 역프록시(Caddy/Nginx) + HTTPS 사용 시 config는 127.0.0.1로 바꾸세요."