Improve log and cleanup code

This commit is contained in:
Yue Zhang
2015-06-30 13:18:13 +08:00
parent 46c6115ee3
commit 9f299a419f
16 changed files with 41 additions and 63 deletions
+1 -6
View File
@@ -58,8 +58,6 @@ def ParseArgs(sysArgv):
cmd = "start"
elif re.match("^([-/]*)version", a):
cmd = "version"
elif re.match("^([-/]*)serialconsole", a):
cmd = "serialconsole"
elif re.match("^([-/]*)verbose", a):
verbose = True
elif re.match("^([-/]*)force", a):
@@ -77,7 +75,7 @@ def Version():
def Usage():
print("")
print(("usage: {0} [-verbose] [-force] "
"[-help|-deprovision[+user]|-version|-serialconsole|-daemon|-start]"
"[-help|-deprovision[+user]|-version|-daemon|-start]"
"").format(sys.argv[0]))
print("")
@@ -93,9 +91,6 @@ def Main():
Usage()
else:
Init(verbose)
if command == "serialconsole":
#TODO
pass
if command == "deprovision+user":
Deprovision(force, deluser=True)
elif command == "deprovision":
+7 -2
View File
@@ -42,12 +42,16 @@ class DhcpHandler(object):
ipv4 = OSUtil.GetIpv4Address()
def probe(self):
logger.Info("Send dhcp request")
self.waitForNetwork()
macAddress = OSUtil.GetMacAddress()
req = BuildDhcpRequest(macAddress)
resp = SendDhcpRequest(req)
endpoint, gateway, routes = ParseDhcpResponse(resp)
self.endpoint = endpoint
logger.Info("Wire server endpoint:{0}", endpoint)
logger.Info("Gateway:{0}", gateway)
logger.Info("Routes:{0}", routes)
if endpoint is not None:
path = os.path.join(OSUtil.GetLibDir(), WireServerAddrFile)
fileutil.SetFileContents(path, endpoint)
@@ -59,6 +63,7 @@ class DhcpHandler(object):
return self.endpoint
def configRoutes(self):
logger.Info("Configure routes")
#Add default gateway
if self.gateway is not None:
OSUtil.RouteAdd(0 , 0, self.gateway)
@@ -246,8 +251,8 @@ def SocketSend(request):
sock.bind(("0.0.0.0", 68))
sock.sendto(request, ("<broadcast>", 67))
sock.settimeout(10)
logger.Info("Send DHCP request: Setting socket.timeout=10, "
"entering recv")
logger.Verbose("Send DHCP request: Setting socket.timeout=10, "
"entering recv")
response = sock.recv(1024)
return response
@@ -72,7 +72,6 @@ def extension_status_to_v2(extStatus, seqNo):
validate_has_key(extStatus['status'], 'status', 'status/status')
validate_has_key(extStatus['status'], 'operation', 'status/operation')
validate_has_key(extStatus['status'], 'code', 'status/code')
#TODO are those fields all mandatory
validate_has_key(extStatus['status'], 'name', 'status/name')
validate_has_key(extStatus['status'], 'formattedMessage',
'status/formattedMessage')
@@ -218,7 +217,6 @@ class ExtensionInstance(object):
elif targetVersion == self.currVersion:
self.enable()
else:
#TODO downgrade is not allowed?
raise ExtensionError("A newer version has already been installed")
else:
if targetVersion > self.getVersion():
+2 -9
View File
@@ -315,8 +315,6 @@ class DefaultOSUtil(object):
def MountDvd(self, maxRetry=6, chk_err=True):
dvd = self.GetDvdDevice()
mountPoint = self.GetDvdMountPoint()
#TODO Why do we need to load atapiix?
#self.LoadAtapiixModule()
mountlist = shellutil.RunGetOutput("mount")[1]
existing = self.GetMountPoint(mountlist, dvd)
if existing is not None: #Already mounted
@@ -329,7 +327,7 @@ class DefaultOSUtil(object):
retcode = self.Mount(dvd, mountPoint, option="-o ro -t iso9660,udf",
chk_err=chk_err)
if retcode == 0:
logger.Info("Successfully mounted provision dvd")
logger.Info("Successfully mounted dvd")
return
if retry < maxRetry - 1:
logger.Warn("Mount dvd failed: retry={0}, ret={1}", retry,
@@ -418,10 +416,6 @@ class DefaultOSUtil(object):
logger.Warn("Move rules file {0} to {1}", fileName, dest)
shutil.move(src, dest)
def CheckDependencies(self):
#TODO Add dependency check
pass
def GetMacAddress(self):
"""
Convienience function, returns mac addr bound to
@@ -571,8 +565,7 @@ class DefaultOSUtil(object):
if original != timeout:
fileutil.SetFileContents(filePath, timeout)
logger.Info("Set block dev timeout: {0} with timeout: {1}",
dev,
timeout)
dev, timeout)
def GetMountPoint(self, mountlist, device):
"""
+12 -4
View File
@@ -40,7 +40,7 @@ class ProvisionHandler(object):
if os.path.isfile(provisioned):
return
logger.Info("Start provisioning.")
logger.Info("Run provision handler.")
protocol = prot.Factory.getDefaultProtocol()
try:
status = prot.ProvisionStatus(status="NotReady",
@@ -85,6 +85,7 @@ class ProvisionHandler(object):
def provision(self):
logger.Info("Copy ovf-env.xml.")
try:
ovfenv = ovf.CopyOvfEnv()
except prot.ProtocolError as e:
@@ -92,24 +93,29 @@ class ProvisionHandler(object):
password = ovfenv.getUserPassword()
ovfenv.clearUserPassword()
logger.Info("Set host name.")
OSUtil.SetHostname(ovfenv.getComputerName())
logger.Info("Publish host name.")
OSUtil.PublishHostname(ovfenv.getComputerName())
logger.Info("Create user account.")
OSUtil.UpdateUserAccount(ovfenv.getUserName(), password)
if password is not None:
userSalt = conf.GetSwitch("Provision.UseSalt", True)
saltType = conf.GetSwitch("Provision.SaltType", 6)
logger.Info("Set user password.")
OSUtil.ChangePassword(ovfenv.getUserName(), password, userSalt,
saltType)
logger.Info("Configure sshd.")
OSUtil.ConfigSshd(ovfenv.getDisableSshPasswordAuthentication())
#Disable selinux temporary
sel = OSUtil.IsSelinuxRunning()
if sel:
OSUtil.SetSelinuxEnforce(0)
self.deploySshPublicKeys(ovfenv)
self.deploySshKeyPairs(ovfenv)
self.saveCustomData(ovfenv)
@@ -124,19 +130,21 @@ class ProvisionHandler(object):
def saveCustomData(self, ovfenv):
logger.Info("Save custom data")
customData = ovfenv.getCustomData()
if customData is None:
return
#TODO port Abel's fix to decoding custom data
libDir = OSUtil.GetLibDir()
fileutil.SetFileContents(os.path.join(libDir, CustomDataFile),
OSUtil.TranslateCustomData(customData))
def deploySshPublicKeys(self, ovfenv):
for thumbprint, path in ovfenv.getSshPublicKeys():
logger.Info("Deploy ssh public key.")
OSUtil.DeploySshPublicKey(ovfenv.getUserName(), thumbprint, path)
def deploySshKeyPairs(self, ovfenv):
for thumbprint, path in ovfenv.getSshKeyPairs():
logger.Info("Deploy ssh key pairs.")
OSUtil.DeploySshKeyPair(ovfenv.getUserName(), thumbprint, path)
+5 -3
View File
@@ -40,9 +40,10 @@ class RunHandler(object):
event.EnableUnhandledErrorDump("Azure Linux Agent")
fileutil.SetFileContents(OSUtil.GetAgentPidPath(),
str(os.getpid()))
if self.handlers.scvmmHandler.detectScvmmEnv():
return
if conf.GetSwitch("DetectScvmmEnv", False):
if self.handlers.scvmmHandler.detectScvmmEnv():
return
self.handlers.dhcpHandler.probe()
@@ -71,6 +72,7 @@ class RunHandler(object):
for handlerStatus in handlerStatusList:
vmStatus.extensionHandlers.append(handlerStatus)
try:
logger.Info("Report vm status")
protocol.reportStatus(vmStatus)
except prot.ProtocolError as e:
logger.Error("Failed to report vm status: {0}", e)
+1 -1
View File
@@ -25,7 +25,7 @@ def GetOSUtil():
Ubuntu14xOSUtil
if DistroVersion == "12.04":
return Ubuntu1204OSUtil()
elif DistroVersion == "14.04" 0r DistroVersion == "14.10":
elif DistroVersion == "14.04" or DistroVersion == "14.10":
return Ubuntu14xOSUtil()
else:
return UbuntuOSUtil()
+2 -1
View File
@@ -33,12 +33,12 @@ On ubuntu image, provision could be disabled.
"""
class UbuntuProvisionHandler(ProvisionHandler):
def process(self):
logger.Info("Run ubuntu provision handler")
#If provision is enabled, run default provision handler
if conf.GetSwitch("Provisioning.Enabled", False):
super(UbuntuProvisionHandler, self).process()
return
logger.Info("Run Ubuntu provision handler")
provisioned = os.path.join(OSUtil.GetLibDir(), "provisioned")
if os.path.isfile(provisioned):
return
@@ -46,6 +46,7 @@ class UbuntuProvisionHandler(ProvisionHandler):
logger.Info("Waiting cloud-init to finish provisioning.")
protocol = prot.Factory.getDefaultProtocol()
try:
logger.Info("Wait for ssh host key to be generated.")
thumbprint = self.waitForSshHostKey()
fileutil.SetFileContents(provisioned, "")
@@ -75,6 +75,7 @@ def DetectAvailableProtocols(probeFuncs=[DetectV1, DetectV2]):
return availableProtocols
def DetectDefaultProtocol():
logger.Info("Detect default protocol.")
availableProtocols = DetectAvailableProtocols()
return ChooseDefaultProtocol(availableProtocols)
-1
View File
@@ -823,7 +823,6 @@ class Certificates(object):
shellutil.Run(cmd)
#The parsing process use public key to match prv and crt.
#TODO: Is there any way better to do so?
buf = []
beginCrt = False
beginPrv = False
-30
View File
@@ -1,30 +0,0 @@
#!/usr/bin/env python
#
# Windows Azure Linux Agent
#
# Copyright 2014 Microsoft Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Requires Python 2.4+ and Openssl 1.0+
#
# Implements parts of RFC 2131, 1541, 1497 and
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
#
import sys
import azurelinuxagent.agent as agent
if __name__ == '__main__' :
agent.Main()
+2
View File
@@ -66,3 +66,5 @@ OS.OpensslPath=None
#HttpProxy.Host=None
#HttpProxy.Port=None
# Detect Scvmm environment, default is n
# DetectScvmmEnv=n
+2
View File
@@ -66,3 +66,5 @@ OS.OpensslPath=None
#HttpProxy.Host=None
#HttpProxy.Port=None
# Detect Scvmm environment, default is n
# DetectScvmmEnv=n
+2 -3
View File
@@ -50,9 +50,6 @@ ResourceDisk.EnableSwap=n
# Size of the swapfile.
ResourceDisk.SwapSizeMB=0
# Respond to load balancer probes if requested by Windows Azure.
LBProbeResponder=y
# Enable verbose logging (y|n)
Logs.Verbose=n
@@ -66,3 +63,5 @@ OS.OpensslPath=None
#HttpProxy.Host=None
#HttpProxy.Port=None
# Detect Scvmm environment, default is n
# DetectScvmmEnv=n
+3
View File
@@ -17,6 +17,9 @@ start()
{
echo -n $"Starting $FriendlyName: "
$WAZD_BIN -start
RETVAL=$?
echo
return $RETVAL
}
stop()
+1 -1
View File
@@ -38,7 +38,7 @@ def get_data_files(name, version, fullname):
#Script file
script_dest = '/usr/sbin'
script_src = ['bin/waagent', 'bin/azurela']
script_src = ['bin/waagent']
if name == 'coreos':
script_dest = '/usr/share/oem/bin'
data_files.append((script_dest, script_src))