Ejemplo n.º 1
0
func (i *Inspector) RunContainer() error {
	artifactsPath := filepath.Join(i.LocalVolumePath, "artifacts")
	sensorPath := filepath.Join(utils.ExeDir(), "docker-slim-sensor")

	artifactsMountInfo := fmt.Sprintf("%s:/opt/dockerslim/artifacts", artifactsPath)
	sensorMountInfo := fmt.Sprintf("%s:/opt/dockerslim/bin/sensor:ro", sensorPath)

	containerOptions := dockerapi.CreateContainerOptions{
		Name: "dockerslimk",
		Config: &dockerapi.Config{
			Image: i.ImageInspector.ImageRef,
			ExposedPorts: map[dockerapi.Port]struct{}{
				i.CmdPort: struct{}{},
				i.EvtPort: struct{}{},
			},
			Entrypoint: []string{"/opt/dockerslim/bin/sensor"},
			Cmd:        i.FatContainerCmd,
			Labels:     map[string]string{"type": "dockerslim"},
		},
		HostConfig: &dockerapi.HostConfig{
			Binds:           []string{artifactsMountInfo, sensorMountInfo},
			PublishAllPorts: true,
			CapAdd:          []string{"SYS_ADMIN"},
			Privileged:      true,
		},
	}

	containerInfo, err := i.ApiClient.CreateContainer(containerOptions)
	if err != nil {
		return err
	}

	i.ContainerID = containerInfo.ID
	log.Infoln("docker-slim: created container =>", i.ContainerID)

	if err := i.ApiClient.StartContainer(i.ContainerID, &dockerapi.HostConfig{
		PublishAllPorts: true,
		CapAdd:          []string{"SYS_ADMIN"},
		Privileged:      true,
	}); err != nil {
		return err
	}

	//inspContainerInfo
	i.ContainerInfo, err = i.ApiClient.InspectContainer(i.ContainerID)
	if err != nil {
		return err
	}

	utils.FailWhen(i.ContainerInfo.NetworkSettings == nil, "docker-slim: error => no network info")
	log.Debugf("container NetworkSettings.Ports => %#v\n", i.ContainerInfo.NetworkSettings.Ports)

	return i.initContainerChannels()
}
Ejemplo n.º 2
0
func main() {
	//log.SetLevel(log.DebugLevel)

	log.Infof("sensor: args => %#v\n", os.Args)
	utils.FailWhen(len(os.Args) < 2, "missing app information")

	dirName, err := os.Getwd()
	utils.FailOn(err)
	log.Debugf("sensor: cwd => %#v\n", dirName)

	appName := os.Args[1]
	var appArgs []string
	if len(os.Args) > 2 {
		appArgs = os.Args[2:]
	}

	initSignalHandlers()
	defer func() {
		log.Debug("defered cleanup on shutdown...")
		cleanupOnShutdown()
	}()

	monDoneChan := make(chan bool, 1)
	monDoneAckChan := make(chan bool)
	pidsChan := make(chan []int, 1)
	ptmonStartChan := make(chan int, 1)
	monitor(monDoneChan, monDoneAckChan, pidsChan, ptmonStartChan, appName, appArgs, dirName)

	//target app started by ptmon... (long story :-))
	//TODO: need to get the target app pid to pemon, so it can filter process events
	log.Debugf("sensor: target app started => %v %#v\n", appName, appArgs)
	time.Sleep(3 * time.Second)

	log.Debug("sensor: setting up channels...")
	doneChan = make(chan struct{})

	err = ipc.InitChannels()
	utils.FailOn(err)

	cmdChan, err := ipc.RunCmdServer(doneChan)
	utils.FailOn(err)
	log.Info("sensor: waiting for commands...")
doneRunning:
	for {
		select {
		case cmd := <-cmdChan:
			log.Debugln("\nsensor: command =>", cmd)
			switch cmd {
			case "monitor.finish":
				log.Debug("sensor: 'monitor.finish' command - stopping monitor...")
				break doneRunning
			default:
				log.Debugln("sensor: ignoring command =>", cmd)
			}

		case <-time.After(time.Second * 5):
			log.Debug(".")
		}
	}

	monDoneChan <- true
	log.Info("sensor: waiting for monitor to finish...")
	<-monDoneAckChan

	ipc.TryPublishEvt(3, "monitor.finish.completed")

	log.Info("sensor: done!")
}