2016-08-14 01:11:52 +02:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2016-12-21 13:13:17 +01:00
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2016-08-14 01:11:52 +02:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-12-31 15:45:46 +01:00
|
|
|
"code.gitea.io/git"
|
2016-11-10 17:24:48 +01:00
|
|
|
"code.gitea.io/gitea/models"
|
2017-12-31 15:45:46 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2016-11-10 17:24:48 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-04-12 09:44:54 +02:00
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
2016-08-14 01:11:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-11-04 12:42:18 +01:00
|
|
|
// CmdAdmin represents the available admin sub-command.
|
2016-08-14 01:11:52 +02:00
|
|
|
CmdAdmin = cli.Command{
|
|
|
|
Name: "admin",
|
2018-01-12 23:16:49 +01:00
|
|
|
Usage: "Command line interface to perform common administrative operations",
|
2016-08-14 01:11:52 +02:00
|
|
|
Subcommands: []cli.Command{
|
|
|
|
subcmdCreateUser,
|
2017-03-20 09:23:38 +01:00
|
|
|
subcmdChangePassword,
|
2017-12-31 15:45:46 +01:00
|
|
|
subcmdRepoSyncReleases,
|
2018-05-17 03:35:07 +02:00
|
|
|
subcmdRegenerate,
|
2016-08-14 01:11:52 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
subcmdCreateUser = cli.Command{
|
|
|
|
Name: "create-user",
|
|
|
|
Usage: "Create a new user in database",
|
|
|
|
Action: runCreateUser,
|
|
|
|
Flags: []cli.Flag{
|
2016-11-09 23:18:22 +01:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "name",
|
|
|
|
Usage: "Username",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "password",
|
2016-11-09 23:32:24 +01:00
|
|
|
Usage: "User password",
|
2016-11-09 23:18:22 +01:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "email",
|
2016-11-09 23:32:24 +01:00
|
|
|
Usage: "User email address",
|
2016-11-09 23:18:22 +01:00
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "admin",
|
|
|
|
Usage: "User is an admin",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: "custom/conf/app.ini",
|
|
|
|
Usage: "Custom configuration file path",
|
|
|
|
},
|
2016-08-14 01:11:52 +02:00
|
|
|
},
|
|
|
|
}
|
2017-03-20 09:23:38 +01:00
|
|
|
|
|
|
|
subcmdChangePassword = cli.Command{
|
|
|
|
Name: "change-password",
|
|
|
|
Usage: "Change a user's password",
|
|
|
|
Action: runChangePassword,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "username,u",
|
|
|
|
Value: "",
|
|
|
|
Usage: "The user to change password for",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "password,p",
|
|
|
|
Value: "",
|
|
|
|
Usage: "New password to set for user",
|
|
|
|
},
|
2018-06-10 00:35:14 +02:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: "custom/conf/app.ini",
|
|
|
|
Usage: "Custom configuration file path",
|
|
|
|
},
|
2017-03-20 09:23:38 +01:00
|
|
|
},
|
|
|
|
}
|
2017-12-31 15:45:46 +01:00
|
|
|
|
|
|
|
subcmdRepoSyncReleases = cli.Command{
|
|
|
|
Name: "repo-sync-releases",
|
|
|
|
Usage: "Synchronize repository releases with tags",
|
|
|
|
Action: runRepoSyncReleases,
|
|
|
|
}
|
2018-05-17 03:35:07 +02:00
|
|
|
|
|
|
|
subcmdRegenerate = cli.Command{
|
|
|
|
Name: "regenerate",
|
|
|
|
Usage: "Regenerate specific files",
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
microcmdRegenHooks,
|
|
|
|
microcmdRegenKeys,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
microcmdRegenHooks = cli.Command{
|
|
|
|
Name: "hooks",
|
|
|
|
Usage: "Regenerate git-hooks",
|
|
|
|
Action: runRegenerateHooks,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: "custom/conf/app.ini",
|
|
|
|
Usage: "Custom configuration file path",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
microcmdRegenKeys = cli.Command{
|
|
|
|
Name: "keys",
|
|
|
|
Usage: "Regenerate authorized_keys file",
|
|
|
|
Action: runRegenerateKeys,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: "custom/conf/app.ini",
|
|
|
|
Usage: "Custom configuration file path",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-08-14 01:11:52 +02:00
|
|
|
)
|
|
|
|
|
2017-03-20 09:23:38 +01:00
|
|
|
func runChangePassword(c *cli.Context) error {
|
2018-01-12 23:16:49 +01:00
|
|
|
if err := argsSet(c, "username", "password"); err != nil {
|
|
|
|
return err
|
2017-03-20 09:23:38 +01:00
|
|
|
}
|
|
|
|
|
2018-06-10 00:35:14 +02:00
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
}
|
|
|
|
|
2018-01-12 23:16:49 +01:00
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
2017-03-20 09:23:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uname := c.String("username")
|
|
|
|
user, err := models.GetUserByName(uname)
|
|
|
|
if err != nil {
|
2018-01-12 23:16:49 +01:00
|
|
|
return err
|
2017-03-20 09:23:38 +01:00
|
|
|
}
|
|
|
|
if user.Salt, err = models.GetUserSalt(); err != nil {
|
2018-01-12 23:16:49 +01:00
|
|
|
return err
|
2017-03-20 09:23:38 +01:00
|
|
|
}
|
2018-01-11 23:19:38 +01:00
|
|
|
user.HashPassword(c.String("password"))
|
2017-08-12 16:18:44 +02:00
|
|
|
if err := models.UpdateUserCols(user, "passwd", "salt"); err != nil {
|
2018-01-12 23:16:49 +01:00
|
|
|
return err
|
2017-03-20 09:23:38 +01:00
|
|
|
}
|
|
|
|
|
2018-01-12 23:16:49 +01:00
|
|
|
fmt.Printf("%s's password has been successfully updated!\n", user.Name)
|
2017-03-20 09:23:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-14 01:11:52 +02:00
|
|
|
func runCreateUser(c *cli.Context) error {
|
2018-01-12 23:16:49 +01:00
|
|
|
if err := argsSet(c, "name", "password", "email"); err != nil {
|
|
|
|
return err
|
2016-08-14 01:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
}
|
|
|
|
|
2018-01-12 23:16:49 +01:00
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
2017-01-05 09:31:07 +01:00
|
|
|
}
|
2016-08-14 01:11:52 +02:00
|
|
|
|
|
|
|
if err := models.CreateUser(&models.User{
|
|
|
|
Name: c.String("name"),
|
|
|
|
Email: c.String("email"),
|
|
|
|
Passwd: c.String("password"),
|
|
|
|
IsActive: true,
|
|
|
|
IsAdmin: c.Bool("admin"),
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("CreateUser: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("New user '%s' has been successfully created!\n", c.String("name"))
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-31 15:45:46 +01:00
|
|
|
|
|
|
|
func runRepoSyncReleases(c *cli.Context) error {
|
2018-01-12 23:16:49 +01:00
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
2017-12-31 15:45:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Synchronizing repository releases (this may take a while)")
|
|
|
|
for page := 1; ; page++ {
|
|
|
|
repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: models.RepositoryListDefaultPageSize,
|
|
|
|
Private: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-12 23:16:49 +01:00
|
|
|
return fmt.Errorf("SearchRepositoryByName: %v", err)
|
2017-12-31 15:45:46 +01:00
|
|
|
}
|
|
|
|
if len(repos) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Trace("Processing next %d repos of %d", len(repos), count)
|
|
|
|
for _, repo := range repos {
|
|
|
|
log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath())
|
|
|
|
gitRepo, err := git.OpenRepository(repo.RepoPath())
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("OpenRepository: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-01-12 23:16:49 +01:00
|
|
|
oldnum, err := getReleaseCount(repo.ID)
|
2017-12-31 15:45:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Warn(" GetReleaseCountByRepoID: %v", err)
|
|
|
|
}
|
|
|
|
log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)
|
|
|
|
|
|
|
|
if err = models.SyncReleasesWithTags(repo, gitRepo); err != nil {
|
|
|
|
log.Warn(" SyncReleasesWithTags: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-01-12 23:16:49 +01:00
|
|
|
count, err = getReleaseCount(repo.ID)
|
2017-12-31 15:45:46 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Warn(" GetReleaseCountByRepoID: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace(" repo %s releases synchronized to tags: from %d to %d",
|
|
|
|
repo.FullName(), oldnum, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-12 23:16:49 +01:00
|
|
|
|
|
|
|
func getReleaseCount(id int64) (int64, error) {
|
|
|
|
return models.GetReleaseCountByRepoID(
|
|
|
|
id,
|
|
|
|
models.FindReleasesOptions{
|
|
|
|
IncludeTags: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2018-05-17 03:35:07 +02:00
|
|
|
|
|
|
|
func runRegenerateHooks(c *cli.Context) error {
|
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return models.SyncRepositoryHooks()
|
|
|
|
}
|
|
|
|
|
|
|
|
func runRegenerateKeys(c *cli.Context) error {
|
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return models.RewriteAllPublicKeys()
|
|
|
|
}
|