Skip to content

Commit

Permalink
Add no_replication flag to command-line argument
Browse files Browse the repository at this point in the history
  • Loading branch information
iychoi committed Jan 24, 2023
1 parent 1e3f6cb commit 2767092
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 35 deletions.
115 changes: 82 additions & 33 deletions commons/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ const (
)

var (
environmentMgr *irodsclient_icommands.ICommandsEnvironmentManager
account *irodsclient_types.IRODSAccount
environmentManager *irodsclient_icommands.ICommandsEnvironmentManager
appConfig *Config
account *irodsclient_types.IRODSAccount

sessionID int
resourceServer string
ticket string
sessionID int
)

func GetEnvironmentManager() *irodsclient_icommands.ICommandsEnvironmentManager {
return environmentMgr
return environmentManager
}

func GetConfig() *Config {
return appConfig
}

func GetAccount() *irodsclient_types.IRODSAccount {
Expand All @@ -55,14 +58,14 @@ func getCWD(env *irodsclient_icommands.ICommandsEnvironment) string {
}

func GetCWD() string {
session := environmentMgr.Session
session := environmentManager.Session
sessionPath := getCWD(session)

if len(sessionPath) > 0 {
return sessionPath
}

env := environmentMgr.Environment
env := environmentManager.Environment
envPath := getCWD(env)

if len(envPath) == 0 {
Expand All @@ -73,25 +76,25 @@ func GetCWD() string {
}

func GetZone() string {
env := environmentMgr.Environment
env := environmentManager.Environment
return env.Zone
}

func GetHomeDir() string {
env := environmentMgr.Environment
env := environmentManager.Environment
return fmt.Sprintf("/%s/home/%s", env.Zone, env.Username)
}

func SetCWD(cwd string) {
env := environmentMgr.Environment
session := environmentMgr.Session
env := environmentManager.Environment
session := environmentManager.Session
if !strings.HasPrefix(cwd, "/") {
// relative path from home
cwd = fmt.Sprintf("/%s/home/%s/%s", env.Zone, env.Username, cwd)
}

session.CurrentWorkingDir = path.Clean(cwd)
environmentMgr.SaveSession(sessionID)
environmentManager.SaveSession(sessionID)
}

func SetCommonFlags(command *cobra.Command) {
Expand All @@ -100,6 +103,7 @@ func SetCommonFlags(command *cobra.Command) {
command.Flags().BoolP("version", "v", false, "Print version")
command.Flags().BoolP("help", "h", false, "Print help")
command.Flags().BoolP("debug", "d", false, "Enable debug mode")
command.Flags().Bool("no_replication", false, "Disable replication")
command.Flags().Int32P("session", "s", -1, "Set session ID")
command.Flags().StringP("resource", "R", "", "Set resource server")
command.Flags().StringP("ticket", "T", "", "Set ticket")
Expand Down Expand Up @@ -171,9 +175,9 @@ func ProcessCommonFlags(command *cobra.Command) (bool, error) {

configFlag := command.Flags().Lookup("config")
if configFlag != nil {
config := configFlag.Value.String()
if len(config) > 0 {
err := loadConfigFile(config)
configFile := configFlag.Value.String()
if len(configFile) > 0 {
err := loadConfigFile(configFile)
if err != nil {
logger.Error(err)
return false, err // stop here
Expand Down Expand Up @@ -229,32 +233,53 @@ func ProcessCommonFlags(command *cobra.Command) (bool, error) {
//}
}

if appConfig == nil {
appConfig = GetDefaultConfig()
}

resourceFlag := command.Flags().Lookup("resource")
if resourceFlag != nil {
// load to global variable
resourceServer = resourceFlag.Value.String()
appConfig.DefaultResource = resourceFlag.Value.String()
if len(appConfig.DefaultResource) > 0 {
logger.Debugf("use default resource server - %s", appConfig.DefaultResource)
}
}

noReplicationFlag := command.Flags().Lookup("no_replication")
if noReplicationFlag != nil {
noReplication, err := strconv.ParseBool(noReplicationFlag.Value.String())
if err != nil {
noReplication = false
}

appConfig.NoReplication = noReplication
if noReplication {
logger.Debug("disabled replication")
}
}

ticketFlag := command.Flags().Lookup("ticket")
if ticketFlag != nil {
// load to global variable
ticket = ticketFlag.Value.String()

logger.Debugf("use ticket - %s", ticket)
appConfig.Ticket = ticketFlag.Value.String()
if len(appConfig.Ticket) > 0 {
logger.Debugf("use ticket - %s", appConfig.Ticket)
}
}

return true, nil // contiue
}

// InputMissingFields inputs missing fields
func InputMissingFields() (bool, error) {
if environmentMgr == nil {
if environmentManager == nil {
envMgr, err := irodsclient_icommands.CreateIcommandsEnvironmentManager()
if err != nil {
return false, err
}

environmentMgr = envMgr
environmentManager = envMgr
account, err = envMgr.ToIRODSAccount()
if err != nil {
return false, err
Expand All @@ -263,7 +288,7 @@ func InputMissingFields() (bool, error) {

updated := false

env := environmentMgr.Environment
env := environmentManager.Environment
if len(env.Host) == 0 {
fmt.Print("iRODS Host [data.cyverse.org]: ")
fmt.Scanln(&env.Host)
Expand Down Expand Up @@ -301,7 +326,7 @@ func InputMissingFields() (bool, error) {
}
}

password := environmentMgr.Password
password := environmentManager.Password
for len(password) == 0 {
fmt.Print("iRODS Password: ")
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
Expand All @@ -325,19 +350,19 @@ func InputMissingFields() (bool, error) {
updated = true
}
}
environmentMgr.Password = password
environmentManager.Password = password

newAccount, err := environmentMgr.ToIRODSAccount()
newAccount, err := environmentManager.ToIRODSAccount()
if err != nil {
return updated, err
}

if len(resourceServer) > 0 {
newAccount.DefaultResource = resourceServer
if len(appConfig.DefaultResource) > 0 {
newAccount.DefaultResource = appConfig.DefaultResource
}

if len(ticket) > 0 {
newAccount.Ticket = ticket
if len(appConfig.Ticket) > 0 {
newAccount.Ticket = appConfig.Ticket
}

account = newAccount
Expand Down Expand Up @@ -412,6 +437,26 @@ func setConfigToICommandsEnvMgr(envManager *irodsclient_icommands.ICommandsEnvir
envManager.Password = config.Password
}

func setICommandsEnvMgrToConfig(config *Config, envManager *irodsclient_icommands.ICommandsEnvironmentManager) {
config.CurrentWorkingDir = envManager.Environment.CurrentWorkingDir
config.Host = envManager.Environment.Host
config.Port = envManager.Environment.Port
config.Username = envManager.Environment.Username
config.Zone = envManager.Environment.Zone
config.DefaultResource = envManager.Environment.DefaultResource
config.LogLevel = envManager.Environment.LogLevel
config.AuthenticationScheme = envManager.Environment.AuthenticationScheme
config.ClientServerNegotiation = envManager.Environment.ClientServerNegotiation
config.ClientServerPolicy = envManager.Environment.ClientServerPolicy
config.SSLCACertificateFile = envManager.Environment.SSLCACertificateFile
config.EncryptionKeySize = envManager.Environment.EncryptionKeySize
config.EncryptionAlgorithm = envManager.Environment.EncryptionAlgorithm
config.EncryptionSaltSize = envManager.Environment.EncryptionSaltSize
config.EncryptionNumHashRounds = envManager.Environment.EncryptionNumHashRounds

config.Password = envManager.Password
}

func getLogrusLogLevel(irodsLogLevel int) log.Level {
switch irodsLogLevel {
case 0:
Expand Down Expand Up @@ -494,7 +539,8 @@ func loadConfigFile(configPath string) error {
loadedAccount.ClientUser = config.ClientUsername
loadedAccount.Ticket = config.Ticket

environmentMgr = iCommandsEnvMgr
environmentManager = iCommandsEnvMgr
appConfig = config
account = loadedAccount
return nil
}
Expand Down Expand Up @@ -532,7 +578,9 @@ func loadConfigFile(configPath string) error {
return err
}

environmentMgr = iCommandsEnvMgr
environmentManager = iCommandsEnvMgr
appConfig = GetDefaultConfig()
setICommandsEnvMgrToConfig(appConfig, iCommandsEnvMgr)
account = loadedAccount

return nil
Expand Down Expand Up @@ -571,7 +619,8 @@ func loadConfigEnv() error {
loadedAccount.ClientUser = config.ClientUsername
loadedAccount.Ticket = config.Ticket

environmentMgr = iCommandsEnvMgr
environmentManager = iCommandsEnvMgr
appConfig = config
account = loadedAccount
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions commons/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Config struct {
EncryptionAlgorithm string `yaml:"irods_encryption_algorithm,omitempty" envconfig:"IRODS_ENCRYPTION_ALGORITHM"`
EncryptionSaltSize int `yaml:"irods_encryption_salt_size,omitempty" envconfig:"IRODS_ENCRYPTION_SALT_SIZE"`
EncryptionNumHashRounds int `yaml:"irods_encryption_num_hash_rounds,omitempty" envconfig:"IRODS_ENCRYPTION_NUM_HASH_ROUNDS"`

NoReplication bool `yaml:"irods_no_replication,omitempty" envconfig:"IRODS_NO_REPLICATION"`
}

func GetDefaultConfig() *Config {
Expand All @@ -52,6 +54,8 @@ func GetDefaultConfig() *Config {
EncryptionAlgorithm: EncryptionAlgorithmDefault,
EncryptionSaltSize: SaltSizeDefault,
EncryptionNumHashRounds: HashRoundsDefault,

NoReplication: false,
}
}

Expand Down
10 changes: 8 additions & 2 deletions commons/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ func (manager *ParallelTransferManager) ScheduleUploadIfDifferent(filesystem *ir
"function": "ScheduleUploadIfDifferent",
})

config := GetConfig()
replicate := !config.NoReplication

task := func() {
logger.Debugf("synchronizing a local file %s to %s", source, target)

Expand Down Expand Up @@ -315,7 +318,7 @@ func (manager *ParallelTransferManager) ScheduleUploadIfDifferent(filesystem *ir
manager.progressCallback(target, processed, total, done)
}

err = filesystem.UploadFileParallel(source, target, "", 0, true, callback)
err = filesystem.UploadFileParallel(source, target, "", 0, replicate, callback)
if err != nil {
manager.mutex.Lock()
defer manager.mutex.Unlock()
Expand Down Expand Up @@ -357,14 +360,17 @@ func (manager *ParallelTransferManager) ScheduleUpload(filesystem *irodsclient_f
"function": "ScheduleUpload",
})

config := GetConfig()
replicate := !config.NoReplication

task := func() {
logger.Debugf("uploading a local file %s to %s", source, target)
callback := func(processed int64, total int64) {
done := processed >= total
manager.progressCallback(target, processed, total, done)
}

err := filesystem.UploadFileParallel(source, target, "", 0, true, callback)
err := filesystem.UploadFileParallel(source, target, "", 0, replicate, callback)
if err != nil {
manager.mutex.Lock()
defer manager.mutex.Unlock()
Expand Down

0 comments on commit 2767092

Please sign in to comment.