github.com/tompao/docker@v1.9.1/contrib/host-integration/manager.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"flag"
     7  	"fmt"
     8  	"github.com/docker/docker"
     9  	"os"
    10  	"strings"
    11  	"text/template"
    12  )
    13  
    14  var templates = map[string]string{
    15  
    16  	"upstart": `description "{{.description}}"
    17  author "{{.author}}"
    18  start on filesystem and started lxc-net and started docker
    19  stop on runlevel [!2345]
    20  respawn
    21  exec /home/vagrant/goroot/bin/docker start -a {{.container_id}}
    22  `,
    23  
    24  	"systemd": `[Unit]
    25  	Description={{.description}}
    26  	Author={{.author}}
    27  	After=docker.service
    28  
    29  [Service]
    30  	Restart=always
    31  	ExecStart=/usr/bin/docker start -a {{.container_id}}
    32  	ExecStop=/usr/bin/docker stop -t 2 {{.container_id}}
    33  
    34  [Install]
    35  	WantedBy=local.target
    36  `,
    37  }
    38  
    39  func main() {
    40  	// Parse command line for custom options
    41  	kind := flag.String("t", "upstart", "Type of manager requested")
    42  	author := flag.String("a", "<none>", "Author of the image")
    43  	description := flag.String("d", "<none>", "Description of the image")
    44  	flag.Usage = func() {
    45  		fmt.Fprintf(os.Stderr, "\nUsage: manager <container id>\n\n")
    46  		flag.PrintDefaults()
    47  	}
    48  	flag.Parse()
    49  
    50  	// We require at least the container ID
    51  	if flag.NArg() != 1 {
    52  		println(flag.NArg())
    53  		flag.Usage()
    54  		return
    55  	}
    56  
    57  	// Check that the requested process manager is supported
    58  	if _, exists := templates[*kind]; !exists {
    59  		panic("Unknown script template")
    60  	}
    61  
    62  	// Load the requested template
    63  	tpl, err := template.New("processManager").Parse(templates[*kind])
    64  	if err != nil {
    65  		panic(err)
    66  	}
    67  
    68  	// Create stdout/stderr buffers
    69  	bufOut := bytes.NewBuffer(nil)
    70  	bufErr := bytes.NewBuffer(nil)
    71  
    72  	// Instanciate the Docker CLI
    73  	cli := docker.NewDockerCli(nil, bufOut, bufErr, "unix", "/var/run/docker.sock", false, nil)
    74  	// Retrieve the container info
    75  	if err := cli.CmdInspect(flag.Arg(0)); err != nil {
    76  		// As of docker v0.6.3, CmdInspect always returns nil
    77  		panic(err)
    78  	}
    79  
    80  	// If there is nothing in the error buffer, then the Docker daemon is there and the container has been found
    81  	if bufErr.Len() == 0 {
    82  		// Unmarshall the resulting container data
    83  		c := []*docker.Container{{}}
    84  		if err := json.Unmarshal(bufOut.Bytes(), &c); err != nil {
    85  			panic(err)
    86  		}
    87  		// Reset the buffers
    88  		bufOut.Reset()
    89  		bufErr.Reset()
    90  		// Retrieve the info of the linked image
    91  		if err := cli.CmdInspect(c[0].Image); err != nil {
    92  			panic(err)
    93  		}
    94  		// If there is nothing in the error buffer, then the image has been found.
    95  		if bufErr.Len() == 0 {
    96  			// Unmarshall the resulting image data
    97  			img := []*docker.Image{{}}
    98  			if err := json.Unmarshal(bufOut.Bytes(), &img); err != nil {
    99  				panic(err)
   100  			}
   101  			// If no author has been set, use the one from the image
   102  			if *author == "<none>" && img[0].Author != "" {
   103  				*author = strings.Replace(img[0].Author, "\"", "", -1)
   104  			}
   105  			// If no description has been set, use the comment from the image
   106  			if *description == "<none>" && img[0].Comment != "" {
   107  				*description = strings.Replace(img[0].Comment, "\"", "", -1)
   108  			}
   109  		}
   110  	}
   111  
   112  	/// Old version: Wrtie the resulting script to file
   113  	// f, err := os.OpenFile(kind, os.O_CREATE|os.O_WRONLY, 0755)
   114  	// if err != nil {
   115  	// 	panic(err)
   116  	// }
   117  	// defer f.Close()
   118  
   119  	// Create a map with needed data
   120  	data := map[string]string{
   121  		"author":       *author,
   122  		"description":  *description,
   123  		"container_id": flag.Arg(0),
   124  	}
   125  
   126  	// Process the template and output it on Stdout
   127  	if err := tpl.Execute(os.Stdout, data); err != nil {
   128  		panic(err)
   129  	}
   130  }