@@ -850,41 +850,41 @@ func enableBalancer(mongosHost string) error {
850
850
}
851
851
852
852
func checkRoleExists (mongoDSN string ) (bool , error ) {
853
- v := make (map [string ]interface {})
854
853
args := append ([]interface {}{
855
854
"admin" ,
856
855
"--host" , mongoDSN ,
857
856
"--quiet" ,
858
857
"--eval" , `JSON.stringify(db.getRole("` + StashRoleName + `"))` ,
859
858
}, mongoCreds ... )
860
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
859
+ output , err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).Output ()
860
+ if err != nil {
861
861
return false , err
862
862
}
863
863
864
- if val , ok := v [ "role" ]. (string ); ok && string ( val ) == StashRoleName {
865
- return true , nil
864
+ if strings . Contains (string ( output ), "null" ) {
865
+ return false , nil
866
866
}
867
867
868
- return false , nil
868
+ return true , nil
869
869
}
870
870
871
871
func checkUserExists (mongoDSN string ) (bool , error ) {
872
- v := make (map [string ]interface {})
873
872
args := append ([]interface {}{
874
873
"admin" ,
875
874
"--host" , mongoDSN ,
876
875
"--quiet" ,
877
876
"--eval" , `JSON.stringify(db.getUser("` + StashUserName + `"))` ,
878
877
}, mongoCreds ... )
879
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
878
+ output , err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).Output ()
879
+ if err != nil {
880
880
return false , err
881
881
}
882
882
883
- if val , ok := v [ "user" ]. (string ); ok && string ( val ) == StashUserName {
884
- return true , nil
883
+ if strings . Contains (string ( output ), "null" ) {
884
+ return false , nil
885
885
}
886
886
887
- return false , nil
887
+ return true , nil
888
888
}
889
889
890
890
func createStashRoleAndUser (mongoDSN string , pass string ) error {
@@ -912,7 +912,16 @@ func createStashBackupRole(mongoDSN string) error {
912
912
"--eval" , `JSON.stringify(db.runCommand({createRole: "` + StashRoleName + `",privileges:[{resource:{db:"config",collection:"system.preimages"},actions:["find"]},{resource:{db:"config",collection:"system.sharding_ddl_coordinators"},actions:["find"]},{resource:{db:"config",collection:"system.*"},actions:["find"]}],roles: []}))` ,
913
913
}, mongoCreds ... )
914
914
915
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
915
+ output , err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).Output ()
916
+ if err != nil {
917
+ return err
918
+ }
919
+ output , err = extractJSON (string (output ))
920
+ if err != nil {
921
+ return err
922
+ }
923
+ err = json .Unmarshal (output , & v )
924
+ if err != nil {
916
925
return err
917
926
}
918
927
@@ -939,7 +948,16 @@ func createStashBackupUser(mongoDSN string, pass string) error {
939
948
"--quiet" ,
940
949
"--eval" , `JSON.stringify(db.runCommand({createUser: "` + StashUserName + `" ,pwd: "` + pass + `", roles:[{role:"backup", db:"admin"}, {role: "` + StashRoleName + `",db:"admin"}]}))` ,
941
950
}, mongoCreds ... )
942
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
951
+ output , err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).Output ()
952
+ if err != nil {
953
+ return err
954
+ }
955
+ output , err = extractJSON (string (output ))
956
+ if err != nil {
957
+ return err
958
+ }
959
+ err = json .Unmarshal (output , & v )
960
+ if err != nil {
943
961
return err
944
962
}
945
963
@@ -951,24 +969,18 @@ func createStashBackupUser(mongoDSN string, pass string) error {
951
969
}
952
970
953
971
func handleReshard (configsvrDSN string ) (bool , error ) {
954
- v := make ([]interface {}, 0 )
955
972
args := append ([]interface {}{
956
973
"config" ,
957
974
"--host" , configsvrDSN ,
958
975
"--quiet" ,
959
976
"--eval" , `JSON.stringify(db.getCollectionNames())` ,
960
977
}, mongoCreds ... )
961
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
978
+ output , err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).Output ()
979
+ if err != nil {
980
+ klog .Errorf ("Error while calling getCollectionNames : %s ; output : %s \n " , err .Error (), output )
962
981
return false , err
963
982
}
964
-
965
- exists := false
966
- for _ , name := range v {
967
- if name .(string ) == "reshardingOperations" {
968
- exists = true
969
- break
970
- }
971
- }
983
+ exists := strings .Contains (string (output ), "reshardingOperations" )
972
984
if ! exists {
973
985
return false , nil
974
986
}
@@ -999,7 +1011,16 @@ func handleReshard(configsvrDSN string) (bool, error) {
999
1011
"--quiet" ,
1000
1012
"--eval" , `JSON.stringify(db.adminCommand( { renameCollection: "config.reshardingOperations", to: "config.reshardingOperations_temp", dropTarget: true}))` ,
1001
1013
}, mongoCreds ... )
1002
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& res ); err != nil {
1014
+ output , err = sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).Output ()
1015
+ if err != nil {
1016
+ return false , err
1017
+ }
1018
+ output , err = extractJSON (string (output ))
1019
+ if err != nil {
1020
+ return false , err
1021
+ }
1022
+ err = json .Unmarshal (output , & res )
1023
+ if err != nil {
1003
1024
return false , err
1004
1025
}
1005
1026
if val , ok := res ["ok" ].(float64 ); ! ok || int (val ) != 1 {
@@ -1017,7 +1038,16 @@ func renameTempReshardCollection(configsvrDSN string) error {
1017
1038
"--quiet" ,
1018
1039
"--eval" , `JSON.stringify(db.adminCommand( { renameCollection: "config.reshardingOperations_temp", to: "config.reshardingOperations" } ))` ,
1019
1040
}, mongoCreds ... )
1020
- if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& res ); err != nil {
1041
+ output , err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).Output ()
1042
+ if err != nil {
1043
+ return err
1044
+ }
1045
+ output , err = extractJSON (string (output ))
1046
+ if err != nil {
1047
+ return err
1048
+ }
1049
+ err = json .Unmarshal (output , & res )
1050
+ if err != nil {
1021
1051
return err
1022
1052
}
1023
1053
if val , ok := res ["ok" ].(float64 ); ! ok || int (val ) != 1 {
0 commit comments