mirror of
https://github.com/criyle/go-judge.git
synced 2025-11-04 14:50:02 +08:00
51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
package pool
|
|
|
|
import (
|
|
"fmt"
|
|
"syscall"
|
|
|
|
"github.com/criyle/go-sandbox/container"
|
|
)
|
|
|
|
type environmentBuilder struct {
|
|
builder EnvironmentBuilder
|
|
cgPool CgroupPool
|
|
workDir string
|
|
cpuset string
|
|
cpuRate bool
|
|
}
|
|
|
|
// NewEnvBuilder creates builder for linux container pools
|
|
func NewEnvBuilder(builder EnvironmentBuilder, cgPool CgroupPool, workDir, cpuset string, cpuRate bool) EnvBuilder {
|
|
return &environmentBuilder{
|
|
builder: builder,
|
|
cgPool: cgPool,
|
|
workDir: workDir,
|
|
cpuset: cpuset,
|
|
cpuRate: cpuRate,
|
|
}
|
|
}
|
|
|
|
// Build creates linux container
|
|
func (b *environmentBuilder) Build() (Environment, error) {
|
|
m, err := b.builder.Build()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
wd, err := m.Open([]container.OpenCmd{{
|
|
Path: b.workDir,
|
|
Flag: syscall.O_CLOEXEC | syscall.O_DIRECTORY,
|
|
Perm: 0777,
|
|
}})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("container: failed to prepare work directory")
|
|
}
|
|
return &environ{
|
|
Environment: m,
|
|
cgPool: b.cgPool,
|
|
wd: wd[0],
|
|
cpuset: b.cpuset,
|
|
cpuRate: b.cpuRate,
|
|
}, nil
|
|
}
|