Skip to content

Commit

Permalink
Merge pull request #27 from vertex-center/feature/kernel
Browse files Browse the repository at this point in the history
Move sudo commands to a new Vertex Kernel
  • Loading branch information
quentinguidee authored Sep 28, 2023
2 parents f02f494 + fdbec33 commit 2b93fcf
Show file tree
Hide file tree
Showing 29 changed files with 1,708 additions and 512 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
.idea

live
live_kernel

vertex
vertex-old
vertex-kernel
vertex-kernel-old
36 changes: 26 additions & 10 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
build:
goos:
- linux
- darwin
- windows
goarch:
- "386"
- amd64
- arm
- arm64
builds:
- main: ./cmd/kernel
id: "vertex-kernel"
binary: vertex-kernel
goos:
- linux
- darwin
- windows
goarch:
- "386"
- amd64
- arm
- arm64

- main: ./cmd/main
id: "vertex"
binary: vertex
goos:
- linux
- darwin
- windows
goarch:
- "386"
- amd64
- arm
- arm64
15 changes: 15 additions & 0 deletions .run/Run Vertex Kernel.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Run Vertex Kernel" type="GoApplicationRunConfiguration" factoryName="Go Application">
<module name="Vertex" />
<working_directory value="$PROJECT_DIR$" />
<envs>
<env name="DEBUG" value="1" />
</envs>
<sudo value="true" />
<kind value="PACKAGE" />
<package value="github.com/vertex-center/vertex/cmd/kernel" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$/kernel/main.go" />
<method v="2" />
</configuration>
</component>
2 changes: 1 addition & 1 deletion .run/Run Vertex.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<env name="DEBUG" value="1" />
</envs>
<kind value="PACKAGE" />
<package value="github.com/vertex-center/vertex" />
<package value="github.com/vertex-center/vertex/cmd/main" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$/init.go" />
<method v="2" />
Expand Down
12 changes: 10 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ RUN go mod download

COPY . ./

RUN go build -o /vertex -ldflags="-X 'main.version=$(git describe --tags --always --dirty)' -X 'main.commit=$(git rev-parse HEAD)' -X 'main.date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')'"
RUN go build -o /vertex -ldflags="-X 'main.version=$(git describe --tags --always --dirty)' -X 'main.commit=$(git rev-parse HEAD)' -X 'main.date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')'" cmd/main/main.go
RUN go build -o /vertex-kernel cmd/kernel/main.go

FROM build-stage AS test-stage
RUN go test -v ./...
Expand All @@ -19,7 +20,14 @@ FROM alpine AS run-stage
WORKDIR /

COPY --from=build-stage /vertex /vertex
COPY --from=build-stage /vertex-kernel /vertex-kernel

RUN apk add sudo

EXPOSE 80
EXPOSE 6130

CMD /vertex
ARG USER_ID=0
ARG GROUP_ID=0

CMD sudo /vertex-kernel -uid "$USER_ID" -gid "$GROUP_ID"
166 changes: 166 additions & 0 deletions adapter/docker_cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package adapter

import (
"context"
"io"

dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
"github.com/vertex-center/vertex/pkg/log"
"github.com/vertex-center/vertex/types"
"github.com/vertex-center/vlog"
)

type DockerCliAdapter struct {
cli *client.Client
}

func NewDockerCliAdapter() DockerCliAdapter {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Warn("couldn't connect with the Docker cli.",
vlog.String("error", err.Error()),
)

return DockerCliAdapter{}
}

return DockerCliAdapter{
cli: cli,
}
}

func (a DockerCliAdapter) ListContainers() ([]types.Container, error) {
res, err := a.cli.ContainerList(context.Background(), dockertypes.ContainerListOptions{All: true})
if err != nil {
return nil, err
}

var containers []types.Container
for _, c := range res {
containers = append(containers, types.NewContainer(c))
}
return containers, nil
}

func (a DockerCliAdapter) DeleteContainer(id string) error {
return a.cli.ContainerRemove(context.Background(), id, dockertypes.ContainerRemoveOptions{})
}

func (a DockerCliAdapter) CreateContainer(options types.CreateContainerOptions) (types.CreateContainerResponse, error) {
config := container.Config{
Image: options.ImageName,
ExposedPorts: options.ExposedPorts,
Env: options.Env,
Tty: true,
AttachStdout: true,
AttachStderr: true,
}

hostConfig := container.HostConfig{
Binds: options.Binds,
PortBindings: options.PortBindings,
CapAdd: options.CapAdd,
Sysctls: options.Sysctls,
}

res, err := a.cli.ContainerCreate(context.Background(), &config, &hostConfig, nil, nil, options.ContainerName)
if err != nil {
return types.CreateContainerResponse{}, err
}

return types.CreateContainerResponse{
ID: res.ID,
Warnings: res.Warnings,
}, nil
}

func (a DockerCliAdapter) StartContainer(id string) error {
return a.cli.ContainerStart(context.Background(), id, dockertypes.ContainerStartOptions{})
}

func (a DockerCliAdapter) StopContainer(id string) error {
return a.cli.ContainerStop(context.Background(), id, container.StopOptions{})
}

func (a DockerCliAdapter) InfoContainer(id string) (types.InfoContainerResponse, error) {
info, err := a.cli.ContainerInspect(context.Background(), id)
if err != nil {
return types.InfoContainerResponse{}, err
}
return types.InfoContainerResponse{
ID: info.ID,
Name: info.Name,
Platform: info.Platform,
Image: info.Image,
}, nil
}

func (a DockerCliAdapter) LogsStdoutContainer(id string) (io.ReadCloser, error) {
return a.cli.ContainerLogs(context.Background(), id, dockertypes.ContainerLogsOptions{
ShowStdout: true,
Timestamps: false,
Follow: true,
Tail: "0",
})
}

func (a DockerCliAdapter) LogsStderrContainer(id string) (io.ReadCloser, error) {
return a.cli.ContainerLogs(context.Background(), id, dockertypes.ContainerLogsOptions{
ShowStderr: true,
Timestamps: false,
Follow: true,
Tail: "0",
})
}

func (a DockerCliAdapter) WaitContainer(id string, cond types.WaitContainerCondition) error {
statusCh, errCh := a.cli.ContainerWait(context.Background(), id, container.WaitCondition(cond))

select {
case err := <-errCh:
if err != nil {
return err
}
case <-statusCh:
}

return nil
}

func (a DockerCliAdapter) InfoImage(id string) (types.InfoImageResponse, error) {
info, _, err := a.cli.ImageInspectWithRaw(context.Background(), id)
if err != nil {
return types.InfoImageResponse{}, nil
}
return types.InfoImageResponse{
ID: info.ID,
Architecture: info.Architecture,
OS: info.Os,
Size: info.Size,
Tags: info.RepoTags,
}, nil
}

func (a DockerCliAdapter) PullImage(options types.PullImageOptions) (io.ReadCloser, error) {
return a.cli.ImagePull(context.Background(), options.Image, dockertypes.ImagePullOptions{})
}

func (a DockerCliAdapter) BuildImage(options types.BuildImageOptions) (dockertypes.ImageBuildResponse, error) {
buildOptions := dockertypes.ImageBuildOptions{
Dockerfile: options.Dockerfile,
Tags: []string{options.Name},
Remove: true,
}

reader, err := archive.TarWithOptions(options.Dir, &archive.TarOptions{
ExcludePatterns: []string{".git/**/*"},
})
if err != nil {
return dockertypes.ImageBuildResponse{}, err
}

return a.cli.ImageBuild(context.Background(), reader, buildOptions)
}
Loading

0 comments on commit 2b93fcf

Please sign in to comment.