Skip to content

サンドボックスのクイックスタート

glac コマンドライン、Python SDK、コンソールで、サンドボックスを作成し、中でコマンドを実行し、ファイルをコピーし、ポートを開き、スナップショットを取ってフォークします。

更新

プロジェクトの API キーを GLAC_API_KEY に設定し、glac か Python SDK をインストールしておく必要があります。方法ははじめににあります。

コマンドラインで

そのために作られ、終わると削除されるサンドボックスで、コマンドを 1 つ実行します。終了コードはコマンドのものです。

glac run --image python:3.12 -- python3 -c 'print(6*7)'

削除するまで残るサンドボックス:

glac sandboxes create --name web --image python:3.12      # prints its id
glac sandboxes exec web -- python3 --version              # exits with the command's code
echo '<h1>hello</h1>' > index.html
glac sandboxes cp index.html web:/srv/                    # copy files in, and out
glac sandboxes exec web --detach -- python3 -m http.server 3000 --directory /srv
glac sandboxes url web 3000                               # a link to port 3000: whoever has it can open it

ターミナル。Enter を押してから ~. で、動かしたまま抜けます。glac sandboxes shell web --join <exec id> で戻ります。

glac sandboxes shell web

スリープ、スナップショット、フォーク、巻き戻し:

glac sandboxes sleep web                                  # sleeps now; the next command wakes it
glac sandboxes snapshots create web --name before-upgrade
copy=$(glac sandboxes fork web)                           # a copy that goes on from this moment
glac sandboxes exec "$copy" -- ls /srv
glac sandboxes restore web before-upgrade                 # back to the snapshot, in place
glac sandboxes rm "$copy" web                             # their snapshots stay
glac sandboxes snapshots rm before-upgrade

復帰のコマンドはありません。サンドボックスを必要とするものが、何であれ復帰させます。glac sbx は glac sandboxes の短縮形です。どのコマンドも --json を受け付け、その場合は API が返したものを表示します。glac help sandboxes でコマンドを一覧表示でき、コマンドラインのページにすべてのコマンドがあります。

Python SDK で

from glac import Glac

glac = Glac()  # GLAC_API_KEY and GLAC_API_URL
print(glac.sandboxes.run("echo hello").stdout)          # one command: made, run, deleted

sbx = glac.sandboxes.create(image="python:3.12", name="web")
res = sbx.run("python3 -c 'print(6*7)'")
print(res.exit_code, res.stdout)                        # 0 42

sbx.files.write("/srv/index.html", "<h1>hello</h1>")
print(sbx.files.read_text("/srv/index.html"))
server = sbx.exec("python3 -m http.server 3000 --directory /srv")   # starts it, does not wait
print(sbx.url(3000))                                    # a link to port 3000

sbx.sleep()                                             # the next call wakes it
snap = sbx.snapshot(name="before-upgrade")
copy = sbx.fork(1)[0]                                   # goes on from this moment
sbx.restore(snap)                                       # back to the snapshot, in place
copy.delete()
sbx.delete()
snap.delete()                                           # a snapshot outlives its sandbox

with glac.sandboxes.create() as sbx: は、ブロックの終わりでサンドボックスを削除します。0 以外で終了したコマンドは例外ではなく結果なので、res.exit_code を読んでください。API のエラーは、status、code、message、request_id を持つ GlacError を送出します。その他の情報は Python SDK のページにあります。

コンソールで

[コンソール](https://console.glac.ai/console/)を開き、プロジェクトを選んで、Compute の下の Sandboxes を選びます。New sandbox で、イメージかテンプレートからサイズを指定してサンドボックスを作成します。サンドボックスのページには、状態と稼働している理由、コマンドとその出力、コマンドを実行する入力欄、そして Terminal、Sleep、Restart、Delete のボタンが表示されます。ターミナルはパネルを閉じても残り、Attach で戻れます。

次のステップ

ナビゲーション

キーワードを入力して検索…

↑↓ 移動↵ 選択Esc 閉じる