コード例 #1
0
// newSandbox will create a new sandbox and start it.
func newSandbox(
	command []string,
	env map[string]string,
	proxies map[string]http.Handler,
	image *image.Instance,
	network *network.Network,
	c *runtime.TaskContext,
	e *engine,
) (*sandbox, error) {
	log := e.Log.WithField("taskId", c.TaskID).WithField("runId", c.RunID)

	vm, err := vm.NewVirtualMachine(
		e.engineConfig.MachineOptions,
		image, network, e.engineConfig.SocketFolder, "", "",
		log.WithField("component", "vm"),
	)
	if err != nil {
		return nil, err
	}

	// Create sandbox
	s := &sandbox{
		vm:      vm,
		context: c,
		engine:  e,
		proxies: proxies,
		log:     log,
	}

	// Setup meta-data service
	s.metaService = metaservice.New(command, env, c.LogDrain(), s.result, e.Environment)

	// Create session manager
	s.sessions = newSessionManager(s.metaService, s.vm)

	// Setup network handler
	s.vm.SetHTTPHandler(http.HandlerFunc(s.handleRequest))

	// Start the VM
	debug("Starting virtual machine")
	s.vm.Start()

	// Resolve when VM is closed
	go s.waitForCrash()

	return s, nil
}
コード例 #2
0
func (c *Case) grepLog(context *runtime.TaskContext) {
	reader, err := context.NewLogReader()
	defer reader.Close()
	nilOrPanic(err, "Failed to open log reader")
	data, err := ioutil.ReadAll(reader)
	nilOrPanic(err, "Failed to read log")
	if c.MatchLog != "" {
		match, err := regexp.MatchString(c.MatchLog, string(data))
		nilOrPanic(err, "Invalid regular expression: ", c.MatchLog)
		assert(match, "Expected log to match regular expression: ", c.MatchLog)
	}
	if c.NotMatchLog != "" {
		match, err := regexp.MatchString(c.NotMatchLog, string(data))
		nilOrPanic(err, "Invalid regular expression: ", c.NotMatchLog)
		assert(!match, "Expected log to _not_ match regular expression: ", c.NotMatchLog)
	}
}
コード例 #3
0
// Get the child process working.
// If we could create a new user, the working is the user home
// directory, otherwise it is the parent's working dir.
func getWorkingDir(u user, context *runtime.TaskContext) string {
	var cwd string
	var err error

	// In case of error we panic here because error at this point means
	// something is terribly wrong
	if u.name != "" {
		userInfo, err2 := osuser.Lookup(u.name)
		if err2 != nil {
			context.LogError("user.Lookup failed: ", err2, "\n")
			panic(err2)
		}
		cwd = userInfo.HomeDir
	} else {
		cwd, err = os.Getwd()
		if err != nil {
			context.LogError("Getwd failed: ", err, "\n")
			panic(err)
		}
	}

	return cwd
}