Fix typos in var names
[snf-image-creator] / image_creator / os_type / linux.py
index 066f6bd..de82e7d 100644 (file)
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.
 
-from image_creator.os_type.unix import Unix
-from image_creator.util import warn
-
-from clint.textui import puts, indent
+from image_creator.os_type.unix import Unix, sysprep
+from image_creator.util import warn, output
 
 import re
 
 import re
+import time
 
 
 class Linux(Unix):
 
 
 class Linux(Unix):
@@ -57,42 +56,83 @@ class Linux(Unix):
                 self._uuid[dev] = attr[1]
                 return attr[1]
 
                 self._uuid[dev] = attr[1]
                 return attr[1]
 
-    def sysprep_acpid(self):
-        """Replace acpid powerdown action scripts to automatically shutdown
-        the system without checking if a GUI is running.
+    @sysprep()
+    def fix_acpid(self, print_header=True):
+        """Replace acpid powerdown action scripts to immediately shutdown the
+        system without checking if a GUI is running.
         """
 
         """
 
-        puts('* Fixing acpid powerdown action')
+        if print_header:
+            output('Fixing acpid powerdown action')
 
 
-        action = '#!/bin/sh\n\nPATH=/sbin:/bin:/usr/bin\n shutdown -h now '
-        '\"Power button pressed\"'
+        powerbtn_action = '#!/bin/sh\n\nPATH=/sbin:/bin:/usr/bin\n' \
+                                'shutdown -h now \"Power button pressed\"\n'
 
 
-        if self.g.is_file('/etc/acpi/powerbtn.sh'):
-            self.g.write('/etc/acpi/powerbtn.sh', action)
-        elif self.g.is_file('/etc/acpi/actions/power.sh'):
-            self.g.write('/etc/acpi/actions/power.sh', action)
-        else:
-            with indent(2):
-                warn("No acpid action file found")
+        events_dir = '/etc/acpi/events'
+        if not self.g.is_dir(events_dir):
+            warn("No acpid event directory found")
+            return
 
 
-    def sysprep_persistent_net_rules(self):
+        event_exp = re.compile('event=(.+)', re.I)
+        action_exp = re.compile('action=(.+)', re.I)
+        for f in self.g.readdir(events_dir):
+            if f['ftyp'] != 'r':
+                continue
+
+            fullpath = "%s/%s" % (events_dir, f['name'])
+            event = ""
+            action = ""
+            for line in self.g.cat(fullpath).splitlines():
+                m = event_exp.match(line)
+                if m:
+                    event = m.group(1)
+                    continue
+                m = action_exp.match(line)
+                if m:
+                    action = m.group(1)
+                    continue
+
+            if event.strip() == "button[ /]power":
+                if action:
+                    if not self.g.is_file(action):
+                        warn("Acpid action file: %s does not exist" % action)
+                        return
+                    self.g.copy_file_to_file(action, \
+                      "%s.orig.snf-image-creator-%d" % (action, time.time()))
+                    self.g.write(action, powerbtn_action)
+                    return
+                else:
+                    warn("Acpid event file %s does not contain and action")
+                    return
+            elif event.strip() == ".*":
+                warn("Found action `.*'. Don't know how to handle this." \
+                    " Please edit \%s' image file manually to make the " \
+                    "system immediatelly shutdown when an power button acpi " \
+                    "event occures" % action)
+                return
+
+    @sysprep()
+    def persistent_net_rules(self, print_header=True):
         """Remove udev rules that will keep network interface names persistent
         after hardware changes and reboots. Those rules will be created again
         the next time the image runs.
         """
 
         """Remove udev rules that will keep network interface names persistent
         after hardware changes and reboots. Those rules will be created again
         the next time the image runs.
         """
 
-        puts('* Removing persistent network interface names')
+        if print_header:
+            output('Removing persistent network interface names')
 
         rule_file = '/etc/udev/rules.d/70-persistent-net.rules'
         if self.g.is_file(rule_file):
             self.g.rm(rule_file)
 
 
         rule_file = '/etc/udev/rules.d/70-persistent-net.rules'
         if self.g.is_file(rule_file):
             self.g.rm(rule_file)
 
-    def sysprep_persistent_devs(self):
-        """Scan fstab and grub configuration files and replace all
-        non-persistent device appearences with UUIDs.
+    @sysprep()
+    def persistent_devs(self, print_header=True):
+        """Scan fstab & grub configuration files and replace all non-persistent
+        device appearences with UUIDs.
         """
 
         """
 
-        puts('* Replacing fstab & grub non-persistent device appearences')
+        if print_header:
+            output('Replacing fstab & grub non-persistent device appearences')
 
         # convert all devices in fstab to persistent
         persistent_root = self._persistent_fstab()
 
         # convert all devices in fstab to persistent
         persistent_root = self._persistent_fstab()