admin.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package cmd
  5. import (
  6. "context"
  7. "fmt"
  8. "reflect"
  9. "runtime"
  10. "github.com/pkg/errors"
  11. "github.com/urfave/cli"
  12. "gogs.io/gogs/internal/conf"
  13. "gogs.io/gogs/internal/db"
  14. )
  15. var (
  16. Admin = cli.Command{
  17. Name: "admin",
  18. Usage: "Perform admin operations on command line",
  19. Description: `Allow using internal logic of Gogs without hacking into the source code
  20. to make automatic initialization process more smoothly`,
  21. Subcommands: []cli.Command{
  22. subcmdCreateUser,
  23. subcmdDeleteInactivateUsers,
  24. subcmdDeleteRepositoryArchives,
  25. subcmdDeleteMissingRepositories,
  26. subcmdGitGcRepos,
  27. subcmdRewriteAuthorizedKeys,
  28. subcmdSyncRepositoryHooks,
  29. subcmdReinitMissingRepositories,
  30. },
  31. }
  32. subcmdCreateUser = cli.Command{
  33. Name: "create-user",
  34. Usage: "Create a new user in database",
  35. Action: runCreateUser,
  36. Flags: []cli.Flag{
  37. stringFlag("name", "", "Username"),
  38. stringFlag("password", "", "User password"),
  39. stringFlag("email", "", "User email address"),
  40. boolFlag("admin", "User is an admin"),
  41. stringFlag("config, c", "", "Custom configuration file path"),
  42. },
  43. }
  44. subcmdDeleteInactivateUsers = cli.Command{
  45. Name: "delete-inactive-users",
  46. Usage: "Delete all inactive accounts",
  47. Action: adminDashboardOperation(
  48. func() error { return db.Users.DeleteInactivated() },
  49. "All inactivated accounts have been deleted successfully",
  50. ),
  51. Flags: []cli.Flag{
  52. stringFlag("config, c", "", "Custom configuration file path"),
  53. },
  54. }
  55. subcmdDeleteRepositoryArchives = cli.Command{
  56. Name: "delete-repository-archives",
  57. Usage: "Delete all repositories archives",
  58. Action: adminDashboardOperation(
  59. db.DeleteRepositoryArchives,
  60. "All repositories archives have been deleted successfully",
  61. ),
  62. Flags: []cli.Flag{
  63. stringFlag("config, c", "", "Custom configuration file path"),
  64. },
  65. }
  66. subcmdDeleteMissingRepositories = cli.Command{
  67. Name: "delete-missing-repositories",
  68. Usage: "Delete all repository records that lost Git files",
  69. Action: adminDashboardOperation(
  70. db.DeleteMissingRepositories,
  71. "All repositories archives have been deleted successfully",
  72. ),
  73. Flags: []cli.Flag{
  74. stringFlag("config, c", "", "Custom configuration file path"),
  75. },
  76. }
  77. subcmdGitGcRepos = cli.Command{
  78. Name: "collect-garbage",
  79. Usage: "Do garbage collection on repositories",
  80. Action: adminDashboardOperation(
  81. db.GitGcRepos,
  82. "All repositories have done garbage collection successfully",
  83. ),
  84. Flags: []cli.Flag{
  85. stringFlag("config, c", "", "Custom configuration file path"),
  86. },
  87. }
  88. subcmdRewriteAuthorizedKeys = cli.Command{
  89. Name: "rewrite-authorized-keys",
  90. Usage: "Rewrite '.ssh/authorized_keys' file (caution: non-Gogs keys will be lost)",
  91. Action: adminDashboardOperation(
  92. db.RewriteAuthorizedKeys,
  93. "All public keys have been rewritten successfully",
  94. ),
  95. Flags: []cli.Flag{
  96. stringFlag("config, c", "", "Custom configuration file path"),
  97. },
  98. }
  99. subcmdSyncRepositoryHooks = cli.Command{
  100. Name: "resync-hooks",
  101. Usage: "Resync pre-receive, update and post-receive hooks",
  102. Action: adminDashboardOperation(
  103. db.SyncRepositoryHooks,
  104. "All repositories' pre-receive, update and post-receive hooks have been resynced successfully",
  105. ),
  106. Flags: []cli.Flag{
  107. stringFlag("config, c", "", "Custom configuration file path"),
  108. },
  109. }
  110. subcmdReinitMissingRepositories = cli.Command{
  111. Name: "reinit-missing-repositories",
  112. Usage: "Reinitialize all repository records that lost Git files",
  113. Action: adminDashboardOperation(
  114. db.ReinitMissingRepositories,
  115. "All repository records that lost Git files have been reinitialized successfully",
  116. ),
  117. Flags: []cli.Flag{
  118. stringFlag("config, c", "", "Custom configuration file path"),
  119. },
  120. }
  121. )
  122. func runCreateUser(c *cli.Context) error {
  123. if !c.IsSet("name") {
  124. return errors.New("Username is not specified")
  125. } else if !c.IsSet("password") {
  126. return errors.New("Password is not specified")
  127. } else if !c.IsSet("email") {
  128. return errors.New("Email is not specified")
  129. }
  130. err := conf.Init(c.String("config"))
  131. if err != nil {
  132. return errors.Wrap(err, "init configuration")
  133. }
  134. conf.InitLogging(true)
  135. if _, err = db.SetEngine(); err != nil {
  136. return errors.Wrap(err, "set engine")
  137. }
  138. user, err := db.Users.Create(
  139. context.Background(),
  140. c.String("name"),
  141. c.String("email"),
  142. db.CreateUserOptions{
  143. Password: c.String("password"),
  144. Activated: true,
  145. Admin: c.Bool("admin"),
  146. },
  147. )
  148. if err != nil {
  149. return errors.Wrap(err, "create user")
  150. }
  151. fmt.Printf("New user %q has been successfully created!\n", user.Name)
  152. return nil
  153. }
  154. func adminDashboardOperation(operation func() error, successMessage string) func(*cli.Context) error {
  155. return func(c *cli.Context) error {
  156. err := conf.Init(c.String("config"))
  157. if err != nil {
  158. return errors.Wrap(err, "init configuration")
  159. }
  160. conf.InitLogging(true)
  161. if _, err = db.SetEngine(); err != nil {
  162. return errors.Wrap(err, "set engine")
  163. }
  164. if err := operation(); err != nil {
  165. functionName := runtime.FuncForPC(reflect.ValueOf(operation).Pointer()).Name()
  166. return fmt.Errorf("%s: %v", functionName, err)
  167. }
  168. fmt.Printf("%s\n", successMessage)
  169. return nil
  170. }
  171. }