Files

144 lines
4.3 KiB
Go

package isolation
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"os"
"os/user"
"path/filepath"
"regexp"
"sea-vhost/internal/hosting"
"strconv"
"strings"
)
const startupStateRoot = "/var/lib/sea-vhost-agent/startup"
type StartupSpec struct {
Project ProjectSpec `json:"project"`
SiteName string `json:"siteName"`
Runtime string `json:"runtime"`
Version string `json:"version"`
Port int `json:"port"`
Config hosting.Startup `json:"config"`
}
type StartupManager interface {
ConfigureStartup(context.Context, StartupSpec) error
}
func (c *Client) ConfigureStartup(ctx context.Context, v StartupSpec) error {
return c.request(ctx, "/v1/site/startup", v, nil)
}
func (Mock) ConfigureStartup(context.Context, StartupSpec) error { return nil }
func (a Agent) configureStartup(w http.ResponseWriter, r *http.Request) {
var v StartupSpec
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if d.Decode(&v) != nil || d.Decode(&struct{}{}) != io.EOF || !phpSiteNamePattern.MatchString(v.SiteName) || v.Config.Validate() != nil || !regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,39}$`).MatchString(v.Version) {
http.Error(w, "invalid startup settings", 400)
return
}
if e := a.prepareStartup(r.Context(), v); e != nil {
http.Error(w, "cannot prepare safe application launcher", 422)
return
}
w.WriteHeader(200)
}
func (a Agent) prepareStartup(ctx context.Context, v StartupSpec) error {
if v.SiteName != v.Project.HostName && !strings.HasPrefix(v.SiteName, v.Project.HostName+"_") {
return errors.New("site identity mismatch")
}
root, e := a.projectPath(v.Project)
if e != nil {
return e
}
working, e := physicalRunDirectory(root, v.Project.RunPath)
if e != nil {
return e
}
cwd := working
if v.Config.WorkDir != "." && v.Config.WorkDir != "" {
cwd, e = physicalRunDirectory(root, filepath.ToSlash(filepath.Join(v.Project.RunPath, v.Config.WorkDir)))
if e != nil {
return e
}
}
account, e := user.Lookup(v.Project.HostName)
if e != nil {
return e
}
uid, e := strconv.ParseUint(account.Uid, 10, 32)
if e != nil || uid == 0 {
return errors.New("invalid uid")
}
entry := filepath.Join(working, v.Config.EntryFile)
var binary string
var args []string
switch v.Runtime {
case "PHP":
binary = filepath.Join("/www/server/php", strings.ReplaceAll(v.Version, ".", ""), "bin/php")
case "Node.js":
binary = filepath.Join("/www/server/nodejs", v.Version, "bin/npm")
args = []string{"run", "start", "--"}
case "Python":
binary = filepath.Join(working, ".sea-runtime/python", v.SiteName+"-"+v.Version, "bin/python3")
args = []string{entry}
case "Go":
binary = entry
case "Java":
binary = filepath.Join("/www/server/java", v.Version, "bin/java")
args = []string{"-jar", entry, "--server.port=" + strconv.Itoa(v.Port)}
default:
return errors.New("invalid runtime")
}
if i, e := os.Stat(binary); e != nil || !i.Mode().IsRegular() || i.Mode()&0111 == 0 {
return errors.New("runtime executable not found")
}
args = append(args, v.Config.Arguments...)
if e = ensureProjectRunDirectory(root, "/.sea-runtime/logs"); e != nil {
return e
}
logdir, e := physicalRunDirectory(root, "/.sea-runtime/logs")
if e != nil {
return e
}
if e = a.command(ctx, "chown", a.fileServiceOwner()+":"+v.Project.HostName, logdir); e != nil {
return e
}
if e = a.command(ctx, "chmod", "0770", logdir); e != nil {
return e
}
if e = a.command(ctx, "setfacl", "-m", "u:www:---,m::rwx,o::---", logdir); e != nil {
return e
}
if e = ensurePhysicalDirectory(startupStateRoot, 0711); e != nil {
return e
}
if e = os.Chmod(startupStateRoot, 0711); e != nil {
return e
}
config := hosting.RunnerConfig{UID: uint32(uid), HostName: v.Project.HostName, Binary: binary, Arguments: args, Environment: v.Config.Environment, WorkDir: cwd, LogDir: logdir, SiteName: v.SiteName}
if config.Environment == nil {
config.Environment = map[string]string{}
}
config.Environment["PORT"] = strconv.Itoa(v.Port)
raw, _ := json.Marshal(config)
file := filepath.Join(startupStateRoot, v.SiteName+".json")
if e = writePrivateFile(file, raw); e != nil {
return e
}
if e = a.command(ctx, "chown", "root:"+v.Project.HostName, file); e != nil {
return e
}
return a.command(ctx, "chmod", "0640", file)
}
func (a Agent) fileServiceOwner() string {
if a.FileOwner != "" {
return a.FileOwner
}
return "sea-vhost"
}