-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from pathwar/dev/moul/docker-hypervisor
feat: initial docker hypervisor
- Loading branch information
Showing
13 changed files
with
388 additions
and
165 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package hypervisor | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
"go.uber.org/zap" | ||
|
||
"pathwar.pw/pkg/cli" | ||
) | ||
|
||
type Options struct { | ||
} | ||
|
||
func (opts Options) String() string { | ||
out, _ := json.Marshal(opts) | ||
return string(out) | ||
} | ||
|
||
func Commands() cli.Commands { | ||
return cli.Commands{ | ||
"hypervisor": &hypervisorCommand{}, | ||
"hypervisor run": &runCommand{}, | ||
} | ||
} | ||
|
||
type hypervisorCommand struct{ opts Options } | ||
|
||
func (cmd *hypervisorCommand) LoadDefaultOptions() error { return viper.Unmarshal(&cmd.opts) } | ||
func (cmd *hypervisorCommand) ParseFlags(flags *pflag.FlagSet) { | ||
if err := viper.BindPFlags(flags); err != nil { | ||
zap.L().Warn("failed to bind viper flags", zap.Error(err)) | ||
} | ||
} | ||
func (cmd *hypervisorCommand) CobraCommand(commands cli.Commands) *cobra.Command { | ||
command := &cobra.Command{ | ||
Use: "hypervisor", | ||
} | ||
command.AddCommand(commands["hypervisor run"].CobraCommand(commands)) | ||
return command | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package hypervisor | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/container" | ||
"github.com/docker/docker/client" | ||
"github.com/docker/go-connections/nat" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
"go.uber.org/zap" | ||
|
||
"pathwar.pw/pkg/cli" | ||
) | ||
|
||
type runOptions struct { | ||
target string `mapstructure:"target"` | ||
webPort int `mapstructure:"web-port"` | ||
// port, other options | ||
// driver=docker | ||
} | ||
|
||
type runCommand struct{ opts runOptions } | ||
|
||
func (cmd *runCommand) CobraCommand(commands cli.Commands) *cobra.Command { | ||
cc := &cobra.Command{ | ||
Use: "run", | ||
Args: func(_ *cobra.Command, args []string) error { | ||
if cmd.opts.target == "" { | ||
return errors.New("--target is mandatory") | ||
} | ||
return nil | ||
}, | ||
RunE: func(_ *cobra.Command, args []string) error { | ||
opts := cmd.opts | ||
return runRun(opts) | ||
}, | ||
} | ||
cmd.ParseFlags(cc.Flags()) | ||
return cc | ||
} | ||
func (cmd *runCommand) LoadDefaultOptions() error { return viper.Unmarshal(&cmd.opts) } | ||
func (cmd *runCommand) ParseFlags(flags *pflag.FlagSet) { | ||
flags.StringVarP(&cmd.opts.target, "target", "t", "", "target (image, path, etc)") | ||
flags.IntVarP(&cmd.opts.webPort, "web-port", "p", 8080, "web listening port") | ||
if err := viper.BindPFlags(flags); err != nil { | ||
zap.L().Warn("failed to bind viper flags", zap.Error(err)) | ||
} | ||
} | ||
|
||
func runRun(opts runOptions) error { | ||
ctx := context.Background() | ||
cli, err := client.NewClientWithOpts(client.FromEnv) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create docker client") | ||
} | ||
|
||
// configure new container | ||
imageInspect, _, err := cli.ImageInspectWithRaw(ctx, opts.target) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to inspect image") | ||
} | ||
containerConfig := &container.Config{ | ||
Image: opts.target, | ||
Tty: true, | ||
OpenStdin: true, | ||
// StdinOnce: true, | ||
// AttachStdin: true, | ||
AttachStdout: true, | ||
AttachStderr: true, | ||
ExposedPorts: nat.PortSet{ | ||
nat.Port("80/tcp"): {}, | ||
}, | ||
// Hostname: "" | ||
Cmd: imageInspect.Config.Cmd, | ||
} | ||
hostConfig := &container.HostConfig{ | ||
// Binds: /etc/timezone | ||
PortBindings: nat.PortMap{ | ||
nat.Port("80/tcp"): []nat.PortBinding{{HostIP: "0.0.0.0", HostPort: fmt.Sprintf("%d", opts.webPort)}}, | ||
}, | ||
AutoRemove: true, | ||
// RestartPolicy: "no" | ||
// Dns | ||
// DnsOptions | ||
// DnsSearch | ||
} | ||
//networkingConfig := &network.NetworkingConfig{SandboxID:"XXX",SandboxKey:"XXX"} | ||
|
||
// FIXME: create a limited network config | ||
// FIXME: restrict resources (cgroups, etc.) | ||
// FIXME: handle exposed port | ||
// FIXME: handle entrypoint/cmd | ||
// FIXME: wrap with custom init | ||
// FIXME: copy init binary into image | ||
// FIXME: configure env | ||
// FIXME: copy tokens somewhere safe in the image | ||
|
||
cont, err := cli.ContainerCreate(ctx, containerConfig, hostConfig, nil, "") | ||
if err != nil { | ||
return err | ||
} | ||
if cont.Warnings != nil && len(cont.Warnings) > 0 { | ||
for _, warn := range cont.Warnings { | ||
zap.L().Warn(warn) | ||
} | ||
} | ||
|
||
if err := cli.ContainerStart(ctx, cont.ID, types.ContainerStartOptions{}); err != nil { | ||
return err | ||
} | ||
|
||
// FIXME: wait for error or ctrl+C | ||
ctxCancel, cancel := context.WithTimeout(ctx, 5*time.Second) | ||
defer cancel() | ||
_, errC := cli.ContainerWait(ctxCancel, cont.ID, "") | ||
if err := <-errC; err != nil { | ||
return err | ||
} | ||
|
||
duration := 2 * time.Second | ||
if err := cli.ContainerStop(ctx, cont.ID, &duration); err != nil { | ||
return errors.Wrap(err, "failed to stop container") | ||
} | ||
|
||
if err := cli.ContainerRemove(ctx, cont.ID, types.ContainerRemoveOptions{ | ||
RemoveVolumes: true, | ||
RemoveLinks: true, | ||
Force: true, | ||
}); err != nil { | ||
return errors.Wrap(err, "failed to remove container") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package hypervisor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FROM nginx | ||
COPY static /usr/share/nginx/html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>Hello World!</h1> |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.