Ejemplo n.º 1
0
    def collect_and_send_events(self):
        event_list = TelemetryEventList()
        event_dir = os.path.join(conf.get_lib_dir(), "events")
        event_files = os.listdir(event_dir)
        for event_file in event_files:
            if not event_file.endswith(".tld"):
                continue
            event_file_path = os.path.join(event_dir, event_file)
            try:
                data_str = self.collect_event(event_file_path)
            except EventError as e:
                logger.error("{0}", e)
                continue

            try:
                event = parse_event(data_str)
                event.parameters.extend(self.sysinfo)
                event_list.events.append(event)
            except (ValueError, ProtocolError) as e:
                logger.warn("Failed to decode event file: {0}", e)
                continue

        if len(event_list.events) == 0:
            return

        try:
            protocol = self.distro.protocol_util.get_protocol()
            protocol.report_event(event_list)
        except ProtocolError as e:
            logger.error("{0}", e)
Ejemplo n.º 2
0
    def add_event(self,
                  name,
                  op="",
                  is_success=True,
                  duration=0,
                  version="1.0",
                  message="",
                  evt_type="",
                  is_internal=False):
        event = TelemetryEvent(1, "69B669B9-4AF8-4C50-BDC4-6006FA76E975")
        event.parameters.append(TelemetryEventParam('Name', name))
        event.parameters.append(TelemetryEventParam('Version', version))
        event.parameters.append(TelemetryEventParam('IsInternal', is_internal))
        event.parameters.append(TelemetryEventParam('Operation', op))
        event.parameters.append(
            TelemetryEventParam('OperationSuccess', is_success))
        event.parameters.append(TelemetryEventParam('Message', message))
        event.parameters.append(TelemetryEventParam('Duration', duration))
        event.parameters.append(TelemetryEventParam('ExtensionType', evt_type))

        data = get_properties(event)
        try:
            self.save_event(json.dumps(data))
        except EventError as e:
            logger.error("{0}", e)
Ejemplo n.º 3
0
    def run(self):
        #If provision is enabled, run default provision handler
        if conf.get_provision_enabled():
            super(UbuntuProvisionHandler, self).run()
            return

        logger.info("run Ubuntu provision handler")
        provisioned = os.path.join(conf.get_lib_dir(), "provisioned")
        if os.path.isfile(provisioned):
            return

        logger.info("Waiting cloud-init to copy ovf-env.xml.")
        self.wait_for_ovfenv()

        protocol = self.distro.protocol_util.detect_protocol()
        self.report_not_ready("Provisioning", "Starting")
        logger.info("Sleep 15 seconds to prevent throttling")
        time.sleep(15) #Sleep to prevent throttling
        try:
            logger.info("Wait for ssh host key to be generated.")
            thumbprint = self.wait_for_ssh_host_key()
            fileutil.write_file(provisioned, "")
            logger.info("Finished provisioning")
           
        except ProvisionError as e:
            logger.error("Provision failed: {0}", e)
            self.report_not_ready("ProvisioningFailed", ustr(e))
            self.report_event(ustr(e))
            return
            
        self.report_ready(thumbprint)
        self.report_event("Provision succeed", is_success=True)
Ejemplo n.º 4
0
    def daemon(self):
        logger.info("Run daemon") 
        #Create lib dir
        if not os.path.isdir(conf.get_lib_dir()):
            fileutil.mkdir(conf.get_lib_dir(), mode=0o700)
            os.chdir(conf.get_lib_dir())

        if conf.get_detect_scvmm_env():
            if self.distro.scvmm_handler.run():
                return

        self.distro.provision_handler.run()
        
        if conf.get_resourcedisk_format():
            self.distro.resource_disk_handler.run()

        try:
            protocol = self.distro.protocol_util.detect_protocol()
        except ProtocolError as e:
            logger.error("Failed to detect protocol, exit", e)
            return
        
        self.distro.event_handler.run()
        self.distro.env_handler.run()
        
        while self.running:
            #Handle extensions
            self.distro.ext_handlers_handler.run()
            time.sleep(25)
Ejemplo n.º 5
0
    def collect_and_send_events(self):
        event_list = TelemetryEventList()
        event_dir = os.path.join(conf.get_lib_dir(), "events")
        event_files = os.listdir(event_dir)
        for event_file in event_files:
            if not event_file.endswith(".tld"):
                continue
            event_file_path = os.path.join(event_dir, event_file)
            try:
                data_str = self.collect_event(event_file_path)
            except EventError as e:
                logger.error("{0}", e)
                continue

            try:
                event = parse_event(data_str)
                event.parameters.extend(self.sysinfo)
                event_list.events.append(event)
            except (ValueError, ProtocolError) as e:
                logger.warn("Failed to decode event file: {0}", e)
                continue

        if len(event_list.events) == 0:
            return
        
        try:
            protocol = self.distro.protocol_util.get_protocol()
            protocol.report_event(event_list)
        except ProtocolError as e:
            logger.error("{0}", e)
Ejemplo n.º 6
0
    def parse_plugin_settings(self, ext_handler, plugin_settings):
        if plugin_settings is None:
            return 

        name = ext_handler.name
        version = ext_handler.properties.version
        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

        runtime_settings = None
        runtime_settings_node = find(settings[0], "RuntimeSettings")
        seqNo = getattrib(runtime_settings_node, "seqNo")
        runtime_settings_str = gettext(runtime_settings_node)
        try:
            runtime_settings = json.loads(runtime_settings_str)
        except ValueError as e:
            logger.error("Invalid extension settings")
            return

        for plugin_settings_list in runtime_settings["runtimeSettings"]:
            handler_settings = plugin_settings_list["handlerSettings"]
            ext = Extension()
            #There is no "extension name" in wire protocol.
            #Put
            ext.name = ext_handler.name
            ext.sequenceNumber = seqNo
            ext.publicSettings = handler_settings.get("publicSettings")
            ext.protectedSettings = handler_settings.get("protectedSettings")
            thumbprint = handler_settings.get("protectedSettingsCertThumbprint")
            ext.certificateThumbprint = thumbprint
            ext_handler.properties.extensions.append(ext)
Ejemplo n.º 7
0
def validate_dhcp_resp(request, response):
    bytes_recv = len(response)
    if bytes_recv < 0xF6:
        logger.error("HandleDhcpResponse: Too few bytes received:{0}",
                     bytes_recv)
        return False

    logger.verb("BytesReceived:{0}", hex(bytes_recv))
    logger.verb("DHCP response:{0}", hex_dump(response, bytes_recv))

    # check transactionId, cookie, MAC address cookie should never mismatch
    # transactionId and MAC address may mismatch if we see a response
    # meant from another machine
    if not compare_bytes(request, response, 0xEC, 4):
        logger.verb("Cookie not match:\nsend={0},\nreceive={1}",
                       hex_dump3(request, 0xEC, 4),
                       hex_dump3(response, 0xEC, 4))
        raise DhcpError("Cookie in dhcp respones doesn't match the request")

    if not compare_bytes(request, response, 4, 4):
        logger.verb("TransactionID not match:\nsend={0},\nreceive={1}",
                       hex_dump3(request, 4, 4),
                       hex_dump3(response, 4, 4))
        raise DhcpError("TransactionID in dhcp respones "
                            "doesn't match the request")

    if not compare_bytes(request, response, 0x1C, 6):
        logger.verb("Mac Address not match:\nsend={0},\nreceive={1}",
                       hex_dump3(request, 0x1C, 6),
                       hex_dump3(response, 0x1C, 6))
        raise DhcpError("Mac Addr in dhcp respones "
                            "doesn't match the request")
Ejemplo n.º 8
0
 def enable_swap(self, mount_point):
     logger.info("Enable swap")
     try:
         size_mb = conf.get_resourcedisk_swap_size_mb()
         self.create_swap_space(mount_point, size_mb)
     except ResourceDiskError as e:
         logger.error("Failed to enable swap {0}", e)
Ejemplo n.º 9
0
 def enable_swap(self, mount_point):
     logger.info("Enable swap")
     try:
         size_mb = conf.get_resourcedisk_swap_size_mb()
         self.create_swap_space(mount_point, size_mb)
     except ResourceDiskError as e:
         logger.error("Failed to enable swap {0}", e)
Ejemplo n.º 10
0
def parse_ip_addr(response, option, i, length, bytes_recv):
    if i + 5 < bytes_recv:
        if length != 4:
            logger.error("Endpoint or Default Gateway not 4 bytes")
            return None
        addr = unpack_big_endian(response, i + 2, 4)
        ip_addr = int_to_ip4_addr(addr)
        return ip_addr
    else:
        logger.error("Data too small for option:{0}", option)
    return None
Ejemplo n.º 11
0
 def del_account(self, username):
     if self.is_sys_user(username):
         logger.error("{0} is a system user. Will not delete it.", username)
     shellutil.run("> /var/run/utmp")
     shellutil.run("userdel -f -r " + username)
     #Remove user from suders
     if os.path.isfile("/etc/suders.d/waagent"):
         try:
             content = fileutil.read_file("/etc/sudoers.d/waagent")
             sudoers = content.split("\n")
             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:
             raise OSUtilError("Failed to remove sudoer: {0}".format(e))
Ejemplo n.º 12
0
 def del_account(self, username):
     if self.is_sys_user(username):
         logger.error("{0} is a system user. Will not delete it.", username)
     shellutil.run("> /var/run/utmp")
     shellutil.run("userdel -f -r " + username)
     #Remove user from suders
     if os.path.isfile("/etc/suders.d/waagent"):
         try:
             content = fileutil.read_file("/etc/sudoers.d/waagent")
             sudoers = content.split("\n")
             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:
             raise OSUtilError("Failed to remove sudoer: {0}".format(e))
Ejemplo n.º 13
0
 def activate_resource_disk(self):
     logger.info("Activate resource disk")
     try:
         mount_point = conf.get_resourcedisk_mountpoint()
         fs = conf.get_resourcedisk_filesystem()
         mount_point = self.mount_resource_disk(mount_point, fs)
         warning_file = os.path.join(mount_point, DATALOSS_WARNING_FILE_NAME)
         try:
             fileutil.write_file(warning_file, DATA_LOSS_WARNING)
         except IOError as e:
             logger.warn("Failed to write data loss warnning:{0}", e)
         return mount_point
     except ResourceDiskError as e:
         logger.error("Failed to mount resource disk {0}", e)
         add_event(name="WALA", is_success=False, message=ustr(e),
                           op=WALAEventOperation.ActivateResourceDisk)
Ejemplo n.º 14
0
    def add_event(self, name, op="", is_success=True, duration=0, version="1.0",
                  message="", evt_type="", is_internal=False):
        event = TelemetryEvent(1, "69B669B9-4AF8-4C50-BDC4-6006FA76E975")
        event.parameters.append(TelemetryEventParam('Name', name))
        event.parameters.append(TelemetryEventParam('Version', version))
        event.parameters.append(TelemetryEventParam('IsInternal', is_internal))
        event.parameters.append(TelemetryEventParam('Operation', op))
        event.parameters.append(TelemetryEventParam('OperationSuccess', 
                                                    is_success))
        event.parameters.append(TelemetryEventParam('Message', message))
        event.parameters.append(TelemetryEventParam('Duration', duration))
        event.parameters.append(TelemetryEventParam('ExtensionType', evt_type))

        data = get_properties(event)
        try:
            self.save_event(json.dumps(data))
        except EventError as e:
            logger.error("{0}", e)
Ejemplo n.º 15
0
 def activate_resource_disk(self):
     logger.info("Activate resource disk")
     try:
         mount_point = conf.get_resourcedisk_mountpoint()
         fs = conf.get_resourcedisk_filesystem()
         mount_point = self.mount_resource_disk(mount_point, fs)
         warning_file = os.path.join(mount_point,
                                     DATALOSS_WARNING_FILE_NAME)
         try:
             fileutil.write_file(warning_file, DATA_LOSS_WARNING)
         except IOError as e:
             logger.warn("Failed to write data loss warnning:{0}", e)
         return mount_point
     except ResourceDiskError as e:
         logger.error("Failed to mount resource disk {0}", e)
         add_event(name="WALA",
                   is_success=False,
                   message=ustr(e),
                   op=WALAEventOperation.ActivateResourceDisk)
Ejemplo n.º 16
0
def replace_file(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 as err:
        logger.error('Write to file {0}, Exception is {1}', filepath, err)
        return 1
    finally:
        os.close(handle)

    try:
        os.rename(temp, filepath)
    except IOError as err:
        logger.info('Rename {0} to {1}, Exception is {2}', temp, filepath, err)
        logger.info('Remove original file and retry')
        try:
            os.remove(filepath)
        except IOError as err:
            logger.error('Remove {0}, Exception is {1}', temp, filepath, err)

        try:
            os.rename(temp, filepath)
        except IOError as err:
            logger.error('Rename {0} to {1}, Exception is {2}', temp, filepath,
                         err)
            return 1
    return 0
Ejemplo n.º 17
0
def replace_file(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 as err:
        logger.error('Write to file {0}, Exception is {1}', filepath, err)
        return 1
    finally:
        os.close(handle)

    try:
        os.rename(temp, filepath)
    except IOError as err:
        logger.info('Rename {0} to {1}, Exception is {2}', temp, filepath, err)
        logger.info('Remove original file and retry')
        try:
            os.remove(filepath)
        except IOError as err:
            logger.error('Remove {0}, Exception is {1}', temp, filepath, err)

        try:
            os.rename(temp, filepath)
        except IOError as err:
            logger.error('Rename {0} to {1}, Exception is {2}', temp, filepath,
                         err)
            return 1
    return 0
Ejemplo n.º 18
0
def parse_route(response, option, i, length, bytes_recv):
    # http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
    logger.verb("Routes at offset: {0} with length:{1}", hex(i), hex(length))
    routes = []
    if length < 5:
        logger.error("Data too small for option:{0}", option)
    j = i + 2
    while j < (i + length + 2):
        mask_len_bits = str_to_ord(response[j])
        mask_len_bytes = (((mask_len_bits + 7) & ~7) >> 3)
        mask = 0xFFFFFFFF & (0xFFFFFFFF << (32 - mask_len_bits))
        j += 1
        net = unpack_big_endian(response, j, mask_len_bytes)
        net <<= (32 - mask_len_bytes * 8)
        net &= mask
        j += mask_len_bytes
        gateway = unpack_big_endian(response, j, 4)
        j += 4
        routes.append((net, mask, gateway))
    if j != (i + length + 2):
        logger.error("Unable to parse routes")
    return routes
Ejemplo n.º 19
0
    def run(self):
        #If provision is not enabled, return
        if not conf.get_provision_enabled():
            logger.info("Provisioning is disabled. Skip.")
            return 

        provisioned = os.path.join(conf.get_lib_dir(), "provisioned")
        if os.path.isfile(provisioned):
            return

        logger.info("Run provision handler.")
        logger.info("Copy ovf-env.xml.")
        try:
            ovfenv = self.distro.protocol_util.copy_ovf_env()
        except ProtocolError as e:
            self.report_event("Failed to copy ovf-env.xml: {0}".format(e))
            return
    
        self.distro.protocol_util.detect_protocol_by_file()

        self.report_not_ready("Provisioning", "Starting")
        
        try:
            logger.info("Start provisioning")
            self.provision(ovfenv)
            fileutil.write_file(provisioned, "")
            thumbprint = self.reg_ssh_host_key()
            logger.info("Finished provisioning")
        except ProvisionError as e:
            logger.error("Provision failed: {0}", e)
            self.report_not_ready("ProvisioningFailed", ustr(e))
            self.report_event(ustr(e))
            return

        self.report_ready(thumbprint)
        self.report_event("Provision succeed", is_success=True)
Ejemplo n.º 20
0
def run_get_output(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:
        logger.verb(u"run cmd '{0}'", cmd)
    try:
        output=subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
        output = ustr(output, encoding='utf-8', errors="backslashreplace")
    except subprocess.CalledProcessError as e :
        output = ustr(e.output, encoding='utf-8', errors="backslashreplace")
        if chk_err:
            if log_cmd:
                logger.error(u"run cmd '{0}' failed", e.cmd)
            logger.error(u"Error Code:{0}", e.returncode)
            logger.error(u"Result:{0}", output)
        return e.returncode, output 
    return 0, output
Ejemplo n.º 21
0
def run_get_output(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:
        logger.verb(u"run cmd '{0}'", cmd)
    try:
        output = subprocess.check_output(cmd,
                                         stderr=subprocess.STDOUT,
                                         shell=True)
        output = ustr(output, encoding='utf-8', errors="backslashreplace")
    except subprocess.CalledProcessError as e:
        output = ustr(e.output, encoding='utf-8', errors="backslashreplace")
        if chk_err:
            if log_cmd:
                logger.error(u"run cmd '{0}' failed", e.cmd)
            logger.error(u"Error Code:{0}", e.returncode)
            logger.error(u"Result:{0}", output)
        return e.returncode, output
    return 0, output