MCPcopy Create free account
hub / github.com/celer-pkg/celer / createSystemGroupAndUser

Function createSystemGroupAndUser

pkgcache/cache_setup.go:429–490  ·  view source on GitHub ↗

createSystemGroupAndUser creates the celer system user/group if it does not already exist. If requiredGID is set, the group must use that numeric gid.

(username, requiredGID string)

Source from the content-addressed store, hash-verified

427// createSystemGroupAndUser creates the celer system user/group if it does not already exist.
428// If requiredGID is set, the group must use that numeric gid.
429func createSystemGroupAndUser(username, requiredGID string) error {
430 // Create system group if not exist.
431 groupExists := true
432 group, err := lookupGroup(username)
433 if err != nil {
434 var unknownGroupError user.UnknownGroupError
435 if !errors.As(err, &unknownGroupError) {
436 return fmt.Errorf("failed to lookup %s group -> %w", username, err)
437 }
438 groupExists = false
439 }
440
441 // Change local GID as the same of remote GID.
442 if groupExists {
443 if requiredGID != "" && group.Gid != requiredGID {
444 if err := changeGroupGID(username, group.Gid, requiredGID); err != nil {
445 return err
446 }
447 } else {
448 color.PrintHint("✔ system group exists: %s", username)
449 }
450 } else {
451 var output string
452 var err error
453
454 if requiredGID != "" {
455 if err := ensureGroupIDUnused(requiredGID, username); err != nil {
456 return err
457 }
458 output, err = cmd.NewExecutor("", "groupadd", "--system", "--gid", requiredGID, username).ExecuteOutput()
459 } else {
460 output, err = cmd.NewExecutor("", "groupadd", "--system", username).ExecuteOutput()
461 }
462 if err != nil {
463 return fmt.Errorf("failed to create %s group -> %s -> %w", username, output, err)
464 }
465 color.PrintHint("✔ create system group: %s", username)
466 }
467
468 // Check if celer user is created.
469 userExists := true
470 if _, err := lookupUser(username); err != nil {
471 var unknownUserError user.UnknownUserError
472 if !errors.As(err, &unknownUserError) {
473 return fmt.Errorf("failed to lookup %s user -> %w", username, err)
474 }
475 userExists = false
476 }
477 if userExists {
478 color.PrintHint("✔ system user exists: %s", username)
479 return nil
480 }
481
482 // Create system user (no login shell, no home directory).
483 args := []string{"--system", "--no-create-home", "--shell", "/usr/sbin/nologin", "--gid", username, username}
484 if output, err := cmd.NewExecutor("", "useradd", args...).ExecuteOutput(); err != nil {
485 return fmt.Errorf("failed to create %s user -> %s -> %w", username, output, err)
486 }

Callers 2

SetupMethod · 0.85
SetupMethod · 0.85

Calls 5

PrintHintFunction · 0.92
NewExecutorFunction · 0.92
changeGroupGIDFunction · 0.85
ensureGroupIDUnusedFunction · 0.85
ExecuteOutputMethod · 0.80

Tested by

no test coverage detected