project-lakitu: add crictl commands to sosreport.

This commit adds a new sosreport 'crictl' plugin on COS. This plugin is
added to collect 'crictl' commands for debugging containers without a
dependence on 'docker' commands.

This includes adding a 'sosreport package manager' to the COS policy,
which parses the /etc/cos-package-info.json file to determine package
existence and version information on COS, which is needed for some
sosreport plugins, including the newly added 'crictl' plugin.

This commit also removes an unused patchfile.

This commit also adds a license to sos.conf.

BUG=b/180949501
TEST=presubmit,manual verification
RELEASE_NOTE=Added crictl commands to sosreport.

Change-Id: I1c54386cf40cea2fc8676a2a74a740f697954609
Reviewed-on: https://cos-review.googlesource.com/c/cos/overlays/board-overlays/+/23150
Reviewed-by: Roy Yang <royyang@google.com>
Tested-by: Cusky Presubmit Bot <presubmit@cos-infra-prod.iam.gserviceaccount.com>
diff --git a/project-lakitu/app-admin/sosreport/files/0004-release-add-Linux-release-support-on-COS.patch b/project-lakitu/app-admin/sosreport/files/0004-release-add-Linux-release-support-on-COS.patch
deleted file mode 100644
index 6a36420..0000000
--- a/project-lakitu/app-admin/sosreport/files/0004-release-add-Linux-release-support-on-COS.patch
+++ /dev/null
@@ -1,31 +0,0 @@
-From 3e8853df3569b4052d78c22afae34935a6e39e07 Mon Sep 17 00:00:00 2001
-From: Xuewei Zhang <xueweiz@google.com>
-Date: Tue, 29 Jan 2019 17:23:53 -0800
-Subject: [PATCH 4/4] [release] add Linux release support on COS
-
-Signed-off-by: Xuewei Zhang <xueweiz@google.com>
----
- sos/plugins/release.py | 5 +++--
- 1 file changed, 3 insertions(+), 2 deletions(-)
-
-diff --git a/sos/report/plugins/release.py b/sos/report/plugins/release.py
-index 62b59255..c90b5d21 100644
---- a/sos/report/plugins/release.py
-+++ b/sos/report/plugins/release.py
-@@ -6,10 +6,11 @@
- #
- # See the LICENSE file in the source distribution for further information.
- 
--from sos.plugins import Plugin, RedHatPlugin, DebianPlugin, UbuntuPlugin
-+from sos.plugins import (Plugin, RedHatPlugin, DebianPlugin, UbuntuPlugin,
-+                         CosPlugin)
- 
- 
--class Release(Plugin, RedHatPlugin, UbuntuPlugin):
-+class Release(Plugin, RedHatPlugin, UbuntuPlugin, CosPlugin):
-     """Linux release information
-     """
- 
--- 
-2.20.1.495.gaa96b0ce6b-goog
-
diff --git a/project-lakitu/app-admin/sosreport/files/0010-add-package-manager-for-COS.patch b/project-lakitu/app-admin/sosreport/files/0010-add-package-manager-for-COS.patch
new file mode 100644
index 0000000..064c8cc
--- /dev/null
+++ b/project-lakitu/app-admin/sosreport/files/0010-add-package-manager-for-COS.patch
@@ -0,0 +1,81 @@
+From 346f46d882416c45b9c3b12b4da85da2cbd5cdbb Mon Sep 17 00:00:00 2001
+From: Sam Kunz <samkunz@google.com>
+Date: Thu, 23 Sep 2021 23:35:02 +0000
+Subject: [PATCH] [policies] Add package manager for COS.
+
+---
+ sos/policies/distros/cos.py          |  4 +++
+ sos/policies/package_managers/cos.py | 39 ++++++++++++++++++++++++++++
+ 2 files changed, 43 insertions(+)
+ create mode 100644 sos/policies/package_managers/cos.py
+
+diff --git a/sos/policies/distros/cos.py b/sos/policies/distros/cos.py
+index 7b03b9d9..b17aa444 100644
+--- a/sos/policies/distros/cos.py
++++ b/sos/policies/distros/cos.py
+@@ -10,6 +10,7 @@
+ 
+ from sos.report.plugins import CosPlugin
+ from sos.policies.distros import LinuxPolicy
++from sos.policies.package_managers.cos import CosPackageManager
+ 
+ 
+ def _blank_or_comment(line):
+@@ -42,6 +43,9 @@ class CosPolicy(LinuxPolicy):
+                                             probe_runtime=probe_runtime)
+         self.valid_subclasses += [CosPlugin]
+ 
++        self.package_manager = CosPackageManager(chroot=sysroot,
++                                                 remote_exec=remote_exec)
++
+     @classmethod
+     def check(cls, remote=''):
+         if remote:
+diff --git a/sos/policies/package_managers/cos.py b/sos/policies/package_managers/cos.py
+new file mode 100644
+index 00000000..664e77c6
+--- /dev/null
++++ b/sos/policies/package_managers/cos.py
+@@ -0,0 +1,39 @@
++# This file is part of the sos project: https://github.com/sosreport/sos
++#
++# This copyrighted material is made available to anyone wishing to use,
++# modify, copy, or redistribute it subject to the terms and conditions of
++# version 2 of the GNU General Public License.
++#
++# See the LICENSE file in the source distribution for further information.
++
++import json
++
++from sos.policies.package_managers import PackageManager
++
++
++class CosPackageManager(PackageManager):
++    """Subclass for Container-Optimized OS (COS).
++
++    By design, COS does not have a package manager. However, package
++    information can be aquired by parsing the contents of the file
++    '/etc/cos-package-info.json'.
++    """
++
++    def get_pkg_list(self):
++        """Returns dictionary of packages.
++
++        This COS specific method only returns packages installed on the root
++        file system.
++        """
++        if len(self.packages):
++            return self.packages
++
++        with open("/etc/cos-package-info.json") as f:
++            data = json.load(f)
++            for p in data["installedPackages"]:
++                self.packages[p["name"]] = {
++                    "name": p["name"],
++                    "version": p['ebuild_version'].split('-')[0],
++                }
++
++        return self.packages
+-- 
+2.31.0
+
diff --git a/project-lakitu/app-admin/sosreport/files/0011-add-crictl-plugin.patch b/project-lakitu/app-admin/sosreport/files/0011-add-crictl-plugin.patch
new file mode 100644
index 0000000..47381a6
--- /dev/null
+++ b/project-lakitu/app-admin/sosreport/files/0011-add-crictl-plugin.patch
@@ -0,0 +1,103 @@
+From fa15f163c340e73b53cd703bb199d15808f7bec8 Mon Sep 17 00:00:00 2001
+From: Sam Kunz <samkunz@google.com>
+Date: Tue, 28 Sep 2021 18:33:01 +0000
+Subject: [PATCH] [plugins] Add crictl plugin.
+
+---
+ sos/report/plugins/crictl.py | 84 ++++++++++++++++++++++++++++++++++++
+ 1 file changed, 84 insertions(+)
+ create mode 100644 sos/report/plugins/crictl.py
+
+diff --git a/sos/report/plugins/crictl.py b/sos/report/plugins/crictl.py
+new file mode 100644
+index 00000000..48367a73
+--- /dev/null
++++ b/sos/report/plugins/crictl.py
+@@ -0,0 +1,84 @@
++# This file is part of the sos project: https://github.com/sosreport/sos
++#
++# This copyrighted material is made available to anyone wishing to use,
++# modify, copy, or redistribute it subject to the terms and conditions of
++# version 2 of the GNU General Public License.
++#
++# See the LICENSE file in the source distribution for further information.
++
++from sos.report.plugins import Plugin, CosPlugin, SoSPredicate
++
++
++class Crictl(Plugin, CosPlugin):
++
++    short_desc = 'crictl containers'
++    plugin_name = 'crictl'
++    profiles = ('container',)
++    packages = ('cri-tools')
++
++    option_list = [
++        ("all", "enable capture for all containers, even containers "
++            "that have terminated", 'fast', False),
++        ("logs", "capture logs for running containers",
++            'fast', False),
++    ]
++
++    def setup(self):
++        self.add_copy_spec([
++            "/etc/crictl.yaml",
++        ])
++
++        self.add_journal(units='containerd')
++
++        # cri-tools supplies the crictl utility
++        self.set_cmd_predicate(SoSPredicate(self, packages=['cri-tools']))
++
++        subcmds = [
++            'info',
++            'images',
++            'pods',
++            'pods -v',
++            'ps',
++            'ps -a',
++            'ps -v',
++            'stats',
++            'version',
++        ]
++
++        self.add_cmd_output(["crictl %s" % s for s in subcmds])
++
++        ps_cmd = 'crictl ps --quiet'
++        if self.get_option('all'):
++            ps_cmd = "%s -a" % ps_cmd
++
++        img_cmd = 'crictl images --quiet'
++        pod_cmd = 'crictl pods --quiet'
++
++        containers = self._get_crictl_list(ps_cmd)
++        images = self._get_crictl_list(img_cmd)
++        pods = self._get_crictl_list(pod_cmd)
++
++        for container in containers:
++            self.add_cmd_output("crictl inspect %s" % container)
++            if self.get_option('logs'):
++                self.add_cmd_output("crictl logs -t %s" % container,
++                                    subdir="containers")
++
++        for image in images:
++            self.add_cmd_output("crictl inspecti %s" % image, subdir="images")
++
++        for pod in pods:
++            self.add_cmd_output("crictl inspectp %s" % pod, subdir="pods")
++
++    def _get_crictl_list(self, cmd):
++        ret = []
++        result = self.exec_cmd(cmd)
++        if result['status'] == 0:
++            for ent in result['output'].splitlines():
++                ret.append(ent)
++            # Prevent the socket deprecation warning from being iterated over
++            if ret and 'deprecated' in ret[0]:
++                ret.pop(0)
++        return ret
++
++# vim: set et ts=4 sw=4 :
+-- 
+2.31.0
+
diff --git a/project-lakitu/app-admin/sosreport/files/sos.conf b/project-lakitu/app-admin/sosreport/files/sos.conf
index 3310671..1a2fb70 100644
--- a/project-lakitu/app-admin/sosreport/files/sos.conf
+++ b/project-lakitu/app-admin/sosreport/files/sos.conf
@@ -1,6 +1,20 @@
+# Copyright 2021 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
 [report]
 
-only-plugins = cgroups, devices, docker, filesys, kdump, kernel, logs, memory, process, release, systemd
+only-plugins = cgroups, crictl, devices, docker, filesys, kdump, kernel, logs, memory, process, release, systemd
 
 [plugin_options]
 
diff --git a/project-lakitu/app-admin/sosreport/sosreport-4.1-r1.ebuild b/project-lakitu/app-admin/sosreport/sosreport-4.1-r2.ebuild
similarity index 100%
rename from project-lakitu/app-admin/sosreport/sosreport-4.1-r1.ebuild
rename to project-lakitu/app-admin/sosreport/sosreport-4.1-r2.ebuild
diff --git a/project-lakitu/app-admin/sosreport/sosreport-4.1.ebuild b/project-lakitu/app-admin/sosreport/sosreport-4.1.ebuild
index 1555ad6..541fc18 100644
--- a/project-lakitu/app-admin/sosreport/sosreport-4.1.ebuild
+++ b/project-lakitu/app-admin/sosreport/sosreport-4.1.ebuild
@@ -27,12 +27,14 @@
 S="${WORKDIR}/sos-${PV}"
 
 PATCHES=(
-	# TODO(b/172074394): Open upstream PR(s) to apply these changes.
+	# TODO(b/172074394,b/201429334): Open upstream PR(s) to apply these changes.
 	"${FILESDIR}/0005-add-remote-exec-arg-to-COS-policy-and-COSPlugin-to-valid-subclasses.patch"
 	"${FILESDIR}/0006-add-journalctl-o-export-to-COS-logs.patch"
 	"${FILESDIR}/0007-add-Linux-release-support-for-COS-on-sos-4-0.patch"
 	"${FILESDIR}/0008-add-kdump-support-on-COS.patch"
 	"${FILESDIR}/0009-prevent-sos.conf-to-be-installed-automatically.patch"
+	"${FILESDIR}/0010-add-package-manager-for-COS.patch"
+	"${FILESDIR}/0011-add-crictl-plugin.patch"
 )
 
 src_prepare() {