build proper dockerized example

This commit is contained in:
Dominic Breuker
2018-02-18 14:03:59 +01:00
parent a31224ff21
commit c56430d8c4
13 changed files with 111 additions and 32 deletions

3
.dockerignore Normal file
View File

@@ -0,0 +1,3 @@
.git
.gitignore
images/

View File

@@ -1,24 +1,41 @@
DEV_IMAGE = local/pspy-dev:latest
PROJECT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) PROJECT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
build-dev: DEV_IMAGE = local/pspy-development:latest
docker build -f docker/Dockerfile -t $(DEV_IMAGE) . DEV_DOCKERFILE = $(PROJECT_DIR)/docker/Dockerfile.development
dev-build:
docker build -f $(DEV_DOCKERFILE) -t $(DEV_IMAGE) .
dev: dev:
docker run -it --rm -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy $(DEV_IMAGE) docker run -it \
--rm \
-v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \
-w "/go/src/github.com/dominicbreuker/pspy" \
$(DEV_IMAGE)
EXAMPLE_IMAGE = local/pspy-example:latest
EXAMPLE_DOCKERFILE = $(PROJECT_DIR)/docker/Dockerfile.example
example:
docker build -t $(EXAMPLE_IMAGE) -f $(EXAMPLE_DOCKERFILE) .
docker run -it --rm $(EXAMPLE_IMAGE)
BUILD_IMAGE = golang:1.10-alpine
release: release:
docker run -it \ docker run -it \
--rm \ --rm \
-v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \ -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \
-w "/go/src/github.com/dominicbreuker" \
--env CGO_ENABLED=0 \ --env CGO_ENABLED=0 \
--env GOOS=linux \ --env GOOS=linux \
--env GOARCH=386 \ --env GOARCH=386 \
$(DEV_IMAGE) go build -a -ldflags '-extldflags "-static"' -o pspy/bin/pspy32 pspy/main.go $(BUILD_IMAGE) go build -a -ldflags '-extldflags "-static"' -o pspy/bin/pspy32 pspy/main.go
docker run -it \ docker run -it \
--rm \ --rm \
-v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \ -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \
-w "/go/src/github.com/dominicbreuker" \
--env CGO_ENABLED=0 \ --env CGO_ENABLED=0 \
--env GOOS=linux \ --env GOOS=linux \
--env GOARCH=amd64 \ --env GOARCH=amd64 \
$(DEV_IMAGE) go build -a -ldflags '-extldflags "-static"' -o pspy/bin/pspy64 pspy/main.go $(BUILD_IMAGE) go build -a -ldflags '-extldflags "-static"' -o pspy/bin/pspy64 pspy/main.go

View File

@@ -1,15 +0,0 @@
FROM golang:1.9-stretch
RUN apt-get update && apt-get -y install cron sudo
COPY docker/etc/cron.d /etc/cron.d
COPY docker/scripts /scripts
RUN useradd -ms /bin/bash myuser && \
adduser myuser sudo && \
echo 'myuser ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER myuser
WORKDIR /go/src/github.com/dominicbreuker

View File

@@ -0,0 +1,20 @@
FROM golang:1.10-stretch
RUN apt-get update && apt-get -y install cron python3 sudo procps
# install root cronjob
COPY docker/var/spool/cron/crontabs /var/spool/cron/crontabs
RUN chmod 600 /var/spool/cron/crontabs/root
COPY docker/root/scripts /root/scripts
# set up unpriviledged user
# allows passwordless sudo to start cron as root on startup
RUN useradd -ms /bin/bash myuser && \
adduser myuser sudo && \
echo 'myuser ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER myuser
# drop into bash shell
COPY docker/entrypoint-development.sh /entrypoint.sh
RUN sudo chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]

25
docker/Dockerfile.example Normal file
View File

@@ -0,0 +1,25 @@
FROM debian:stretch
RUN apt-get update && apt-get -y install cron python3 sudo procps
# install root cronjob
COPY docker/var/spool/cron/crontabs /var/spool/cron/crontabs
RUN chmod 600 /var/spool/cron/crontabs/root
COPY docker/root/scripts /root/scripts
# install pspy
COPY bin/pspy64 /usr/bin/pspy
# set up unpriviledged user
# allows passwordless sudo to start cron as root on startup
RUN useradd -ms /bin/bash myuser && \
adduser myuser sudo && \
echo 'myuser ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER myuser
# deploy startup script
COPY docker/entrypoint-example.sh /entrypoint.sh
RUN sudo chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]

View File

@@ -0,0 +1,13 @@
#!/bin/bash
set -e
sudo cron -f &
sleep 1
sudo ps | grep cron 1>/dev/null
echo "[+] cron started"
echo "[+] Running as user `id`"
echo "[+] Dropping into shell..."
exec /bin/bash

View File

@@ -0,0 +1,13 @@
#!/bin/bash
set -e
sudo cron -f &
sleep 1
sudo ps | grep cron 1>/dev/null
echo "[+] cron started"
echo "[+] Running as user `id`"
echo "[+] Starting pspy now..."
pspy 2>/dev/null

View File

@@ -0,0 +1 @@
* * * * * root python /root/scripts/password_reset.py

View File

@@ -1 +0,0 @@
* * * * * root echo 'this is some text' >> /tmp/myjob.log

View File

@@ -1 +0,0 @@
* * * * * root python /scripts/print_stuff.py >> /tmp/print.log

View File

@@ -0,0 +1,12 @@
#!/usr/bin/python
import string
import random
from subprocess import call
new_password = ''.join(random.SystemRandom()
.choice(string.ascii_uppercase + string.digits)
for _ in range(16))
call("/bin/echo -e \"{}\\n{}\" | passwd myuser"
.format(new_password, new_password), shell=True)

View File

@@ -1,9 +0,0 @@
#!/usr/bin/python
user = "myusername"
password = "thepw"
for i in range(100):
print("a"*i)
print("done")

View File

@@ -0,0 +1 @@
* * * * * python3 /root/scripts/password_reset.py