サンドボックスでのコマンドの実行

実行中のサンドボックスで、シェルコマンドを実行し、出力を待機し、イベントをストリームし、作業を取り消します。

RUNNING状態のサンドボックスから開始します。セッション認証プロファイル、リージョンおよびサンドボックスIDを設定します。

profile_name="DEFAULT"
region="us-chicago-1"
sandbox_id="SANDBOX_ID"

コマンドの実行と出力の待機

続行する前に、結果が必要なコマンドにrun-and-waitを使用します。

oci --profile "$profile_name" --auth security_token --region "$region" \
  generative-ai-sandbox sandbox-command run-and-wait \
  --sandbox-id "$sandbox_id" \
  --command "pwd && ls -la /workspace" \
  --timeout PT5M

返されたコマンドID、標準出力、標準エラー、および終了コードを検査します。ゼロ以外の終了コードは、コマンドがエラーをレポートしたことを示します。

ストリーミング出力

run-and-streamを使用して、コマンドの実行時に出力を読み取ります。この例では、次の3つのメッセージが出力されます。

oci --profile "$profile_name" --auth security_token --region "$region" \
  generative-ai-sandbox sandbox-command run-and-stream \
  --sandbox-id "$sandbox_id" \
  --command "sh -lc 'for i in 1 2 3; do echo message-\$i; sleep 1; done'" \
  --timeout PT5M \
  --file -

コマンドの非同期起動

runを使用してコマンドを開始し、出力の取得または取消しのためにそのIDを保存します。

command_id=$(oci --profile "$profile_name" --auth security_token --region "$region" \
  generative-ai-sandbox sandbox-command run \
  --sandbox-id "$sandbox_id" \
  --command "sleep 30" \
  --timeout PT2M \
  --query 'data.id' --raw-output)

取得された出力の取得:

oci --profile "$profile_name" --auth security_token --region "$region" \
  generative-ai-sandbox sandbox-command get-output \
  --sandbox-id "$sandbox_id" \
  --command-id "$command_id" \
  --from-seq 0

または、既存のコマンドのストリームイベント:

oci --profile "$profile_name" --auth security_token --region "$region" \
  generative-ai-sandbox sandbox-command stream \
  --sandbox-id "$sandbox_id" \
  --command-id "$command_id" \
  --from-seq 0

順序値0は、最初から検索を開始します。

コマンドの取消し

実行中のコマンドの終了をリクエストするには、SIGTERMを送信します:

oci --profile "$profile_name" --auth security_token --region "$region" \
  generative-ai-sandbox sandbox-command cancel \
  --sandbox-id "$sandbox_id" \
  --command-id "$command_id" \
  --signal SIGTERM

コマンドを取り消してサンドボックスを停止することは、別々の操作です。ランタイムを終了するには、「サンドボックスの停止」を参照してください。

パブリッシュされたポートでのサービスの実行

サンドボックスを作成する場合は、--publish-portsを使用して、サービスに必要なポートを公開します。例:

project_id="PROJECT_OCID"

sandbox_id=$(oci --profile "$profile_name" --auth security_token --region "$region" \
  generative-ai-sandbox sandbox create \
  --project-id "$project_id" \
  --display-name preview-sandbox \
  --runtime python-3.11 \
  --shape SMALL \
  --publish-ports '["8080"]' \
  --expiration-duration PT30M \
  --wait-for-state RUNNING \
  --query 'data.id' --raw-output)
重要

パブリッシュされたポートには、プラットフォーム提供のアプリケーション認証がありません。公開されたURLを取得したユーザーは、アプリケーションがアクセスを制限しないかぎり、サンドボックスの実行中にそのポートに到達できます。必要に応じて認証を追加し、ワークロードに必要なポートのみを公開します。

非機密ファイルのみを含む一時プレビューの場合、専用ディレクトリでサーバーを起動します。

oci --profile "$profile_name" --auth security_token --region "$region" \
  generative-ai-sandbox sandbox-command run \
  --sandbox-id "$sandbox_id" \
  --command "mkdir -p /workspace/preview && python3 -m http.server 8080 --bind 0.0.0.0 --directory /workspace/preview" \
  --timeout PT30M

サンドボックスを取得し、keyが8080のpublished-portsエントリを読み取ります。valueは、サービスのURLです。

oci --profile "$profile_name" --auth security_token --region "$region" \
  generative-ai-sandbox sandbox get \
  --sandbox-id "$sandbox_id"

接続する前にサーバーの準備が整うまで待ちます。プレビューが終了したら、コマンドまたはサンドボックスを停止します。