Move systemd service files to Google meta-layer

Test: remove systemd files of Google AGL services in opsy's layer, then build
Bug: 170350192
Change-Id: I3ab5af338e8693a4c4924f2104003b01378211e4
diff --git a/agl_services_build/yocto-layer/meta-google/classes/google-image-overlay.bbclass b/agl_services_build/yocto-layer/meta-google/classes/google-image-overlay.bbclass
new file mode 100644
index 0000000..e0611ae
--- /dev/null
+++ b/agl_services_build/yocto-layer/meta-google/classes/google-image-overlay.bbclass
@@ -0,0 +1,40 @@
+# When inherited by an image recipe, this class provide simple way to
+# put configuration files into rootfs.
+
+ROOTFS_POSTPROCESS_COMMAND_append = "install_google_overlay ; "
+
+python install_google_overlay() {
+    """Fetch and install overlays into rootfs
+
+       Fetch overlays from GOOGLE_OVERLAY_SRC_URI and copy the files
+       inside the folders listed in GOOGLE_OVERLAY_ROOT_DIRS to rootfs.
+
+       GOOGLE_OVERLAY_ROOT_DIRS contains only names of folders, no path.
+
+       GOOGLE_OVERLAY_SRC_URI must be used to get dirs in GOOGLE_OVERLAY_ROOT_DIRS
+       imported to the workdir, it's the same as SRC_URI, so it's
+       possible to use distant repository, tarballs, etc.
+    """
+    workdir = d.getVar('WORKDIR', True)
+    rootfs = d.getVar('IMAGE_ROOTFS', True)
+    srcs = (d.getVar("GOOGLE_OVERLAY_SRC_URI", True) or "").split()
+    overlay_root_dirs = (d.getVar('GOOGLE_OVERLAY_ROOT_DIRS', True) or '').split()
+
+    fetcher = bb.fetch2.Fetch(srcs, d)
+    fetcher.download()
+    fetcher.unpack(workdir)
+
+    for i in range(len(overlay_root_dirs)):
+        fullpath = os.path.join(workdir, overlay_root_dirs[i])
+
+        if not os.path.isdir(fullpath):
+            bb.fatal(fullpath +
+                     " : directory not found, please check your"\
+                     " GOOGLE_OVERLAY_ROOT_DIRS and GOOGLE_OVERLAY_SRC_URI variables.")
+            break
+        os.system("cp -dr " + os.path.join(fullpath, "*") + " " + rootfs)
+}
+
+do_rootfs[vardeps] += "GOOGLE_OVERLAY_ROOT_DIRS GOOGLE_OVERLAY_SRC_URI"
+
+# Copied from https://patchwork.openembedded.org/patch/138100/
diff --git a/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image.bbappend b/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image.bbappend
index 5913c45..4fd6ac9 100644
--- a/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image.bbappend
+++ b/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image.bbappend
@@ -1 +1,9 @@
+FILESEXTRAPATHS_prepend := "${THISDIR}/${BPN}:"
+
+GOOGLE_OVERLAY_SRC_URI = "file://google-overlay"
+
+GOOGLE_OVERLAY_ROOT_DIRS = "google-overlay"
+
 IMAGE_INSTALL += "packagegroup-google-agl"
+
+inherit google-image-overlay
diff --git a/hal/dumpstate/1.1/agl_build/scripts/dumpstate_grpc_server.service b/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image/sa8155/google-overlay/etc/systemd/system/dumpstate_grpc_server.service
similarity index 72%
rename from hal/dumpstate/1.1/agl_build/scripts/dumpstate_grpc_server.service
rename to agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image/sa8155/google-overlay/etc/systemd/system/dumpstate_grpc_server.service
index f709125..72289fb 100644
--- a/hal/dumpstate/1.1/agl_build/scripts/dumpstate_grpc_server.service
+++ b/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image/sa8155/google-overlay/etc/systemd/system/dumpstate_grpc_server.service
@@ -5,7 +5,7 @@
 After=coqos-virtio-vsock.service
 
 [Service]
-ExecStart=/bin/dumpstate_grpc_server -server_addr vsock:2:9310
+ExecStart=/usr/bin/dumpstate_grpc_server -server_addr vsock:2:9310
 Restart=always
 
 [Install]
diff --git a/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image/sa8155/google-overlay/etc/systemd/system/multi-user.target.wants/dumpstate_grpc_server.service b/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image/sa8155/google-overlay/etc/systemd/system/multi-user.target.wants/dumpstate_grpc_server.service
new file mode 120000
index 0000000..e2a37ae
--- /dev/null
+++ b/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image/sa8155/google-overlay/etc/systemd/system/multi-user.target.wants/dumpstate_grpc_server.service
@@ -0,0 +1 @@
+/etc/systemd/system/dumpstate_grpc_server.service
\ No newline at end of file
diff --git a/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image/sa8155/google-overlay/etc/systemd/system/multi-user.target.wants/vehicle_hal_grpc_server.service b/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image/sa8155/google-overlay/etc/systemd/system/multi-user.target.wants/vehicle_hal_grpc_server.service
new file mode 120000
index 0000000..533ed97
--- /dev/null
+++ b/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image/sa8155/google-overlay/etc/systemd/system/multi-user.target.wants/vehicle_hal_grpc_server.service
@@ -0,0 +1 @@
+/etc/systemd/system/vehicle_hal_grpc_server.service
\ No newline at end of file
diff --git a/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image/sa8155/google-overlay/etc/systemd/system/vehicle_hal_grpc_server.service b/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image/sa8155/google-overlay/etc/systemd/system/vehicle_hal_grpc_server.service
new file mode 100644
index 0000000..64d950f
--- /dev/null
+++ b/agl_services_build/yocto-layer/meta-google/recipes-trout/images/machine-image/sa8155/google-overlay/etc/systemd/system/vehicle_hal_grpc_server.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Android Vehicle HAL GRPC Server
+
+Wants=coqos-virtio-vsock.service
+After=coqos-virtio-vsock.service
+
+[Service]
+ExecStart=/usr/bin/vehicle_hal_grpc_server -server_cid 2 -server_port 9210 -power_state_file /tmp/power_state_marker -power_state_socket /tmp/power_state_socket
+Restart=always
+
+[Install]
+WantedBy=multi-user.target
diff --git a/hal/vehicle/2.0/agl_build/scripts/vehicle_hal_grpc_server.service b/hal/vehicle/2.0/agl_build/scripts/vehicle_hal_grpc_server.service
deleted file mode 100644
index ff8c602..0000000
--- a/hal/vehicle/2.0/agl_build/scripts/vehicle_hal_grpc_server.service
+++ /dev/null
@@ -1,12 +0,0 @@
-[Unit]
-Description=Android Vehicle HAL GRPC Server
-
-Wants=coqos-virtio-vsock.service
-After=coqos-virtio-vsock.service
-
-[Service]
-ExecStart=/bin/vehicle_hal_grpc_server -server_cid 2 -server_port 9210 -power_state_file /tmp/power_state_marker -power_state_socket /tmp/power_state_socket
-Restart=always
-
-[Install]
-WantedBy=multi-user.target