2016-08-14 02:11:52 +03:00
// Copyright 2016 The Gogs Authors. All rights reserved.
2016-12-21 15:13:17 +03:00
// Copyright 2016 The Gitea Authors. All rights reserved.
2016-08-14 02:11:52 +03: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 17:45:46 +03:00
"code.gitea.io/git"
2016-11-10 19:24:48 +03:00
"code.gitea.io/gitea/models"
2017-12-31 17:45:46 +03:00
"code.gitea.io/gitea/modules/log"
2016-11-10 19:24:48 +03:00
"code.gitea.io/gitea/modules/setting"
2017-04-12 10:44:54 +03:00
"github.com/urfave/cli"
2016-08-14 02:11:52 +03:00
)
var (
2016-11-04 14:42:18 +03:00
// CmdAdmin represents the available admin sub-command.
2016-08-14 02:11:52 +03:00
CmdAdmin = cli . Command {
Name : "admin" ,
2018-01-13 01:16:49 +03:00
Usage : "Command line interface to perform common administrative operations" ,
2016-08-14 02:11:52 +03:00
Subcommands : [ ] cli . Command {
subcmdCreateUser ,
2017-03-20 11:23:38 +03:00
subcmdChangePassword ,
2017-12-31 17:45:46 +03:00
subcmdRepoSyncReleases ,
2018-05-17 04:35:07 +03:00
subcmdRegenerate ,
2016-08-14 02:11:52 +03:00
} ,
}
subcmdCreateUser = cli . Command {
Name : "create-user" ,
Usage : "Create a new user in database" ,
Action : runCreateUser ,
Flags : [ ] cli . Flag {
2016-11-10 01:18:22 +03:00
cli . StringFlag {
Name : "name" ,
Usage : "Username" ,
} ,
cli . StringFlag {
Name : "password" ,
2016-11-10 01:32:24 +03:00
Usage : "User password" ,
2016-11-10 01:18:22 +03:00
} ,
cli . StringFlag {
Name : "email" ,
2016-11-10 01:32:24 +03:00
Usage : "User email address" ,
2016-11-10 01:18:22 +03: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 02:11:52 +03:00
} ,
}
2017-03-20 11:23:38 +03: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 01:35:14 +03:00
cli . StringFlag {
Name : "config, c" ,
Value : "custom/conf/app.ini" ,
Usage : "Custom configuration file path" ,
} ,
2017-03-20 11:23:38 +03:00
} ,
}
2017-12-31 17:45:46 +03:00
subcmdRepoSyncReleases = cli . Command {
Name : "repo-sync-releases" ,
Usage : "Synchronize repository releases with tags" ,
Action : runRepoSyncReleases ,
}
2018-05-17 04:35:07 +03: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 02:11:52 +03:00
)
2017-03-20 11:23:38 +03:00
func runChangePassword ( c * cli . Context ) error {
2018-01-13 01:16:49 +03:00
if err := argsSet ( c , "username" , "password" ) ; err != nil {
return err
2017-03-20 11:23:38 +03:00
}
2018-06-10 01:35:14 +03:00
if c . IsSet ( "config" ) {
setting . CustomConf = c . String ( "config" )
}
2018-01-13 01:16:49 +03:00
if err := initDB ( ) ; err != nil {
return err
2017-03-20 11:23:38 +03:00
}
uname := c . String ( "username" )
user , err := models . GetUserByName ( uname )
if err != nil {
2018-01-13 01:16:49 +03:00
return err
2017-03-20 11:23:38 +03:00
}
if user . Salt , err = models . GetUserSalt ( ) ; err != nil {
2018-01-13 01:16:49 +03:00
return err
2017-03-20 11:23:38 +03:00
}
2018-01-12 01:19:38 +03:00
user . HashPassword ( c . String ( "password" ) )
2017-08-12 17:18:44 +03:00
if err := models . UpdateUserCols ( user , "passwd" , "salt" ) ; err != nil {
2018-01-13 01:16:49 +03:00
return err
2017-03-20 11:23:38 +03:00
}
2018-01-13 01:16:49 +03:00
fmt . Printf ( "%s's password has been successfully updated!\n" , user . Name )
2017-03-20 11:23:38 +03:00
return nil
}
2016-08-14 02:11:52 +03:00
func runCreateUser ( c * cli . Context ) error {
2018-01-13 01:16:49 +03:00
if err := argsSet ( c , "name" , "password" , "email" ) ; err != nil {
return err
2016-08-14 02:11:52 +03:00
}
if c . IsSet ( "config" ) {
setting . CustomConf = c . String ( "config" )
}
2018-01-13 01:16:49 +03:00
if err := initDB ( ) ; err != nil {
return err
2017-01-05 11:31:07 +03:00
}
2016-08-14 02:11:52 +03: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 17:45:46 +03:00
func runRepoSyncReleases ( c * cli . Context ) error {
2018-01-13 01:16:49 +03:00
if err := initDB ( ) ; err != nil {
return err
2017-12-31 17:45:46 +03: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-13 01:16:49 +03:00
return fmt . Errorf ( "SearchRepositoryByName: %v" , err )
2017-12-31 17:45:46 +03: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-13 01:16:49 +03:00
oldnum , err := getReleaseCount ( repo . ID )
2017-12-31 17:45:46 +03: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-13 01:16:49 +03:00
count , err = getReleaseCount ( repo . ID )
2017-12-31 17:45:46 +03: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-13 01:16:49 +03:00
func getReleaseCount ( id int64 ) ( int64 , error ) {
return models . GetReleaseCountByRepoID (
id ,
models . FindReleasesOptions {
IncludeTags : true ,
} ,
)
}
2018-05-17 04:35:07 +03: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 ( )
}