from abc import ABCMeta, abstractmethod

import guestfs

from xii import util


class NeedGuestFS():
    __metaclass__ = ABCMeta

    @abstractmethod
    def get_tmp_volume_path(self):
        pass

    def guest(self):
        def _start_guestfs():
            path = self.get_tmp_volume_path()
            guest = guestfs.GuestFS()

            guest.add_drive(path)
            guest.launch()
            guest.mount("/dev/sda1", "/")

            if guest.exists('/etc/sysconfig/selinux'):
                guest.get_selinux = lambda: 1

            return guest

        def _close_guest(guest):
            guest.sync()
            guest.umount("/")
            guest.close()

        return self.share("guestfs", _start_guestfs, _close_guest)

    def guest_get_users(self):
        content = self.guest().cat('/etc/passwd').split("\n")
        return util.parse_passwd(content)

    def guest_user_home(self, name):
        users = self.guest_get_users()
        if name not in users:
            return None

        return users[name]["home"]

    def guest_get_groups(self):
        content = self.guest().cat('/etc/group').split("\n")
        return util.parse_groups(content)

from abc import ABCMeta, abstractmethod

import guestfs

from xii import util, error


class NeedGuestFS():
    __metaclass__ = ABCMeta

    @abstractmethod
    def get_tmp_volume_path(self):
        pass

    def guest(self):
        def _start_guestfs():
            try:
                path = self.get_tmp_volume_path()
                guest = guestfs.GuestFS()

                guest.add_drive(path)
                guest.launch()
                guest.mount("/dev/sda1", "/")

                if guest.exists('/etc/sysconfig/selinux'):
                    guest.get_selinux = lambda: 1
            except RuntimeError as e:
                raise error.ExecError("guestfs error: {}".format(e))

            return guest

        def _close_guest(guest):
            guest.sync()
            guest.umount("/")
            guest.close()

        return self.share("guestfs", _start_guestfs, _close_guest)

    def guest_get_users(self):
        content = self.guest().cat('/etc/passwd').split("\n")
        return util.parse_passwd(content)

    def guest_user_home(self, name):
        users = self.guest_get_users()
        if name not in users:
            return None

        return users[name]["home"]

    def guest_get_groups(self):
        content = self.guest().cat('/etc/group').split("\n")
        return util.parse_groups(content)

Add error handling to guestfs initializer
Add error handling to guestfs initializer

  from abc import ABCMeta, abstractmethod
  
  import guestfs
  
- from xii import util
+ from xii import util, error
  
  
  class NeedGuestFS():
      __metaclass__ = ABCMeta
  
      @abstractmethod
      def get_tmp_volume_path(self):
          pass
  
      def guest(self):
          def _start_guestfs():
+             try:
-             path = self.get_tmp_volume_path()
+                 path = self.get_tmp_volume_path()
-             guest = guestfs.GuestFS()
+                 guest = guestfs.GuestFS()
  
-             guest.add_drive(path)
+                 guest.add_drive(path)
-             guest.launch()
+                 guest.launch()
-             guest.mount("/dev/sda1", "/")
+                 guest.mount("/dev/sda1", "/")
  
-             if guest.exists('/etc/sysconfig/selinux'):
+                 if guest.exists('/etc/sysconfig/selinux'):
-                 guest.get_selinux = lambda: 1
+                     guest.get_selinux = lambda: 1
+             except RuntimeError as e:
+                 raise error.ExecError("guestfs error: {}".format(e))
  
              return guest
  
          def _close_guest(guest):
              guest.sync()
              guest.umount("/")
              guest.close()
  
          return self.share("guestfs", _start_guestfs, _close_guest)
  
      def guest_get_users(self):
          content = self.guest().cat('/etc/passwd').split("\n")
          return util.parse_passwd(content)
  
      def guest_user_home(self, name):
          users = self.guest_get_users()
          if name not in users:
              return None
  
          return users[name]["home"]
  
      def guest_get_groups(self):
          content = self.guest().cat('/etc/group').split("\n")
          return util.parse_groups(content)
  
Add error handling to guestfs initializer
## Code Before:
from abc import ABCMeta, abstractmethod

import guestfs

from xii import util


class NeedGuestFS():
    __metaclass__ = ABCMeta

    @abstractmethod
    def get_tmp_volume_path(self):
        pass

    def guest(self):
        def _start_guestfs():
            path = self.get_tmp_volume_path()
            guest = guestfs.GuestFS()

            guest.add_drive(path)
            guest.launch()
            guest.mount("/dev/sda1", "/")

            if guest.exists('/etc/sysconfig/selinux'):
                guest.get_selinux = lambda: 1

            return guest

        def _close_guest(guest):
            guest.sync()
            guest.umount("/")
            guest.close()

        return self.share("guestfs", _start_guestfs, _close_guest)

    def guest_get_users(self):
        content = self.guest().cat('/etc/passwd').split("\n")
        return util.parse_passwd(content)

    def guest_user_home(self, name):
        users = self.guest_get_users()
        if name not in users:
            return None

        return users[name]["home"]

    def guest_get_groups(self):
        content = self.guest().cat('/etc/group').split("\n")
        return util.parse_groups(content)

## Instruction:
Add error handling to guestfs initializer
## Code After:
from abc import ABCMeta, abstractmethod

import guestfs

from xii import util, error


class NeedGuestFS():
    __metaclass__ = ABCMeta

    @abstractmethod
    def get_tmp_volume_path(self):
        pass

    def guest(self):
        def _start_guestfs():
            try:
                path = self.get_tmp_volume_path()
                guest = guestfs.GuestFS()

                guest.add_drive(path)
                guest.launch()
                guest.mount("/dev/sda1", "/")

                if guest.exists('/etc/sysconfig/selinux'):
                    guest.get_selinux = lambda: 1
            except RuntimeError as e:
                raise error.ExecError("guestfs error: {}".format(e))

            return guest

        def _close_guest(guest):
            guest.sync()
            guest.umount("/")
            guest.close()

        return self.share("guestfs", _start_guestfs, _close_guest)

    def guest_get_users(self):
        content = self.guest().cat('/etc/passwd').split("\n")
        return util.parse_passwd(content)

    def guest_user_home(self, name):
        users = self.guest_get_users()
        if name not in users:
            return None

        return users[name]["home"]

    def guest_get_groups(self):
        content = self.guest().cat('/etc/group').split("\n")
        return util.parse_groups(content)
