@@ -570,100 +570,27 @@ def SaveToSysconfig(self):
570570 if file is not None : file .close ()
571571 self .UpdateFromSysconfig ()
572572
573- def SaveToNTPConf (self ):
574- # Double-check authentication
573+ def SetNTPMode (self , inMode ):
575574 Auth .Inst ().AssertAuthenticated ()
575+ self .RequireSession ()
576+ self .session .xenapi .host .set_ntp_mode (self .host .opaqueref (), inMode )
576577
577- try :
578- with open ("/etc/chrony.conf" , "w" ) as confFile :
579- for other in self .ntp .othercontents ([]):
580- confFile .write (other + "\n " )
581- for server in self .ntp .servers ([]):
582- confFile .write ("server " + server + " iburst\n " )
583- finally :
584- self .UpdateFromNTPConf ()
585-
586- # Force chronyd to update the time
587- if self .data ['ntp' ]['method' ] != "Disabled" :
588- # Prompt chrony to update the time immediately
589- getoutput ("chronyc makestep" )
590- # Write the system time (set by the single shot NTP) to the HW clock
591- getoutput ("hwclock -w" )
592-
593- def AddDHCPNTP (self ):
594- # Double-check authentication
578+ def SetNTPManualServers (self , inServers ):
595579 Auth .Inst ().AssertAuthenticated ()
596-
597- oldPermissions = os .stat ("/etc/dhcp/dhclient.d/chrony.sh" ).st_mode
598- newPermissions = oldPermissions | (stat .S_IXUSR | stat .S_IXGRP | stat .S_IXOTH )
599- os .chmod ("/etc/dhcp/dhclient.d/chrony.sh" , newPermissions )
600-
601- interfaces = self .GetDHClientInterfaces ()
602- for interface in interfaces :
603- ntpServer = self .GetDHCPNTPServer (interface )
604-
605- with open ("/run/chrony-dhcp/%s.sources" % interface , "w" ) as chronyFile :
606- chronyFile .write ("server %s iburst prefer\n " % ntpServer )
607-
608- # Ensure chrony is enabled
609- self .EnableService ("chronyd" )
610- self .EnableService ("chrony-wait" )
580+ self .RequireSession ()
581+ self .session .xenapi .host .set_ntp_custom_servers (self .host .opaqueref (), inServers )
611582
612583 def ResetDefaultNTPServers (self ):
613584 # Double-check authentication
614585 Auth .Inst ().AssertAuthenticated ()
615586 Data .Inst ().NTPServersSet (DEFAULT_NTP_SERVERS )
616587
617- def GetDHClientInterfaces (self ):
618- try :
619- leases = [filename for filename in os .listdir ("/var/lib/xcp" ) if "leases" in filename ]
620- except OSError :
621- return []
622-
623- pattern = "dhclient-(.*).leases"
624- interfaces = []
625- for dhclientFile in leases :
626- match = re .match (pattern , dhclientFile )
627- if match :
628- interfaces .append (match .group (1 ))
629-
630- return interfaces
631-
632- def GetDHCPNTPServer (self , interface ):
633- expectedLeaseFile = "/var/lib/xcp/dhclient-%s.leases" % interface
634- if not os .path .isfile (expectedLeaseFile ):
635- return None
636-
637- with open (expectedLeaseFile , "r" ) as leaseFile :
638- data = leaseFile .read ().splitlines ()
639-
640- ntpServerLines = [line for line in data if "ntp-servers" in line ]
641-
642- # Extract <ip-addr> from " option ntp-servers <ip-addr>;"
643- try :
644- ntpServer = ntpServerLines [- 1 ].split ()[- 1 ][:- 1 ]
645- except :
646- ntpServer = None
647-
648- return ntpServer
649-
650- def RemoveDHCPNTP (self ):
651- # Double-check authentication
652- Auth .Inst ().AssertAuthenticated ()
653-
654- oldPermissions = os .stat ("/etc/dhcp/dhclient.d/chrony.sh" ).st_mode
655- newPermissions = oldPermissions & ~ (stat .S_IXUSR | stat .S_IXGRP | stat .S_IXOTH )
656- os .chmod ("/etc/dhcp/dhclient.d/chrony.sh" , newPermissions )
657-
658- getstatusoutput ("rm -f /run/chrony-dhcp/*.sources" )
659-
660588 def SetTimeManually (self , date ):
661589 # Double-check authentication
662590 Auth .Inst ().AssertAuthenticated ()
663591
664592 self .NTPServersSet ([])
665- self .RemoveDHCPNTP ()
666- self .SaveToNTPConf ()
593+ self .SetNTPMode ('Disabled' )
667594
668595 self .DisableService ("chrony-wait" )
669596 self .DisableService ("chronyd" )
@@ -923,22 +850,9 @@ def UpdateFromTimezone(self):
923850 file .close ()
924851
925852 def TimezoneSet (self , inTimezone ):
926- localtimeFile = '/etc/localtime'
927- if os .path .isfile (localtimeFile ):
928- os .remove (localtimeFile )
929- os .symlink (self .timezones .cities ({})[inTimezone ], localtimeFile )
930-
931- file = open ('/etc/timezone' , 'w' )
932- file .write (inTimezone + "\n " )
933- file .close ()
934-
935- if os .path .exists ('/etc/sysconfig/clock' ):
936- cfg = SimpleConfigFile ()
937- cfg .read ('/etc/sysconfig/clock' )
938- cfg .info ["ZONE" ] = inTimezone
939- cfg .write ('/etc/sysconfig/clock' )
940-
941- time .tzset ()
853+ Auth .Inst ().AssertAuthenticated ()
854+ self .RequireSession ()
855+ self .session .xenapi .host .set_timezone (self .host .opaqueref (), inTimezone )
942856
943857 def CurrentTimeString (self ):
944858 return getoutput ('/bin/date -R' )
@@ -1099,10 +1013,12 @@ def DisableManagement(self):
10991013 self .session = Auth .Inst ().CloseSession (self .session )
11001014
11011015 def AdjustNTPForStaticNetwork (self ):
1102- self .RemoveDHCPNTP ()
11031016 if not self .data ['ntp' ]['servers' ]: # No NTP servers after removing DHCP
11041017 self .ResetDefaultNTPServers ()
1105- self .SaveToNTPConf ()
1018+ self .SetNTPMode ('Factory' )
1019+ else :
1020+ self .SetNTPManualServers (self .data ['ntp' ]['servers' ])
1021+ self .SetNTPMode ('Custom' )
11061022
11071023 def LocalHostEnable (self ):
11081024 Auth .Inst ().AssertAuthenticatedOrPasswordUnset ()
0 commit comments