Skip to content

Commit

Permalink
feat(sbx): add init sandbox command (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
rektdeckard authored Sep 11, 2024
1 parent 4052314 commit 45c9dbf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
70 changes: 58 additions & 12 deletions cmd/lk/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"os/exec"
"path"
Expand All @@ -32,7 +34,9 @@ import (
)

const (
templateBaseUrl = "https://github.com/livekit-examples"
templateBaseURL = "https://github.com/livekit-examples"
sandboxDashboardURL = "https://cloud.livekit.io/projects/p_/v2/sandbox"
sandboxTemplateEndpoint = "/api/sandbox/template"
)

var (
Expand All @@ -58,7 +62,7 @@ var (
Flags: []cli.Flag{
&cli.StringFlag{
Name: "template",
Usage: "`TEMPLATE` to instantiate, see " + templateBaseUrl,
Usage: "`TEMPLATE` to instantiate, see " + templateBaseURL,
Destination: &templateName,
},
&cli.StringFlag{
Expand All @@ -81,6 +85,20 @@ var (
Destination: &sandboxID,
Required: true,
},
&cli.StringFlag{
// Use "http://cloud-api.livekit.run" in local dev
Name: "server-url",
Value: cloudAPIServerURL,
Destination: &serverURL,
Hidden: true,
},
&cli.StringFlag{
// Use "https://cloud.livekit.run" in local dev
Name: "dashboard-url",
Value: cloudDashboardURL,
Destination: &dashboardURL,
Hidden: true,
},
},
},
{
Expand Down Expand Up @@ -251,32 +269,61 @@ func setupBootstrapTemplate(ctx context.Context, cmd *cli.Command) error {
}

func setupSandboxTemplate(ctx context.Context, cmd *cli.Command) error {
// TODO: implement endpoints to fetch sandbox Template for authorized users
if true {
return errors.New("not implemented")
if sandboxID == "" {
return errors.New("sandbox ID is required")
}

if err := cloneTemplate(ctx, cmd, templateName, appName); err != nil {
_, token, err := requireToken(ctx, cmd)
if err != nil {
return err
}

if err := instantiateEnv(ctx, cmd, appName); err != nil {
req, err := http.NewRequestWithContext(ctx, "GET", serverURL+sandboxTemplateEndpoint, nil)
req.Header = newHeaderWithToken(token)
query := req.URL.Query()
query.Add("id", sandboxID)
req.URL.RawQuery = query.Encode()
if err != nil {
return err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New("access denied")
}

var template bootstrap.Template
if err := json.NewDecoder(resp.Body).Decode(&template); err != nil {
return err
}

fmt.Println("Cloning template...")
if err := cloneTemplate(ctx, cmd, template.URL, template.Name); err != nil {
return err
}

fmt.Println("Instantiating environment...")
if err := instantiateEnv(ctx, cmd, template.Name); err != nil {
return err
}

if err := doInstall(ctx, bootstrap.TaskInstallSandbox, appName, cmd.Bool("verbose")); err != nil {
fmt.Println("Installing template...")
if err := doInstall(ctx, bootstrap.TaskInstallSandbox, template.Name, cmd.Bool("verbose")); err != nil {
return err
}

return nil
}

func cloneTemplate(_ context.Context, cmd *cli.Command, templateURL, appName string) error {
func cloneTemplate(_ context.Context, cmd *cli.Command, url, appName string) error {
var cmdErr error
if err := spinner.New().
Title("Cloning template from " + templateURL).
Title("Cloning template from " + url).
Action(func() {
c := exec.Command("git", "clone", "--depth=1", templateURL, appName)
c := exec.Command("git", "clone", "--depth=1", url, appName)
var out []byte
if out, cmdErr = c.CombinedOutput(); len(out) > 0 && cmd.Bool("verbose") {
fmt.Println(string(out))
Expand All @@ -294,7 +341,6 @@ func instantiateEnv(ctx context.Context, cmd *cli.Command, rootPath string) erro
"LIVEKIT_API_KEY": project.APIKey,
"LIVEKIT_API_SECRET": project.APISecret,
"LIVEKIT_URL": project.URL,
"LIVEKIT_SANDBOX": "TODO",
}

prompt := func(key, oldValue string) (string, error) {
Expand Down
12 changes: 6 additions & 6 deletions pkg/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ const (
)

type Template struct {
Name string `yaml:"name"`
Desc string `yaml:"desc"`
URL string `yaml:"url"`
Docs string `yaml:"docs"`
Image string `yaml:"image"`
Tags []string `yaml:"tags"`
Name string `yaml:"name" json:"name"`
Desc string `yaml:"desc" json:"description,omitempty"`
URL string `yaml:"url" json:"url,omitempty"`
Docs string `yaml:"docs" json:"docs_url,omitempty"`
Image string `yaml:"image" json:"image_ref,omitempty"`
Tags []string `yaml:"tags" json:"tags,omitempty"`
}

func FetchTemplates(ctx context.Context) ([]Template, error) {
Expand Down

0 comments on commit 45c9dbf

Please sign in to comment.