0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【ただのメモ】CoderでGemini動かすためのTemplate設定

0
Last updated at Posted at 2026-01-11
  • main.tf
terraform {
  required_providers {
    coder = { source = "coder/coder" }
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 3.0"
    }
  }
}

variable "docker_socket" {
  default     = ""
  description = "(Optional) Docker socket URI"
  type        = string
}

provider "docker" {
  host = var.docker_socket != "" ? var.docker_socket : null
}

data "coder_provisioner" "me" {}
data "coder_workspace" "me" {}
data "coder_workspace_owner" "me" {}

resource "coder_agent" "main" {
  arch = data.coder_provisioner.me.arch
  os   = "linux"
  
  startup_script = <<-EOT
    set -e

    # 1. フォルダ作成と初期化
    WORKSPACE_DIR="/home/coder/${data.coder_workspace.me.name}"
    mkdir -p "$WORKSPACE_DIR"
    if [ ! -f ~/.init_done ]; then
      cp -rT /etc/skel ~
      touch ~/.init_done
    fi

    # 2. code-server の準備待ち
    until command -v code-server >/dev/null 2>&1; do sleep 1; done
    
    # 3. Gemini (Cloud Code) のインストール
    code-server --install-extension googlecloudtools.cloudcode

    # 4. Google Cloud CLI (gcloud) のインストール
    if ! command -v gcloud >/dev/null 2>&1; then
      echo "Installing Google Cloud CLI..."
      curl https://sdk.cloud.google.com | bash -s -- --disable-prompts --install-dir=/tmp
      echo 'export PATH=$PATH:/tmp/google-cloud-sdk/bin' >> ~/.bashrc
      export PATH=$PATH:/tmp/google-cloud-sdk/bin
    fi

    # 5. settings.json への設定注入
    SETTINGS_PATH="$HOME/.local/share/code-server/User/settings.json"
    mkdir -p "$(dirname "$SETTINGS_PATH")"
    python3 -c "
import json, os
path = '$SETTINGS_PATH'
new_data = {
    'geminicodeassist.project': 'platinum-pager-481412-h6',
    'cloudcode.project': 'platinum-pager-481412-h6',
    
    # Gemini UI 最適化
    'google.cloud.code.gemini.chat.agent.visible': True,
    'google.cloud.code.gemini.chat.suggestions.visible': False,
    'google.cloud.code.gemini.chat.defaultCodeBlockDisplay': 'Expanded',
    # 「Prompts to try」を消し去るための最新設定キーたち
    'geminicodeassist.chat.suggestions.visible': False,       # 拡張機能 google.geminicodeassist 用
    #'google.cloud.code.gemini.chat.onboarding.visible': False,  # オンボーディング表示
    'google.cloud.code.gemini.chat.samplePrompts.visible': False, # サンプルプロンプト
    'google.cloud.code.gemini.chat.suggestions.visible': False, # 念のためこれも(旧 Cloud Code 用)
    
    # Copilot は「削除」ではなく「設定」で封鎖
    'chat.defaultProvider': 'google.cloud.code.gemini',
    'chat.disableAIFeatures': True,
    'github.copilot.enable': {'*': False},
    'github.copilot.chat.enable': False,

    'workbench.colorTheme': 'Default Dark Modern',
    'terminal.integrated.copyOnSelection': True,
    'http.systemCertificatesNode': True
}
data = json.load(open(path)) if os.path.exists(path) and os.path.getsize(path) > 0 else {}
data.update(new_data)
with open(path, 'w') as f:
    json.dump(data, f, indent=4)
"
  EOT
}

# --- 永続ボリューム (image_3f4172.png のエラーを解消) ---
resource "docker_volume" "home_volume" {
  # $$ を $ に戻しました。これで Terraform が正しく値を埋め込みます
  name = "coder-${data.coder_workspace.me.id}-home"
}

# --- ワークスペースコンテナ ---
resource "docker_container" "workspace" {
  count = data.coder_workspace.me.start_count
  image = "codercom/enterprise-base:ubuntu"
  name  = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}"
  
  volumes {
    container_path = "/home/coder"
    volume_name    = docker_volume.home_volume.name
  }
  volumes {
    host_path      = "/var/run/docker.sock"
    container_path = "/var/run/docker.sock"
  }

  host {
    host = "host.docker.internal"
    ip   = "host-gateway"
  }

  entrypoint = [
    "sh", "-c",
    <<-EOT
    set -e
    OS_TYPE=$(uname -s | tr '[:upper:]' '[:lower:]')
    ARCH_TYPE=$(uname -m)
    case "$ARCH_TYPE" in
      x86_64)  CODER_ARCH="amd64" ;;
      aarch64|arm64) CODER_ARCH="arm64" ;;
      *)       echo "Unsupported arch: $ARCH_TYPE"; exit 1 ;;
    esac
    curl -fsSL "http://host.docker.internal/bin/coder-$OS_TYPE-$CODER_ARCH" -o /tmp/coder
    chmod +x /tmp/coder
    /tmp/coder agent --auth token --agent-token $CODER_AGENT_TOKEN --agent-url http://host.docker.internal
    EOT
  ]

  env = [
    "CODER_AGENT_TOKEN=${coder_agent.main.token}"
  ]
}

module "code-server" {
  source   = "registry.coder.com/coder/code-server/coder"
  agent_id = coder_agent.main.id
  folder   = "/home/coder/${data.coder_workspace.me.name}"
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?