@@ -113,11 +113,15 @@ def __post_init__(self):
113113 self ._lock = asyncio .Lock ()
114114 with self .pool .connection () as conn :
115115 with conn .transaction ():
116- conn .execute ("INSERT INTO servers (server_name) VALUES (%s) ON CONFLICT (server_name) DO NOTHING" ,
117- (self .name , ))
118- row = conn .execute ("SELECT maintenance FROM servers WHERE server_name = %s" , (self .name ,)).fetchone ()
119- if row :
120- self ._maintenance = row [0 ]
116+ cursor = conn .execute ("""
117+ INSERT INTO servers (server_name)
118+ VALUES (%s)
119+ ON CONFLICT (server_name) DO NOTHING
120+ RETURNING maintenance
121+ """ , (self .name , ))
122+ row = cursor .fetchone ()
123+ if row :
124+ self ._maintenance = row [0 ]
121125 atexit .register (self .stop_observer )
122126
123127 @override
@@ -141,6 +145,7 @@ def settings(self) -> dict:
141145 # if someone managed to destroy the mission list, fix it...
142146 if 'missionList' not in self ._settings :
143147 self ._settings ['missionList' ] = []
148+ self ._settings ['listStartIndex' ] = 0
144149 elif isinstance (self ._settings ['missionList' ], dict ):
145150 self ._settings ['missionList' ] = list (self ._settings ['missionList' ].values ())
146151 return self ._settings
@@ -447,9 +452,11 @@ async def update_database(old_name: str, new_name: str):
447452 # rename the server in the database
448453 async with self .apool .connection () as conn :
449454 async with conn .transaction ():
450- # we need to remove any older server that might have had the same name
451455 await conn .execute ("UPDATE servers SET server_name = %s WHERE server_name = %s" ,
452- (new_name , old_name ))
456+ (new_name , old_name or 'n/a' ))
457+ if not old_name :
458+ await conn .execute ("UPDATE instances SET server_name = %s WHERE instance = %s" ,
459+ (new_name , self .instance .name ))
453460
454461 async def update_cluster (new_name : str ):
455462 # only the master can take care of a cluster-wide rename
@@ -484,6 +491,13 @@ async def update_cluster(new_name: str):
484491 except Exception :
485492 self .log .exception (f"Error during renaming of server { old_name } to { new_name } : " , exc_info = True )
486493
494+ async def unlink (self ):
495+ if self .name == 'n/a' :
496+ async with self .apool .connection () as conn :
497+ async with conn .transaction ():
498+ await conn .execute ("DELETE FROM servers WHERE server_name = 'n/a'" )
499+ self .instance .server = None
500+
487501 @performance_log ()
488502 def do_startup (self ):
489503 basepath = self .node .installation
@@ -788,20 +802,8 @@ async def apply_mission_changes(self, filename: str | None = None, *, use_orig:
788802 if _dirty :
789803 self .log .info (f' => { ext .name } applied on { new_filename } .' )
790804 dirty |= _dirty
791- # we did not change anything in the mission
792- if not dirty :
793- return filename
794- # check if the original mission can be written
795- if filename != new_filename :
796- missions : list [str ] = self .settings ['missionList' ]
797- try :
798- index = missions .index (filename ) + 1
799- await self .replaceMission (index , new_filename )
800- except ValueError :
801- # we should not be here, but just in case
802- if new_filename not in missions :
803- await self .addMission (new_filename )
804- return new_filename
805+
806+ return filename if not dirty else new_filename
805807 except Exception as ex :
806808 self .log .error (ex )
807809 if filename != new_filename and os .path .exists (new_filename ):
@@ -899,7 +901,7 @@ async def render_extensions(self) -> list[dict]:
899901
900902 @override
901903 async def restart (self , modify_mission : bool | None = True , use_orig : bool | None = True ) -> None :
902- await self .loadMission (int ( self .settings [ 'listStartIndex' ] ), modify_mission = modify_mission , use_orig = use_orig )
904+ await self .loadMission (self ._get_current_mission_file ( ), modify_mission = modify_mission , use_orig = use_orig )
903905
904906 @override
905907 async def setStartIndex (self , mission_id : int ) -> None :
@@ -912,22 +914,32 @@ async def setStartIndex(self, mission_id: int) -> None:
912914
913915 @override
914916 async def setPassword (self , password : str ):
915- self .settings ['password' ] = password or ''
917+ if self .status in [Status .STOPPED , Status .PAUSED , Status .RUNNING ]:
918+ await self .send_to_dcs ({"command" : "setPassword" , "password" : password })
919+ else :
920+ self .settings ['password' ] = password or ''
916921
917922 @override
918923 async def setCoalitionPassword (self , coalition : Coalition , password : str ):
919- advanced = self .settings ['advanced' ]
920- if coalition == Coalition .BLUE :
921- if password :
922- advanced ['bluePasswordHash' ] = utils .hash_password (password )
923- else :
924- advanced .pop ('bluePasswordHash' , None )
924+ if self .status in [Status .STOPPED , Status .PAUSED , Status .RUNNING ]:
925+ if coalition == Coalition .BLUE :
926+ await self .send_to_dcs ({"command" : "setCoalitionPassword" , "bluePassword" : password or '' })
927+ elif coalition == Coalition .RED :
928+ await self .send_to_dcs ({"command" : "setCoalitionPassword" , "redPassword" : password or '' })
925929 else :
926- if password :
927- advanced ['redPasswordHash' ] = utils .hash_password (password )
930+ advanced = self .settings ['advanced' ]
931+ if coalition == Coalition .BLUE :
932+ if password :
933+ advanced ['bluePasswordHash' ] = utils .hash_password (password )
934+ else :
935+ advanced .pop ('bluePasswordHash' , None )
928936 else :
929- advanced .pop ('redPasswordHash' , None )
930- self .settings ['advanced' ] = advanced
937+ if password :
938+ advanced ['redPasswordHash' ] = utils .hash_password (password )
939+ else :
940+ advanced .pop ('redPasswordHash' , None )
941+ self .settings ['advanced' ] = advanced
942+
931943 async with self .apool .connection () as conn :
932944 async with conn .transaction ():
933945 await conn .execute ('UPDATE servers SET {} = %s WHERE server_name = %s' .format (
@@ -994,38 +1006,34 @@ async def replaceMission(self, mission_id: int, path: str) -> list[str]:
9941006 @override
9951007 async def loadMission (self , mission : int | str , modify_mission : bool | None = True ,
9961008 use_orig : bool | None = True , no_reload : bool | None = False ) -> bool | None :
997- start_index = int ( self . settings . get ( 'listStartIndex' , 1 ))
1009+
9981010 mission_list = self .settings ['missionList' ]
999- # check if we re-load the running mission
1000- if ((isinstance (mission , int ) and mission == start_index ) or
1001- (isinstance (mission , str ) and mission == self ._get_current_mission_file ())):
1002- # if we should not reload, then return here
1003- if no_reload :
1004- return None
1005- mission = self ._get_current_mission_file ()
1006- if not mission :
1007- return False
1008- if use_orig :
1009- new_filename = utils .create_writable_mission (mission )
1010- orig_mission = utils .get_orig_file (mission )
1011- shutil .copy2 (orig_mission , new_filename )
1012- if new_filename != mission :
1013- mission_list = await self .replaceMission (start_index , new_filename )
1014- elif modify_mission :
1015- # don't use the orig file, still make sure we have a writable mission
1016- new_filename = utils .create_writable_mission (mission )
1017- if new_filename != mission :
1018- shutil .copy2 (mission , new_filename )
1019- mission_list = await self .replaceMission (start_index , new_filename )
1011+ start_index = int (self .settings .get ('listStartIndex' , 1 ))
1012+ try :
1013+ current_mission = self ._get_current_mission_file ()
1014+ current_index = mission_list .index (current_mission ) + 1
1015+ except ValueError :
1016+ current_index = start_index
1017+ current_mission = mission_list [current_index - 1 ]
10201018
10211019 if isinstance (mission , int ):
1022- if mission > len (mission_list ):
1023- mission = 1
1024- filename = mission_list [mission - 1 ]
1020+ mission = mission_list [mission - 1 ]
1021+
1022+ # we should not reload the running mission
1023+ if no_reload and mission == current_mission :
1024+ return None
1025+
1026+ if modify_mission :
1027+ filename = await self .apply_mission_changes (mission , use_orig = use_orig )
1028+ elif use_orig :
1029+ filename = utils .create_writable_mission (mission )
1030+ orig_mission = utils .get_orig_file (mission )
1031+ shutil .copy2 (orig_mission , filename )
10251032 else :
10261033 filename = mission
1027- if modify_mission :
1028- filename = await self .apply_mission_changes (filename )
1034+
1035+ if mission == current_mission and filename != mission :
1036+ mission_list = await self .replaceMission (current_index , filename )
10291037
10301038 if self .status == Status .STOPPED :
10311039 try :
@@ -1039,7 +1047,7 @@ async def loadMission(self, mission: int | str, modify_mission: bool | None = Tr
10391047 timeout = 300 if self .node .locals .get ('slow_system' , False ) else 180
10401048 try :
10411049 idx = mission_list .index (filename ) + 1
1042- if idx == start_index :
1050+ if idx == current_index :
10431051 rc = await self .send_to_dcs_sync ({
10441052 "command" : "startMission" ,
10451053 "filename" : filename
@@ -1054,6 +1062,7 @@ async def loadMission(self, mission: int | str, modify_mission: bool | None = Tr
10541062 "command" : "startMission" ,
10551063 "filename" : filename
10561064 }, timeout = timeout )
1065+
10571066 # We could not load the mission
10581067 result = rc ['result' ] if isinstance (rc ['result' ], bool ) else (rc ['result' ] == 0 )
10591068 if not result :
0 commit comments