Skip to content

Commit

Permalink
Add support for file editor
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosnils committed Jan 5, 2018
1 parent f564f1f commit 386bd87
Show file tree
Hide file tree
Showing 32 changed files with 621 additions and 93 deletions.
65 changes: 43 additions & 22 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,34 @@ const (

type DockerApi interface {
GetClient() *client.Client
CreateNetwork(id string, opts types.NetworkCreate) error
ConnectNetwork(container, network, ip string) (string, error)

NetworkCreate(id string, opts types.NetworkCreate) error
NetworkConnect(container, network, ip string) (string, error)
NetworkInspect(id string) (types.NetworkResource, error)
GetDaemonInfo() (types.Info, error)
GetDaemonHost() string
NetworkDelete(id string) error
NetworkDisconnect(containerId, networkId string) error

DaemonInfo() (types.Info, error)
DaemonHost() string

GetSwarmPorts() ([]string, []uint16, error)
GetPorts() ([]uint16, error)
GetContainerStats(name string) (io.ReadCloser, error)

ContainerStats(name string) (io.ReadCloser, error)
ContainerResize(name string, rows, cols uint) error
ContainerRename(old, new string) error
CreateAttachConnection(name string) (net.Conn, error)
CopyToContainer(containerName, destination, fileName string, content io.Reader) error
DeleteContainer(name string) error
CreateContainer(opts CreateContainerOpts) error
GetContainerIPs(id string) (map[string]string, error)
ContainerDelete(name string) error
ContainerCreate(opts CreateContainerOpts) error
ContainerIPs(id string) (map[string]string, error)
ExecAttach(instanceName string, command []string, out io.Writer) (int, error)
DisconnectNetwork(containerId, networkId string) error
DeleteNetwork(id string) error
Exec(instanceName string, command []string) (int, error)

CreateAttachConnection(name string) (net.Conn, error)
CopyToContainer(containerName, destination, fileName string, content io.Reader) error
CopyFromContainer(containerName, filePath string) (io.Reader, error)
SwarmInit(advertiseAddr string) (*SwarmTokens, error)
SwarmJoin(addr, token string) error

ConfigCreate(name string, labels map[string]string, data []byte) error
ConfigDelete(name string) error
}
Expand Down Expand Up @@ -82,7 +89,7 @@ func (d *docker) ConfigDelete(name string) error {
return d.c.ConfigRemove(context.Background(), name)
}

func (d *docker) CreateNetwork(id string, opts types.NetworkCreate) error {
func (d *docker) NetworkCreate(id string, opts types.NetworkCreate) error {
_, err := d.c.NetworkCreate(context.Background(), id, opts)

if err != nil {
Expand All @@ -94,7 +101,7 @@ func (d *docker) CreateNetwork(id string, opts types.NetworkCreate) error {
return nil
}

func (d *docker) ConnectNetwork(containerId, networkId, ip string) (string, error) {
func (d *docker) NetworkConnect(containerId, networkId, ip string) (string, error) {
settings := &network.EndpointSettings{}
if ip != "" {
settings.IPAddress = ip
Expand Down Expand Up @@ -125,11 +132,11 @@ func (d *docker) NetworkInspect(id string) (types.NetworkResource, error) {
return d.c.NetworkInspect(context.Background(), id, types.NetworkInspectOptions{})
}

func (d *docker) GetDaemonInfo() (types.Info, error) {
func (d *docker) DaemonInfo() (types.Info, error) {
return d.c.Info(context.Background())
}

func (d *docker) GetDaemonHost() string {
func (d *docker) DaemonHost() string {
return d.c.DaemonHost()
}

Expand Down Expand Up @@ -180,7 +187,7 @@ func (d *docker) GetPorts() ([]uint16, error) {
return openPorts, nil
}

func (d *docker) GetContainerStats(name string) (io.ReadCloser, error) {
func (d *docker) ContainerStats(name string) (io.ReadCloser, error) {
stats, err := d.c.ContainerStats(context.Background(), name, false)

return stats.Body, err
Expand Down Expand Up @@ -222,7 +229,21 @@ func (d *docker) CopyToContainer(containerName, destination, fileName string, co
return d.c.CopyToContainer(context.Background(), containerName, destination, r, types.CopyToContainerOptions{AllowOverwriteDirWithFile: true})
}

func (d *docker) DeleteContainer(name string) error {
func (d *docker) CopyFromContainer(containerName, filePath string) (io.Reader, error) {
rc, stat, err := d.c.CopyFromContainer(context.Background(), containerName, filePath)
if err != nil {
return nil, err
}
if stat.Mode.IsDir() {
return nil, fmt.Errorf("Copying directories is not supported")
}
tr := tar.NewReader(rc)
// advance to the only possible file in the tar archive
tr.Next()
return tr, nil
}

func (d *docker) ContainerDelete(name string) error {
err := d.c.ContainerRemove(context.Background(), name, types.ContainerRemoveOptions{Force: true, RemoveVolumes: true})
d.c.VolumeRemove(context.Background(), name, true)
return err
Expand All @@ -242,7 +263,7 @@ type CreateContainerOpts struct {
Networks []string
}

func (d *docker) CreateContainer(opts CreateContainerOpts) (err error) {
func (d *docker) ContainerCreate(opts CreateContainerOpts) (err error) {
// Make sure directories are available for the new instance container
containerDir := "/var/run/pwd"
containerCertDir := fmt.Sprintf("%s/certs", containerDir)
Expand Down Expand Up @@ -382,7 +403,7 @@ func (d *docker) CreateContainer(opts CreateContainerOpts) (err error) {
return
}

func (d *docker) GetContainerIPs(id string) (map[string]string, error) {
func (d *docker) ContainerIPs(id string) (map[string]string, error) {
cinfo, err := d.c.ContainerInspect(context.Background(), id)
if err != nil {
return nil, err
Expand Down Expand Up @@ -468,7 +489,7 @@ func (d *docker) Exec(instanceName string, command []string) (int, error) {
return ins.ExitCode, nil
}

func (d *docker) DisconnectNetwork(containerId, networkId string) error {
func (d *docker) NetworkDisconnect(containerId, networkId string) error {
err := d.c.NetworkDisconnect(context.Background(), networkId, containerId, true)

if err != nil {
Expand All @@ -480,7 +501,7 @@ func (d *docker) DisconnectNetwork(containerId, networkId string) error {
return nil
}

func (d *docker) DeleteNetwork(id string) error {
func (d *docker) NetworkDelete(id string) error {
err := d.c.NetworkRemove(context.Background(), id)

if err != nil {
Expand Down
27 changes: 16 additions & 11 deletions docker/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"net"
"time"

"docker.io/go-docker/api/types"
client "docker.io/go-docker"
"docker.io/go-docker/api/types"
"github.com/stretchr/testify/mock"
)

Expand All @@ -19,12 +19,12 @@ func (m *Mock) GetClient() *client.Client {
return args.Get(0).(*client.Client)
}

func (m *Mock) CreateNetwork(id string, opts types.NetworkCreate) error {
func (m *Mock) NetworkCreate(id string, opts types.NetworkCreate) error {
args := m.Called(id, opts)
return args.Error(0)
}

func (m *Mock) ConnectNetwork(container, network, ip string) (string, error) {
func (m *Mock) NetworkConnect(container, network, ip string) (string, error) {
args := m.Called(container, network, ip)
return args.String(0), args.Error(1)
}
Expand All @@ -34,12 +34,12 @@ func (m *Mock) NetworkInspect(id string) (types.NetworkResource, error) {
return args.Get(0).(types.NetworkResource), args.Error(1)
}

func (m *Mock) GetDaemonInfo() (types.Info, error) {
func (m *Mock) DaemonInfo() (types.Info, error) {
args := m.Called()
return args.Get(0).(types.Info), args.Error(1)
}

func (m *Mock) GetDaemonHost() string {
func (m *Mock) DaemonHost() string {
args := m.Called()
return args.String(0)
}
Expand All @@ -53,7 +53,7 @@ func (m *Mock) GetPorts() ([]uint16, error) {
args := m.Called()
return args.Get(0).([]uint16), args.Error(1)
}
func (m *Mock) GetContainerStats(name string) (io.ReadCloser, error) {
func (m *Mock) ContainerStats(name string) (io.ReadCloser, error) {
args := m.Called(name)
return args.Get(0).(io.ReadCloser), args.Error(1)
}
Expand All @@ -73,15 +73,20 @@ func (m *Mock) CopyToContainer(containerName, destination, fileName string, cont
args := m.Called(containerName, destination, fileName, content)
return args.Error(0)
}
func (m *Mock) DeleteContainer(id string) error {

func (m *Mock) CopyFromContainer(containerName, filePath string) (io.Reader, error) {
args := m.Called(containerName, filePath)
return args.Get(0).(io.Reader), args.Error(1)
}
func (m *Mock) ContainerDelete(id string) error {
args := m.Called(id)
return args.Error(0)
}
func (m *Mock) CreateContainer(opts CreateContainerOpts) error {
func (m *Mock) ContainerCreate(opts CreateContainerOpts) error {
args := m.Called(opts)
return args.Error(0)
}
func (m *Mock) GetContainerIPs(id string) (map[string]string, error) {
func (m *Mock) ContainerIPs(id string) (map[string]string, error) {
args := m.Called(id)
return args.Get(0).(map[string]string), args.Error(1)
}
Expand All @@ -90,11 +95,11 @@ func (m *Mock) ExecAttach(instanceName string, command []string, out io.Writer)
args := m.Called(instanceName, command, out)
return args.Int(0), args.Error(1)
}
func (m *Mock) DisconnectNetwork(containerId, networkId string) error {
func (m *Mock) NetworkDisconnect(containerId, networkId string) error {
args := m.Called(containerId, networkId)
return args.Error(0)
}
func (m *Mock) DeleteNetwork(id string) error {
func (m *Mock) NetworkDelete(id string) error {
args := m.Called(id)
return args.Error(0)
}
Expand Down
File renamed without changes.
8 changes: 0 additions & 8 deletions dockerfiles/k8s/Dockerfile.final

This file was deleted.

2 changes: 2 additions & 0 deletions dockerfiles/pwm/.gitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[url "https://"]
insteadOf = git://
73 changes: 73 additions & 0 deletions dockerfiles/pwm/.inputrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# /etc/inputrc - global inputrc for libreadline
# See readline(3readline) and `info rluserman' for more information.

# Be 8 bit clean.
set input-meta on
set output-meta on

# To allow the use of 8bit-characters like the german umlauts, uncomment
# the line below. However this makes the meta key not work as a meta key,
# which is annoying to those which don't need to type in 8-bit characters.

# set convert-meta off

# try to enable the application keypad when it is called. Some systems
# need this to enable the arrow keys.
# set enable-keypad on

# see /usr/share/doc/bash/inputrc.arrows for other codes of arrow keys

# do not bell on tab-completion
# set bell-style none
# set bell-style visible

# some defaults / modifications for the emacs mode
$if mode=emacs

# allow the use of the Home/End keys
"\e[1~": beginning-of-line
"\e[4~": end-of-line

# allow the use of the Delete/Insert keys
"\e[3~": delete-char
"\e[2~": quoted-insert

# mappings for "page up" and "page down" to step to the beginning/end
# of the history
# "\e[5~": beginning-of-history
# "\e[6~": end-of-history

# alternate mappings for "page up" and "page down" to search the history
# "\e[5~": history-search-backward
# "\e[6~": history-search-forward

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
"\e[1;5C": forward-word
"\e[1;5D": backward-word
"\e[5C": forward-word
"\e[5D": backward-word
"\e\e[C": forward-word
"\e\e[D": backward-word

$if term=rxvt
"\e[7~": beginning-of-line
"\e[8~": end-of-line
"\eOc": forward-word
"\eOd": backward-word
$endif

# for non RH/Debian xterm, can't hurt for RH/Debian xterm
# "\eOH": beginning-of-line
# "\eOF": end-of-line

# for freebsd console
# "\e[H": beginning-of-line
# "\e[F": end-of-line

$endif

# faster completion
set show-all-if-ambiguous on

"\e[A": history-search-backward
"\e[B": history-search-forward
5 changes: 5 additions & 0 deletions dockerfiles/pwm/.profile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export PS1='\e[1m\e[31m[\h] \e[32m\e[34m\u@$(hostname -i)\e[35m \w\e[0m\n$ '
alias vi='vim'
export PATH=$PATH:/root/go/bin
cat /etc/motd
echo $BASHPID > /var/run/cwd
6 changes: 6 additions & 0 deletions dockerfiles/pwm/.vimrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax on
set autoindent
set expandtab
set number
set shiftwidth=2
set softtabstop=2
44 changes: 44 additions & 0 deletions dockerfiles/pwm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
ARG VERSION=docker:stable-dind
FROM ${VERSION}

RUN apk add --no-cache git tmux vim curl bash build-base qemu-img qemu-system-x86_64

ENV GOPATH /root/go
ENV PATH $PATH:$GOPATH

# Use specific moby commit due to vendoring mismatch
ENV MOBY_COMMIT="d9d2a91780b34b92e669bbfa099f613bd9fad6bb"

RUN mkdir /root/go && apk add --no-cache go \
&& go get -u -d github.com/moby/tool/cmd/moby && (cd $GOPATH/src/github.com/moby/tool/cmd/moby && git checkout $MOBY_COMMIT && go install) \
&& go get -u github.com/linuxkit/linuxkit/src/cmd/linuxkit \
&& rm -rf /root/go/pkg && rm -rf /root/go/src && rm -rf /usr/lib/go


# Add bash completion and set bash as default shell
RUN mkdir /etc/bash_completion.d \
&& curl https://raw.githubusercontent.com/docker/cli/master/contrib/completion/bash/docker -o /etc/bash_completion.d/docker \
&& sed -i "s/ash/bash/" /etc/passwd


# Replace modprobe with a no-op to get rid of spurious warnings
# (note: we can't just symlink to /bin/true because it might be busybox)
RUN rm /sbin/modprobe && echo '#!/bin/true' >/sbin/modprobe && chmod +x /sbin/modprobe

# Install a nice vimrc file and prompt (by soulshake)
COPY ["sudo", "/usr/local/bin/"]
COPY [".vimrc", ".profile", ".inputrc", ".gitconfig", "./root/"]
COPY ["motd", "/etc/motd"]
COPY ["daemon.json", "/etc/docker/"]

# Move to our home
WORKDIR /root


# Remove IPv6 alias for localhost and start docker in the background ...
CMD cat /etc/hosts >/etc/hosts.bak && \
sed 's/^::1.*//' /etc/hosts.bak > /etc/hosts && \
mount -t securityfs none /sys/kernel/security && \
dockerd &>/docker.log & \
while true ; do /bin/bash -l; done
# ... and then put a shell in the foreground, restarting it if it exits
7 changes: 7 additions & 0 deletions dockerfiles/pwm/daemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"experimental": true,
"debug": true,
"log-level": "info",
"insecure-registries": ["127.0.0.1"],
"hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"]
}
8 changes: 8 additions & 0 deletions dockerfiles/pwm/motd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
###############################################################
# WARNING!!!! #
# This is a sandbox environment. Using personal credentials #
# is HIGHLY! discouraged. Any consequences of doing so are #
# completely the user's responsibilites. #
# #
# The PWD team. #
###############################################################
Loading

0 comments on commit 386bd87

Please sign in to comment.