Repository: opensearch-project/opensearch-build

Issue:
[BUG] Only send `-d` to core components, instead of everything.
As of now, build workflow will not send `-d` if user does not specify `--distribution`.
However, if user specify things such as `--distribution rpm` and try to build core+all plugins,
`-d` will be sent to plugins as well.

The plugin build script does not know how to interpret `-d` thus fail.
```
+ echo 'Invalid option: -?'
Invalid option: -?
+ exit 1
2022-02-17 23:58:36 ERROR    Error building common-utils, retry with: ./build.sh manifests/1.3.0/opensearch-1.3.0.yml --component common-utils
Traceback (most recent call last):
  File "./src/run_build.py", line 79, in <module>
    sys.exit(main())
  File "./src/run_build.py", line 67, in main
    builder.build(build_recorder)
  File "/local/home/zhujiaxi/opensearch-build-peterzhuamazon/src/build_workflow/builder_from_source.py", line 49, in build
    self.git_repo.execute(build_command)
  File "/local/home/zhujiaxi/opensearch-build-peterzhuamazon/src/git/git_repository.py", line 83, in execute
    subprocess.check_call(command, cwd=cwd, shell=True)
  File "/usr/lib64/python3.7/subprocess.py", line 363, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command 'bash /local/home/zhujiaxi/opensearch-build-peterzhuamazon/scripts/components/common-utils/build.sh -v 1.3.0 -p linux -a x64 -d rpm -s false -o builds' returned non-zero exit status 1.
```

Need to add a condition where if component != OpenSearch/OpenSearch-Dashboards, then `-d` will not be sent even if not None.


Discussion:
> Need to add a condition where if component != OpenSearch/OpenSearch-Dashboards, then `-d` will not be sent even if not None.

That's too bad. Maybe it's easier to just add required `-d` into all the scripts? 


Resolving patch:
diff --git a/src/build_workflow/builder_from_source.py b/src/build_workflow/builder_from_source.py
--- a/src/build_workflow/builder_from_source.py
+++ b/src/build_workflow/builder_from_source.py
@@ -28,6 +28,11 @@ def checkout(self, work_dir: str) -> None:
         )
 
     def build(self, build_recorder: BuildRecorder) -> None:
+
+        # List of components whose build scripts support `-d` parameter
+        # Bundled plugins do not need `-d` as they are java based zips
+        DISTRIBUTION_SUPPORTED_COMPONENTS = ["OpenSearch", "OpenSearch-Dashboards"]
+
         build_script = ScriptFinder.find_build_script(self.target.name, self.component.name, self.git_repo.working_directory)
 
         build_command = " ".join(
@@ -39,7 +44,7 @@ def build(self, build_recorder: BuildRecorder) -> None:
                     f"-v {self.target.version}",
                     f"-p {self.target.platform}",
                     f"-a {self.target.architecture}",
-                    f"-d {self.target.distribution}" if self.target.distribution else None,
+                    f"-d {self.target.distribution}" if self.target.distribution and (self.component.name in DISTRIBUTION_SUPPORTED_COMPONENTS) else None,
                     f"-s {str(self.target.snapshot).lower()}",
                     f"-o {self.output_path}",
                 ]


Test patch:
diff --git a/tests/tests_build_workflow/test_builder_from_source.py b/tests/tests_build_workflow/test_builder_from_source.py
--- a/tests/tests_build_workflow/test_builder_from_source.py
+++ b/tests/tests_build_workflow/test_builder_from_source.py
@@ -40,6 +40,18 @@ def setUp(self) -> None:
             ),
         )
 
+        self.builder_distribution_support = BuilderFromSource(
+            InputComponentFromSource({"name": "common-utils", "repository": "url", "ref": "ref"}),
+            BuildTarget(
+                name="OpenSearch",
+                version="1.3.0",
+                platform="linux",
+                architecture="x64",
+                distribution="rpm",
+                snapshot=False,
+            ),
+        )
+
     def test_builder(self) -> None:
         self.assertEqual(self.builder.component.name, "common-utils")
 
@@ -86,6 +98,27 @@ def test_build_distribution(self, mock_git_repo: Mock) -> None:
         )
         build_recorder.record_component.assert_called_with("OpenSearch", mock_git_repo.return_value)
 
+    @patch("build_workflow.builder_from_source.GitRepository")
+    def test_build_distribution_support(self, mock_git_repo: Mock) -> None:
+        mock_git_repo.return_value = MagicMock(working_directory="dir")
+        build_recorder = MagicMock()
+        self.builder_distribution_support.checkout("dir")
+        self.builder_distribution_support.build(build_recorder)
+        mock_git_repo.return_value.execute.assert_called_with(
+            " ".join(
+                [
+                    "bash",
+                    os.path.realpath(os.path.join(ScriptFinder.component_scripts_path, "common-utils", "build.sh")),
+                    "-v 1.3.0",
+                    "-p linux",
+                    "-a x64",
+                    "-s false",
+                    "-o builds",
+                ]
+            )
+        )
+        build_recorder.record_component.assert_called_with("common-utils", mock_git_repo.return_value)
+
     @patch("build_workflow.builder_from_source.GitRepository")
     def test_build_snapshot(self, mock_git_repo: Mock) -> None:
         self.builder.target.snapshot = True