Support py3

This commit is contained in:
Yue Zhang
2015-08-02 06:16:55 +08:00
parent 1846145720
commit 082bbb6ffc
50 changed files with 314 additions and 6256 deletions
+6 -6
View File
@@ -85,16 +85,16 @@ def version():
"""
Show agent version
"""
print("{0} running on {1} {2}".format(AGENT_LONG_VERSION, DISTRO_NAME,
DISTRO_VERSION))
print(("{0} running on {1} {2}".format(AGENT_LONG_VERSION, DISTRO_NAME,
DISTRO_VERSION)))
def usage():
"""
Show agent usage
"""
print("")
print(("usage: {0} [-verbose] [-force] [-help]"
print((("usage: {0} [-verbose] [-force] [-help]"
"-deprovision[+user]|-register-service|-version|-daemon|-start]"
"").format(sys.argv[0]))
"").format(sys.argv[0])))
print("")
def start():
@@ -109,9 +109,9 @@ def register_service():
"""
Register agent as a service
"""
print "Register {0} service".format(AGENT_NAME)
print("Register {0} service".format(AGENT_NAME))
OSUTIL.register_agent_service()
print "Start {0} service".format(AGENT_NAME)
print("Start {0} service".format(AGENT_NAME))
OSUTIL.start_agent_service()
def main():
@@ -17,7 +17,7 @@
# Requires Python 2.4+ and Openssl 1.0+
#
from deprovision import CoreOSDeprovisionHandler
from .deprovision import CoreOSDeprovisionHandler
from azurelinuxagent.distro.default.handlerFactory import DefaultHandlerFactory
class CoreOSHandlerFactory(DefaultHandlerFactory):
@@ -104,10 +104,10 @@ class DeprovisionHandler(object):
def deprovision(self, force=False, deluser=False):
warnings, actions = self.setup(deluser)
for warning in warnings:
print warning
print(warning)
if not force:
confirm = raw_input("Do you want to proceed (y/n)")
confirm = input("Do you want to proceed (y/n)")
if not confirm.lower().startswith('y'):
return
+5 -6
View File
@@ -183,7 +183,7 @@ class ExtensionInstance(object):
def init_logger(self):
#Init logger appender for extension
fileutil.mkdir(self.get_log_dir(), mode=0700)
fileutil.mkdir(self.get_log_dir(), mode=0o700)
log_file = os.path.join(self.get_log_dir(), "CommandExecution.log")
self.logger.add_appender(logger.AppenderType.FILE,
logger.LogLevel.INFO, log_file)
@@ -282,7 +282,7 @@ class ExtensionInstance(object):
self.logger.info("Unpack extension package")
pkg_file = os.path.join(self.lib_dir, os.path.basename(uri.uri) + ".zip")
fileutil.write_file(pkg_file, bytearray(package))
fileutil.write_file(pkg_file, bytearray(package), asbin=True)
zipfile.ZipFile(pkg_file).extractall(self.get_base_dir())
chmod = "find {0} -type f | xargs chmod u+x".format(self.get_base_dir())
shellutil.run(chmod)
@@ -299,9 +299,9 @@ class ExtensionInstance(object):
#Create status and config dir
status_dir = self.get_status_dir()
fileutil.mkdir(status_dir, mode=0700)
fileutil.mkdir(status_dir, mode=0o700)
conf_dir = self.get_conf_dir()
fileutil.mkdir(conf_dir, mode=0700)
fileutil.mkdir(conf_dir, mode=0o700)
#Init handler state to uninstall
self.set_handler_status("NotReady")
@@ -508,8 +508,7 @@ class ExtensionInstance(object):
if major is None:
raise ExtensionError("Wrong version format: {0}".format(version))
packages = filter(lambda x : x.version.startswith(major + "."),
self.pkg_list.versions)
packages = [x for x in self.pkg_list.versions if x.version.startswith(major + ".")]
packages = sorted(packages, key=lambda x: x.version, reverse=True)
if len(packages) <= 0:
raise ExtensionError("Can't find version: {0}.*".format(major))
@@ -16,15 +16,15 @@
#
# Requires Python 2.4+ and Openssl 1.0+
#
from init import InitHandler
from run import MainHandler
from scvmm import ScvmmHandler
from dhcp import DhcpHandler
from env import EnvHandler
from provision import ProvisionHandler
from resourceDisk import ResourceDiskHandler
from extension import ExtensionsHandler
from deprovision import DeprovisionHandler
from .init import InitHandler
from .run import MainHandler
from .scvmm import ScvmmHandler
from .dhcp import DhcpHandler
from .env import EnvHandler
from .provision import ProvisionHandler
from .resourceDisk import ResourceDiskHandler
from .extension import ExtensionsHandler
from .deprovision import DeprovisionHandler
class DefaultHandlerFactory(object):
def __init__(self):
+1 -1
View File
@@ -43,7 +43,7 @@ class InitHandler(object):
path="/dev/console")
#Create lib dir
fileutil.mkdir(OSUTIL.get_lib_dir(), mode=0700)
fileutil.mkdir(OSUTIL.get_lib_dir(), mode=0o700)
os.chdir(OSUTIL.get_lib_dir())
+14 -13
View File
@@ -126,7 +126,7 @@ class DefaultOSUtil(object):
try:
passwd_content = fileutil.read_file(self.passwd_file_path)
passwd = passwd_content.split("\n")
new_passwd = filter(lambda x : not x.startswith(username), passwd)
new_passwd = [x for x in passwd if not x.startswith(username)]
new_passwd.append("{0}:{1}:14600::::::".format(username, passwd_hash))
fileutil.write_file(self.passwd_file_path, "\n".join(new_passwd))
except IOError as e:
@@ -147,13 +147,13 @@ class DefaultOSUtil(object):
else:
sudoer = "{0} ALL = (ALL) ALL\n".format(username)
fileutil.append_file('/etc/sudoers.d/waagent', sudoer)
fileutil.chmod('/etc/sudoers.d/waagent', 0440)
fileutil.chmod('/etc/sudoers.d/waagent', 0o440)
def del_root_password(self):
try:
passwd_content = fileutil.read_file(self.passwd_file_path)
passwd = passwd_content.split('\n')
new_passwd = filter(lambda x : not x.startswith("root:"), passwd)
new_passwd = [x for x in passwd if not x.startswith("root:")]
new_passwd.insert(0, "root:*LOCK*:14600::::::")
fileutil.write_file(self.passwd_file_path, "\n".join(new_passwd))
except IOError as e:
@@ -194,7 +194,7 @@ class DefaultOSUtil(object):
path, thumbprint = keypair
path = self._norm_path(path)
dir_path = os.path.dirname(path)
fileutil.mkdir(dir_path, mode=0700, owner=username)
fileutil.mkdir(dir_path, mode=0o700, owner=username)
lib_dir = self.get_lib_dir()
prv_path = os.path.join(lib_dir, thumbprint + '.prv')
if not os.path.isfile(prv_path):
@@ -205,8 +205,8 @@ class DefaultOSUtil(object):
fileutil.write_file(pub_path, pub)
self.set_selinux_context(pub_path, 'unconfined_u:object_r:ssh_home_t:s0')
self.set_selinux_context(path, 'unconfined_u:object_r:ssh_home_t:s0')
os.chmod(path, 0644)
os.chmod(pub_path, 0600)
os.chmod(path, 0o644)
os.chmod(pub_path, 0o600)
def openssl_to_openssh(self, input_file, output_file):
shellutil.run("ssh-keygen -i -m PKCS8 -f {0} >> {1}".format(input_file,
@@ -222,7 +222,7 @@ class DefaultOSUtil(object):
path = self._norm_path(path)
dir_path = os.path.dirname(path)
fileutil.mkdir(dir_path, mode=0700, owner=username)
fileutil.mkdir(dir_path, mode=0o700, owner=username)
if value is not None:
if not value.startswith("ssh-"):
raise OSUtilError("Bad public key: {0}".format(value))
@@ -238,13 +238,13 @@ class DefaultOSUtil(object):
self.set_selinux_context(pub_path,
'unconfined_u:object_r:ssh_home_t:s0')
self.openssl_to_openssh(pub_path, path)
fileutil.chmod(pub_path, 0600)
fileutil.chmod(pub_path, 0o600)
else:
raise OSUtilError("SSH public key Fingerprint and Value are None")
self.set_selinux_context(path, 'unconfined_u:object_r:ssh_home_t:s0')
fileutil.chowner(path, username)
fileutil.chmod(path, 0644)
fileutil.chmod(path, 0o644)
def is_selinux_system(self):
"""
@@ -292,7 +292,7 @@ class DefaultOSUtil(object):
conf_file_path = self.get_sshd_conf_file_path()
conf = fileutil.read_file(conf_file_path).split("\n")
textutil.set_ssh_config(conf, "ClientAliveInterval", "180")
fileutil.replace_file(conf_file_path, '\n'.join(conf))
fileutil.write_file(conf_file_path, '\n'.join(conf))
logger.info("Configured SSH client probing to keep connections alive.")
def conf_sshd(self, disable_password):
@@ -301,7 +301,7 @@ class DefaultOSUtil(object):
conf = fileutil.read_file(conf_file_path).split("\n")
textutil.set_ssh_config(conf, "PasswordAuthentication", option)
textutil.set_ssh_config(conf, "ChallengeResponseAuthentication", option)
fileutil.replace_file(conf_file_path, "\n".join(conf))
fileutil.write_file(conf_file_path, "\n".join(conf))
logger.info("Disabled SSH password-based authentication methods.")
@@ -469,7 +469,8 @@ class DefaultOSUtil(object):
def is_missing_default_route(self):
routes = shellutil.run_get_output("route -n")[1]
for route in routes:
for route in routes.split("\n"):
print(route)
if route.startswith("0.0.0.0 ") or route.startswith("default "):
return False
return True
@@ -625,7 +626,7 @@ class DefaultOSUtil(object):
try:
content = fileutil.read_file("/etc/sudoers.d/waagent")
sudoers = content.split("\n")
sudoers = filter(lambda x : username not in x, sudoers)
sudoers = [x for x in sudoers if username not in x]
fileutil.write_file("/etc/sudoers.d/waagent",
"\n".join(sudoers))
except IOError as e:
@@ -91,7 +91,7 @@ class ResourceDiskHandler(object):
logger.info("Resource disk {0}1 is already mounted", device)
return existing
fileutil.mkdir(mount_point, mode=0755)
fileutil.mkdir(mount_point, mode=0o755)
logger.info("Detect GPT...")
partition = device + "1"
@@ -102,8 +102,7 @@ class ResourceDiskHandler(object):
if "gpt" in ret[1]:
logger.info("GPT detected")
logger.info("Get GPT partitions")
parts = filter(lambda x : re.match("^\s*[0-9]+", x),
ret[1].split("\n"))
parts = [x for x in ret[1].split("\n") if re.match("^\s*[0-9]+", x)]
logger.info("Found more than {0} GPT partitions.", len(parts))
if len(parts) > 1:
logger.info("Remove old GPT partitions")
+45 -16
View File
@@ -60,30 +60,59 @@ class Redhat6xOSUtil(DefaultOSUtil):
def asn1_to_ssh_rsa(self, pubkey):
lines = pubkey.split("\n")
lines = filter(lambda x : not x.startswith("----"), lines)
lines = [x for x in lines if not x.startswith("----")]
base64_encoded = "".join(lines)
try:
#TODO remove pyasn1 dependency
from pyasn1.codec.der import decoder as der_decoder
der_encoded = base64.b64decode(base64_encoded)
der_encoded = der_decoder.decode(der_encoded)[0][1]
k = der_decoder.decode(textutil.bits_to_str(der_encoded))[0]
n=k[0]
e=k[1]
keydata=""
keydata += struct.pack('>I',len("ssh-rsa"))
keydata += "ssh-rsa"
keydata += struct.pack('>I',len(textutil.num_to_bytes(e)))
keydata += textutil.num_to_bytes(e)
keydata += struct.pack('>I',len(textutil.num_to_bytes(n)) + 1)
keydata += "\0"
keydata += textutil.num_to_bytes(n)
return "ssh-rsa " + base64.b64encode(keydata) + "\n"
key = der_decoder.decode(self.bits_to_bytes(der_encoded))[0]
n=key[0]
e=key[1]
print(n)
print(e)
keydata = bytearray()
keydata.extend(struct.pack('>I', len("ssh-rsa")))
keydata.extend(b"ssh-rsa")
keydata.extend(struct.pack('>I', len(self.num_to_bytes(e))))
keydata.extend(self.num_to_bytes(e))
keydata.extend(struct.pack('>I', len(self.num_to_bytes(n)) + 1))
keydata.extend(b"\0")
keydata.extend(self.num_to_bytes(n))
return str(b"ssh-rsa " + base64.b64encode(keydata) + b"\n",
encoding='utf-8')
except ImportError as e:
raise OSUtilError("Failed to load pyasn1.codec.der")
except Exception as e:
raise OSUtilError(("Failed to convert public key: {0} {1}"
"").format(type(e).__name__, e))
#except Exception as e:
#raise OSUtilError(("Failed to convert public key: {0} {1}"
#"").format(type(e).__name__, e))
def num_to_bytes(self, num):
"""
Pack number into bytes. Retun as string.
"""
result = bytearray()
while num:
result.append(num & 0xFF)
num >>= 8
result.reverse()
return result
def bits_to_bytes(self, bits):
"""
Convert an array contains bits, [0,1] to a byte array
"""
index = 7
byte_array = bytearray()
curr = 0
for bit in bits:
curr = curr | (bit << index)
index = index - 1
if index == -1:
byte_array.append(curr)
curr = 0
index = 7
return bytes(byte_array)
def openssl_to_openssh(self, input_file, output_file):
pubkey = fileutil.read_file(input_file)
+1 -1
View File
@@ -138,7 +138,7 @@ def save_event(data):
event_dir = os.path.join(OSUTIL.get_lib_dir(), 'events')
if not os.path.exists(event_dir):
os.mkdir(event_dir)
os.chmod(event_dir,0700)
os.chmod(event_dir,0o700)
if len(os.listdir(event_dir)) > 1000:
raise EventError("Too many files under: {0}", event_dir)
+4 -6
View File
@@ -48,13 +48,11 @@ class Logger(object):
self.log(LogLevel.ERROR, msg_format, *args)
def log(self, level, msg_format, *args):
msg_format = textutil.ascii(msg_format)
args = map(lambda x: textutil.ascii(x), args)
if len(args) > 0:
msg = msg_format.format(*args)
else:
msg = msg_format
time = datetime.now().strftime('%Y/%m/%d %H:%M:%S.%f')
time = datetime.now().strftime('%Y/%m/%d %H:%M:%S.%f\n')
level_str = LogLevel.STRINGS[level]
if self.prefix is not None:
log_item = "{0} {1} {2} {3}".format(time, level_str, self.prefix,
@@ -79,7 +77,7 @@ class ConsoleAppender(object):
if self.level <= level:
try:
with open(self.path, "w") as console:
console.write(msg.encode('ascii', 'ignore') + "\n")
console.write(msg)
except IOError:
pass
@@ -92,7 +90,7 @@ class FileAppender(object):
if self.level <= level:
try:
with open(self.path, "a+") as log_file:
log_file.write(msg.encode('ascii', 'ignore') + "\n")
log_file.write(msg)
except IOError:
pass
@@ -103,7 +101,7 @@ class StdoutAppender(object):
def write(self, level, msg):
if self.level <= level:
try:
sys.stdout.write(msg.encode('ascii', 'ignore') + "\n")
sys.stdout.write(msg)
except IOError:
pass
+2 -2
View File
@@ -42,7 +42,7 @@ def set_properties(obj, data):
validata_param("data", data, dict)
props = vars(obj)
for name, val in props.items():
for name, val in list(props.items()):
try:
new_val = data[name]
except KeyError:
@@ -64,7 +64,7 @@ def get_properties(obj):
data = {}
props = vars(obj)
for name, val in props.items():
for name, val in list(props.items()):
if isinstance(val, DataContract):
data[name] = get_properties(val)
elif isinstance(val, DataContractList):
@@ -46,11 +46,7 @@ def detect_wire_protocol():
OSUTIL.gen_transport_cert()
protocol = WireProtocol(endpoint)
protocol.initialize()
logger.info("Protocol V1 found.")
path = os.path.join(OSUTIL.get_lib_dir(), WireProtocol)
fileutil.write_file(path, "")
return protocol
def detect_metadata_protocol():
+35 -35
View File
@@ -21,7 +21,7 @@ import json
import re
import time
import traceback
import httplib
import http.client
import xml.sax.saxutils as saxutils
import xml.etree.ElementTree as ET
import azurelinuxagent.logger as logger
@@ -118,24 +118,24 @@ def _fetch_uri(uri, headers, chk_proxy=False):
except restutil.HttpError as e:
raise ProtocolError(str(e))
if(resp.status == httplib.GONE):
if(resp.status == http.client.GONE):
raise WireProtocolResourceGone(uri)
if(resp.status != httplib.OK):
if(resp.status != http.client.OK):
raise ProtocolError("{0} - {1}".format(resp.status, uri))
return resp.read()
return str(resp.read(), encoding='utf-8')
def _fetch_manifest(version_uris):
for version_uri in version_uris:
try:
xml_text = _fetch_uri(version_uri.uri, None, chk_proxy=True)
return xml_text
except IOError, e:
except IOError as e:
logger.warn("Failed to fetch ExtensionManifest: {0}, {1}", e,
version_uri.uri)
raise ProtocolError("Failed to fetch ExtensionManifest from all sources")
def _build_role_properties(container_id, role_instance_id, thumbprint):
xml = (u"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
xml = ("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<RoleProperties>"
"<Container>"
"<ContainerId>{0}</ContainerId>"
@@ -160,7 +160,7 @@ def _build_health_report(incarnation, container_id, role_instance_id,
"<SubStatus>{0}</SubStatus>"
"<Description>{1}</Description>"
"</Details>").format(substatus, description)
xml = (u"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
xml = ("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<Health "
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
@@ -302,7 +302,7 @@ class StatusBlob(object):
"x-ms-date" : timestamp,
'x-ms-version' : self.__class__.__storage_version__
})
if resp is None or resp.status != httplib.OK:
if resp is None or resp.status != http.client.OK:
raise ProtocolError(("Failed to get status blob type: {0}"
"").format(resp.status))
@@ -319,7 +319,7 @@ class StatusBlob(object):
"Content-Length": str(len(data)),
"x-ms-version" : self.__class__.__storage_version__
})
if resp is None or resp.status != httplib.CREATED:
if resp is None or resp.status != http.client.CREATED:
raise ProtocolError(("Failed to upload block blob: {0}"
"").format(resp.status))
@@ -335,7 +335,7 @@ class StatusBlob(object):
"x-ms-blob-content-length" : str(page_blob_size),
"x-ms-version" : self.__class__.__storage_version__
})
if resp is None or resp.status != httplib.CREATED:
if resp is None or resp.status != http.client.CREATED:
raise ProtocolError(("Failed to clean up page blob: {0}"
"").format(resp.status))
@@ -352,36 +352,36 @@ class StatusBlob(object):
end = min(len(data), start + page_max)
content_size = end - start
#Align to 512 bytes
page_end = ((end + 511) / 512) * 512
page_end = int((end + 511) / 512) * 512
buf_size = page_end - start
buf = bytearray(buf_size)
buf[0 : content_size] = data[start : end]
resp = restutil.http_put(url, buffer(buf), {
buf = bytearray(source=data[start:end], encoding="utf-8")
#TODO buffer is not defined in python3, however we need this to make httplib to work on python 2.6
resp = restutil.http_put(url, buf, {
"x-ms-date" : timestamp,
"x-ms-range" : "bytes={0}-{1}".format(start, page_end - 1),
"x-ms-page-write" : "update",
"x-ms-version" : self.__class__.__storage_version__,
"Content-Length": str(page_end - start)
})
if resp is None or resp.status != httplib.CREATED:
if resp is None or resp.status != http.client.CREATED:
raise ProtocolError(("Failed to upload page blob: {0}"
"").format(resp.status))
start = end
def event_param_to_v1(param):
param_format = u'<Param Name="{0}" Value={1} T="{2}" />'
param_format = '<Param Name="{0}" Value={1} T="{2}" />'
param_type = type(param.value)
attr_type = ""
if param_type is int:
attr_type = u'mt:uint64'
attr_type = 'mt:uint64'
elif param_type is str:
attr_type = u'mt:wstr'
attr_type = 'mt:wstr'
elif str(param_type).count("'unicode'") > 0:
attr_type = u'mt:wstr'
attr_type = 'mt:wstr'
elif param_type is bool:
attr_type = u'mt:bool'
attr_type = 'mt:bool'
elif param_type is float:
attr_type = u'mt:float64'
attr_type = 'mt:float64'
return param_format.format(param.name, saxutils.quoteattr(str(param.value)),
attr_type)
@@ -389,9 +389,9 @@ def event_to_v1(event):
params = ""
for param in event.parameters:
params += event_param_to_v1(param)
event_str = (u'<Event id="{0}">'
u'<![CDATA[{1}]]>'
u'</Event>').format(event.eventId, params)
event_str = ('<Event id="{0}">'
'<![CDATA[{1}]]>'
'</Event>').format(event.eventId, params)
return event_str
class WireClient(object):
@@ -568,11 +568,11 @@ class WireClient(object):
def send_event(self, provider_id, event_str):
uri = TELEMETRY_URI.format(self.endpoint)
data_format = (u'<?xml version="1.0"?>'
u'<TelemetryData version="1.0">'
u'<Provider id="{0}">{1}'
u'</Provider>'
u'</TelemetryData>')
data_format = ('<?xml version="1.0"?>'
'<TelemetryData version="1.0">'
'<Provider id="{0}">{1}'
'</Provider>'
'</TelemetryData>')
data = data_format.format(provider_id, event_str)
try:
self.prevent_throttling()
@@ -581,7 +581,7 @@ class WireClient(object):
except restutil.HttpError as e:
raise ProtocolError("Failed to send events:{0}".format(e))
if resp.status != httplib.OK:
if resp.status != http.client.OK:
logger.verb(resp.read())
raise ProtocolError("Failed to send events:{0}".format(resp.status))
@@ -601,7 +601,7 @@ class WireClient(object):
buf[event.providerId] = buf[event.providerId] + event_str
#Send out all events left in buffer.
for provider_id in buf.keys():
for provider_id in list(buf.keys()):
if len(buf[provider_id]) > 0:
self.send_event(provider_id, buf[provider_id])
@@ -896,9 +896,9 @@ class ExtensionsConfig(object):
name = ext.name
version = ext.properties.version
settings = filter(lambda x: getattrib(x, "name") == name and \
getattrib(x ,"version") == version,
plugin_settings)
settings = [x for x in plugin_settings \
if getattrib(x, "name") == name and \
getattrib(x ,"version") == version]
if settings is None or len(settings) == 0:
return
@@ -938,7 +938,7 @@ class ExtensionManifest(object):
version = findtext(package, "Version")
uris = find(package, "Uris")
uri_list = findall(uris, "Uri")
uri_list = map(lambda x : gettext(x), uri_list)
uri_list = [gettext(x) for x in uri_list]
package = ExtensionPackage()
package.version = version
for uri in uri_list:
+4 -4
View File
@@ -16,7 +16,7 @@
#
# Requires Python 2.4+ and Openssl 1.0+
import httplib
import http.client
import json
import azurelinuxagent.utils.restutil as restutil
from azurelinuxagent.protocol.common import *
@@ -57,7 +57,7 @@ class MetadataProtocol(Protocol):
except restutil.HttpError as e:
raise ProtocolError(str(e))
if resp.status != httplib.OK:
if resp.status != http.client.OK:
raise ProtocolError("{0} - GET: {1}".format(resp.status, url))
try:
data = json.loads(resp.read())
@@ -74,7 +74,7 @@ class MetadataProtocol(Protocol):
resp = restutil.http_put(url, json.dumps(data), headers=headers)
except restutil.HttpError as e:
raise ProtocolError(str(e))
if resp.status != httplib.OK:
if resp.status != http.client.OK:
raise ProtocolError("{0} - PUT: {1}".format(resp.status, url))
def _post_data(self, url, obj, headers=None):
@@ -84,7 +84,7 @@ class MetadataProtocol(Protocol):
resp = restutil.http_post(url, json.dumps(data), headers=headers)
except restutil.HttpError as e:
raise ProtocolError(str(e))
if resp.status != httplib.CREATED:
if resp.status != http.client.CREATED:
raise ProtocolError("{0} - POST: {1}".format(resp.status, url))
def initialize(self):
+18 -6
View File
@@ -42,18 +42,30 @@ def read_file(filepath, asbin=False, remove_bom=False):
contents = textutil.remove_bom(contents)
return contents
def write_file(filepath, contents):
def write_file(filepath, contents, asbin=False):
"""
Write 'contents' to 'filepath'.
"""
with open(filepath, "wb") as out_file:
if asbin:
mode = 'wb'
else:
mode = 'w'
if type(contents) != str:
contents = str(contents)
with open(filepath, mode) as out_file:
out_file.write(contents)
def append_file(filepath, contents):
def append_file(filepath, contents, asbin=False):
"""
Append 'contents' to 'filepath'.
"""
with open(filepath, "a+") as out_file:
if asbin:
mode = 'ab'
else:
mode = 'a'
if type(contents) != str:
contents = str(contents)
with open(filepath, mode) as out_file:
out_file.write(contents)
def replace_file(filepath, contents):
@@ -84,7 +96,7 @@ def replace_file(filepath, contents):
try:
os.rename(temp, filepath)
except IOError, err:
except IOError as err:
logger.error('Rename {0} to {1}, Exception is {2}', temp, filepath,
err)
return 1
@@ -143,7 +155,7 @@ def update_conf_file(path, line_start, val, chk_err=False):
if not os.path.isfile(path) and chk_err:
raise Exception("Can't find config file:{0}".format(path))
conf = read_file(path).split('\n')
conf = filter(lambda x: not x.startswith(line_start), conf)
conf = [x for x in conf if not x.startswith(line_start)]
conf.append(val)
replace_file(path, '\n'.join(conf))
+9 -9
View File
@@ -22,9 +22,9 @@ import os
import subprocess
import azurelinuxagent.logger as logger
import azurelinuxagent.conf as conf
import httplib
import http.client
import time
from urlparse import urlparse
from urllib.parse import urlparse
"""
REST api util functions
@@ -61,21 +61,21 @@ def _http_request(method, host, rel_uri, port=None, data=None, secure=False,
if secure:
port = 443 if port is None else port
if proxy_host is not None and proxy_port is not None:
conn = httplib.HTTPSConnection(proxy_host, proxy_port)
conn = http.client.HTTPSConnection(proxy_host, proxy_port)
conn.set_tunnel(host, port)
#If proxy is used, full url is needed.
url = "https://{0}:{1}{2}".format(host, port, rel_uri)
else:
conn = httplib.HTTPSConnection(host, port)
conn = http.client.HTTPSConnection(host, port)
url = rel_uri
else:
port = 80 if port is None else port
if proxy_host is not None and proxy_port is not None:
conn = httplib.HTTPConnection(proxy_host, proxy_port)
conn = http.client.HTTPConnection(proxy_host, proxy_port)
#If proxy is used, full url is needed.
url = "http://{0}:{1}{2}".format(host, port, rel_uri)
else:
conn = httplib.HTTPConnection(host, port)
conn = http.client.HTTPConnection(host, port)
url = rel_uri
if headers == None:
conn.request(method, url, data)
@@ -100,7 +100,7 @@ def http_request(method, url, data, headers=None, max_retry=3, chk_proxy=False):
proxy_host, proxy_port = get_http_proxy()
#If httplib module is not built with ssl support. Fallback to http
if secure and not hasattr(httplib, "HTTPSConnection"):
if secure and not hasattr(http.client, "HTTPSConnection"):
logger.warn("httplib is not built with ssl support")
secure = False
@@ -108,7 +108,7 @@ def http_request(method, url, data, headers=None, max_retry=3, chk_proxy=False):
if secure and \
proxy_host is not None and \
proxy_port is not None and \
not hasattr(httplib.HTTPSConnection, "set_tunnel"):
not hasattr(http.client.HTTPSConnection, "set_tunnel"):
logger.warn("httplib doesn't support https tunnelling(new in python 2.7)")
secure = False
@@ -119,7 +119,7 @@ def http_request(method, url, data, headers=None, max_retry=3, chk_proxy=False):
logger.verb("HTTP Resp: Status={0}", resp.status)
logger.verb(" Header={0}", resp.getheaders())
return resp
except httplib.HTTPException as e:
except http.client.HTTPException as e:
logger.warn('HTTPException {0}, args:{1}', e, repr(e.args))
except IOError as e:
logger.warn('Socket IOError {0}, args:{1}', e, repr(e.args))
+2 -2
View File
@@ -73,12 +73,12 @@ def run_get_output(cmd, chk_err=True):
logger.verb("run cmd '{0}'", cmd)
try:
output=subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
except subprocess.CalledProcessError,e :
except subprocess.CalledProcessError as e :
if chk_err :
logger.error("run cmd '{0}' failed", e.cmd)
logger.error("Error Code:{0}", e.returncode)
logger.error("Result:{0}", e.output[:-1].decode('latin-1'))
return e.returncode, e.output.decode('latin-1')
return 0, output
return 0, str(output, encoding="utf-8")
#End shell command util functions
+9 -34
View File
@@ -188,9 +188,10 @@ def int_to_ip4_addr(a):
def ascii(val):
uni = None
if type(val) == str:
uni = unicode(val, 'utf-8', errors='ignore')
pass
#uni = str(val, 'utf-8', errors='ignore')
else:
uni = unicode(val)
uni = str(val)
if uni is None:
raise ValueError('<Unsupported charset>')
else:
@@ -224,37 +225,11 @@ def remove_bom(c):
return c
def gen_password_hash(password, use_salt, salt_type, salt_len):
salt="$6$"
if use_salt:
collection = string.ascii_letters + string.digits
salt = ''.join(random.choice(collection) for _ in range(salt_len))
salt = "${0}${1}".format(salt_type, salt)
return crypt.crypt(password, salt)
salt="$6$"
if use_salt:
collection = string.ascii_letters + string.digits
salt = ''.join(random.choice(collection) for _ in range(salt_len))
salt = "${0}${1}".format(salt_type, salt)
return crypt.crypt(password, salt)
def num_to_bytes(i):
"""
Pack number into bytes. Retun as string.
"""
result = []
while i:
result.append(chr(i & 0xFF))
i >>= 8
result.reverse()
return ''.join(result)
def bits_to_str(a):
"""
Return string representation of bits in a.
"""
index=7
s = ""
c = 0
for bit in a:
c = c | (bit << index)
index = index - 1
if index == -1:
s = s + struct.pack('>B', c)
c = 0
index = 7
return s
+1 -6032
View File
File diff suppressed because it is too large Load Diff
Executable
+31
View File
@@ -0,0 +1,31 @@
#!/usr/bin/env python
#
# Windows 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.
#
# Requires Python 2.6+ and Openssl 1.0+
#
# Implements parts of RFC 2131, 1541, 1497 and
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
#
if __name__ == '__main__' :
import sys
import azurelinuxagent.agent as agent
agent.main()
sys.exit()
Executable
+31
View File
@@ -0,0 +1,31 @@
#!/usr/bin/env python3
#
# Windows 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.
#
# Requires Python 2.6+ and Openssl 1.0+
#
# Implements parts of RFC 2131, 1541, 1497 and
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
#
if __name__ == '__main__' :
import sys
import azurelinuxagent.agent as agent
agent.main()
sys.exit()
+1 -1
View File
@@ -39,7 +39,7 @@ def get_data_files(name, version, fullname):
#Script file
script_dest = '/usr/sbin'
script_src = ['bin/waagent']
script_src = ['bin/waagent', 'bin/waagent2', 'bin/waagent3']
if name == 'coreos':
script_dest = '/usr/share/oem/bin'
data_files.append((script_dest, script_src))
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
import tests.tools as tools
import uuid
import unittest
+2 -2
View File
@@ -18,8 +18,8 @@
# 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 env
from tools import *
from . import env
from .tools import *
import uuid
import unittest
import os
+1 -1
View File
@@ -19,7 +19,7 @@
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
import os
import env
from . import env
import uuid
import unittest
import tests.tools as tools
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
from tests.tools import *
import unittest
import azurelinuxagent.distro.default.deprovision as deprovision_handler
+3 -3
View File
@@ -18,8 +18,8 @@
# 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 env
from tools import *
from . import env
from .tools import *
import uuid
import unittest
import os
@@ -28,7 +28,7 @@ import azurelinuxagent.utils.fileutil as fileutil
import azurelinuxagent.distro.default.dhcp as dhcp_handler
SampleDhcpResponse = None
with open(os.path.join(env.test_root, "dhcp")) as F:
with open(os.path.join(env.test_root, "dhcp"), 'rb') as F:
SampleDhcpResponse = F.read()
mock_socket_send = MockFunc('socket_send', SampleDhcpResponse)
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
from tests.tools import *
import unittest
from azurelinuxagent.utils.osutil import OSUTIL, OSUtilError
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
from tests.tools import *
import unittest
import time
+2 -2
View File
@@ -18,8 +18,8 @@
# 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 env
from tools import *
from . import env
from .tools import *
import uuid
import unittest
import os
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
from tests.tools import *
import uuid
import unittest
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
import tests.tools as tools
import uuid
import unittest
+18 -15
View File
@@ -18,22 +18,36 @@
# 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 env
import tests.env
import tests.tools as tools
import uuid
import unittest
import os
import sys
import azurelinuxagent.utils.fileutil as fileutil
import test
class TestFileOperations(unittest.TestCase):
def test_get_set_file_contents(self):
def test_read_write_file(self):
test_file='/tmp/test_file'
content = str(uuid.uuid4())
fileutil.write_file(test_file, content)
self.assertTrue(tools.simple_file_grep(test_file, content))
self.assertEquals(content, fileutil.read_file('/tmp/test_file'))
content_read = fileutil.read_file('/tmp/test_file')
print(type(content_read))
self.assertEquals(content, content_read)
os.remove(test_file)
def test_rw_utf8_file(self):
test_file='/tmp/test_file3'
content = "\u6211"
fileutil.write_file(test_file, content)
self.assertTrue(tools.simple_file_grep(test_file, content))
content_read = fileutil.read_file('/tmp/test_file3')
self.assertEquals(content, content_read)
os.remove(test_file)
def test_append_file(self):
test_file='/tmp/test_file2'
@@ -42,17 +56,6 @@ class TestFileOperations(unittest.TestCase):
self.assertTrue(tools.simple_file_grep(test_file, content))
os.remove(test_file)
def test_replace_file(self):
test_file='/tmp/test_file3'
old_content = str(uuid.uuid4())
content = str(uuid.uuid4())
with open(test_file, "a+") as F:
F.write(old_content)
fileutil.replace_file(test_file, content)
self.assertFalse(tools.simple_file_grep(test_file, old_content))
self.assertTrue(tools.simple_file_grep(test_file, content))
os.remove(test_file)
def test_get_last_path_element(self):
filepath = '/tmp/abc.def'
filename = fileutil.base_name(filepath)
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
import tests.tools as tools
import uuid
import unittest
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
import tests.tools as tools
import uuid
import unittest
+20 -5
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
import tests.tools as tools
import uuid
import unittest
@@ -45,8 +45,8 @@ class TestLogger(unittest.TestCase):
_logger.info("{0} {1}", 0, 1)
_logger.warn("{0} {1}", 0, 1)
_logger.error("{0} {1}", 0, 1)
_logger.info("this is a unicode {0}", u'\u6211')
_logger.info("this is a utf-8 {0}", u'\u6211'.encode('utf-8'))
_logger.info("this is a unicode {0}", '\u6211')
_logger.info("this is a utf-8 {0}", '\u6211'.encode('utf-8'))
_logger.info("this is a gbk {0}", 0xff )
def test_file_appender(self):
@@ -63,10 +63,25 @@ class TestLogger(unittest.TestCase):
_logger.verbose("Verbose should not be logged: {0}", msg)
self.assertFalse(tools.simple_file_grep('/tmp/testlog', msg))
_logger.info("this is a unicode {0}", u'\u6211')
_logger.info("this is a utf-8 {0}", u'\u6211'.encode('utf-8'))
_logger.info("this is a unicode {0}", '\u6211')
_logger.info("this is a utf-8 {0}", '\u6211'.encode('utf-8'))
_logger.info("this is a gbk {0}", 0xff)
def test_concole_appender(self):
_logger = logger.Logger()
_logger.add_appender(logger.AppenderType.CONSOLE,
logger.LogLevel.VERBOSE,
'/tmp/testlog')
msg = str(uuid.uuid4())
_logger.info("Test logger: {0}", msg)
self.assertTrue(tools.simple_file_grep('/tmp/testlog', msg))
msg = str(uuid.uuid4())
_logger.verbose("Test logger: {0}", msg)
self.assertFalse(tools.simple_file_grep('/tmp/testlog', msg))
def test_log_to_non_exists_dev(self):
_logger = logger.Logger()
_logger.add_appender(logger.AppenderType.CONSOLE,
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
from tests.tools import *
import unittest
from azurelinuxagent.metadata import AGENT_NAME, AGENT_VERSION, \
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
from tests.tools import *
import uuid
import unittest
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
import tests.tools as tools
import uuid
import unittest
+3 -3
View File
@@ -18,15 +18,15 @@
# 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 env
from . import env
import tests.tools as tools
from tools import *
from .tools import *
import uuid
import unittest
import os
import time
import json
import httplib
import http.client
from azurelinuxagent.protocol.common import *
extensionDataStr = """
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
from tests.tools import *
import uuid
import unittest
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
from tests.tools import *
import unittest
from azurelinuxagent.distro.redhat.osutil import RedhatOSUtil
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
from tests.tools import *
import unittest
import azurelinuxagent.distro.default.resourceDisk as rdh
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
from tests.tools import *
import uuid
import unittest
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
import tests.tools as tools
import uuid
import unittest
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
import tests.tools as tools
import uuid
import unittest
+2 -2
View File
@@ -18,8 +18,8 @@
# 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 env
from tools import *
from . import env
from .tools import *
import uuid
import unittest
import os
+12 -12
View File
@@ -18,22 +18,22 @@
# 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 env
from . import env
import tests.tools as tools
from tools import *
from .tools import *
import uuid
import unittest
import os
import time
import httplib
import http.client
import azurelinuxagent.logger as logger
import azurelinuxagent.protocol.v1 as v1
from test_version import VersionInfoSample
from test_goalstate import goal_state_sample
from test_hostingenv import hosting_env_sample
from test_sharedconfig import shared_config_sample
from test_certificates import certs_sample, transport_cert
from test_extensionsconfig import ext_conf_sample, manifest_sample
from .test_version import VersionInfoSample
from .test_goalstate import goal_state_sample
from .test_hostingenv import hosting_env_sample
from .test_sharedconfig import shared_config_sample
from .test_certificates import certs_sample, transport_cert
from .test_extensionsconfig import ext_conf_sample, manifest_sample
#logger.LoggerInit("/dev/stdout", "/dev/null", verbose=True)
#logger.LoggerInit("/dev/stdout", "/dev/null", verbose=False)
@@ -130,12 +130,12 @@ class TestStatusBlob(unittest.TestCase):
status_blob = v1.StatusBlob(vm_status)
self.assertNotEquals(None, status_blob.to_json())
@mock(v1.restutil, 'http_put', MockFunc(retval=MockResp(httplib.CREATED)))
@mock(v1.restutil, 'http_head', MockFunc(retval=MockResp(httplib.OK)))
@mock(v1.restutil, 'http_put', MockFunc(retval=MockResp(http.client.CREATED)))
@mock(v1.restutil, 'http_head', MockFunc(retval=MockResp(http.client.OK)))
def test_put_page_blob(self):
vm_status = v1.VMStatus()
status_blob = v1.StatusBlob(vm_status)
data = ['a'] * 100
data = 'a' * 100
status_blob.put_page_blob("http://foo.bar", data)
class TestConvert(unittest.TestCase):
+1 -1
View File
@@ -18,7 +18,7 @@
# 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 env
from . import env
import tests.tools as tools
import uuid
import unittest