move default uncloud socket to /run/uncloud/uncloud.sock

This commit is contained in:
Pavel Sviderski
2024-11-04 20:02:48 +10:00
parent a54666a7ac
commit 17b96a8744
2 changed files with 33 additions and 18 deletions
+5 -5
View File
@@ -18,7 +18,7 @@ type SSHConnectorConfig struct {
Port int Port int
KeyPath string KeyPath string
APISockPath string SockPath string
} }
// SSHConnector establishes a connection to the machine API through an SSH tunnel to the machine. // SSHConnector establishes a connection to the machine API through an SSH tunnel to the machine.
@@ -49,12 +49,12 @@ func (c *SSHConnector) Connect(ctx context.Context) (*grpc.ClientConn, error) {
} }
} }
apiSockPath := c.config.APISockPath sockPath := c.config.SockPath
if apiSockPath == "" { if sockPath == "" {
apiSockPath = machine.DefaultAPISockPath sockPath = machine.DefaultUncloudSockPath
} }
conn, err := grpc.NewClient( conn, err := grpc.NewClient(
"unix://"+apiSockPath, "unix://"+sockPath,
grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer( grpc.WithContextDialer(
func(ctx context.Context, addr string) (net.Conn, error) { func(ctx context.Context, addr string) (net.Conn, error) {
+28 -13
View File
@@ -28,14 +28,16 @@ import (
) )
const ( const (
DefaultAPISockPath = "/run/uncloud.sock" DefaultMachineSockPath = "/run/uncloud/machine.sock"
DefaultAPISockGroup = "uncloud" DefaultUncloudSockPath = "/run/uncloud/uncloud.sock"
DefaultUncloudSockGroup = "uncloud"
) )
type Config struct { type Config struct {
// DataDir is the directory where the machine stores its persistent state. // DataDir is the directory where the machine stores its persistent state.
DataDir string DataDir string
APISockPath string MachineSockPath string
UncloudSockPath string
CorrosionDir string CorrosionDir string
CorrosionAPIListenAddr netip.AddrPort CorrosionAPIListenAddr netip.AddrPort
@@ -51,8 +53,11 @@ func (c *Config) SetDefaults() *Config {
if cfg.DataDir == "" { if cfg.DataDir == "" {
cfg.DataDir = "/var/lib/uncloud" cfg.DataDir = "/var/lib/uncloud"
} }
if cfg.APISockPath == "" { if cfg.MachineSockPath == "" {
cfg.APISockPath = DefaultAPISockPath cfg.MachineSockPath = DefaultMachineSockPath
}
if cfg.UncloudSockPath == "" {
cfg.UncloudSockPath = DefaultUncloudSockPath
} }
if cfg.CorrosionDir == "" { if cfg.CorrosionDir == "" {
cfg.CorrosionDir = filepath.Join(cfg.DataDir, "corrosion") cfg.CorrosionDir = filepath.Join(cfg.DataDir, "corrosion")
@@ -191,13 +196,14 @@ func (m *Machine) Run(ctx context.Context) error {
errGroup, ctx := errgroup.WithContext(ctx) errGroup, ctx := errgroup.WithContext(ctx)
// Start the machine local API server. // Start the machine local API server.
localListener, err := listenUnixSocket(m.config.APISockPath) // TODO: start this on machine.sock and start grpc-proxy on uncloud.sock. Use 700 mode for machine.sock.
localListener, err := listenUnixSocket(m.config.UncloudSockPath)
if err != nil { if err != nil {
return fmt.Errorf("listen API unix socket %q: %w", m.config.APISockPath, err) return fmt.Errorf("listen API unix socket %q: %w", m.config.UncloudSockPath, err)
} }
errGroup.Go( errGroup.Go(
func() error { func() error {
slog.Info("Starting local API server.", "path", m.config.APISockPath) slog.Info("Starting local API server.", "path", m.config.UncloudSockPath)
if err := m.localServer.Serve(localListener); err != nil { if err := m.localServer.Serve(localListener); err != nil {
return fmt.Errorf("local API server failed: %w", err) return fmt.Errorf("local API server failed: %w", err)
} }
@@ -286,24 +292,33 @@ func (m *Machine) Run(ctx context.Context) error {
// access mode and uncloud group if the group is found, otherwise it falls back to the root group. // access mode and uncloud group if the group is found, otherwise it falls back to the root group.
func listenUnixSocket(path string) (net.Listener, error) { func listenUnixSocket(path string) (net.Listener, error) {
gid := 0 // Fall back to the root group if the uncloud group is not found. gid := 0 // Fall back to the root group if the uncloud group is not found.
group, err := user.LookupGroup(DefaultAPISockGroup) group, err := user.LookupGroup(DefaultUncloudSockGroup)
if err != nil { if err != nil {
//goland:noinspection GoTypeAssertionOnErrors //goland:noinspection GoTypeAssertionOnErrors
if _, ok := err.(user.UnknownGroupError); ok { if _, ok := err.(user.UnknownGroupError); ok {
slog.Info( slog.Info(
"Specified group not found, using root group for the API socket.", "Specified group not found, using root group for the API socket.",
"group", DefaultAPISockGroup, "path", path, "group", DefaultUncloudSockGroup, "path", path,
) )
} else { } else {
return nil, fmt.Errorf("lookup %q group ID (GID): %w", DefaultAPISockGroup, err) return nil, fmt.Errorf("lookup %q group ID (GID): %w", DefaultUncloudSockGroup, err)
} }
} else { } else {
gid, err = strconv.Atoi(group.Gid) gid, err = strconv.Atoi(group.Gid)
if err != nil { if err != nil {
return nil, fmt.Errorf("parse %q group ID (GID) %q: %w", DefaultAPISockGroup, group.Gid, err) return nil, fmt.Errorf("parse %q group ID (GID) %q: %w", DefaultUncloudSockGroup, group.Gid, err)
} }
} }
// Ensure the parent directory exists and has the correct group permissions.
parent, _ := filepath.Split(path)
if err = os.MkdirAll(parent, 0750); err != nil {
return nil, fmt.Errorf("create directory %q: %w", parent, err)
}
if err = os.Chown(parent, -1, gid); err != nil {
return nil, fmt.Errorf("chown directory %q: %w", parent, err)
}
return sockets.NewUnixSocket(path, gid) return sockets.NewUnixSocket(path, gid)
} }