Skip to content

Commit

Permalink
feat(CLI): Support context (#512)
Browse files Browse the repository at this point in the history
* feat(CLI): Support context

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fix: Add remove and fix test cases

Signed-off-by: Ce Gao <cegao@tensorchord.ai>
  • Loading branch information
gaocegege committed Jun 28, 2022
1 parent fde448a commit 523fb40
Show file tree
Hide file tree
Showing 21 changed files with 696 additions and 168 deletions.
7 changes: 1 addition & 6 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,12 @@ func New() EnvdApp {
Usage: "docker image to use for buildkitd",
Value: "docker.io/moby/buildkit:v0.10.3",
},
&cli.StringFlag{
Name: flag.FlagBuildkitdContainer,
Usage: "buildkitd container to use for buildkitd",
Value: "envd_buildkitd",
},
}

internalApp.Commands = []*cli.Command{
CommandBootstrap,
CommandBuild,
CommandContext,
CommandDestroy,
CommandGet,
CommandInit,
Expand All @@ -84,7 +80,6 @@ func New() EnvdApp {
}

// TODO(gaocegege): Add a config struct to keep them.
viper.Set(flag.FlagBuildkitdContainer, context.String(flag.FlagBuildkitdContainer))
viper.Set(flag.FlagBuildkitdImage, context.String(flag.FlagBuildkitdImage))
viper.Set(flag.FlagDebug, debugEnabled)
return nil
Expand Down
8 changes: 7 additions & 1 deletion pkg/app/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

ac "github.com/tensorchord/envd/pkg/autocomplete"
"github.com/tensorchord/envd/pkg/buildkitd"
"github.com/tensorchord/envd/pkg/home"
sshconfig "github.com/tensorchord/envd/pkg/ssh/config"
"github.com/tensorchord/envd/pkg/util/fileutil"
)
Expand Down Expand Up @@ -142,8 +143,13 @@ func bootstrap(clicontext *cli.Context) error {
buildkit := clicontext.Bool("buildkit")

if buildkit {
currentDriver, currentSocket, err := home.GetManager().ContextGetCurrent()
if err != nil {
return errors.Wrap(err, "failed to get the current context")
}
logrus.Debug("bootstrap the buildkitd container")
bkClient, err := buildkitd.NewClient(clicontext.Context, clicontext.String("dockerhub-mirror"))
bkClient, err := buildkitd.NewClient(clicontext.Context,
currentDriver, currentSocket, clicontext.String("dockerhub-mirror"))
if err != nil {
return errors.Wrap(err, "failed to create buildkit client")
}
Expand Down
11 changes: 5 additions & 6 deletions pkg/app/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,11 @@ func build(clicontext *cli.Context) error {
}

logger := logrus.WithFields(logrus.Fields{
"build-context": buildContext,
"build-file": manifest,
"config": config,
"tag": tag,
flag.FlagBuildkitdImage: viper.GetString(flag.FlagBuildkitdImage),
flag.FlagBuildkitdContainer: viper.GetString(flag.FlagBuildkitdContainer),
"build-context": buildContext,
"build-file": manifest,
"config": config,
"tag": tag,
flag.FlagBuildkitdImage: viper.GetString(flag.FlagBuildkitdImage),
})
logger.Debug("starting build command")
debug := clicontext.Bool("debug")
Expand Down
30 changes: 30 additions & 0 deletions pkg/app/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2022 The envd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package app

import (
"github.com/urfave/cli/v2"
)

var CommandContext = &cli.Command{
Name: "context",
Usage: "Manage envd contexts",
Subcommands: []*cli.Command{
CommandContextCreate,
CommandContextList,
CommandContextRemove,
CommandContextUse,
},
}
68 changes: 68 additions & 0 deletions pkg/app/context_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2022 The envd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package app

import (
"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/types"
"github.com/urfave/cli/v2"
)

var CommandContextCreate = &cli.Command{
Name: "create",
Usage: "Create envd context",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Usage: "Name of the context",
Value: "",
},
&cli.StringFlag{
Name: "builder",
Usage: "Builder to use (docker-container, kube-pod)",
Value: string(types.BuilderTypeDocker),
},
&cli.StringFlag{
Name: "builder-name",
Usage: "Builder name",
Value: "envd_buildkitd",
},
&cli.BoolFlag{
Name: "use",
Usage: "Use the context",
},
},
Action: contextCreate,
}

func contextCreate(clicontext *cli.Context) error {
name := clicontext.String("name")
builder := clicontext.String("builder")
builderName := clicontext.String("builder-name")
use := clicontext.Bool("use")

err := home.GetManager().ContextCreate(name,
types.BuilderType(builder), builderName, use)
if err != nil {
return errors.Wrap(err, "failed to create context")
}
logrus.Infof("Context %s is created", name)
if use {
logrus.Infof("Current context is now \"%s\"", name)
}
return nil
}
73 changes: 73 additions & 0 deletions pkg/app/context_ls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2022 The envd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package app

import (
"fmt"
"io"
"os"

"github.com/cockroachdb/errors"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli/v2"

"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/types"
)

var CommandContextList = &cli.Command{
Name: "ls",
Usage: "List envd contexts",
Action: contextList,
}

func contextList(clicontext *cli.Context) error {
contexts, err := home.GetManager().ContextList()
if err != nil {
return errors.Wrap(err, "failed to list context")
}
renderContext(contexts, os.Stdout)
return nil
}

func renderContext(contexts types.EnvdContext, w io.Writer) {
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"context", "builder", "socket"})

table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)

for _, p := range contexts.Contexts {
envRow := make([]string, 3)
if p.Name == contexts.Current {
envRow[0] = fmt.Sprintf("%s (current)", p.Name)
} else {
envRow[0] = p.Name
}
envRow[1] = string(p.Builder)
envRow[2] = fmt.Sprintf("%s://%s", p.Builder, p.BuilderSocket)
table.Append(envRow)
}
table.Render()
}
46 changes: 46 additions & 0 deletions pkg/app/context_rm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2022 The envd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package app

import (
"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
"github.com/tensorchord/envd/pkg/home"
"github.com/urfave/cli/v2"
)

var CommandContextRemove = &cli.Command{
Name: "rm",
Usage: "Remove envd context",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Usage: "Name of the context",
Value: "",
},
},
Action: contextRemove,
}

func contextRemove(clicontext *cli.Context) error {
name := clicontext.String("name")

err := home.GetManager().ContextRemove(name)
if err != nil {
return errors.Wrap(err, "failed to remove context")
}
logrus.Infof("Context %s is removed", name)
return nil
}
46 changes: 46 additions & 0 deletions pkg/app/context_use.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2022 The envd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package app

import (
"github.com/cockroachdb/errors"
"github.com/sirupsen/logrus"
"github.com/tensorchord/envd/pkg/home"
"github.com/urfave/cli/v2"
)

var CommandContextUse = &cli.Command{
Name: "use",
Usage: "Use the specified envd context",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Usage: "Name of the context",
Value: "",
},
},
Action: contextUse,
}

func contextUse(clicontext *cli.Context) error {
name := clicontext.String("name")

err := home.GetManager().ContextUse(name)
if err != nil {
return errors.Wrapf(err, "failed to use the specified context %s", name)
}
logrus.Infof("Current context is now \"%s\"", name)
return nil
}
8 changes: 7 additions & 1 deletion pkg/app/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ func prune(clicontext *cli.Context) error {
keepStorage := clicontext.Float64("keep-storage")
filter := clicontext.StringSlice("filter")
verbose := clicontext.Bool("verbose")
bkClient, err := buildkitd.NewClient(clicontext.Context, "")

currentDriver, currentSocket, err := home.GetManager().ContextGetCurrent()
if err != nil {
return errors.Wrap(err, "failed to get the current context")
}
bkClient, err := buildkitd.NewClient(clicontext.Context,
currentDriver, currentSocket, "")
if err != nil {
return errors.Wrap(err, "failed to create buildkit client")
}
Expand Down
15 changes: 7 additions & 8 deletions pkg/app/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,13 @@ func up(clicontext *cli.Context) error {
detach := clicontext.Bool("detach")

logger := logrus.WithFields(logrus.Fields{
"build-context": buildContext,
"build-file": manifest,
"config": config,
"tag": tag,
"container-name": ctr,
"detach": detach,
flag.FlagBuildkitdImage: viper.GetString(flag.FlagBuildkitdImage),
flag.FlagBuildkitdContainer: viper.GetString(flag.FlagBuildkitdContainer),
"build-context": buildContext,
"build-file": manifest,
"config": config,
"tag": tag,
"container-name": ctr,
"detach": detach,
flag.FlagBuildkitdImage: viper.GetString(flag.FlagBuildkitdImage),
})
logger.Debug("starting up command")
debug := clicontext.Bool("debug")
Expand Down
6 changes: 5 additions & 1 deletion pkg/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ func New(ctx context.Context, configFilePath, manifestFilePath, buildContextDir,
}),
}

cli, err := buildkitd.NewClient(ctx, "")
currentDriver, currentSocket, err := home.GetManager().ContextGetCurrent()
if err != nil {
return nil, errors.Wrap(err, "failed to get the current context")
}
cli, err := buildkitd.NewClient(ctx, currentDriver, currentSocket, "")
if err != nil {
return nil, errors.Wrap(err, "failed to create buildkit client")
}
Expand Down
Loading

0 comments on commit 523fb40

Please sign in to comment.