classes-systemd: Add environment file shortcuts

To cut down on typing add a new variable
SYSTEMD_ENVIRONMENT_FILE_${PN} that simply installs the listed files.

Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
Change-Id: I9b82e99816bc09d264a644f91c45472b59a5d343
diff --git a/meta-phosphor/classes/obmc-phosphor-systemd.bbclass b/meta-phosphor/classes/obmc-phosphor-systemd.bbclass
index 85089f8..3b70737 100644
--- a/meta-phosphor/classes/obmc-phosphor-systemd.bbclass
+++ b/meta-phosphor/classes/obmc-phosphor-systemd.bbclass
@@ -25,6 +25,9 @@
 # SYSTEMD_USER_${PN}.service = "foo"
 # SYSTEMD_USER_${unit}.service = "foo"
 #    The user for the unit/package.
+#
+# SYSTEMD_ENVIRONMENT_FILE_${PN} = "foo"
+#    One or more environment files to be installed.
 
 
 inherit obmc-phosphor-utils
@@ -33,6 +36,7 @@
 
 _INSTALL_SD_UNITS=""
 SYSTEMD_DEFAULT_TARGET ?= "obmc-standby.target"
+envfiledir ?= "${sysconfdir}/default"
 
 # Big ugly hack to prevent useradd.bbclass post-parse sanity checker failure.
 # If there are users to be added, we'll add them in our post-parse.
@@ -111,6 +115,7 @@
                 'base_bindir',
                 'bindir',
                 'sbindir',
+                'envfiledir',
                 'SYSTEMD_DEFAULT_TARGET' ]:
             set_append(d, 'SYSTEMD_SUBSTITUTIONS_%s' % file,
                 '%s:%s' % (x, d.getVar(x, True)))
@@ -156,6 +161,13 @@
                 set_append(d, 'USERADD_PACKAGES', pkg)
 
 
+    def add_env_file(d, name, pkg):
+        set_append(d, 'SRC_URI', 'file://%s' % name)
+        set_append(d, 'FILES_%s' % pkg, '%s/%s' \
+            % (d.getVar('envfiledir', True), name))
+        set_append(d, '_INSTALL_ENV_FILES', name)
+
+
     pn = d.getVar('PN', True)
     if d.getVar('SYSTEMD_SERVICE_%s' % pn, True) is None:
         d.setVar('SYSTEMD_SERVICE_%s' % pn, '%s.service' % pn)
@@ -172,6 +184,8 @@
             check_sd_unit(d, unit)
             add_sd_unit(d, unit, pkg)
             add_sd_user(d, unit.name, pkg)
+        for name in listvar_to_list(d, 'SYSTEMD_ENVIRONMENT_FILE_%s' % pkg):
+            add_env_file(d, name, pkg)
 }
 
 
@@ -196,6 +210,29 @@
                         'file \'%s\'' % (e, f))
 
 
+    def install_envs(d):
+        install_dir = d.getVar('D', True)
+        install_dir += d.getVar('envfiledir', True)
+        searchpaths = d.getVar('FILESPATH', True)
+
+        for f in listvar_to_list(d, '_INSTALL_ENV_FILES'):
+            src = bb.utils.which(searchpaths, f)
+            if not os.path.isfile(src):
+                bb.fatal('Did not find SYSTEMD_ENVIRONMENT_FILE:'
+                    '\'%s\'' % src)
+
+            dest = os.path.join(install_dir, f)
+            parent = os.path.dirname(dest)
+            if not os.path.exists(parent):
+                os.makedirs(parent)
+
+            with open(src, 'r') as fd:
+                content = fd.read()
+            with open(dest, 'w+') as fd:
+                fd.write(content)
+
+
+    install_envs(d)
     make_subs(d)
 }