You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
5.6 KiB

  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package cmd
  6. import (
  7. "fmt"
  8. "code.gitea.io/git"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/urfave/cli"
  13. )
  14. var (
  15. // CmdAdmin represents the available admin sub-command.
  16. CmdAdmin = cli.Command{
  17. Name: "admin",
  18. Usage: "Command line interface to perform common administrative operations",
  19. Subcommands: []cli.Command{
  20. subcmdCreateUser,
  21. subcmdChangePassword,
  22. subcmdRepoSyncReleases,
  23. subcmdRegenerate,
  24. },
  25. }
  26. subcmdCreateUser = cli.Command{
  27. Name: "create-user",
  28. Usage: "Create a new user in database",
  29. Action: runCreateUser,
  30. Flags: []cli.Flag{
  31. cli.StringFlag{
  32. Name: "name",
  33. Usage: "Username",
  34. },
  35. cli.StringFlag{
  36. Name: "password",
  37. Usage: "User password",
  38. },
  39. cli.StringFlag{
  40. Name: "email",
  41. Usage: "User email address",
  42. },
  43. cli.BoolFlag{
  44. Name: "admin",
  45. Usage: "User is an admin",
  46. },
  47. cli.StringFlag{
  48. Name: "config, c",
  49. Value: "custom/conf/app.ini",
  50. Usage: "Custom configuration file path",
  51. },
  52. },
  53. }
  54. subcmdChangePassword = cli.Command{
  55. Name: "change-password",
  56. Usage: "Change a user's password",
  57. Action: runChangePassword,
  58. Flags: []cli.Flag{
  59. cli.StringFlag{
  60. Name: "username,u",
  61. Value: "",
  62. Usage: "The user to change password for",
  63. },
  64. cli.StringFlag{
  65. Name: "password,p",
  66. Value: "",
  67. Usage: "New password to set for user",
  68. },
  69. },
  70. }
  71. subcmdRepoSyncReleases = cli.Command{
  72. Name: "repo-sync-releases",
  73. Usage: "Synchronize repository releases with tags",
  74. Action: runRepoSyncReleases,
  75. }
  76. subcmdRegenerate = cli.Command{
  77. Name: "regenerate",
  78. Usage: "Regenerate specific files",
  79. Subcommands: []cli.Command{
  80. microcmdRegenHooks,
  81. microcmdRegenKeys,
  82. },
  83. }
  84. microcmdRegenHooks = cli.Command{
  85. Name: "hooks",
  86. Usage: "Regenerate git-hooks",
  87. Action: runRegenerateHooks,
  88. Flags: []cli.Flag{
  89. cli.StringFlag{
  90. Name: "config, c",
  91. Value: "custom/conf/app.ini",
  92. Usage: "Custom configuration file path",
  93. },
  94. },
  95. }
  96. microcmdRegenKeys = cli.Command{
  97. Name: "keys",
  98. Usage: "Regenerate authorized_keys file",
  99. Action: runRegenerateKeys,
  100. Flags: []cli.Flag{
  101. cli.StringFlag{
  102. Name: "config, c",
  103. Value: "custom/conf/app.ini",
  104. Usage: "Custom configuration file path",
  105. },
  106. },
  107. }
  108. )
  109. func runChangePassword(c *cli.Context) error {
  110. if err := argsSet(c, "username", "password"); err != nil {
  111. return err
  112. }
  113. if err := initDB(); err != nil {
  114. return err
  115. }
  116. uname := c.String("username")
  117. user, err := models.GetUserByName(uname)
  118. if err != nil {
  119. return err
  120. }
  121. if user.Salt, err = models.GetUserSalt(); err != nil {
  122. return err
  123. }
  124. user.HashPassword(c.String("password"))
  125. if err := models.UpdateUserCols(user, "passwd", "salt"); err != nil {
  126. return err
  127. }
  128. fmt.Printf("%s's password has been successfully updated!\n", user.Name)
  129. return nil
  130. }
  131. func runCreateUser(c *cli.Context) error {
  132. if err := argsSet(c, "name", "password", "email"); err != nil {
  133. return err
  134. }
  135. if c.IsSet("config") {
  136. setting.CustomConf = c.String("config")
  137. }
  138. if err := initDB(); err != nil {
  139. return err
  140. }
  141. if err := models.CreateUser(&models.User{
  142. Name: c.String("name"),
  143. Email: c.String("email"),
  144. Passwd: c.String("password"),
  145. IsActive: true,
  146. IsAdmin: c.Bool("admin"),
  147. }); err != nil {
  148. return fmt.Errorf("CreateUser: %v", err)
  149. }
  150. fmt.Printf("New user '%s' has been successfully created!\n", c.String("name"))
  151. return nil
  152. }
  153. func runRepoSyncReleases(c *cli.Context) error {
  154. if err := initDB(); err != nil {
  155. return err
  156. }
  157. log.Trace("Synchronizing repository releases (this may take a while)")
  158. for page := 1; ; page++ {
  159. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  160. Page: page,
  161. PageSize: models.RepositoryListDefaultPageSize,
  162. Private: true,
  163. })
  164. if err != nil {
  165. return fmt.Errorf("SearchRepositoryByName: %v", err)
  166. }
  167. if len(repos) == 0 {
  168. break
  169. }
  170. log.Trace("Processing next %d repos of %d", len(repos), count)
  171. for _, repo := range repos {
  172. log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath())
  173. gitRepo, err := git.OpenRepository(repo.RepoPath())
  174. if err != nil {
  175. log.Warn("OpenRepository: %v", err)
  176. continue
  177. }
  178. oldnum, err := getReleaseCount(repo.ID)
  179. if err != nil {
  180. log.Warn(" GetReleaseCountByRepoID: %v", err)
  181. }
  182. log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)
  183. if err = models.SyncReleasesWithTags(repo, gitRepo); err != nil {
  184. log.Warn(" SyncReleasesWithTags: %v", err)
  185. continue
  186. }
  187. count, err = getReleaseCount(repo.ID)
  188. if err != nil {
  189. log.Warn(" GetReleaseCountByRepoID: %v", err)
  190. continue
  191. }
  192. log.Trace(" repo %s releases synchronized to tags: from %d to %d",
  193. repo.FullName(), oldnum, count)
  194. }
  195. }
  196. return nil
  197. }
  198. func getReleaseCount(id int64) (int64, error) {
  199. return models.GetReleaseCountByRepoID(
  200. id,
  201. models.FindReleasesOptions{
  202. IncludeTags: true,
  203. },
  204. )
  205. }
  206. func runRegenerateHooks(c *cli.Context) error {
  207. if c.IsSet("config") {
  208. setting.CustomConf = c.String("config")
  209. }
  210. if err := initDB(); err != nil {
  211. return err
  212. }
  213. return models.SyncRepositoryHooks()
  214. }
  215. func runRegenerateKeys(c *cli.Context) error {
  216. if c.IsSet("config") {
  217. setting.CustomConf = c.String("config")
  218. }
  219. if err := initDB(); err != nil {
  220. return err
  221. }
  222. return models.RewriteAllPublicKeys()
  223. }