From 93874784c7f1038981678e03efec441b0c79001c Mon Sep 17 00:00:00 2001 From: Michael Faille Date: Wed, 2 Sep 2015 23:39:58 -0400 Subject: [PATCH] Use upstart daemon tmpl --- docker-upstart.tmpl | 58 ++++++++++++ dockerGrp/daemonGenerator.go | 172 ++++++++++++++++++++++++----------- main.go | 3 +- unixGrp/parser.go | 5 +- 4 files changed, 178 insertions(+), 60 deletions(-) create mode 100755 docker-upstart.tmpl diff --git a/docker-upstart.tmpl b/docker-upstart.tmpl new file mode 100755 index 0000000..9d25def --- /dev/null +++ b/docker-upstart.tmpl @@ -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 diff --git a/dockerGrp/daemonGenerator.go b/dockerGrp/daemonGenerator.go index 631711f..9295546 100644 --- a/dockerGrp/daemonGenerator.go +++ b/dockerGrp/daemonGenerator.go @@ -1,13 +1,15 @@ package dockerGrp import ( + "bytes" "errors" "fmt" + "io/ioutil" "log" "net" "regexp" "strconv" - "time" + "text/template" c "github.com/alyu/configparser" @@ -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() { @@ -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 @@ -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") @@ -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) + } + } diff --git a/main.go b/main.go index 8507edc..6aad55b 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/unixGrp/parser.go b/unixGrp/parser.go index 793c81c..46edfc4 100644 --- a/unixGrp/parser.go +++ b/unixGrp/parser.go @@ -2,7 +2,6 @@ package unixGrp import ( "bufio" - "fmt" "log" "os" "strings" @@ -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) @@ -40,7 +39,7 @@ func TakeAllGroups() (chanGroup chan Group) { currentChanGroup <- currentG } - fmt.Println(2) + close(currentChanGroup) file.Close() }()