admin.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. "gogs.io/gogs/internal/conf"
  12. "gogs.io/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. },
  30. }
  31. subcmdCreateUser = cli.Command{
  32. Name: "create-user",
  33. Usage: "Create a new user in database",
  34. Action: runCreateUser,
  35. Flags: []cli.Flag{
  36. stringFlag("name", "", "Username"),
  37. stringFlag("password", "", "User password"),
  38. stringFlag("email", "", "User email address"),
  39. boolFlag("admin", "User is an admin"),
  40. stringFlag("config, c", "", "Custom configuration file path"),
  41. },
  42. }
  43. subcmdDeleteInactivateUsers = cli.Command{
  44. Name: "delete-inactive-users",
  45. Usage: "Delete all inactive accounts",
  46. Action: adminDashboardOperation(
  47. db.DeleteInactivateUsers,
  48. "All inactivate accounts have been deleted successfully",
  49. ),
  50. Flags: []cli.Flag{
  51. stringFlag("config, c", "", "Custom configuration file path"),
  52. },
  53. }
  54. subcmdDeleteRepositoryArchives = cli.Command{
  55. Name: "delete-repository-archives",
  56. Usage: "Delete all repositories archives",
  57. Action: adminDashboardOperation(
  58. db.DeleteRepositoryArchives,
  59. "All repositories archives have been deleted successfully",
  60. ),
  61. Flags: []cli.Flag{
  62. stringFlag("config, c", "", "Custom configuration file path"),
  63. },
  64. }
  65. subcmdDeleteMissingRepositories = cli.Command{
  66. Name: "delete-missing-repositories",
  67. Usage: "Delete all repository records that lost Git files",
  68. Action: adminDashboardOperation(
  69. db.DeleteMissingRepositories,
  70. "All repositories archives have been deleted successfully",
  71. ),
  72. Flags: []cli.Flag{
  73. stringFlag("config, c", "", "Custom configuration file path"),
  74. },
  75. }
  76. subcmdGitGcRepos = cli.Command{
  77. Name: "collect-garbage",
  78. Usage: "Do garbage collection on repositories",
  79. Action: adminDashboardOperation(
  80. db.GitGcRepos,
  81. "All repositories have done garbage collection successfully",
  82. ),
  83. Flags: []cli.Flag{
  84. stringFlag("config, c", "", "Custom configuration file path"),
  85. },
  86. }
  87. subcmdRewriteAuthorizedKeys = cli.Command{
  88. Name: "rewrite-authorized-keys",
  89. Usage: "Rewrite '.ssh/authorized_keys' file (caution: non-Gogs keys will be lost)",
  90. Action: adminDashboardOperation(
  91. db.RewriteAuthorizedKeys,
  92. "All public keys have been rewritten successfully",
  93. ),
  94. Flags: []cli.Flag{
  95. stringFlag("config, c", "", "Custom configuration file path"),
  96. },
  97. }
  98. subcmdSyncRepositoryHooks = cli.Command{
  99. Name: "resync-hooks",
  100. Usage: "Resync pre-receive, update and post-receive hooks",
  101. Action: adminDashboardOperation(
  102. db.SyncRepositoryHooks,
  103. "All repositories' pre-receive, update and post-receive hooks have been resynced successfully",
  104. ),
  105. Flags: []cli.Flag{
  106. stringFlag("config, c", "", "Custom configuration file path"),
  107. },
  108. }
  109. subcmdReinitMissingRepositories = cli.Command{
  110. Name: "reinit-missing-repositories",
  111. Usage: "Reinitialize all repository records that lost Git files",
  112. Action: adminDashboardOperation(
  113. db.ReinitMissingRepositories,
  114. "All repository records that lost Git files have been reinitialized successfully",
  115. ),
  116. Flags: []cli.Flag{
  117. stringFlag("config, c", "", "Custom configuration file path"),
  118. },
  119. }
  120. )
  121. func runCreateUser(c *cli.Context) error {
  122. if !c.IsSet("name") {
  123. return errors.New("Username is not specified")
  124. } else if !c.IsSet("password") {
  125. return errors.New("Password is not specified")
  126. } else if !c.IsSet("email") {
  127. return errors.New("Email is not specified")
  128. }
  129. err := conf.Init(c.String("config"))
  130. if err != nil {
  131. return errors.Wrap(err, "init configuration")
  132. }
  133. conf.InitLogging(true)
  134. if _, err = db.SetEngine(); err != nil {
  135. return errors.Wrap(err, "set engine")
  136. }
  137. if err := db.CreateUser(&db.User{
  138. Name: c.String("name"),
  139. Email: c.String("email"),
  140. Passwd: c.String("password"),
  141. IsActive: true,
  142. IsAdmin: c.Bool("admin"),
  143. }); err != nil {
  144. return fmt.Errorf("CreateUser: %v", err)
  145. }
  146. fmt.Printf("New user '%s' has been successfully created!\n", c.String("name"))
  147. return nil
  148. }
  149. func adminDashboardOperation(operation func() error, successMessage string) func(*cli.Context) error {
  150. return func(c *cli.Context) error {
  151. err := conf.Init(c.String("config"))
  152. if err != nil {
  153. return errors.Wrap(err, "init configuration")
  154. }
  155. conf.InitLogging(true)
  156. if _, err = db.SetEngine(); err != nil {
  157. return errors.Wrap(err, "set engine")
  158. }
  159. if err := operation(); err != nil {
  160. functionName := runtime.FuncForPC(reflect.ValueOf(operation).Pointer()).Name()
  161. return fmt.Errorf("%s: %v", functionName, err)
  162. }
  163. fmt.Printf("%s\n", successMessage)
  164. return nil
  165. }
  166. }