// Start a container func Start(c *models.Container, status chan string) { if !initialized { log.Fatal("Package not initialized. Call .Init() function.") } c.Hostname = getHostName() //Get a random name to use as a hostname c.Url = fmt.Sprintf("http://%s.%s", c.Hostname, c.Domainname) c.StartTime = time.Now() db.SaveContainer(c) //Save startup state in the db status <- c.Hostname //Signal the caller that the record is ready to be read // Create the container containerConfig := &dockerclient.ContainerConfig{ Image: c.Image, Cmd: []string{"/bin/sh", "-c", "ipython notebook --no-browser --port 8888 --ip=* --NotebookApp.allow_origin=*"}, ExposedPorts: map[string]struct{}{ "8888/tcp": {}, }, Hostname: c.Hostname, Domainname: c.Domainname, } containerId, err := docker.CreateContainer(containerConfig, c.Hostname) if err != nil { log.Println(err) } c.ContainerId = containerId // Start the container hostConfig := &dockerclient.HostConfig{ PublishAllPorts: true, } err = docker.StartContainer(containerId, hostConfig) if err != nil { log.Println(err) } ks := kernelStarted(c) if ks { c.Status = "ACTIVE" } else { c.Status = "ERROR" } db.SaveContainer(c) //Save state in the db log.Println("Started container ", c.Serialize()) }
func NoOp(c *models.Container, status chan string) { c.Hostname = getHostName() status <- c.Hostname fmt.Println(c.Serialize()) db.SaveContainer(c) time.Sleep(5 * time.Second) c.Status = "READY" db.SaveContainer(c) fmt.Println(c.Serialize()) }
// Kill a container func Kill(c *models.Container, status chan string) { if !initialized { log.Fatal("Package not initialized. Call .Init() function.") } c.Status = "DELETING" db.SaveContainer(c) //Save startup state in the db status <- c.Status err := docker.StopContainer(c.ContainerId, 5) if err != nil { log.Println("Could not kill container", c.Hostname) } docker.RemoveContainer(c.Hostname, true, true) if err != nil { log.Println("Could not remove container ", c.Hostname) } c.Status = "REMOVED" db.SaveContainer(c) //Save startup state in the db log.Println("Removed container ", c.Serialize()) }