워크로드가 시작될 때 갖는 것

워크로드가 시작될 때 갖는 것 (What your workload starts with)

프로세스가 받는 환경을 확인하고 한 명령에 값을 덮어쓰는 방법을 알아볼게요.

출처: 문서

본문

프로세스가 받는 환경을 확인하고 한 명령의 값을 덮어써요. 이렇게 하면 워크스페이스 경로나 설정 값이 누락된 문제를 진단할 수 있어요.

실행 중인 샌드박스 핸들을 사용해요. 환경에는 자격 증명이 포함될 수 있으니, 전체 결과를 로그에 덤프하거나 신뢰할 수 없는 클라이언트에 보내지 마세요.

프로세스 환경 읽기 (Read the process environment)

env를 실행하고 출력을 파싱해요. 고정된 경로를 가정하지 말고 WORKSPACE_DIR을 읽어 워크스페이스를 찾으세요.

이것은 그 프로세스가 보는 환경을 알려줘요. 저장된 시크릿 값을 가져오는 방법이 아니라 진단용 예시예요.

TypeScript

export async function readFloor(sandbox: Sandbox) {
  const result = requireSuccess(
    await sandbox.processes.run({ args: ['env'] }, { timeoutMs: 300_000 }),
  );
  return Object.fromEntries(
    result.stdout
      .split('\n')
      .filter((line) => line.includes('='))
      .map((line) => {
        const delimiter = line.indexOf('=');
        return [line.slice(0, delimiter), line.slice(delimiter + 1)];
      }),
  );
}

전체 TypeScript 예시: runtime/read.ts

import { requireSuccess, type Sandbox } from '@docker/sandboxes';

export async function readFloor(sandbox: Sandbox) {
  const result = requireSuccess(
    await sandbox.processes.run({ args: ['env'] }, { timeoutMs: 300_000 }),
  );
  return Object.fromEntries(
    result.stdout
      .split('\n')
      .filter((line) => line.includes('='))
      .map((line) => {
        const delimiter = line.indexOf('=');
        return [line.slice(0, delimiter), line.slice(delimiter + 1)];
      }),
  );
}

한 명령에 변수 덮어쓰기 (Override a variable for one command)

프로세스 요청의 환경 맵에 값을 넣어요. 첫 명령은 그 덮어쓴 값을 읽어요. 덮어쓰기가 없는 두 번째 명령은 샌드박스의 원래 값을 읽어요.

덮어쓰기는 프로세스 요청에 속하며, 이후 명령을 위해 샌드박스의 환경을 바꾸지 않아요. 작업별 설정에 사용하세요. 제공 업체 자격 증명에는 저장된 시크릿(stored secrets)을 선호해요.

TypeScript

const fromRequest = requireSuccess(
  await sandbox.processes.run(
    {
      args: ['printenv', name],
      env: { [name]: value },
    },
    { timeoutMs: 300_000 },
  ),
);
const fromSandbox = await sandbox.processes.run(
  {
    args: ['printenv', name],
  },
  { timeoutMs: 300_000 },
);
return {
  fromRequest: fromRequest.stdout.trimEnd(),
  fromSandbox: fromSandbox.stdout.trimEnd(),
};

전체 TypeScript 예시: runtime/precedence.ts

import { requireSuccess, type Sandbox } from '@docker/sandboxes';

export async function overrideVariable(
  sandbox: Sandbox,
  name: string,
  value: string,
) {
  const fromRequest = requireSuccess(
    await sandbox.processes.run(
      {
        args: ['printenv', name],
        env: { [name]: value },
      },
      { timeoutMs: 300_000 },
    ),
  );
  const fromSandbox = await sandbox.processes.run(
    {
      args: ['printenv', name],
    },
    { timeoutMs: 300_000 },
  );
  return {
    fromRequest: fromRequest.stdout.trimEnd(),
    fromSandbox: fromSandbox.stdout.trimEnd(),
  };
}

더 알아보기 (Learn more)

전체 레시피 목록은 Browse all recipes 에서 확인할 수 있어요.