admin.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. "fmt"
  7. "reflect"
  8. "runtime"
  9. "github.com/pkg/errors"
  10. "github.com/urfave/cli"
  11. "github.com/G-Node/gogs/internal/conf"
  12. "github.com/G-Node/gogs/internal/db"
  13. )
  14. var (
  15. Admin = cli.Command{
  16. Name: "admin",
  17. Usage: "Perform admin operations on command line",
  18. Description: `Allow using internal logic of Gogs without hacking into the source code
  19. to make automatic initialization process more smoothly`,
  20. Subcommands: []cli.Command{
  21. subcmdCreateUser,
  22. subcmdDeleteInactivateUsers,
  23. subcmdDeleteRepositoryArchives,
  24. subcmdDeleteMissingRepositories,
  25. subcmdGitGcRepos,
  26. subcmdRewriteAuthorizedKeys,
  27. subcmdSyncRepositoryHooks,
  28. subcmdReinitMissingRepositories,
  29. subcmdRebuildSearchIndex, // GIN search feature
  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. db.DeleteInactivateUsers,
  49. "All inactivate 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. if err := db.CreateUser(&db.User{
  139. Name: c.String("name"),
  140. Email: c.String("email"),
  141. Passwd: c.String("password"),
  142. IsActive: true,
  143. IsAdmin: c.Bool("admin"),
  144. }); err != nil {
  145. return fmt.Errorf("CreateUser: %v", err)
  146. }
  147. fmt.Printf("New user '%s' has been successfully created!\n", c.String("name"))
  148. return nil
  149. }
  150. func adminDashboardOperation(operation func() error, successMessage string) func(*cli.Context) error {
  151. return func(c *cli.Context) error {
  152. err := conf.Init(c.String("config"))
  153. if err != nil {
  154. return errors.Wrap(err, "init configuration")
  155. }
  156. conf.InitLogging(true)
  157. if _, err = db.SetEngine(); err != nil {
  158. return errors.Wrap(err, "set engine")
  159. }
  160. if err := operation(); err != nil {
  161. functionName := runtime.FuncForPC(reflect.ValueOf(operation).Pointer()).Name()
  162. return fmt.Errorf("%s: %v", functionName, err)
  163. }
  164. fmt.Printf("%s\n", successMessage)
  165. return nil
  166. }
  167. }