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