Skip to content

Commit

Permalink
Use upstart daemon tmpl
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefaille committed Sep 3, 2015
1 parent a8fed33 commit 9387478
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 60 deletions.
58 changes: 58 additions & 0 deletions docker-upstart.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
description "Docker daemon for team #{{.Number}}"

start on (local-filesystems and net-device-up IFACE!=lo)
stop on runlevel [!2345]
limit nofile 524288 1048576
limit nproc 524288 1048576

respawn

kill timeout 20

pre-start script
# see also https://github.com/tianon/cgroupfs-mount/blob/master/cgroupfs-mount
if grep -v '^#' /etc/fstab | grep -q cgroup \
|| [ ! -e /proc/cgroups ] \
|| [ ! -d /sys/fs/cgroup ]; then
exit 0
fi
if ! mountpoint -q /sys/fs/cgroup; then
mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup
fi
(
cd /sys/fs/cgroup
for sys in $(awk '!/^#/ { if ($4 == 1) print $1 }' /proc/cgroups); do
mkdir -p $sys
if ! mountpoint -q $sys; then
if ! mount -n -t cgroup -o $sys cgroup $sys; then
rmdir $sys || true
fi
fi
done
)
end script

script
# modify these in /etc/default/$UPSTART_JOB (/etc/default/docker)
DOCKER=/usr/bin/docker
DOCKER_OPTS=
. /etc/default/$UPSTART_JOB
exec "$DOCKER" daemon $DOCKER_OPTS
end script

# Don't emit "started" event until docker.sock is ready.
# See https://github.com/docker/docker/issues/6647
post-start script
DOCKER_OPTS=
if [ -f /etc/default/$UPSTART_JOB ]; then
. /etc/default/$UPSTART_JOB
fi

while ! [ -e /var/run/{{.Name}}.sock ]; do
initctl status $UPSTART_JOB | grep -qE "(stop|respawn)/" && exit 1
echo "Waiting for /var/run/{{.Name}}.sock"
sleep 0.1
done
echo "/var/run/{{.Name}} is up"

end script
172 changes: 117 additions & 55 deletions dockerGrp/daemonGenerator.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package dockerGrp

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"regexp"
"strconv"
"time"
"text/template"

c "github.com/alyu/configparser"

Expand All @@ -17,46 +19,8 @@ import (

type DockerGroup struct {
unixGrp.Group
Number int64
}

func (dGroup DockerGroup) GenerateDockerSocket() {
// [Unit]
// Description=Docker Socket for the API
// PartOf=docker.service

// [Socket]
// ListenStream=/var/run/docker.sock
// SocketMode=0660
// SocketUser=root
// SocketGroup=docker

// [Install]
// WantedBy=sockets.target

conf := c.NewConfiguration()
sectionUnit := conf.NewSection("Unit")
descr := fmt.Sprint("Docker Socket for the API for Team no", dGroup.Number)
sectionUnit.Add("Description", descr)
servicePath := fmt.Sprint(dGroup.Name, ".service")
sectionUnit.Add("PartOf", servicePath)

sectionSocket := conf.NewSection("Socket")
socketPath := fmt.Sprint("/var/run/", dGroup.Name, ".socket")
sectionSocket.Add("ListenStream", socketPath)
sectionSocket.Add("SocketMode", "0660")
sectionSocket.Add("SocketUser", "root")
sectionSocket.Add("SocketGroup", dGroup.Name)

sectionInstall := conf.NewSection("Install")
sectionInstall.Add("WantedBy", "socket.target")

fmt.Println(conf)
socketConfigPath := fmt.Sprint("/etc/systemd/system/", dGroup.Name, ".socket")
err := c.Save(conf, socketConfigPath)
if err != nil {
log.Println(err)
}
Number int64
Options string
}

func (gDocker DockerGroup) AddNewDockerBr() {
Expand All @@ -76,15 +40,59 @@ func (gDocker DockerGroup) AddNewDockerBr() {
fmt.Println(err)
}

// Nécéssaire car il semble y avoir un «Race condition»
time.Sleep(time.Millisecond * 100)
if err = br.SetLinkUp(); err != nil {
fmt.Println(err)
}

}

func (dGroup DockerGroup) GenerateDockerService() {
func (dGroup DockerGroup) GenerateDockerDaemon() {

tmpl, err := template.New("dockerOpts").Parse("-b {{.Name}} -g /var/lib/{{.Name}} -G {{.Name}} --exec-root=/var/run/docker{{.Number}} --pidfile=\"/var/run/{{.Name}}.pid\" --label=[\"equipe={{.Number}}\"] -H unix:///var/run/{{.Name}}.sock")
if err != nil {
panic(err)
}

optsBuf := new(bytes.Buffer)

err = tmpl.Execute(optsBuf, dGroup)
dGroup.Options = optsBuf.String()

dGroup.generateUpstartDaemon()

// lsb := exec.Command("cat", "/etc/lsb-release", "|", "grep", "DISTRIB_RELEASE")
// lsbOutput, err := lsb.CombinedOutput()
// check(err)

// switch string(lsbOutput) {
// case "DISTRIB_RELEASE=14.04":
// dGroup.generateUpstartDaemon()

// default:
// dGroup.GenerateSystemdService()
// dGroup.GenerateSystemdSocket()
// }

}

func CatchDockerEqGroup(group unixGrp.Group) (DockerGroup, error) {
var validDockerGroup = regexp.MustCompile(`^docker-eq([0-9]+)$`)
if validDockerGroup.MatchString(group.Name) {
num, err := strconv.ParseInt(validDockerGroup.FindStringSubmatch(group.Name)[1], 10, 64)
check(err)
return DockerGroup{Group: group, Number: int64(num)}, nil

}
return DockerGroup{Group: unixGrp.Group{Name: "", Members: nil}, Number: 0}, errors.New("This is not a docker group")
}

func check(e error) {
if e != nil {
panic(e)
}
}

func (dGroup DockerGroup) GenerateSystemdService() {
// [Unit]
// Description=Docker Application Container Engine
// Documentation=https://docs.docker.com
Expand All @@ -111,7 +119,7 @@ func (dGroup DockerGroup) GenerateDockerService() {

sectionService := conf.NewSection("Service")
sectionService.Add("Type", "notify")
execStart := fmt.Sprint("/usr/bin/docker daemon -b ", dGroup.Name, " -g /var/lib/docker", strconv.FormatInt(dGroup.Number, 10), " -G ", dGroup.Name, " --exec-root=/var/run/docker", strconv.FormatInt(dGroup.Number, 10), " --pidfile=\"/var/run/", dGroup.Name, ".pid\" -H fd://")
execStart := fmt.Sprint("/usr/bin/docker daemon ", dGroup.Options)
// execStart := fmt.Sprint("/usr/bin/docker daemon -b docker", strconv.FormatInt(dGroup.Number, 10), " -g /var/lib/docker", " -G ", dGroup.Name, " --exec-root=/var/run/docker --pidfile=\"/var/run/", dGroup.Name, ".pid\" --bip 192.168.", dGroup.Number, ".1/24", " -H fd://")
sectionService.Add("ExecStart", execStart)
sectionService.Add("MountFlags", "slave")
Expand All @@ -130,19 +138,73 @@ func (dGroup DockerGroup) GenerateDockerService() {
}
}

func CatchDockerEqGroup(group unixGrp.Group) (DockerGroup, error) {
var validDockerGroup = regexp.MustCompile(`^docker-eq([0-9]+)$`)
if validDockerGroup.MatchString(group.Name) {
num, err := strconv.ParseInt(validDockerGroup.FindStringSubmatch(group.Name)[1], 10, 64)
check(err)
return DockerGroup{Group: group, Number: int64(num)}, nil
func (dGroup DockerGroup) GenerateSystemdSocket() {
// [Unit]
// Description=Docker Socket for the API
// PartOf=docker.service

// [Socket]
// ListenStream=/var/run/docker.sock
// SocketMode=0660
// SocketUser=root
// SocketGroup=docker

// [Install]
// WantedBy=sockets.target

conf := c.NewConfiguration()
sectionUnit := conf.NewSection("Unit")
descr := fmt.Sprint("Docker Socket for the API for Team no", dGroup.Number)
sectionUnit.Add("Description", descr)
servicePath := fmt.Sprint(dGroup.Name, ".service")
sectionUnit.Add("PartOf", servicePath)

sectionSocket := conf.NewSection("Socket")
socketPath := fmt.Sprint("/var/run/", dGroup.Name, ".socket")
sectionSocket.Add("ListenStream", socketPath)
sectionSocket.Add("SocketMode", "0660")
sectionSocket.Add("SocketUser", "root")
sectionSocket.Add("SocketGroup", dGroup.Name)

sectionInstall := conf.NewSection("Install")
sectionInstall.Add("WantedBy", "socket.target")

fmt.Println(conf)
socketConfigPath := fmt.Sprint("/etc/systemd/system/", dGroup.Name, ".socket")
err := c.Save(conf, socketConfigPath)
if err != nil {
log.Println(err)
}
return DockerGroup{Group: unixGrp.Group{Name: "", Members: nil}, Number: 0}, errors.New("This is not a docker group")
}

func check(e error) {
if e != nil {
panic(e)
func (dGroup DockerGroup) generateUpstartDaemon() {

dat, err := ioutil.ReadFile("docker-upstart.tmpl")
if err != nil {
fmt.Println("File named \"docker-upstart.tmpl\" must be loaded")
}

tmpl, err := template.New("upstartDaemon").Parse(string(dat))
if err != nil {
panic(err)
}

check(err)
buf := new(bytes.Buffer)

err = tmpl.Execute(buf, dGroup)

upstartFilePath := fmt.Sprint("/etc/init/", dGroup.Name, ".conf")
err = ioutil.WriteFile(upstartFilePath, buf.Bytes(), 0755)
if err != nil {
panic(err)
}

dockerOPTS := fmt.Sprint("DOCKER_OPTS=\"", dGroup.Options, "\"")
defaultFilePath := fmt.Sprint("/etc/default/", dGroup.Name)
err = ioutil.WriteFile(defaultFilePath, []byte(dockerOPTS), 0644)
if err != nil {
panic(err)
}

}
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func main() {

if err == nil {

gDocker.GenerateDockerService()
gDocker.GenerateDockerSocket()
gDocker.GenerateDockerDaemon()

if ok, err := tenus.IsInterfaceExist(gDocker.Name); !ok || err != nil {
check(err)
Expand Down
5 changes: 2 additions & 3 deletions unixGrp/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package unixGrp

import (
"bufio"
"fmt"
"log"
"os"
"strings"
Expand All @@ -24,7 +23,7 @@ func TakeAllGroups() (chanGroup chan Group) {
go func() {

for scanner.Scan() {
fmt.Println(1)

out := scanner.Bytes()
lineB := make([]byte, len(out))
copy(lineB, out)
Expand All @@ -40,7 +39,7 @@ func TakeAllGroups() (chanGroup chan Group) {
currentChanGroup <- currentG

}
fmt.Println(2)

close(currentChanGroup)
file.Close()
}()
Expand Down

0 comments on commit 9387478

Please sign in to comment.