Permalink
Switch branches/tags
show linux-dscext-2.70.0.10 linux-dscext-2.70.0.9 linux-dscext-2.70.0.8 linux-dscext-2.70.0.7 linux-dscext-2.70.0.6 linux-dscext-2.70.0.5-2 linux-dscext-2.70.0.4 linux-dscext-2.70.0.3 linux-dscext-2.70.0.2 linux-dscext-2.70.0.1 linux-dscext-2.70.0.0 linux-dscext-2.6.0.0 linux-dscext-2.5.0.0 linux-dscext-2.4.0.0 linux-dscext-2.3.0.1 linux-dscext-2.3.0.0 linux-dscext-2.2.0.0 linux-dscext-2.1.0.0 linux-dsc-2.1.0.0 linux-diagnostic-2.3.3 linux-diagnostic-1.0.0 ladtest-3.0.112.3 ladtest-3.0.112.2 ladtest-3.0.112.1 ladtest-3.0.111.1 ladtest-3.0.110.5 ladtest-3.0.110.4 ladtest-3.0.110.3 ladtest-3.0.110.2 ladtest-3.0.110.1 ladtest-3.0.108.4 ladtest-3.0.108.3 ladtest-3.0.108.2 ladtest-3.0.108.1 ladtest-3.0.104.2 ladtest-3.0.104.1 ladtest-3.0.102.1 ladtest-3.0.100.919 ladtest-3.0.100.918 ladtest-3.0.100.917 ladtest-3.0.100.916 ladtest-3.0.100.915 ladtest-3.0.100.914 ladtest-3.0.100.913 ladtest-3.0.100.912 ladtest-3.0.100.911 ladtest-3.0.100.910 ladtest-3.0.100.909 ladtest-3.0.100.908 ladtest-3.0.100.907 ladtest-3.0.100.906 ladtest-3.0.100.905 ladtest-3.0.100.904 ladtest-3.0.100.902 ladtest-3.0.100.901 ladtest-3.0.100.9 ladtest-3.0.100.8 ladtest-3.0.100.7 ladtest-3.0.100.6 ladtest-3.0.100.5 ladtest-3.0.100.4 ladtest-3.0.100.3 ladtest-3.0.100.2 ladtest-3.0.100.1 ladtest-3.0.0.5 ladtest-3.0.0.4 ladtest-3.0.0.3 ladtest-3.0.0.2 ladtest-3.0.0.1 ladtest-2.3.9028.6 ladtest-2.3.9028.5 ladtest-2.3.9028.4 ladtest-2.3.9028.3 ladtest-2.3.9028.2 ladtest-2.3.9028.1 ladtest-2.3.9026.3 ladtest-2.3.9026.2 ladtest-2.3.9026.1 ladtest-2.3.9024.1 ladtest-2.3.9022.5 ladtest-2.3.9022.3 ladtest-2.3.9022.2 ladtest-2.3.9022.1 ladtest-2.3.9018.3 ladtest-2.3.9018.2 ladtest-2.3.9018.1 ladtest-2.3.9016.3 ladtest-2.3.9016.2 ladtest-2.3.9016.1 ladtest-2.3.9014.5 ladtest-2.3.9014.4 ladtest-2.3.9014.3 ladtest-2.3.9014.2 ladtest-2.3.9014.1 ladtest-2.3.9012.1 ladtest-2.3.9010.93 ladtest-2.3.9010.92 ladtest-2.3.9010.91 ladtest-2.3.9010.90
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
6477 lines (5860 sloc) 257 KB
#!/usr/bin/env python
#
# Azure Linux Agent
#
# Copyright 2015 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.
#
# 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 crypt
import random
import array
import base64
import httplib
import os
import os.path
import platform
import pwd
import re
import shutil
import socket
import SocketServer
import struct
import string
import subprocess
import sys
import tempfile
import textwrap
import threading
import time
import traceback
import xml.dom.minidom
import fcntl
import inspect
import zipfile
import json
import datetime
import xml.sax.saxutils
from distutils.version import LooseVersion
if not hasattr(subprocess,'check_output'):
def check_output(*popenargs, **kwargs):
r"""Backport from subprocess module from python 2.7"""
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise subprocess.CalledProcessError(retcode, cmd, output=output)
return output
# Exception classes used by this module.
class CalledProcessError(Exception):
def __init__(self, returncode, cmd, output=None):
self.returncode = returncode
self.cmd = cmd
self.output = output
def __str__(self):
return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode)
subprocess.check_output=check_output
subprocess.CalledProcessError=CalledProcessError
GuestAgentName = "WALinuxAgent"
GuestAgentLongName = "Azure Linux Agent"
GuestAgentVersion = "WALinuxAgent-2.0.16"
ProtocolVersion = "2012-11-30" #WARNING this value is used to confirm the correct fabric protocol.
Config = None
WaAgent = None
DiskActivated = False
Openssl = "openssl"
Children = []
ExtensionChildren = []
VMM_STARTUP_SCRIPT_NAME='install'
VMM_CONFIG_FILE_NAME='linuxosconfiguration.xml'
global RulesFiles
RulesFiles = [ "/lib/udev/rules.d/75-persistent-net-generator.rules",
"/etc/udev/rules.d/70-persistent-net.rules" ]
VarLibDhcpDirectories = ["/var/lib/dhclient", "/var/lib/dhcpcd", "/var/lib/dhcp"]
EtcDhcpClientConfFiles = ["/etc/dhcp/dhclient.conf", "/etc/dhcp3/dhclient.conf"]
global LibDir
LibDir = "/var/lib/waagent"
global provisioned
provisioned=False
global provisionError
provisionError=None
HandlerStatusToAggStatus = {"installed":"Installing", "enabled":"Ready", "unintalled":"NotReady", "disabled":"NotReady"}
WaagentConf = """\
#
# Azure Linux Agent Configuration
#
Role.StateConsumer=None # Specified program is invoked with the argument "Ready" when we report ready status
# to the endpoint server.
Role.ConfigurationConsumer=None # Specified program is invoked with XML file argument specifying role configuration.
Role.TopologyConsumer=None # Specified program is invoked with XML file argument specifying role topology.
Provisioning.Enabled=y #
Provisioning.DeleteRootPassword=y # Password authentication for root account will be unavailable.
Provisioning.RegenerateSshHostKeyPair=y # Generate fresh host key pair.
Provisioning.SshHostKeyPairType=rsa # Supported values are "rsa", "dsa" and "ecdsa".
Provisioning.MonitorHostName=y # Monitor host name changes and publish changes via DHCP requests.
ResourceDisk.Format=y # Format if unformatted. If 'n', resource disk will not be mounted.
ResourceDisk.Filesystem=ext4 # Typically ext3 or ext4. FreeBSD images should use 'ufs2' here.
ResourceDisk.MountPoint=/mnt/resource #
ResourceDisk.EnableSwap=n # Create and use swapfile on resource disk.
ResourceDisk.SwapSizeMB=0 # Size of the swapfile.
LBProbeResponder=y # Respond to load balancer probes if requested by Azure.
Logs.Verbose=n # Enable verbose logs
OS.RootDeviceScsiTimeout=300 # Root device timeout in seconds.
OS.OpensslPath=None # If "None", the system default version is used.
"""
README_FILENAME="DATALOSS_WARNING_README.txt"
README_FILECONTENT="""\
WARNING: THIS IS A TEMPORARY DISK.
Any data stored on this drive is SUBJECT TO LOSS and THERE IS NO WAY TO RECOVER IT.
Please do not use this disk for storing any personal or application data.
For additional details to please refer to the MSDN documentation at : http://msdn.microsoft.com/en-us/library/windowsazure/jj672979.aspx
"""
############################################################
# BEGIN DISTRO CLASS DEFS
############################################################
############################################################
# AbstractDistro
############################################################
class AbstractDistro(object):
"""
AbstractDistro defines a skeleton neccesary for a concrete Distro class.
Generic methods and attributes are kept here, distribution specific attributes
and behavior are to be placed in the concrete child named distroDistro, where
distro is the string returned by calling python platform.linux_distribution()[0].
So for CentOS the derived class is called 'centosDistro'.
"""
def __init__(self):
"""
Generic Attributes go here. These are based on 'majority rules'.
This __init__() may be called or overriden by the child.
"""
self.agent_service_name = os.path.basename(sys.argv[0])
self.selinux=None
self.service_cmd='/usr/sbin/service'
self.ssh_service_restart_option='restart'
self.ssh_service_name='ssh'
self.ssh_config_file='/etc/ssh/sshd_config'
self.hostname_file_path='/etc/hostname'
self.dhcp_client_name='dhclient'
self.requiredDeps = [ 'route', 'shutdown', 'ssh-keygen', 'useradd', 'usermod',
'openssl', 'sfdisk', 'fdisk', 'mkfs',
'sed', 'grep', 'sudo', 'parted' ]
self.init_script_file='/etc/init.d/waagent'
self.agent_package_name='WALinuxAgent'
self.fileBlackList = [ "/root/.bash_history", "/var/log/waagent.log",'/etc/resolv.conf' ]
self.agent_files_to_uninstall = ["/etc/waagent.conf", "/etc/logrotate.d/waagent"]
self.grubKernelBootOptionsFile = '/etc/default/grub'
self.grubKernelBootOptionsLine = 'GRUB_CMDLINE_LINUX_DEFAULT='
self.getpidcmd = 'pidof'
self.mount_dvd_cmd = 'mount'
self.sudoers_dir_base = '/etc'
self.waagent_conf_file = WaagentConf
self.shadow_file_mode=0600
self.shadow_file_path="/etc/shadow"
self.dhcp_enabled = False
def isSelinuxSystem(self):
"""
Checks and sets self.selinux = True if SELinux is available on system.
"""
if self.selinux == None:
if Run("which getenforce",chk_err=False):
self.selinux = False
else:
self.selinux = True
return self.selinux
def isSelinuxRunning(self):
"""
Calls shell command 'getenforce' and returns True if 'Enforcing'.
"""
if self.isSelinuxSystem():
return RunGetOutput("getenforce")[1].startswith("Enforcing")
else:
return False
def setSelinuxEnforce(self,state):
"""
Calls shell command 'setenforce' with 'state' and returns resulting exit code.
"""
if self.isSelinuxSystem():
if state: s = '1'
else: s='0'
return Run("setenforce "+s)
def setSelinuxContext(self,path,cn):
"""
Calls shell 'chcon' with 'path' and 'cn' context.
Returns exit result.
"""
if self.isSelinuxSystem():
return Run('chcon ' + cn + ' ' + path)
def setHostname(self,name):
"""
Shell call to hostname.
Returns resulting exit code.
"""
return Run('hostname ' + name)
def publishHostname(self,name):
"""
Set the contents of the hostname file to 'name'.
Return 1 on failure.
"""
try:
r=SetFileContents(self.hostname_file_path, name)
for f in EtcDhcpClientConfFiles:
if os.path.exists(f) and FindStringInFile(f,r'^[^#]*?send\s*host-name.*?(<hostname>|gethostname[(,)])') == None :
r=ReplaceFileContentsAtomic('/etc/dhcp/dhclient.conf', "send host-name \"" + name + "\";\n"
+ "\n".join(filter(lambda a: not a.startswith("send host-name"), GetFileContents('/etc/dhcp/dhclient.conf').split('\n'))))
except:
return 1
return r
def installAgentServiceScriptFiles(self):
"""
Create the waagent support files for service installation.
Called by registerAgentService()
Abstract Virtual Function. Over-ridden in concrete Distro classes.
"""
pass
def registerAgentService(self):
"""
Calls installAgentService to create service files.
Shell exec service registration commands. (e.g. chkconfig --add waagent)
Abstract Virtual Function. Over-ridden in concrete Distro classes.
"""
pass
def uninstallAgentService(self):
"""
Call service subsystem to remove waagent script.
Abstract Virtual Function. Over-ridden in concrete Distro classes.
"""
pass
def unregisterAgentService(self):
"""
Calls self.stopAgentService and call self.uninstallAgentService()
"""
self.stopAgentService()
self.uninstallAgentService()
def startAgentService(self):
"""
Service call to start the Agent service
"""
return Run(self.service_cmd + ' ' + self.agent_service_name + ' start')
def stopAgentService(self):
"""
Service call to stop the Agent service
"""
return Run(self.service_cmd + ' ' + self.agent_service_name + ' stop',False)
def restartSshService(self):
"""
Service call to re(start) the SSH service
"""
sshRestartCmd = self.service_cmd + " " + self.ssh_service_name + " " + self.ssh_service_restart_option
retcode = Run(sshRestartCmd)
if retcode > 0:
Error("Failed to restart SSH service with return code:" + str(retcode))
return retcode
def sshDeployPublicKey(self,fprint,path):
"""
Generic sshDeployPublicKey - over-ridden in some concrete Distro classes due to minor differences in openssl packages deployed
"""
error=0
SshPubKey = OvfEnv().OpensslToSsh(fprint)
if SshPubKey != None:
AppendFileContents(path, SshPubKey)
else:
Error("Failed: " + fprint + ".crt -> " + path)
error = 1
return error
def checkPackageInstalled(self,p):
"""
Query package database for prescence of an installed package.
Abstract Virtual Function. Over-ridden in concrete Distro classes.
"""
pass
def checkPackageUpdateable(self,p):
"""
Online check if updated package of walinuxagent is available.
Abstract Virtual Function. Over-ridden in concrete Distro classes.
"""
pass
def deleteRootPassword(self):
"""
Generic root password removal.
"""
filepath="/etc/shadow"
ReplaceFileContentsAtomic(filepath,"root:*LOCK*:14600::::::\n"
+ "\n".join(filter(lambda a: not a.startswith("root:"),GetFileContents(filepath).split('\n'))))
os.chmod(filepath,self.shadow_file_mode)
if self.isSelinuxSystem():
self.setSelinuxContext(filepath,'system_u:object_r:shadow_t:s0')
Log("Root password deleted.")
return 0
def changePass(self,user,password):
Log("Change user password")
crypt_id = Config.get("Provisioning.PasswordCryptId")
if crypt_id is None:
crypt_id = "6"
salt_len = Config.get("Provisioning.PasswordCryptSaltLength")
try:
salt_len = int(salt_len)
if salt_len < 0 or salt_len > 10:
salt_len = 10
except (ValueError, TypeError):
salt_len = 10
return self.chpasswd(user, password, crypt_id=crypt_id,
salt_len=salt_len)
def chpasswd(self, username, password, crypt_id=6, salt_len=10):
passwd_hash = self.gen_password_hash(password, crypt_id, salt_len)
cmd = "usermod -p '{0}' {1}".format(passwd_hash, username)
ret, output = RunGetOutput(cmd, log_cmd=False)
if ret != 0:
return "Failed to set password for {0}: {1}".format(username, output)
def gen_password_hash(self, password, crypt_id, salt_len):
collection = string.ascii_letters + string.digits
salt = ''.join(random.choice(collection) for _ in range(salt_len))
salt = "${0}${1}".format(crypt_id, salt)
return crypt.crypt(password, salt)
def load_ata_piix(self):
return WaAgent.TryLoadAtapiix()
def unload_ata_piix(self):
"""
Generic function to remove ata_piix.ko.
"""
return WaAgent.TryUnloadAtapiix()
def deprovisionWarnUser(self):
"""
Generic user warnings used at deprovision.
"""
print("WARNING! Nameserver configuration in /etc/resolv.conf will be deleted.")
def deprovisionDeleteFiles(self):
"""
Files to delete when VM is deprovisioned
"""
for a in VarLibDhcpDirectories:
Run("rm -f " + a + "/*")
# Clear LibDir, remove nameserver and root bash history
for f in os.listdir(LibDir) + self.fileBlackList:
try:
os.remove(f)
except:
pass
return 0
def uninstallDeleteFiles(self):
"""
Files to delete when agent is uninstalled.
"""
for f in self.agent_files_to_uninstall:
try:
os.remove(f)
except:
pass
return 0
def checkDependencies(self):
"""
Generic dependency check.
Return 1 unless all dependencies are satisfied.
"""
if self.checkPackageInstalled('NetworkManager'):
Error(GuestAgentLongName + " is not compatible with network-manager.")
return 1
try:
m= __import__('pyasn1')
except ImportError:
Error(GuestAgentLongName + " requires python-pyasn1 for your Linux distribution.")
return 1
for a in self.requiredDeps:
if Run("which " + a + " > /dev/null 2>&1",chk_err=False):
Error("Missing required dependency: " + a)
return 1
return 0
def packagedInstall(self,buildroot):
"""
Called from setup.py for use by RPM.
Copies generated files waagent.conf, under the buildroot.
"""
if not os.path.exists(buildroot+'/etc'):
os.mkdir(buildroot+'/etc')
SetFileContents(buildroot+'/etc/waagent.conf', MyDistro.waagent_conf_file)
if not os.path.exists(buildroot+'/etc/logrotate.d'):
os.mkdir(buildroot+'/etc/logrotate.d')
SetFileContents(buildroot+'/etc/logrotate.d/waagent', WaagentLogrotate)
self.init_script_file=buildroot+self.init_script_file
# this allows us to call installAgentServiceScriptFiles()
if not os.path.exists(os.path.dirname(self.init_script_file)):
os.mkdir(os.path.dirname(self.init_script_file))
self.installAgentServiceScriptFiles()
def GetIpv4Address(self):
"""
Return the ip of the
first active non-loopback interface.
"""
addr=''
iface,addr=GetFirstActiveNetworkInterfaceNonLoopback()
return addr
def GetMacAddress(self):
return GetMacAddress()
def GetInterfaceName(self):
return GetFirstActiveNetworkInterfaceNonLoopback()[0]
def RestartInterface(self, iface, max_retry=3):
for retry in range(1, max_retry + 1):
ret = Run("ifdown " + iface + " && ifup " + iface)
if ret == 0:
return
Log("Failed to restart interface: {0}, ret={1}".format(iface, ret))
if retry < max_retry:
Log("Retry restart interface in 5 seconds")
time.sleep(5)
def CreateAccount(self,user, password, expiration, thumbprint):
return CreateAccount(user, password, expiration, thumbprint)
def DeleteAccount(self,user):
return DeleteAccount(user)
def ActivateResourceDisk(self):
"""
Format, mount, and if specified in the configuration
set resource disk as swap.
"""
global DiskActivated
format = Config.get("ResourceDisk.Format")
if format == None or format.lower().startswith("n"):
DiskActivated = True
return
device = DeviceForIdePort(1)
if device == None:
Error("ActivateResourceDisk: Unable to detect disk topology.")
return
device = "/dev/" + device
mountlist = RunGetOutput("mount")[1]
mountpoint = GetMountPoint(mountlist, device)
if(mountpoint):
Log("ActivateResourceDisk: " + device + "1 is already mounted.")
else:
mountpoint = Config.get("ResourceDisk.MountPoint")
if mountpoint == None:
mountpoint = "/mnt/resource"
CreateDir(mountpoint, "root", 0755)
fs = Config.get("ResourceDisk.Filesystem")
if fs == None:
fs = "ext3"
partition = device + "1"
#Check partition type
Log("Detect GPT...")
ret = RunGetOutput("parted {0} print".format(device))
if ret[0] == 0 and "gpt" in ret[1]:
Log("GPT detected.")
#GPT(Guid Partition Table) is used.
#Get partitions.
parts = filter(lambda x : re.match("^\s*[0-9]+", x), ret[1].split("\n"))
#If there are more than 1 partitions, remove all partitions
#and create a new one using the entire disk space.
if len(parts) > 1:
for i in range(1, len(parts) + 1):
Run("parted {0} rm {1}".format(device, i))
Run("parted {0} mkpart primary 0% 100%".format(device))
Run("mkfs." + fs + " " + partition + " -F")
else:
existingFS = RunGetOutput("sfdisk -q -c " + device + " 1", chk_err=False)[1].rstrip()
if existingFS == "7" and fs != "ntfs":
Run("sfdisk -c " + device + " 1 83")
Run("mkfs." + fs + " " + partition)
if Run("mount " + partition + " " + mountpoint, chk_err=False):
#If mount failed, try to format the partition and mount again
Warn("Failed to mount resource disk. Retry mounting.")
Run("mkfs." + fs + " " + partition + " -F")
if Run("mount " + partition + " " + mountpoint):
Error("ActivateResourceDisk: Failed to mount resource disk (" + partition + ").")
return
Log("Resource disk (" + partition + ") is mounted at " + mountpoint + " with fstype " + fs)
#Create README file under the root of resource disk
SetFileContents(os.path.join(mountpoint,README_FILENAME), README_FILECONTENT)
DiskActivated = True
#Create swap space
swap = Config.get("ResourceDisk.EnableSwap")
if swap == None or swap.lower().startswith("n"):
return
sizeKB = int(Config.get("ResourceDisk.SwapSizeMB")) * 1024
if os.path.isfile(mountpoint + "/swapfile") and os.path.getsize(mountpoint + "/swapfile") != (sizeKB * 1024):
os.remove(mountpoint + "/swapfile")
if not os.path.isfile(mountpoint + "/swapfile"):
Run("dd if=/dev/zero of=" + mountpoint + "/swapfile bs=1024 count=" + str(sizeKB))
Run("mkswap " + mountpoint + "/swapfile")
Run("chmod 600 " + mountpoint + "/swapfile")
if not Run("swapon " + mountpoint + "/swapfile"):
Log("Enabled " + str(sizeKB) + " KB of swap at " + mountpoint + "/swapfile")
else:
Error("ActivateResourceDisk: Failed to activate swap at " + mountpoint + "/swapfile")
def Install(self):
return Install()
def mediaHasFilesystem(self,dsk):
if len(dsk) == 0 :
return False
if Run("LC_ALL=C fdisk -l " + dsk + " | grep Disk"):
return False
return True
def mountDVD(self,dvd,location):
return RunGetOutput(self.mount_dvd_cmd + ' ' + dvd + ' ' + location)
def GetHome(self):
return GetHome()
def getDhcpClientName(self):
return self.dhcp_client_name
def initScsiDiskTimeout(self):
"""
Set the SCSI disk timeout when the agent starts running
"""
self.setScsiDiskTimeout()
def setScsiDiskTimeout(self):
"""
Iterate all SCSI disks(include hot-add) and set their timeout if their value are different from the OS.RootDeviceScsiTimeout
"""
try:
scsiTimeout = Config.get("OS.RootDeviceScsiTimeout")
for diskName in [disk for disk in os.listdir("/sys/block") if disk.startswith("sd")]:
self.setBlockDeviceTimeout(diskName, scsiTimeout)
except:
pass
def setBlockDeviceTimeout(self, device, timeout):
"""
Set SCSI disk timeout by set /sys/block/sd*/device/timeout
"""
if timeout != None and device:
filePath = "/sys/block/" + device + "/device/timeout"
if(GetFileContents(filePath).splitlines()[0].rstrip() != timeout):
SetFileContents(filePath,timeout)
Log("SetBlockDeviceTimeout: Update the device " + device + " with timeout " + timeout)
def waitForSshHostKey(self, path):
"""
Provide a dummy waiting, since by default, ssh host key is created by waagent and the key
should already been created.
"""
if(os.path.isfile(path)):
return True
else:
Error("Can't find host key: {0}".format(path))
return False
def isDHCPEnabled(self):
return self.dhcp_enabled
def stopDHCP(self):
"""
Stop the system DHCP client so that the agent can bind on its port. If
the distro has set dhcp_enabled to True, it will need to provide an
implementation of this method.
"""
raise NotImplementedError('stopDHCP method missing')
def startDHCP(self):
"""
Start the system DHCP client. If the distro has set dhcp_enabled to
True, it will need to provide an implementation of this method.
"""
raise NotImplementedError('startDHCP method missing')
def translateCustomData(self, data):
"""
Translate the custom data from a Base64 encoding. Default to no-op.
"""
decodeCustomData = Config.get("Provisioning.DecodeCustomData")
if decodeCustomData != None and decodeCustomData.lower().startswith("y"):
return base64.b64decode(data)
return data
def getConfigurationPath(self):
return "/etc/waagent.conf"
def getProcessorCores(self):
return int(RunGetOutput("grep 'processor.*:' /proc/cpuinfo |wc -l")[1])
def getTotalMemory(self):
return int(RunGetOutput("grep MemTotal /proc/meminfo |awk '{print $2}'")[1])/1024
def getInterfaceNameByMac(self, mac):
ret, output = RunGetOutput("ifconfig -a")
if ret != 0:
raise Exception("Failed to get network interface info")
output = output.replace('\n', '')
match = re.search(r"(eth\d).*(HWaddr|ether) {0}".format(mac),
output, re.IGNORECASE)
if match is None:
raise Exception("Failed to get ifname with mac: {0}".format(mac))
output = match.group(0)
eths = re.findall(r"eth\d", output)
if eths is None or len(eths) == 0:
raise Exception("Failed to get ifname with mac: {0}".format(mac))
return eths[-1]
def configIpV4(self, ifName, addr, netmask=24):
ret, output = RunGetOutput("ifconfig {0} up".format(ifName))
if ret != 0:
raise Exception("Failed to bring up {0}: {1}".format(ifName,
output))
ret, output = RunGetOutput("ifconfig {0} {1}/{2}".format(ifName, addr,
netmask))
if ret != 0:
raise Exception("Failed to config ipv4 for {0}: {1}".format(ifName,
output))
def setDefaultGateway(self, gateway):
Run("/sbin/route add default gw" + gateway, chk_err=False)
def routeAdd(self, net, mask, gateway):
Run("/sbin/route add -net " + net + " netmask " + mask + " gw " + gateway,
chk_err=False)
def getNdDriverVersion(self):
"""
if error happens, raise a RdmaError
"""
try:
with open("/var/lib/hyperv/.kvp_pool_0", "r") as f:
lines = f.read()
r = re.search("NdDriverVersion\0+(\d\d\d\.\d)", lines)
if r is not None:
NdDriverVersion = r.groups()[0]
return NdDriverVersion #e.g. NdDriverVersion = 142.0
else :
Log("Error: NdDriverVersion not found.")
return None
except Exception as e:
errMsg = 'Cannot update status: Failed to enable the extension with error: %s, stack trace: %s' % (str(e), traceback.format_exc())
Log(errMsg)
raise RdmaError(RdmaConfig.nd_driver_detect_error)
def checkInstallHyperV(self):
return None
def getRdmaPackageVersion(self):
return None
def rdmaUpdate(self,updateRdmaRepository=None):
Log("rdmaUpdate in base class")
pass
def checkRDMA(self):
Log("checkRDMA in base class")
pass
############################################################
# GentooDistro
############################################################
gentoo_init_file = """\
#!/sbin/runscript
command=/usr/sbin/waagent
pidfile=/var/run/waagent.pid
command_args=-daemon
command_background=true
name="Azure Linux Agent"
depend()
{
need localmount
use logger network
after bootmisc modules
}
"""
class gentooDistro(AbstractDistro):
"""
Gentoo distro concrete class
"""
def __init__(self): #
super(gentooDistro,self).__init__()
self.service_cmd='/sbin/service'
self.ssh_service_name='sshd'
self.hostname_file_path='/etc/conf.d/hostname'
self.dhcp_client_name='dhcpcd'
self.shadow_file_mode=0640
self.init_file=gentoo_init_file
def publishHostname(self,name):
try:
if (os.path.isfile(self.hostname_file_path)):
r=ReplaceFileContentsAtomic(self.hostname_file_path, "hostname=\"" + name + "\"\n"
+ "\n".join(filter(lambda a: not a.startswith("hostname="), GetFileContents(self.hostname_file_path).split("\n"))))
except:
return 1
return r
def installAgentServiceScriptFiles(self):
SetFileContents(self.init_script_file, self.init_file)
os.chmod(self.init_script_file, 0755)
def registerAgentService(self):
self.installAgentServiceScriptFiles()
return Run('rc-update add ' + self.agent_service_name + ' default')
def uninstallAgentService(self):
return Run('rc-update del ' + self.agent_service_name + ' default')
def unregisterAgentService(self):
self.stopAgentService()
return self.uninstallAgentService()
def checkPackageInstalled(self,p):
if Run('eix -I ^' + p + '$',chk_err=False):
return 0
else:
return 1
def checkPackageUpdateable(self,p):
if Run('eix -u ^' + p + '$',chk_err=False):
return 0
else:
return 1
def RestartInterface(self, iface):
Run("/etc/init.d/net." + iface + " restart")
############################################################
# SuSEDistro
############################################################
suse_init_file = """\
#! /bin/sh
#
# Azure Linux Agent sysV init script
#
# Copyright 2013 Microsoft Corporation
# Copyright SUSE LLC
#
# 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.
#
# /etc/init.d/waagent
#
# and symbolic link
#
# /usr/sbin/rcwaagent
#
# System startup script for the waagent
#
### BEGIN INIT INFO
# Provides: AzureLinuxAgent
# Required-Start: $network sshd
# Required-Stop: $network sshd
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start the AzureLinuxAgent
### END INIT INFO
PYTHON=/usr/bin/python
WAZD_BIN=/usr/sbin/waagent
WAZD_CONF=/etc/waagent.conf
WAZD_PIDFILE=/var/run/waagent.pid
test -x "$WAZD_BIN" || { echo "$WAZD_BIN not installed"; exit 5; }
test -e "$WAZD_CONF" || { echo "$WAZD_CONF not found"; exit 6; }
. /etc/rc.status
# First reset status of this service
rc_reset
# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - misc error
# 2 - invalid or excess args
# 3 - unimplemented feature (e.g. reload)
# 4 - insufficient privilege
# 5 - program not installed
# 6 - program not configured
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.
case "$1" in
start)
echo -n "Starting AzureLinuxAgent"
## Start daemon with startproc(8). If this fails
## the echo return value is set appropriate.
startproc -f ${PYTHON} ${WAZD_BIN} -daemon
rc_status -v
;;
stop)
echo -n "Shutting down AzureLinuxAgent"
## Stop daemon with killproc(8) and if this fails
## set echo the echo return value.
killproc -p ${WAZD_PIDFILE} ${PYTHON} ${WAZD_BIN}
rc_status -v
;;
try-restart)
## Stop the service and if this succeeds (i.e. the
## service was running before), start it again.
$0 status >/dev/null && $0 restart
rc_status
;;
restart)
## Stop the service and regardless of whether it was
## running or not, start it again.
$0 stop
sleep 1
$0 start
rc_status
;;
force-reload|reload)
rc_status
;;
status)
echo -n "Checking for service AzureLinuxAgent "
## Check status with checkproc(8), if process is running
## checkproc will return with exit status 0.
checkproc -p ${WAZD_PIDFILE} ${PYTHON} ${WAZD_BIN}
rc_status -v
;;
probe)
;;
*)
echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
exit 1
;;
esac
rc_exit
"""
class SuSEDistro(AbstractDistro):
"""
SuSE Distro concrete class
Put SuSE specific behavior here...
"""
def __init__(self):
super(SuSEDistro,self).__init__()
dist_info = DistInfo()
dist_info_fullname = DistInfo(fullname=1)
self.dhcp_client_name = 'dhcpcd'
if ((dist_info_fullname[0] == 'SUSE Linux Enterprise Server' and dist_info[1] >= '12') or \
(dist_info_fullname[0] == 'openSUSE' and dist_info[1] >= '13.2')):
self.dhcp_client_name = 'wickedd-dhcp4'
self.dhcp_enabled = True
self.grubKernelBootOptionsFile = '/boot/grub/menu.lst'
self.grubKernelBootOptionsLine = 'kernel'
self.getpidcmd = 'pidof '
self.hostname_file_path = '/etc/HOSTNAME'
self.init_file = suse_init_file
self.kernel_boot_options_file = '/boot/grub/menu.lst'
self.modprobe_path = '/usr/bin/modprobe'
self.requiredDeps += [ "/sbin/insserv" ]
self.reboot_path = '/sbin/reboot'
self.rpm_path = '/bin/rpm'
self.service_cmd = '/sbin/service'
self.ssh_service_name ='sshd'
if(dist_info[1] == "11"):
self.ps_path = '/bin/ps'
else:
self.ps_path = '/usr/bin/ps'
self.zypper_path = '/usr/bin/zypper'
def checkPackageInstalled(self,p):
if Run("rpm -q " + p,chk_err=False):
return 0
else:
return 1
def checkPackageUpdateable(self,p):
if Run("zypper list-updates | grep " + p,chk_err=False):
return 1
else:
return 0
def installAgentServiceScriptFiles(self):
try:
SetFileContents(self.init_script_file, self.init_file)
os.chmod(self.init_script_file, 0744)
except:
pass
def registerAgentService(self):
self.installAgentServiceScriptFiles()
return Run('insserv ' + self.agent_service_name)
def uninstallAgentService(self):
return Run('insserv -r ' + self.agent_service_name)
def unregisterAgentService(self):
self.stopAgentService()
return self.uninstallAgentService()
def startDHCP(self):
Run("service " + self.dhcp_client_name + " start", chk_err=False)
def stopDHCP(self):
Run("service " + self.dhcp_client_name + " stop", chk_err=False)
def getRdmaPackageVersion(self):
"""
"""
error, output = RunGetOutput(self.zypper_path + " info " + RdmaConfig.rmda_package_name)
if(error == RdmaConfig.process_success):
r = re.search("Version: (\S+)", output)
if r is not None:
package_version = r.groups()[0] # e.g. package_version is "20150707.140.0_k3.12.28_4-3.1."
return package_version
else:
return None
else:
return None
def checkInstallHyperV(self):
error, output = RunGetOutput(self.ps_path + " -ef")
if(error != RdmaConfig.process_success):
return RdmaConfig.common_failed
else:
hv_kvp_daemon_service_process_name = "hv_kvp_daemon"
hv_kvp_daemon_service_name = "hv_kvp_daemon"
r = re.search(hv_kvp_daemon_service_process_name, output)
if r is None :
# if the
Log("hv kvp daemon is not running.")
error,output = RunGetOutput(self.rpm_path + " -q hyper-v", chk_err=False,log_cmd=False)
if(error == RdmaConfig.process_success):
Log("the hyper-v package is installed, but hv_kvp_daemon not started")
return RdmaConfig.hv_kvp_daemon_not_started
else:
error,output = RunGetOutput(self.zypper_path + " -n install --force hyper-v")
Log("install hyper-v return code: " + str(error) + " output:" + str(output))
if(error != RdmaConfig.process_success):
return RdmaConfig.common_failed
else:
self.rebootMachine()
return RdmaConfig.process_success
else :
Log("KVP daemon is running")
return RdmaConfig.process_success
def rdmaUpdate(self,updateRdmaRepository=None):
# give some time for the hv_hvp_daemon to start up.
time.sleep(10)
check_install_result = self.checkInstallHyperV()
if(check_install_result == RdmaConfig.process_success):
# wait for sometime the RDMA Driver not passed in by KVP in time.
time.sleep(10)
nd_driver_version = self.getNdDriverVersion()
if(nd_driver_version is None):
raise RdmaError(RdmaConfig.driver_version_not_found)
else:
check_result = self.checkRDMA(nd_driver_version=nd_driver_version)
Log("RDMA version check result is " + str(check_result))
if(check_result == RdmaConfig.UpToDate):
return
elif(check_result == RdmaConfig.OutOfDate):
update_rdma_driver_result = self.rdmaUpdatePackage(host_version=nd_driver_version,updateRdmaRepository=updateRdmaRepository)
elif(check_result == RdmaConfig.DriverVersionNotFound):
raise RdmaError(RdmaConfig.driver_version_not_found)
elif(check_result == RdmaConfig.Unknown):
raise RdmaError(RdmaConfig.unknown_error)
else:
raise RdmaError(RdmaConfig.check_install_hv_utils_failed)
def rdmaUpdatePackage(self, host_version, updateRdmaRepository = None):
# check the repository first
if(updateRdmaRepository is not None):
error,output = RunGetOutput(self.zypper_path + " lr -u")
rdma_pack_repository_name = "msft-rdma-pack"
rdma_pack_result = re.search(rdma_pack_repository_name, output)
if rdma_pack_result is None :
Log("rdma_pack_result is None")
error, output = RunGetOutput(self.zypper_path + " ar " + str(updateRdmaRepository) + " " + rdma_pack_repository_name)
#wait for the cache build.
time.sleep(20)
Log("error result is " + str(error) + " output is : " + str(output))
else:
Log("output is: " + str(output))
Log("msft-rdma-pack found")
returnCode, message = RunGetOutput(self.zypper_path + " --no-gpg-checks refresh")
Log("refresh repo return code is " + str(returnCode) + " output is: " + str(message))
#install the wrapper package, that will put the driver RPM packages under /opt/microsoft/rdma
returnCode, message = RunGetOutput(self.zypper_path + " -n remove " + RdmaConfig.wrapper_package_name)
Log("remove wrapper package return code is " + str(returnCode) + " output is: " + str(message))
returnCode, message = RunGetOutput(self.zypper_path + " --non-interactive install --force " + RdmaConfig.wrapper_package_name)
Log("install wrapper package return code is " + str(returnCode) + " output is: " + str(message))
r = os.listdir("/opt/microsoft/rdma")
if r is not None :
for filename in r :
if re.match(RdmaConfig.rmda_package_name + "-\d{8}\.(%s).+" % host_version, filename) :
error, output = RunGetOutput(self.zypper_path + " --non-interactive remove " + RdmaConfig.rmda_package_name)
Log("remove rdma package result is " + str(error) + " output is: " + str(output))
Log("Installing RPM /opt/microsoft/rdma/" + filename)
error, output = RunGetOutput(self.zypper_path + " --non-interactive install --force /opt/microsoft/rdma/%s" % filename)
Log("Install rdma package result is " + str(error) + " output is: " + str(output))
if(error == RdmaConfig.process_success):
self.rebootMachine()
else:
raise RdmaError(RdmaConfig.package_install_failed)
else:
Log("RDMA drivers not found in /opt/microsoft/rdma")
raise RdmaError(RdmaConfig.package_not_found)
def checkRDMA(self, nd_driver_version = None):
if(nd_driver_version is None):
nd_driver_version = self.getNdDriverVersion()
if(nd_driver_version is None or nd_driver_version == ""):
return RdmaConfig.DriverVersionNotFound
package_version = self.getRdmaPackageVersion()
if(package_version is None or package_version == ""):
return RdmaConfig.OutOfDate
else:
# package_version would be like this :20150707_k3.12.28_4-3.1 20150707.140.0_k3.12.28_4-1.1
# nd_driver_version 140.0
Log("nd_driver_version is " + str(nd_driver_version) + " package_version is " + str(package_version))
if(nd_driver_version is not None):
r = re.match("^[0-9]+[.](%s).+" % nd_driver_version, package_version)# NdDriverVersion should be at the end of package version
if not r : #host ND version is the same as the package version, do an update
return RdmaConfig.OutOfDate
else:
return RdmaConfig.UpToDate
return RdmaConfig.Unknown
def rebootMachine(self):
Log("rebooting the machine")
RunGetOutput(self.reboot_path)
############################################################
# redhatDistro
############################################################
redhat_init_file= """\
#!/bin/bash
#
# Init file for AzureLinuxAgent.
#
# chkconfig: 2345 60 80
# description: AzureLinuxAgent
#
# source function library
. /etc/rc.d/init.d/functions
RETVAL=0
FriendlyName="AzureLinuxAgent"
WAZD_BIN=/usr/sbin/waagent
start()
{
echo -n $"Starting $FriendlyName: "
$WAZD_BIN -daemon &
}
stop()
{
echo -n $"Stopping $FriendlyName: "
killproc -p /var/run/waagent.pid $WAZD_BIN
RETVAL=$?
echo
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
reload)
;;
report)
;;
status)
status $WAZD_BIN
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
RETVAL=1
esac
exit $RETVAL
"""
class redhatDistro(AbstractDistro):
"""
Redhat Distro concrete class
Put Redhat specific behavior here...
"""
def __init__(self):
super(redhatDistro,self).__init__()
self.service_cmd='/sbin/service'
self.ssh_service_restart_option='condrestart'
self.ssh_service_name='sshd'
self.hostname_file_path= None if DistInfo()[1] < '7.0' else '/etc/hostname'
self.init_file=redhat_init_file
self.grubKernelBootOptionsFile = '/boot/grub/menu.lst'
self.grubKernelBootOptionsLine = 'kernel'
def publishHostname(self,name):
super(redhatDistro,self).publishHostname(name)
if DistInfo()[1] < '7.0' :
filepath = "/etc/sysconfig/network"
if os.path.isfile(filepath):
ReplaceFileContentsAtomic(filepath, "HOSTNAME=" + name + "\n"
+ "\n".join(filter(lambda a: not a.startswith("HOSTNAME"), GetFileContents(filepath).split('\n'))))
ethernetInterface = MyDistro.GetInterfaceName()
filepath = "/etc/sysconfig/network-scripts/ifcfg-" + ethernetInterface
if os.path.isfile(filepath):
ReplaceFileContentsAtomic(filepath, "DHCP_HOSTNAME=" + name + "\n"
+ "\n".join(filter(lambda a: not a.startswith("DHCP_HOSTNAME"), GetFileContents(filepath).split('\n'))))
return 0
def installAgentServiceScriptFiles(self):
SetFileContents(self.init_script_file, self.init_file)
os.chmod(self.init_script_file, 0744)
return 0
def registerAgentService(self):
self.installAgentServiceScriptFiles()
return Run('chkconfig --add waagent')
def uninstallAgentService(self):
return Run('chkconfig --del ' + self.agent_service_name)
def unregisterAgentService(self):
self.stopAgentService()
return self.uninstallAgentService()
def checkPackageInstalled(self,p):
if Run("yum list installed " + p,chk_err=False):
return 0
else:
return 1
def checkPackageUpdateable(self,p):
if Run("yum check-update | grep "+ p,chk_err=False):
return 1
else:
return 0
def checkDependencies(self):
"""
Generic dependency check.
Return 1 unless all dependencies are satisfied.
"""
if DistInfo()[1] < '7.0' and self.checkPackageInstalled('NetworkManager'):
Error(GuestAgentLongName + " is not compatible with network-manager.")
return 1
try:
m= __import__('pyasn1')
except ImportError:
Error(GuestAgentLongName + " requires python-pyasn1 for your Linux distribution.")
return 1
for a in self.requiredDeps:
if Run("which " + a + " > /dev/null 2>&1",chk_err=False):
Error("Missing required dependency: " + a)
return 1
return 0
############################################################
# centosDistro
############################################################
class centosDistro(redhatDistro):
"""
CentOS Distro concrete class
Put CentOS specific behavior here...
"""
def __init__(self):
super(centosDistro,self).__init__()
def rdmaUpdate(self,updateRdmaRepository=None):
pass
def checkRDMA(self):
pass
############################################################
# oracleDistro
############################################################
class oracleDistro(redhatDistro):
"""
Oracle Distro concrete class
Put Oracle specific behavior here...
"""
def __init__(self):
super(oracleDistro, self).__init__()
############################################################
# asianuxDistro
############################################################
class asianuxDistro(redhatDistro):
"""
Asianux Distro concrete class
Put Asianux specific behavior here...
"""
def __init__(self):
super(asianuxDistro,self).__init__()
############################################################
# CoreOSDistro
############################################################
class CoreOSDistro(AbstractDistro):
"""
CoreOS Distro concrete class
Put CoreOS specific behavior here...
"""
CORE_UID = 500
def __init__(self):
super(CoreOSDistro,self).__init__()
self.requiredDeps += [ "/usr/bin/systemctl" ]
self.agent_service_name = 'waagent'
self.init_script_file='/etc/systemd/system/waagent.service'
self.fileBlackList.append("/etc/machine-id")
self.dhcp_client_name='systemd-networkd'
self.getpidcmd='pidof '
self.shadow_file_mode=0640
self.waagent_path='/usr/share/oem/bin'
self.python_path='/usr/share/oem/python/bin'
self.dhcp_enabled=True
if 'PATH' in os.environ:
os.environ['PATH'] = "{0}:{1}".format(os.environ['PATH'], self.python_path)
else:
os.environ['PATH'] = self.python_path
if 'PYTHONPATH' in os.environ:
os.environ['PYTHONPATH'] = "{0}:{1}".format(os.environ['PYTHONPATH'], self.waagent_path)
else:
os.environ['PYTHONPATH'] = self.waagent_path
def checkPackageInstalled(self,p):
"""
There is no package manager in CoreOS. Return 1 since it must be preinstalled.
"""
return 1
def checkDependencies(self):
for a in self.requiredDeps:
if Run("which " + a + " > /dev/null 2>&1",chk_err=False):
Error("Missing required dependency: " + a)
return 1
return 0
def checkPackageUpdateable(self,p):
"""
There is no package manager in CoreOS. Return 0 since it can't be updated via package.
"""
return 0
def startAgentService(self):
return Run('systemctl start ' + self.agent_service_name)
def stopAgentService(self):
return Run('systemctl stop ' + self.agent_service_name)
def restartSshService(self):
"""
SSH is socket activated on CoreOS. No need to restart it.
"""
return 0
def sshDeployPublicKey(self,fprint,path):
"""
We support PKCS8.
"""
if Run("ssh-keygen -i -m PKCS8 -f " + fprint + " >> " + path):
return 1
else :
return 0
def RestartInterface(self, iface):
Run("systemctl restart systemd-networkd")
def CreateAccount(self, user, password, expiration, thumbprint):
"""
Create a user account, with 'user', 'password', 'expiration', ssh keys
and sudo permissions.
Returns None if successful, error string on failure.
"""
userentry = None
try:
userentry = pwd.getpwnam(user)
except:
pass
uidmin = None
try:
uidmin = int(GetLineStartingWith("UID_MIN", "/etc/login.defs").split()[1])
except:
pass
if uidmin == None:
uidmin = 100
if userentry != None and userentry[2] < uidmin and userentry[2] != self.CORE_UID:
Error("CreateAccount: " + user + " is a system user. Will not set password.")
return "Failed to set password for system user: " + user + " (0x06)."
if userentry == None:
command = "useradd --create-home --password '*' " + user
if expiration != None:
command += " --expiredate " + expiration.split('.')[0]
if Run(command):
Error("Failed to create user account: " + user)
return "Failed to create user account: " + user + " (0x07)."
else:
Log("CreateAccount: " + user + " already exists. Will update password.")
if password != None:
self.changePass(user, password)
try:
if password == None:
SetFileContents("/etc/sudoers.d/waagent", user + " ALL = (ALL) NOPASSWD: ALL\n")
else:
SetFileContents("/etc/sudoers.d/waagent", user + " ALL = (ALL) ALL\n")
os.chmod("/etc/sudoers.d/waagent", 0440)
except:
Error("CreateAccount: Failed to configure sudo access for user.")
return "Failed to configure sudo privileges (0x08)."
home = MyDistro.GetHome()
if thumbprint != None:
dir = home + "/" + user + "/.ssh"
CreateDir(dir, user, 0700)
pub = dir + "/id_rsa.pub"
prv = dir + "/id_rsa"
Run("ssh-keygen -y -f " + thumbprint + ".prv > " + pub)
SetFileContents(prv, GetFileContents(thumbprint + ".prv"))
for f in [pub, prv]:
os.chmod(f, 0600)
ChangeOwner(f, user)
SetFileContents(dir + "/authorized_keys", GetFileContents(pub))
ChangeOwner(dir + "/authorized_keys", user)
Log("Created user account: " + user)
return None
def startDHCP(self):
Run("systemctl start " + self.dhcp_client_name, chk_err=False)
def stopDHCP(self):
Run("systemctl stop " + self.dhcp_client_name, chk_err=False)
def translateCustomData(self, data):
return base64.b64decode(data)
def getConfigurationPath(self):
return "/usr/share/oem/waagent.conf"
############################################################
# debianDistro
############################################################
debian_init_file = """\
#!/bin/sh
### BEGIN INIT INFO
# Provides: AzureLinuxAgent
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Should-Start: $network $syslog
# Should-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: AzureLinuxAgent
# Description: AzureLinuxAgent
### END INIT INFO
. /lib/lsb/init-functions
OPTIONS="-daemon"
WAZD_BIN=/usr/sbin/waagent
WAZD_PID=/var/run/waagent.pid
case "$1" in
start)
log_begin_msg "Starting AzureLinuxAgent..."
pid=$( pidofproc $WAZD_BIN )
if [ -n "$pid" ] ; then
log_begin_msg "Already running."
log_end_msg 0
exit 0
fi
start-stop-daemon --start --quiet --oknodo --background --exec $WAZD_BIN -- $OPTIONS
log_end_msg $?
;;
stop)
log_begin_msg "Stopping AzureLinuxAgent..."
start-stop-daemon --stop --quiet --oknodo --pidfile $WAZD_PID
ret=$?
rm -f $WAZD_PID
log_end_msg $ret
;;
force-reload)
$0 restart
;;
restart)
$0 stop
$0 start
;;
status)
status_of_proc $WAZD_BIN && exit 0 || exit $?
;;
*)
log_success_msg "Usage: /etc/init.d/waagent {start|stop|force-reload|restart|status}"
exit 1
;;
esac
exit 0
"""
class debianDistro(AbstractDistro):
"""
debian Distro concrete class
Put debian specific behavior here...
"""
def __init__(self):
super(debianDistro,self).__init__()
self.requiredDeps += [ "/usr/sbin/update-rc.d" ]
self.init_file=debian_init_file
self.agent_package_name='walinuxagent'
self.dhcp_client_name='dhclient'
self.getpidcmd='pidof '
self.shadow_file_mode=0640
def checkPackageInstalled(self,p):
"""
Check that the package is installed.
Return 1 if installed, 0 if not installed.
This method of using dpkg-query
allows wildcards to be present in the
package name.
"""
if not Run("dpkg-query -W -f='${Status}\n' '" + p + "' | grep ' installed' 2>&1",chk_err=False):
return 1
else:
return 0
def checkDependencies(self):
"""
Debian dependency check. python-pyasn1 is NOT needed.
Return 1 unless all dependencies are satisfied.
NOTE: using network*manager will catch either package name in Ubuntu or debian.
"""
if self.checkPackageInstalled('network*manager'):
Error(GuestAgentLongName + " is not compatible with network-manager.")
return 1
for a in self.requiredDeps:
if Run("which " + a + " > /dev/null 2>&1",chk_err=False):
Error("Missing required dependency: " + a)
return 1
return 0
def checkPackageUpdateable(self,p):
if Run("apt-get update ; apt-get upgrade -us | grep " + p,chk_err=False):
return 1
else:
return 0
def installAgentServiceScriptFiles(self):
"""
If we are packaged - the service name is walinuxagent, do nothing.
"""
if self.agent_service_name == 'walinuxagent':
return 0
try:
SetFileContents(self.init_script_file, self.init_file)
os.chmod(self.init_script_file, 0744)
except OSError, e:
ErrorWithPrefix('installAgentServiceScriptFiles','Exception: '+str(e)+' occured creating ' + self.init_script_file)
return 1
return 0
def registerAgentService(self):
if self.installAgentServiceScriptFiles() == 0:
return Run('update-rc.d waagent defaults')
else :
return 1
def uninstallAgentService(self):
return Run('update-rc.d -f ' + self.agent_service_name + ' remove')
def unregisterAgentService(self):
self.stopAgentService()
return self.uninstallAgentService()
def sshDeployPublicKey(self,fprint,path):
"""
We support PKCS8.
"""
if Run("ssh-keygen -i -m PKCS8 -f " + fprint + " >> " + path):
return 1
else :
return 0
############################################################
# KaliDistro - WIP
# Functioning on Kali 1.1.0a so far
############################################################
class KaliDistro(debianDistro):
"""
Kali Distro concrete class
Put Kali specific behavior here...
"""
def __init__(self):
super(KaliDistro,self).__init__()
############################################################
# UbuntuDistro
############################################################
ubuntu_upstart_file = """\
#walinuxagent - start Azure agent
description "walinuxagent"
author "Ben Howard <ben.howard@canonical.com>"
start on (filesystem and started rsyslog)
pre-start script
WALINUXAGENT_ENABLED=1
[ -r /etc/default/walinuxagent ] && . /etc/default/walinuxagent
if [ "$WALINUXAGENT_ENABLED" != "1" ]; then
exit 1
fi
if [ ! -x /usr/sbin/waagent ]; then
exit 1
fi
#Load the udf module
modprobe -b udf
end script
exec /usr/sbin/waagent -daemon
"""
class UbuntuDistro(debianDistro):
"""
Ubuntu Distro concrete class
Put Ubuntu specific behavior here...
"""
def __init__(self):
super(UbuntuDistro,self).__init__()
self.init_script_file='/etc/init/waagent.conf'
self.init_file=ubuntu_upstart_file
self.fileBlackList = [ "/root/.bash_history", "/var/log/waagent.log"]
self.dhcp_client_name=None
self.getpidcmd='pidof '
def registerAgentService(self):
return self.installAgentServiceScriptFiles()
def uninstallAgentService(self):
"""
If we are packaged - the service name is walinuxagent, do nothing.
"""
if self.agent_service_name == 'walinuxagent':
return 0
os.remove('/etc/init/' + self.agent_service_name + '.conf')
def unregisterAgentService(self):
"""
If we are packaged - the service name is walinuxagent, do nothing.
"""
if self.agent_service_name == 'walinuxagent':
return
self.stopAgentService()
return self.uninstallAgentService()
def deprovisionWarnUser(self):
"""
Ubuntu specific warning string from Deprovision.
"""
print("WARNING! Nameserver configuration in /etc/resolvconf/resolv.conf.d/{tail,original} will be deleted.")
def deprovisionDeleteFiles(self):
"""
Ubuntu uses resolv.conf by default, so removing /etc/resolv.conf will
break resolvconf. Therefore, we check to see if resolvconf is in use,
and if so, we remove the resolvconf artifacts.
"""
if os.path.realpath('/etc/resolv.conf') != '/run/resolvconf/resolv.conf':
Log("resolvconf is not configured. Removing /etc/resolv.conf")
self.fileBlackList.append('/etc/resolv.conf')
else:
Log("resolvconf is enabled; leaving /etc/resolv.conf intact")
resolvConfD = '/etc/resolvconf/resolv.conf.d/'
self.fileBlackList.extend([resolvConfD + 'tail', resolvConfD + 'original'])
for f in os.listdir(LibDir)+self.fileBlackList:
try:
os.remove(f)
except:
pass
return 0
def getDhcpClientName(self):
if self.dhcp_client_name != None :
return self.dhcp_client_name
if DistInfo()[1] == '12.04' :
self.dhcp_client_name='dhclient3'
else :
self.dhcp_client_name='dhclient'
return self.dhcp_client_name
def waitForSshHostKey(self, path):
"""
Wait until the ssh host key is generated by cloud init.
"""
for retry in range(0, 10):
if(os.path.isfile(path)):
return True
time.sleep(1)
Error("Can't find host key: {0}".format(path))
return False
############################################################
# LinuxMintDistro
############################################################
class LinuxMintDistro(UbuntuDistro):
"""
LinuxMint Distro concrete class
Put LinuxMint specific behavior here...
"""
def __init__(self):
super(LinuxMintDistro,self).__init__()
############################################################
# fedoraDistro
############################################################
fedora_systemd_service = """\
[Unit]
Description=Azure Linux Agent
After=network.target
After=sshd.service
ConditionFileIsExecutable=/usr/sbin/waagent
ConditionPathExists=/etc/waagent.conf
[Service]
Type=simple
ExecStart=/usr/sbin/waagent -daemon
[Install]
WantedBy=multi-user.target
"""
class fedoraDistro(redhatDistro):
"""
FedoraDistro concrete class
Put Fedora specific behavior here...
"""
def __init__(self):
super(fedoraDistro,self).__init__()
self.service_cmd = '/usr/bin/systemctl'
self.hostname_file_path = '/etc/hostname'
self.init_script_file = '/usr/lib/systemd/system/' + self.agent_service_name + '.service'
self.init_file = fedora_systemd_service
self.grubKernelBootOptionsFile = '/etc/default/grub'
self.grubKernelBootOptionsLine = 'GRUB_CMDLINE_LINUX='
def publishHostname(self, name):
SetFileContents(self.hostname_file_path, name + '\n')
ethernetInterface = MyDistro.GetInterfaceName()
filepath = "/etc/sysconfig/network-scripts/ifcfg-" + ethernetInterface
if os.path.isfile(filepath):
ReplaceFileContentsAtomic(filepath, "DHCP_HOSTNAME=" + name + "\n"
+ "\n".join(filter(lambda a: not a.startswith("DHCP_HOSTNAME"), GetFileContents(filepath).split('\n'))))
return 0
def installAgentServiceScriptFiles(self):
SetFileContents(self.init_script_file, self.init_file)
os.chmod(self.init_script_file, 0644)
return Run(self.service_cmd + ' daemon-reload')
def registerAgentService(self):
self.installAgentServiceScriptFiles()
return Run(self.service_cmd + ' enable ' + self.agent_service_name)
def uninstallAgentService(self):
"""
Call service subsystem to remove waagent script.
"""
return Run(self.service_cmd + ' disable ' + self.agent_service_name)
def unregisterAgentService(self):
"""
Calls self.stopAgentService and call self.uninstallAgentService()
"""
self.stopAgentService()
self.uninstallAgentService()
def startAgentService(self):
"""
Service call to start the Agent service
"""
return Run(self.service_cmd + ' start ' + self.agent_service_name)
def stopAgentService(self):
"""
Service call to stop the Agent service
"""
return Run(self.service_cmd + ' stop ' + self.agent_service_name, False)
def restartSshService(self):
"""
Service call to re(start) the SSH service
"""
sshRestartCmd = self.service_cmd + " " + self.ssh_service_restart_option + " " + self.ssh_service_name
retcode = Run(sshRestartCmd)
if retcode > 0:
Error("Failed to restart SSH service with return code:" + str(retcode))
return retcode
def checkPackageInstalled(self, p):
"""
Query package database for prescence of an installed package.
"""
import rpm
ts = rpm.TransactionSet()
rpms = ts.dbMatch(rpm.RPMTAG_PROVIDES, p)
return bool(len(rpms) > 0)
def deleteRootPassword(self):
return Run("/sbin/usermod root -p '!!'")
def packagedInstall(self,buildroot):
"""
Called from setup.py for use by RPM.
Copies generated files waagent.conf, under the buildroot.
"""
if not os.path.exists(buildroot+'/etc'):
os.mkdir(buildroot+'/etc')
SetFileContents(buildroot+'/etc/waagent.conf', MyDistro.waagent_conf_file)
if not os.path.exists(buildroot+'/etc/logrotate.d'):
os.mkdir(buildroot+'/etc/logrotate.d')
SetFileContents(buildroot+'/etc/logrotate.d/WALinuxAgent', WaagentLogrotate)
self.init_script_file=buildroot+self.init_script_file
# this allows us to call installAgentServiceScriptFiles()
if not os.path.exists(os.path.dirname(self.init_script_file)):
os.mkdir(os.path.dirname(self.init_script_file))
self.installAgentServiceScriptFiles()
def CreateAccount(self, user, password, expiration, thumbprint):
super(fedoraDistro, self).CreateAccount(user, password, expiration, thumbprint)
Run('/sbin/usermod ' + user + ' -G wheel')
def DeleteAccount(self, user):
Run('/sbin/usermod ' + user + ' -G ""')
super(fedoraDistro, self).DeleteAccount(user)
############################################################
# FreeBSD
############################################################
FreeBSDWaagentConf = """\
#
# Azure Linux Agent Configuration
#
Role.StateConsumer=None # Specified program is invoked with the argument "Ready" when we report ready status
# to the endpoint server.
Role.ConfigurationConsumer=None # Specified program is invoked with XML file argument specifying role configuration.
Role.TopologyConsumer=None # Specified program is invoked with XML file argument specifying role topology.
Provisioning.Enabled=y #
Provisioning.DeleteRootPassword=y # Password authentication for root account will be unavailable.
Provisioning.RegenerateSshHostKeyPair=y # Generate fresh host key pair.
Provisioning.SshHostKeyPairType=rsa # Supported values are "rsa", "dsa" and "ecdsa".
Provisioning.MonitorHostName=y # Monitor host name changes and publish changes via DHCP requests.
ResourceDisk.Format=y # Format if unformatted. If 'n', resource disk will not be mounted.
ResourceDisk.Filesystem=ufs2 #
ResourceDisk.MountPoint=/mnt/resource #
ResourceDisk.EnableSwap=n # Create and use swapfile on resource disk.
ResourceDisk.SwapSizeMB=0 # Size of the swapfile.
LBProbeResponder=y # Respond to load balancer probes if requested by Azure.
Logs.Verbose=n # Enable verbose logs
OS.RootDeviceScsiTimeout=300 # Root device timeout in seconds.
OS.OpensslPath=None # If "None", the system default version is used.
"""
bsd_init_file="""\
#! /bin/sh
# PROVIDE: waagent
# REQUIRE: DAEMON cleanvar sshd
# BEFORE: LOGIN
# KEYWORD: nojail
. /etc/rc.subr
export PATH=$PATH:/usr/local/bin
name="waagent"
rcvar="waagent_enable"
command="/usr/sbin/${name}"
command_interpreter="/usr/local/bin/python"
waagent_flags=" daemon &"
pidfile="/var/run/waagent.pid"
load_rc_config $name
run_rc_command "$1"
"""
bsd_activate_resource_disk_txt="""\
#!/usr/bin/env python
import os
import sys
import imp
# waagent has no '.py' therefore create waagent module import manually.
__name__='setupmain' #prevent waagent.__main__ from executing
waagent=imp.load_source('waagent','/tmp/waagent')
waagent.LoggerInit('/var/log/waagent.log','/dev/console')
from waagent import RunGetOutput,Run
Config=waagent.ConfigurationProvider(None)
format = Config.get("ResourceDisk.Format")
if format == None or format.lower().startswith("n"):
sys.exit(0)
device_base = 'da1'
device = "/dev/" + device_base
for entry in RunGetOutput("mount")[1].split():
if entry.startswith(device + "s1"):
waagent.Log("ActivateResourceDisk: " + device + "s1 is already mounted.")
sys.exit(0)
mountpoint = Config.get("ResourceDisk.MountPoint")
if mountpoint == None:
mountpoint = "/mnt/resource"
waagent.CreateDir(mountpoint, "root", 0755)
fs = Config.get("ResourceDisk.Filesystem")
if waagent.FreeBSDDistro().mediaHasFilesystem(device) == False :
Run("newfs " + device + "s1")
if Run("mount " + device + "s1 " + mountpoint):
waagent.Error("ActivateResourceDisk: Failed to mount resource disk (" + device + "s1).")
sys.exit(0)
waagent.Log("Resource disk (" + device + "s1) is mounted at " + mountpoint + " with fstype " + fs)
waagent.SetFileContents(os.path.join(mountpoint,waagent.README_FILENAME), waagent.README_FILECONTENT)
swap = Config.get("ResourceDisk.EnableSwap")
if swap == None or swap.lower().startswith("n"):
sys.exit(0)
sizeKB = int(Config.get("ResourceDisk.SwapSizeMB")) * 1024
if os.path.isfile(mountpoint + "/swapfile") and os.path.getsize(mountpoint + "/swapfile") != (sizeKB * 1024):
os.remove(mountpoint + "/swapfile")
if not os.path.isfile(mountpoint + "/swapfile"):
Run("dd if=/dev/zero of=" + mountpoint + "/swapfile bs=1024 count=" + str(sizeKB))
if Run("mdconfig -a -t vnode -f " + mountpoint + "/swapfile -u 0"):
waagent.Error("ActivateResourceDisk: Configuring swap - Failed to create md0")
if not Run("swapon /dev/md0"):
waagent.Log("Enabled " + str(sizeKB) + " KB of swap at " + mountpoint + "/swapfile")
else:
waagent.Error("ActivateResourceDisk: Failed to activate swap at " + mountpoint + "/swapfile")
"""
class FreeBSDDistro(AbstractDistro):
"""
"""
def __init__(self):
"""
Generic Attributes go here. These are based on 'majority rules'.
This __init__() may be called or overriden by the child.
"""
super(FreeBSDDistro,self).__init__()
self.agent_service_name = os.path.basename(sys.argv[0])
self.selinux=False
self.ssh_service_name='sshd'
self.ssh_config_file='/etc/ssh/sshd_config'
self.hostname_file_path='/etc/hostname'
self.dhcp_client_name='dhclient'
self.requiredDeps = [ 'route', 'shutdown', 'ssh-keygen', 'pw'
, 'openssl', 'fdisk', 'sed', 'grep' , 'sudo']
self.init_script_file='/etc/rc.d/waagent'
self.init_file=bsd_init_file
self.agent_package_name='WALinuxAgent'
self.fileBlackList = [ "/root/.bash_history", "/var/log/waagent.log",'/etc/resolv.conf' ]
self.agent_files_to_uninstall = ["/etc/waagent.conf"]
self.grubKernelBootOptionsFile = '/boot/loader.conf'
self.grubKernelBootOptionsLine = ''
self.getpidcmd = 'pgrep -n'
self.mount_dvd_cmd = 'dd bs=2048 count=33 skip=295 if=' # custom data max len is 64k
self.sudoers_dir_base = '/usr/local/etc'
self.waagent_conf_file = FreeBSDWaagentConf
def installAgentServiceScriptFiles(self):
SetFileContents(self.init_script_file, self.init_file)
os.chmod(self.init_script_file, 0777)
AppendFileContents("/etc/rc.conf","waagent_enable='YES'\n")
return 0
def registerAgentService(self):
self.installAgentServiceScriptFiles()
return Run("services_mkdb " + self.init_script_file)
def sshDeployPublicKey(self,fprint,path):
"""
We support PKCS8.
"""
if Run("ssh-keygen -i -m PKCS8 -f " + fprint + " >> " + path):
return 1
else :
return 0
def deleteRootPassword(self):
"""
BSD root password removal.
"""
filepath="/etc/master.passwd"
ReplaceStringInFile(filepath,r'root:.*?:','root::')
#ReplaceFileContentsAtomic(filepath,"root:*LOCK*:14600::::::\n"
# + "\n".join(filter(lambda a: not a.startswith("root:"),GetFileContents(filepath).split('\n'))))
os.chmod(filepath,self.shadow_file_mode)
if self.isSelinuxSystem():
self.setSelinuxContext(filepath,'system_u:object_r:shadow_t:s0')
RunGetOutput("pwd_mkdb -u root /etc/master.passwd")
Log("Root password deleted.")
return 0
def changePass(self,user,password):
return RunSendStdin("pw usermod " + user + " -h 0 ",password, log_cmd=False)
def load_ata_piix(self):
return 0
def unload_ata_piix(self):
return 0
def checkDependencies(self):
"""
FreeBSD dependency check.
Return 1 unless all dependencies are satisfied.
"""
for a in self.requiredDeps:
if Run("which " + a + " > /dev/null 2>&1",chk_err=False):
Error("Missing required dependency: " + a)
return 1
return 0
def packagedInstall(self,buildroot):
pass
def GetInterfaceName(self):
"""
Return the ip of the
active ethernet interface.
"""
iface,inet,mac=self.GetFreeBSDEthernetInfo()
return iface
def RestartInterface(self, iface):
Run("service netif restart")
def GetIpv4Address(self):
"""
Return the ip of the
active ethernet interface.
"""
iface,inet,mac=self.GetFreeBSDEthernetInfo()
return inet
def GetMacAddress(self):
"""
Return the ip of the
active ethernet interface.
"""
iface,inet,mac=self.GetFreeBSDEthernetInfo()
l=mac.split(':')
r=[]
for i in l:
r.append(string.atoi(i,16))
return r
def GetFreeBSDEthernetInfo(self):
"""
There is no SIOCGIFCONF
on freeBSD - just parse ifconfig.
Returns strings: iface, inet4_addr, and mac
or 'None,None,None' if unable to parse.
We will sleep and retry as the network must be up.
"""
code,output=RunGetOutput("ifconfig",chk_err=False)
Log(output)
retries=10
cmd='ifconfig | grep -A2 -B2 ether | grep -B3 inet | grep -A4 UP '
code=1
while code > 0 :
if code > 0 and retries == 0:
Error("GetFreeBSDEthernetInfo - Failed to detect ethernet interface")
return None, None, None
code,output=RunGetOutput(cmd,chk_err=False)
retries-=1
if code > 0 and retries > 0 :
Log("GetFreeBSDEthernetInfo - Error: retry ethernet detection " + str(retries))
if retries == 9 :
c,o=RunGetOutput("ifconfig | grep -A1 -B2 ether",chk_err=False)
if c == 0:
t=o.replace('\n',' ')
t=t.split()
i=t[0][:-1]
Log(RunGetOutput('id')[1])
Run('dhclient '+i)
time.sleep(10)
j=output.replace('\n',' ')
j=j.split()
iface=j[0][:-1]
for i in range(len(j)):
if j[i] == 'inet' :
inet=j[i+1]
elif j[i] == 'ether' :
mac=j[i+1]
return iface, inet, mac
def CreateAccount(self,user, password, expiration, thumbprint):
"""
Create a user account, with 'user', 'password', 'expiration', ssh keys
and sudo permissions.
Returns None if successful, error string on failure.
"""
userentry = None
try:
userentry = pwd.getpwnam(user)
except:
pass
uidmin = None
try:
if os.path.isfile("/etc/login.defs"):
uidmin = int(GetLineStartingWith("UID_MIN", "/etc/login.defs").split()[1])
except:
pass
if uidmin == None:
uidmin = 100
if userentry != None and userentry[2] < uidmin:
Error("CreateAccount: " + user + " is a system user. Will not set password.")
return "Failed to set password for system user: " + user + " (0x06)."
if userentry == None:
command = "pw useradd " + user + " -m"
if expiration != None:
command += " -e " + expiration.split('.')[0]
if Run(command):
Error("Failed to create user account: " + user)
return "Failed to create user account: " + user + " (0x07)."
else:
Log("CreateAccount: " + user + " already exists. Will update password.")
if password != None:
self.changePass(user,password)
try:
# for older distros create sudoers.d
if not os.path.isdir(MyDistro.sudoers_dir_base+'/sudoers.d/'):
# create the /etc/sudoers.d/ directory
os.mkdir(MyDistro.sudoers_dir_base+'/sudoers.d')
# add the include of sudoers.d to the /etc/sudoers
SetFileContents(MyDistro.sudoers_dir_base+'/sudoers',GetFileContents(MyDistro.sudoers_dir_base+'/sudoers')+'\n#includedir ' + MyDistro.sudoers_dir_base + '/sudoers.d\n')
if password == None:
SetFileContents(MyDistro.sudoers_dir_base+"/sudoers.d/waagent", user + " ALL = (ALL) NOPASSWD: ALL\n")
else:
SetFileContents(MyDistro.sudoers_dir_base+"/sudoers.d/waagent", user + " ALL = (ALL) ALL\n")
os.chmod(MyDistro.sudoers_dir_base+"/sudoers.d/waagent", 0440)
except:
Error("CreateAccount: Failed to configure sudo access for user.")
return "Failed to configure sudo privileges (0x08)."
home = MyDistro.GetHome()
if thumbprint != None:
dir = home + "/" + user + "/.ssh"
CreateDir(dir, user, 0700)
pub = dir + "/id_rsa.pub"
prv = dir + "/id_rsa"
Run("ssh-keygen -y -f " + thumbprint + ".prv > " + pub)
SetFileContents(prv, GetFileContents(thumbprint + ".prv"))
for f in [pub, prv]:
os.chmod(f, 0600)
ChangeOwner(f, user)
SetFileContents(dir + "/authorized_keys", GetFileContents(pub))
ChangeOwner(dir + "/authorized_keys", user)
Log("Created user account: " + user)
return None
def DeleteAccount(self,user):
"""
Delete the 'user'.
Clear utmp first, to avoid error.
Removes the /etc/sudoers.d/waagent file.
"""
userentry = None
try:
userentry = pwd.getpwnam(user)
except:
pass
if userentry == None:
Error("DeleteAccount: " + user + " not found.")
return
uidmin = None
try:
if os.path.isfile("/etc/login.defs"):
uidmin = int(GetLineStartingWith("UID_MIN", "/etc/login.defs").split()[1])
except:
pass
if uidmin == None:
uidmin = 100
if userentry[2] < uidmin:
Error("DeleteAccount: " + user + " is a system user. Will not delete account.")
return
Run("> /var/run/utmp") #Delete utmp to prevent error if we are the 'user' deleted
pid = subprocess.Popen(['rmuser', '-y', user], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE).pid
try:
os.remove(MyDistro.sudoers_dir_base+"/sudoers.d/waagent")
except:
pass
return
def ActivateResourceDiskNoThread(self):
"""
Format, mount, and if specified in the configuration
set resource disk as swap.
"""
global DiskActivated
Run('cp /usr/sbin/waagent /tmp/')
SetFileContents('/tmp/bsd_activate_resource_disk.py',bsd_activate_resource_disk_txt)
Run('chmod +x /tmp/bsd_activate_resource_disk.py')
pid = subprocess.Popen(["/tmp/bsd_activate_resource_disk.py", ""]).pid
Log("Spawning bsd_activate_resource_disk.py")
DiskActivated = True
return
def Install(self):
"""
Install the agent service.
Check dependencies.
Create /etc/waagent.conf and move old version to
/etc/waagent.conf.old
Copy RulesFiles to /var/lib/waagent
Create /etc/logrotate.d/waagent
Set /etc/ssh/sshd_config ClientAliveInterval to 180
Call ApplyVNUMAWorkaround()
"""
if MyDistro.checkDependencies():
return 1
os.chmod(sys.argv[0], 0755)
SwitchCwd()
for a in RulesFiles:
if os.path.isfile(a):
if os.path.isfile(GetLastPathElement(a)):
os.remove(GetLastPathElement(a))
shutil.move(a, ".")
Warn("Moved " + a + " -> " + LibDir + "/" + GetLastPathElement(a) )
MyDistro.registerAgentService()
if os.path.isfile("/etc/waagent.conf"):
try:
os.remove("/etc/waagent.conf.old")
except:
pass
try:
os.rename("/etc/waagent.conf", "/etc/waagent.conf.old")
Warn("Existing /etc/waagent.conf has been renamed to /etc/waagent.conf.old")
except:
pass
SetFileContents("/etc/waagent.conf", self.waagent_conf_file)
if os.path.exists('/usr/local/etc/logrotate.d/'):
SetFileContents("/usr/local/etc/logrotate.d/waagent", WaagentLogrotate)
filepath = "/etc/ssh/sshd_config"
ReplaceFileContentsAtomic(filepath, "\n".join(filter(lambda a: not
a.startswith("ClientAliveInterval"),
GetFileContents(filepath).split('\n'))) + "\nClientAliveInterval 180\n")
Log("Configured SSH client probing to keep connections alive.")
#ApplyVNUMAWorkaround()
return 0
def mediaHasFilesystem(self,dsk):
if Run('LC_ALL=C fdisk -p ' + dsk + ' | grep "invalid fdisk partition table found" ',False):
return False
return True
def mountDVD(self,dvd,location):
#At this point we cannot read a joliet option udf DVD in freebsd10 - so we 'dd' it into our location
retcode,out = RunGetOutput(self.mount_dvd_cmd + dvd + ' of=' + location + '/ovf-env.xml')
if retcode != 0:
return retcode,out
ovfxml = (GetFileContents(location+"/ovf-env.xml",asbin=False))
if ord(ovfxml[0]) > 128 and ord(ovfxml[1]) > 128 and ord(ovfxml[2]) > 128 :
ovfxml = ovfxml[3:] # BOM is not stripped. First three bytes are > 128 and not unicode chars so we ignore them.
ovfxml = ovfxml.strip(chr(0x00))
ovfxml = "".join(filter(lambda x: ord(x)<128, ovfxml))
ovfxml = re.sub(r'</Environment>.*\Z','',ovfxml,0,re.DOTALL)
ovfxml += '</Environment>'
SetFileContents(location+"/ovf-env.xml", ovfxml)
return retcode,out
def GetHome(self):
return '/home'
def initScsiDiskTimeout(self):
"""
Set the SCSI disk timeout by updating the kernal config
"""
timeout = Config.get("OS.RootDeviceScsiTimeout")
if timeout:
Run("sysctl kern.cam.da.default_timeout=" + timeout)
def setScsiDiskTimeout(self):
return
def setBlockDeviceTimeout(self, device, timeout):
return
def getProcessorCores(self):
return int(RunGetOutput("sysctl hw.ncpu | awk '{print $2}'")[1])
def getTotalMemory(self):
return int(RunGetOutput("sysctl hw.realmem | awk '{print $2}'")[1])/1024
def setDefaultGateway(self, gateway):
Run("/sbin/route add default " + gateway, chk_err=False)
def routeAdd(self, net, mask, gateway):
Run("/sbin/route add -net " + net + " " + mask + " " + gateway, chk_err=False)
############################################################
# END DISTRO CLASS DEFS
############################################################
# This lets us index into a string or an array of integers transparently.
def Ord(a):
"""
Allows indexing into a string or an array of integers transparently.
Generic utility function.
"""
if type(a) == type("a"):
a = ord(a)
return a
def IsLinux():
"""
Returns True if platform is Linux.
Generic utility function.
"""
return (platform.uname()[0] == "Linux")
def GetLastPathElement(path):
"""
Similar to basename.
Generic utility function.
"""
return path.rsplit('/', 1)[1]
def GetFileContents(filepath,asbin=False):
"""
Read and return contents of 'filepath'.
"""
mode='r'
if asbin:
mode+='b'
c=None
try:
with open(filepath, mode) as F :
c=F.read()
except IOError, e:
ErrorWithPrefix('GetFileContents','Reading from file ' + filepath + ' Exception is ' + str(e))
return None
return c
def SetFileContents(filepath, contents):
"""
Write 'contents' to 'filepath'.
"""
if type(contents) == str :
contents=contents.encode('latin-1', 'ignore')
try:
with open(filepath, "wb+") as F :
F.write(contents)
except IOError, e:
ErrorWithPrefix('SetFileContents','Writing to file ' + filepath + ' Exception is ' + str(e))
return None
return 0
def AppendFileContents(filepath, contents):
"""
Append 'contents' to 'filepath'.
"""
if type(contents) == str :
contents=contents.encode('latin-1')
try:
with open(filepath, "a+") as F :
F.write(contents)
except IOError, e:
ErrorWithPrefix('AppendFileContents','Appending to file ' + filepath + ' Exception is ' + str(e))
return None
return 0
def ReplaceFileContentsAtomic(filepath, contents):
"""
Write 'contents' to 'filepath' by creating a temp file, and replacing original.
"""
handle, temp = tempfile.mkstemp(dir = os.path.dirname(filepath))
if type(contents) == str :
contents=contents.encode('latin-1')
try:
os.write(handle, contents)
except IOError, e:
ErrorWithPrefix('ReplaceFileContentsAtomic','Writing to file ' + filepath + ' Exception is ' + str(e))
return None
finally:
os.close(handle)
try:
os.rename(temp, filepath)
return None
except IOError, e:
ErrorWithPrefix('ReplaceFileContentsAtomic','Renaming ' + temp+ ' to ' + filepath + ' Exception is ' + str(e))
try:
os.remove(filepath)
except IOError, e:
ErrorWithPrefix('ReplaceFileContentsAtomic','Removing '+ filepath + ' Exception is ' + str(e))
try:
os.rename(temp,filepath)
except IOError, e:
ErrorWithPrefix('ReplaceFileContentsAtomic','Removing '+ filepath + ' Exception is ' + str(e))
return 1
return 0
def GetLineStartingWith(prefix, filepath):
"""
Return line from 'filepath' if the line startswith 'prefix'
"""
for line in GetFileContents(filepath).split('\n'):
if line.startswith(prefix):
return line
return None
def Run(cmd,chk_err=True):
"""
Calls RunGetOutput on 'cmd', returning only the return code.
If chk_err=True then errors will be reported in the log.
If chk_err=False then errors will be suppressed from the log.
"""
retcode,out=RunGetOutput(cmd,chk_err)
return retcode
def RunGetOutput(cmd, chk_err=True, log_cmd=True):
"""
Wrapper for subprocess.check_output.
Execute 'cmd'. Returns return code and STDOUT, trapping expected exceptions.
Reports exceptions to Error if chk_err parameter is True
"""
if log_cmd:
LogIfVerbose(cmd)
try:
output=subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
except subprocess.CalledProcessError,e :
if chk_err and log_cmd:
Error('CalledProcessError. Error Code is ' + str(e.returncode) )
Error('CalledProcessError. Command string was ' + e.cmd )
Error('CalledProcessError. Command result was ' + (e.output[:-1]).decode('latin-1'))
return e.returncode,e.output.decode('latin-1')
return 0,output.decode('latin-1')
def RunSendStdin(cmd, input, chk_err=True, log_cmd=True):
"""
Wrapper for subprocess.Popen.
Execute 'cmd', sending 'input' to STDIN of 'cmd'.
Returns return code and STDOUT, trapping expected exceptions.
Reports exceptions to Error if chk_err parameter is True
"""
if log_cmd:
LogIfVerbose(cmd+input)
try:
me=subprocess.Popen([cmd], shell=True, stdin=subprocess.PIPE,stderr=subprocess.STDOUT,stdout=subprocess.PIPE)
output=me.communicate(input)
except OSError , e :
if chk_err and log_cmd:
Error('CalledProcessError. Error Code is ' + str(me.returncode) )
Error('CalledProcessError. Command string was ' + cmd )
Error('CalledProcessError. Command result was ' + output[0].decode('latin-1'))
return 1,output[0].decode('latin-1')
if me.returncode is not 0 and chk_err is True and log_cmd:
Error('CalledProcessError. Error Code is ' + str(me.returncode) )
Error('CalledProcessError. Command string was ' + cmd )
Error('CalledProcessError. Command result was ' + output[0].decode('latin-1'))
return me.returncode,output[0].decode('latin-1')
def GetNodeTextData(a):
"""
Filter non-text nodes from DOM tree
"""
for b in a.childNodes:
if b.nodeType == b.TEXT_NODE:
return b.data
def GetHome():
"""
Attempt to guess the $HOME location.
Return the path string.
"""
home = None
try:
home = GetLineStartingWith("HOME", "/etc/default/useradd").split('=')[1].strip()
except:
pass
if (home == None) or (home.startswith("/") == False):
home = "/home"
return home
def ChangeOwner(filepath, user):
"""
Lookup user. Attempt chown 'filepath' to 'user'.
"""
p = None
try:
p = pwd.getpwnam(user)
except:
pass
if p != None:
os.chown(filepath, p[2], p[3])
def CreateDir(dirpath, user, mode):
"""
Attempt os.makedirs, catch all exceptions.
Call ChangeOwner afterwards.
"""
try:
os.makedirs(dirpath, mode)
except:
pass
ChangeOwner(dirpath, user)
def CreateAccount(user, password, expiration, thumbprint):
"""
Create a user account, with 'user', 'password', 'expiration', ssh keys
and sudo permissions.
Returns None if successful, error string on failure.
"""
userentry = None
try:
userentry = pwd.getpwnam(user)
except:
pass
uidmin = None
try:
uidmin = int(GetLineStartingWith("UID_MIN", "/etc/login.defs").split()[1])
except:
pass
if uidmin == None:
uidmin = 100
if userentry != None and userentry[2] < uidmin:
Error("CreateAccount: " + user + " is a system user. Will not set password.")
return "Failed to set password for system user: " + user + " (0x06)."
if userentry == None:
command = "useradd -m " + user
if expiration != None:
command += " -e " + expiration.split('.')[0]
if Run(command):
Error("Failed to create user account: " + user)
return "Failed to create user account: " + user + " (0x07)."
else:
Log("CreateAccount: " + user + " already exists. Will update password.")
if password != None:
MyDistro.changePass(user, password)
try:
# for older distros create sudoers.d
if not os.path.isdir('/etc/sudoers.d/'):
# create the /etc/sudoers.d/ directory
os.mkdir('/etc/sudoers.d/')
# add the include of sudoers.d to the /etc/sudoers
SetFileContents('/etc/sudoers',GetFileContents('/etc/sudoers')+'\n#includedir /etc/sudoers.d\n')
if password == None:
SetFileContents("/etc/sudoers.d/waagent", user + " ALL = (ALL) NOPASSWD: ALL\n")
else:
SetFileContents("/etc/sudoers.d/waagent", user + " ALL = (ALL) ALL\n")
os.chmod("/etc/sudoers.d/waagent", 0440)
except:
Error("CreateAccount: Failed to configure sudo access for user.")
return "Failed to configure sudo privileges (0x08)."
home = MyDistro.GetHome()
if thumbprint != None:
dir = home + "/" + user + "/.ssh"
CreateDir(dir, user, 0700)
pub = dir + "/id_rsa.pub"
prv = dir + "/id_rsa"
Run("ssh-keygen -y -f " + thumbprint + ".prv > " + pub)
SetFileContents(prv, GetFileContents(thumbprint + ".prv"))
for f in [pub, prv]:
os.chmod(f, 0600)
ChangeOwner(f, user)
SetFileContents(dir + "/authorized_keys", GetFileContents(pub))
ChangeOwner(dir + "/authorized_keys", user)
Log("Created user account: " + user)
return None
def DeleteAccount(user):
"""
Delete the 'user'.
Clear utmp first, to avoid error.
Removes the /etc/sudoers.d/waagent file.
"""
userentry = None
try:
userentry = pwd.getpwnam(user)
except:
pass
if userentry == None:
Error("DeleteAccount: " + user + " not found.")
return
uidmin = None
try:
uidmin = int(GetLineStartingWith("UID_MIN", "/etc/login.defs").split()[1])
except:
pass
if uidmin == None:
uidmin = 100
if userentry[2] < uidmin:
Error("DeleteAccount: " + user + " is a system user. Will not delete account.")
return
Run("> /var/run/utmp") #Delete utmp to prevent error if we are the 'user' deleted
Run("userdel -f -r " + user)
try:
os.remove("/etc/sudoers.d/waagent")
except:
pass
return
def IsInRangeInclusive(a, low, high):
"""
Return True if 'a' in 'low' <= a >= 'high'
"""
return (a >= low and a <= high)
def IsPrintable(ch):
"""
Return True if character is displayable.
"""
return IsInRangeInclusive(ch, Ord('A'), Ord('Z')) or IsInRangeInclusive(ch, Ord('a'), Ord('z')) or IsInRangeInclusive(ch, Ord('0'), Ord('9'))
def HexDump(buffer, size):
"""
Return Hex formated dump of a 'buffer' of 'size'.
"""
if size < 0:
size = len(buffer)
result = ""
for i in range(0, size):
if (i % 16) == 0:
result += "%06X: " % i
byte = buffer[i]
if type(byte) == str:
byte = ord(byte.decode('latin1'))
result += "%02X " % byte
if (i & 15) == 7:
result += " "
if ((i + 1) % 16) == 0 or (i + 1) == size:
j = i
while ((j + 1) % 16) != 0:
result += " "
if (j & 7) == 7:
result += " "
j += 1
result += " "
for j in range(i - (i % 16), i + 1):
byte=buffer[j]
if type(byte) == str:
byte = ord(byte.decode('latin1'))
k = '.'
if IsPrintable(byte):
k = chr(byte)
result += k
if (i + 1) != size:
result += "\n"
return result
def SimpleLog(file_path,message):
if not file_path or len(message) < 1:
return
t = time.localtime()
t = "%04u/%02u/%02u %02u:%02u:%02u " % (t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
lines=re.sub(re.compile(r'^(.)',re.MULTILINE),t+r'\1',message)
with open(file_path, "a") as F :
lines = filter(lambda x : x in string.printable, lines)
F.write(lines.encode('ascii','ignore') + "\n")
class Logger(object):
"""
The Agent's logging assumptions are:
For Log, and LogWithPrefix all messages are logged to the
self.file_path and to the self.con_path. Setting either path
parameter to None skips that log. If Verbose is enabled, messages
calling the LogIfVerbose method will be logged to file_path yet
not to con_path. Error and Warn messages are normal log messages
with the 'ERROR:' or 'WARNING:' prefix added.
"""
def __init__(self,filepath,conpath,verbose=False):
"""
Construct an instance of Logger.
"""
self.file_path=filepath
self.con_path=conpath
self.verbose=verbose
def ThrottleLog(self,counter):
"""
Log everything up to 10, every 10 up to 100, then every 100.
"""
return (counter < 10) or ((counter < 100) and ((counter % 10) == 0)) or ((counter % 100) == 0)
def LogToFile(self,message):
"""
Write 'message' to logfile.
"""
if self.file_path:
try:
with open(self.file_path, "a") as F :
message = filter(lambda x : x in string.printable, message)
F.write(message.encode('ascii','ignore') + "\n")
except IOError, e:
print e
pass
def LogToCon(self,message):
"""
Write 'message' to /dev/console.
This supports serial port logging if the /dev/console
is redirected to ttys0 in kernel boot options.
"""
if self.con_path:
try:
with open(self.con_path, "w") as C :
message = filter(lambda x : x in string.printable, message)
C.write(message.encode('ascii','ignore') + "\n")
except IOError, e:
pass
def Log(self,message):
"""
Standard Log function.
Logs to self.file_path, and con_path
"""
self.LogWithPrefix("", message)
def LogWithPrefix(self,prefix, message):
"""
Prefix each line of 'message' with current time+'prefix'.
"""
t = time.localtime()
t = "%04u/%02u/%02u %02u:%02u:%02u " % (t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
t += prefix
for line in message.split('\n'):
line = t + line
self.LogToFile(line)
self.LogToCon(line)
def NoLog(self,message):
"""
Don't Log.
"""
pass
def LogIfVerbose(self,message):
"""
Only log 'message' if global Verbose is True.
"""
self.LogWithPrefixIfVerbose('',message)
def LogWithPrefixIfVerbose(self,prefix, message):
"""
Only log 'message' if global Verbose is True.
Prefix each line of 'message' with current time+'prefix'.
"""
if self.verbose == True:
t = time.localtime()
t = "%04u/%02u/%02u %02u:%02u:%02u " % (t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
t += prefix
for line in message.split('\n'):
line = t + line
self.LogToFile(line)
self.LogToCon(line)
def Warn(self,message):
"""
Prepend the text "WARNING:" to the prefix for each line in 'message'.
"""
self.LogWithPrefix("WARNING:", message)
def Error(self,message):
"""
Call ErrorWithPrefix(message).
"""
ErrorWithPrefix("", message)
def ErrorWithPrefix(self,prefix, message):
"""
Prepend the text "ERROR:" to the prefix for each line in 'message'.
Errors written to logfile, and /dev/console
"""
self.LogWithPrefix("ERROR:", message)
def LoggerInit(log_file_path,log_con_path,verbose=False):
"""
Create log object and export its methods to global scope.
"""
global Log,LogWithPrefix,LogIfVerbose,LogWithPrefixIfVerbose,Error,ErrorWithPrefix,Warn,NoLog,ThrottleLog,myLogger
l=Logger(log_file_path,log_con_path,verbose)
Log,LogWithPrefix,LogIfVerbose,LogWithPrefixIfVerbose,Error,ErrorWithPrefix,Warn,NoLog,ThrottleLog,myLogger = l.Log,l.LogWithPrefix,l.LogIfVerbose,l.LogWithPrefixIfVerbose,l.Error,l.ErrorWithPrefix,l.Warn,l.NoLog,l.ThrottleLog,l
def Linux_ioctl_GetInterfaceMac(ifname):
"""
Return the mac-address bound to the socket.
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', (ifname[:15]+('\0'*241)).encode('latin-1')))
return ''.join(['%02X' % Ord(char) for char in info[18:24]])
def GetFirstActiveNetworkInterfaceNonLoopback():
"""
Return the interface name, and ip addr of the
first active non-loopback interface.
"""
iface=''
expected=16 # how many devices should I expect...
is_64bits = sys.maxsize > 2**32
struct_size=40 if is_64bits else 32 # for 64bit the size is 40 bytes, for 32bits it is 32 bytes.
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
buff=array.array('B', b'\0' * (expected*struct_size))
retsize=(struct.unpack('iL', fcntl.ioctl(s.fileno(), 0x8912, struct.pack('iL',expected*struct_size,buff.buffer_info()[0]))))[0]
if retsize == (expected*struct_size) :
Warn('SIOCGIFCONF returned more than ' + str(expected) + ' up network interfaces.')
s=buff.tostring()
preferred_nic = Config.get("Network.Interface")
for i in range(0,struct_size*expected,struct_size):
iface=s[i:i+16].split(b'\0', 1)[0]
if iface == b'lo':
continue
elif preferred_nic is None:
break
elif iface == preferred_nic:
break
return iface.decode('latin-1'), socket.inet_ntoa(s[i+20:i+24])
def GetIpv4Address():
"""
Return the ip of the
first active non-loopback interface.
"""
iface,addr=GetFirstActiveNetworkInterfaceNonLoopback()
return addr
def HexStringToByteArray(a):
"""
Return hex string packed into a binary struct.
"""
b = b""
for c in range(0, len(a) // 2):
b += struct.pack("B", int(a[c * 2:c * 2 + 2], 16))
return b
def GetMacAddress():
"""
Convienience function, returns mac addr bound to
first non-loobback interface.
"""
ifname=''
while len(ifname) < 2 :
ifname=GetFirstActiveNetworkInterfaceNonLoopback()[0]
a = Linux_ioctl_GetInterfaceMac(ifname)
return HexStringToByteArray(a)
def DeviceForIdePort(n):
"""
Return device name attached to ide port 'n'.
"""
if n > 3:
return None
g0 = "00000000"
if n > 1:
g0 = "00000001"
n = n - 2
device = None
path = "/sys/bus/vmbus/devices/"
for vmbus in os.listdir(path):
guid = GetFileContents(path + vmbus + "/device_id").lstrip('{').split('-')
if guid[0] == g0 and guid[1] == "000" + str(n):
for root, dirs, files in os.walk(path + vmbus):
if root.endswith("/block"):
device = dirs[0]
break
else : #older distros
for d in dirs:
if ':' in d and "block" == d.split(':')[0]:
device = d.split(':')[1]
break
break
return device
class HttpResourceGoneError(Exception):
pass
def DoInstallRHUIRPM():
"""
Install RHUI RPM according to VM region
"""
rhuiRPMinstalled = os.path.exists(LibDir + "/rhuirpminstalled")
if rhuiRPMinstalled:
return
else:
SetFileContents(LibDir + "/rhuirpminstalled", "")
Log("Begin to install RHUI RPM")
cmd = "grep '<Location>' /var/lib/waagent/ExtensionsConfig* --no-filename | sed 's/<Location>//g' | sed 's/<\/Location>//g' | sed 's/ //g' | tr 'A-Z' 'a-z' | uniq"
retcode,out = RunGetOutput(cmd, True)
region = out.rstrip("\n")
#try a few times at most to get the region info
retry = 0
for i in range(0, 8):
if (region != ""):
break
Log("region info is empty, now wait 15 seconds...")
time.sleep(15)
retcode,out = RunGetOutput(cmd, True)
region = out.rstrip("\n")
if region == "":
Log("could not detect region info, now use the default region: eastus2")
region = "eastus2"
scriptFilePath = "/tmp/install-rhui-rpm.sh"
if not os.path.exists(scriptFilePath):
Error(scriptFilePath + " does not exist, now quit RHUI RPM installation.");
return
#chmod a+x script file
os.chmod(scriptFilePath, 0100)
Log("begin to run " + scriptFilePath)
#execute the downloaded script file
retcode,out = RunGetOutput(scriptFilePath, True)
if retcode != 0:
Error("execute script " + scriptFilePath + " failed, return code: " + str(retcode) + ", now exit RHUI RPM installation.");
return
Log("install RHUI RPM completed")
class Util(object):
"""
Http communication class.
Base of GoalState, and Agent classes.
"""
RetryWaitingInterval=10
def __init__(self):
self.Endpoint = None
def _ParseUrl(self, url):
secure = False
host = self.Endpoint
path = url
port = None
#"http[s]://hostname[:port][/]"
if url.startswith("http://"):
url = url[7:]
if "/" in url:
host = url[0: url.index("/")]
path = url[url.index("/"):]
else:
host = url
path = "/"
elif url.startswith("https://"):
secure = True
url = url[8:]
if "/" in url:
host = url[0: url.index("/")]
path = url[url.index("/"):]
else:
host = url
path = "/"
if host is None:
raise ValueError("Host is invalid:{0}".format(url))
if(":" in host):
pos = host.rfind(":")
port = int(host[pos + 1:])
host = host[0:pos]
return host, port, secure, path
def GetHttpProxy(self, secure):
"""
Get http_proxy and https_proxy from environment variables.
Username and password is not supported now.
"""
host = Config.get("HttpProxy.Host")
port = Config.get("HttpProxy.Port")
return (host, port)
def _HttpRequest(self, method, host, path, port=None, data=None, secure=False,
headers=None, proxyHost=None, proxyPort=None):
resp = None
conn = None
try:
if secure:
port = 443 if port is None else port
if proxyHost is not None and proxyPort is not None:
conn = httplib.HTTPSConnection(proxyHost, proxyPort, timeout=10)
conn.set_tunnel(host, port)
#If proxy is used, full url is needed.
path = "https://{0}:{1}{2}".format(host, port, path)
else:
conn = httplib.HTTPSConnection(host, port, timeout=10)
else:
port = 80 if port is None else port
if proxyHost is not None and proxyPort is not None:
conn = httplib.HTTPConnection(proxyHost, proxyPort, timeout=10)
#If proxy is used, full url is needed.
path = "http://{0}:{1}{2}".format(host, port, path)
else:
conn = httplib.HTTPConnection(host, port, timeout=10)
if headers == None:
conn.request(method, path, data)
else:
conn.request(method, path, data, headers)
resp = conn.getresponse()
except httplib.HTTPException, e:
Error('HTTPException {0}, args:{1}'.format(e, repr(e.args)))
except IOError, e:
Error('Socket IOError {0}, args:{1}'.format(e, repr(e.args)))
return resp
def HttpRequest(self, method, url, data=None,
headers=None, maxRetry=3, chkProxy=False):
"""
Sending http request to server
On error, sleep 10 and maxRetry times.
Return the output buffer or None.
"""
LogIfVerbose("HTTP Req: {0} {1}".format(method, url))
LogIfVerbose("HTTP Req: Data={0}".format(data))
LogIfVerbose("HTTP Req: Header={0}".format(headers))
try:
host, port, secure, path = self._ParseUrl(url)
except ValueError, e:
Error("Failed to parse url:{0}".format(url))
return None
#Check proxy
proxyHost, proxyPort = (None, None)
if chkProxy:
proxyHost, proxyPort = self.GetHttpProxy(secure)
#If httplib module is not built with ssl support. Fallback to http
if secure and not hasattr(httplib, "HTTPSConnection"):
Warn("httplib is not built with ssl support")
secure = False
proxyHost, proxyPort = self.GetHttpProxy(secure)
#If httplib module doesn't support https tunnelling. Fallback to http
if secure and \
proxyHost is not None and \
proxyPort is not None and \
not hasattr(httplib.HTTPSConnection, "set_tunnel"):
Warn("httplib doesn't support https tunnelling(new in python 2.7)")
secure = False
proxyHost, proxyPort = self.GetHttpProxy(secure)
resp = self._HttpRequest(method, host, path, port=port, data=data,
secure=secure, headers=headers,
proxyHost=proxyHost, proxyPort=proxyPort)
for retry in range(0, maxRetry):
if resp is not None and \
(resp.status == httplib.OK or \
resp.status == httplib.CREATED or \
resp.status == httplib.ACCEPTED):
return resp;
if resp is not None and resp.status == httplib.GONE:
raise HttpResourceGoneError("Http resource gone.")
Error("Retry={0}".format(retry))
Error("HTTP Req: {0} {1}".format(method, url))
Error("HTTP Req: Data={0}".format(data))
Error("HTTP Req: Header={0}".format(headers))
if resp is None:
Error("HTTP Err: response is empty.".format(retry))
else:
Error("HTTP Err: Status={0}".format(resp.status))
Error("HTTP Err: Reason={0}".format(resp.reason))
Error("HTTP Err: Header={0}".format(resp.getheaders()))
Error("HTTP Err: Body={0}".format(resp.read()))
time.sleep(self.__class__.RetryWaitingInterval)
resp = self._HttpRequest(method, host, path, port=port, data=data,
secure=secure, headers=headers,
proxyHost=proxyHost, proxyPort=proxyPort)
return None
def HttpGet(self, url, headers=None, maxRetry=3, chkProxy=False):
return self.HttpRequest("GET", url, headers=headers,
maxRetry=maxRetry, chkProxy=chkProxy)
def HttpHead(self, url, headers=None, maxRetry=3, chkProxy=False):
return self.HttpRequest("HEAD", url, headers=headers,
maxRetry=maxRetry, chkProxy=chkProxy)
def HttpPost(self, url, data, headers=None, maxRetry=3, chkProxy=False):
return self.HttpRequest("POST", url, data=data, headers=headers,
maxRetry=maxRetry, chkProxy=chkProxy)
def HttpPut(self, url, data, headers=None, maxRetry=3, chkProxy=False):
return self.HttpRequest("PUT", url, data=data, headers=headers,
maxRetry=maxRetry, chkProxy=chkProxy)
def HttpDelete(self, url, headers=None, maxRetry=3, chkProxy=False):
return self.HttpRequest("DELETE", url, headers=headers,
maxRetry=maxRetry, chkProxy=chkProxy)
def HttpGetWithoutHeaders(self, url, maxRetry=3, chkProxy=False):
"""
Return data from an HTTP get on 'url'.
"""
resp = self.HttpGet(url, headers=None, maxRetry=maxRetry,
chkProxy=chkProxy)
return resp.read() if resp is not None else None
def HttpGetWithHeaders(self, url, maxRetry=3, chkProxy=False):
"""
Return data from an HTTP get on 'url' with
x-ms-agent-name and x-ms-version
headers.
"""
resp = self.HttpGet(url, headers={
"x-ms-agent-name":