{"action":"created","comment":{"url":"https://api.github.com/repos/CpanelInc/SSP/pulls/comments/197169925","pull_request_review_id":130861506,"id":197169925,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDE5NzE2OTkyNQ==","diff_hunk":"@@ -8363,29 +8372,68 @@ sub get_hostname {\n     sub json {\n         return $json if $json;\n         return if $tried_import;\n-        $json = import_json();\n+        $json = load_module_with_fallbacks(\n+            'needed_subs' => [qw{encode decode}],\n+            'modules'     => [qw{Cpanel::JSON::XS JSON::XS JSON::PP}],\n+        );\n         return $json;\n     }\n+}\n \n-    sub import_json {\n-        return if $tried_import;\n-        $tried_import = 1;\n-        my $try = _choose_json_package();\n-        eval {\n-            return unless $try->can('decode');\n-            return unless $try->can('encode');\n-            1;\n-        } and return $try;\n+# SUB load_module_with_fallbacks(\n+#   'modules'     => [ 'module1', 'module2', ... ],\n+#   'needed_subs' => [ 'do_needful', ... ],\n+#   'fallback'    => sub { *do_needful = sub { ... }; return; },\n+#   'fail_safe'   => 1,\n+# );\n+#\n+# Input is HASH of options:\n+#   'modules'     => ARRAYREF of SCALAR strings corresponding to module names to attempt to import. These are attempted first.\n+#   'needed_subs' => ARRAYREF of SCALAR strings corresponding to subroutine names you need defined from the module(s).\n+#   'fallback'    => CODEREF which defines the needed subs manually. Only used if all modules passed in above fail to load. Optional.\n+#   'fail_safe'   => BOOL whether or not to die if you fail to load the needed subs/modules via all available methods.\n+#\n+# Returns the module/namespace that loaded correctly, throws if all available attempts at finding the desired needed_subs subs fail (unless fail_safe passed).\n+sub load_module_with_fallbacks {\n+    my %opts = @_;\n+    my $namespace_loaded;\n+    foreach my $module2try ( @{ $opts{'modules'} } ) {\n+\n+        # Don't 'require' it if we already have it.\n+        my $inc_entry = join( \"/\", split( \"::\", $module2try ) ) . \".pm\";\n+        if ( !$INC{$module2try} ) {\n+            local $@;\n+            next if !eval \"require $module2try; 1\";\n+        }\n+\n+        # Check if the imported modules 'can' do the job\n+        next if ( scalar( grep { $module2try->can($_) } @{ $opts{'needed_subs'} } ) != scalar( @{ $opts{'needed_subs'} } ) );\n+\n+        # Ok, we're good to go!\n+        $namespace_loaded = $module2try;\n+        last;\n     }\n \n-    sub _choose_json_package {\n-        return 'Cpanel::JSON::XS' if $INC{'Cpanel/JSON/XS.pm'};\n-        return 'JSON::XS'         if $INC{'JSON/XS.pm'};\n-        return 'Cpanel::JSON::XS' if eval { require Cpanel::JSON::XS; 1; };\n-        return 'JSON::XS'         if eval { require JSON::XS; 1; };\n-        return 'JSON::PP'         if eval { require JSON::PP; 1; };\n-    }\n+    # Fallback to coderef, but don't do sanity checking on this, as it is presumed the caller \"knows what they are doing\" if passing a coderef.\n+    if ( !$namespace_loaded ) {\n+        if ( !$opts{'fallback'} || ref $opts{'fallback'} != 'CODE' ) {","path":"ssp","position":98,"original_position":98,"commit_id":"909e7e283e0ecea203947a3625fcb5039ed59806","original_commit_id":"909e7e283e0ecea203947a3625fcb5039ed59806","user":{"login":"cPanelChrisD","id":7917106,"node_id":"MDQ6VXNlcjc5MTcxMDY=","avatar_url":"https://avatars0.githubusercontent.com/u/7917106?v=4","gravatar_id":"","url":"https://api.github.com/users/cPanelChrisD","html_url":"https://github.com/cPanelChrisD","followers_url":"https://api.github.com/users/cPanelChrisD/followers","following_url":"https://api.github.com/users/cPanelChrisD/following{/other_user}","gists_url":"https://api.github.com/users/cPanelChrisD/gists{/gist_id}","starred_url":"https://api.github.com/users/cPanelChrisD/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cPanelChrisD/subscriptions","organizations_url":"https://api.github.com/users/cPanelChrisD/orgs","repos_url":"https://api.github.com/users/cPanelChrisD/repos","events_url":"https://api.github.com/users/cPanelChrisD/events{/privacy}","received_events_url":"https://api.github.com/users/cPanelChrisD/received_events","type":"User","site_admin":false},"body":"The multi-line warning is a bit too verbose for SSP, and it currently doesn't directly connect the failure to load the module as the reason that a check can't be performed:\r\n```\r\n[WARN] * SSP::load_module_with_fallbacks():\r\n        Failed to load any of the following modules:\r\n        YAML::Syck\r\n        OR they did not have the wanted subroutines defined in the modules:\r\n        LoadFile\r\n        ...and no fallback subroutines exist within SSP to accomodate this.\r\n[WARN] * SSP can't check rpm.versions.d/local.versions for customization -- Try using /usr/local/cpanel/3rdparty/bin/perl\r\n```\r\n\r\nThey're printed consecutively but consecutive lines in SSP aren't necessarily related to each other, so it's not obvious that the failure to load YAML::Syck is the reason that SSP can't check rpm.versions.d/local.versions.\r\n\r\nI think the solution to that is to be able to provide some additional help text to load_module_with_fallbacks().  For example:\r\n```\r\nmy $yaml_module = load_module_with_fallbacks(\r\n        'needed_subs' => [qw{LoadFile}],\r\n        'modules' => [qw{YAML::Syck}],\r\n        'fail_warning' => 'SSP can\\'t check rpm.versions.d/local.versions for customization -- Try using /usr/local/cpanel/3rdparty/bin/perl',\r\n```\r\nAlso, IF there is no fail_warning, do not print any warning.  This would allow a silent failure, if necessary.\r\n\r\nExample:\r\n```\r\n[WARN] * Missing Perl Module: YAML::Syck -- SSP can't check rpm.versions.d/local.versions for customization -- Try using /usr/local/cpanel/3rdparty/bin/perl\r\n```\r\n","created_at":"2018-06-21T15:08:32Z","updated_at":"2018-06-21T15:29:45Z","html_url":"https://github.com/CpanelInc/SSP/pull/57#discussion_r197169925","pull_request_url":"https://api.github.com/repos/CpanelInc/SSP/pulls/57","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/CpanelInc/SSP/pulls/comments/197169925"},"html":{"href":"https://github.com/CpanelInc/SSP/pull/57#discussion_r197169925"},"pull_request":{"href":"https://api.github.com/repos/CpanelInc/SSP/pulls/57"}}},"pull_request":{"url":"https://api.github.com/repos/CpanelInc/SSP/pulls/57","id":196468604,"node_id":"MDExOlB1bGxSZXF1ZXN0MTk2NDY4NjA0","html_url":"https://github.com/CpanelInc/SSP/pull/57","diff_url":"https://github.com/CpanelInc/SSP/pull/57.diff","patch_url":"https://github.com/CpanelInc/SSP/pull/57.patch","issue_url":"https://api.github.com/repos/CpanelInc/SSP/issues/57","number":57,"state":"open","locked":false,"title":"Add generic method for loading modules with fallbacks","user":{"login":"troglodyne","id":20960409,"node_id":"MDQ6VXNlcjIwOTYwNDA5","avatar_url":"https://avatars1.githubusercontent.com/u/20960409?v=4","gravatar_id":"","url":"https://api.github.com/users/troglodyne","html_url":"https://github.com/troglodyne","followers_url":"https://api.github.com/users/troglodyne/followers","following_url":"https://api.github.com/users/troglodyne/following{/other_user}","gists_url":"https://api.github.com/users/troglodyne/gists{/gist_id}","starred_url":"https://api.github.com/users/troglodyne/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/troglodyne/subscriptions","organizations_url":"https://api.github.com/users/troglodyne/orgs","repos_url":"https://api.github.com/users/troglodyne/repos","events_url":"https://api.github.com/users/troglodyne/events{/privacy}","received_events_url":"https://api.github.com/users/troglodyne/received_events","type":"User","site_admin":false},"body":"This is based on the most complex use case for this I saw mentioned\r\nin comments by Chris Dillon for TECH-113 (internal case) --\r\nwhere we are loading JSON modules. I noticed that we additionally\r\ncould allow a coderef to define the wanted subroutines, so figured we\r\nmay as well add functionality to do that if we wanted to in the future.\r\n\r\nThis also deduplicates the logic for \"safely/conditionally\"\r\nloading modules for other dependencies in SSP.\r\n\r\nTesting:\r\n* I edited each of these modules to be something slightly \"wrong\" (namewise), observed expected failure modes.\r\n* Removed needed module from manual edit to upstream dependency. Observed expected failure modes.\r\n* DID NOT TEST coderef fallback path, as currently nothing needs logic like that in SSP.","created_at":"2018-06-21T14:19:13Z","updated_at":"2018-06-21T15:29:45Z","closed_at":null,"merged_at":null,"merge_commit_sha":"17f519bc05949570cdacb67f1f7b1d4f942c667a","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/CpanelInc/SSP/pulls/57/commits","review_comments_url":"https://api.github.com/repos/CpanelInc/SSP/pulls/57/comments","review_comment_url":"https://api.github.com/repos/CpanelInc/SSP/pulls/comments{/number}","comments_url":"https://api.github.com/repos/CpanelInc/SSP/issues/57/comments","statuses_url":"https://api.github.com/repos/CpanelInc/SSP/statuses/909e7e283e0ecea203947a3625fcb5039ed59806","head":{"label":"troglodyne:load_with_fallback","ref":"load_with_fallback","sha":"909e7e283e0ecea203947a3625fcb5039ed59806","user":{"login":"troglodyne","id":20960409,"node_id":"MDQ6VXNlcjIwOTYwNDA5","avatar_url":"https://avatars1.githubusercontent.com/u/20960409?v=4","gravatar_id":"","url":"https://api.github.com/users/troglodyne","html_url":"https://github.com/troglodyne","followers_url":"https://api.github.com/users/troglodyne/followers","following_url":"https://api.github.com/users/troglodyne/following{/other_user}","gists_url":"https://api.github.com/users/troglodyne/gists{/gist_id}","starred_url":"https://api.github.com/users/troglodyne/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/troglodyne/subscriptions","organizations_url":"https://api.github.com/users/troglodyne/orgs","repos_url":"https://api.github.com/users/troglodyne/repos","events_url":"https://api.github.com/users/troglodyne/events{/privacy}","received_events_url":"https://api.github.com/users/troglodyne/received_events","type":"User","site_admin":false},"repo":{"id":138076268,"node_id":"MDEwOlJlcG9zaXRvcnkxMzgwNzYyNjg=","name":"SSP","full_name":"troglodyne/SSP","owner":{"login":"troglodyne","id":20960409,"node_id":"MDQ6VXNlcjIwOTYwNDA5","avatar_url":"https://avatars1.githubusercontent.com/u/20960409?v=4","gravatar_id":"","url":"https://api.github.com/users/troglodyne","html_url":"https://github.com/troglodyne","followers_url":"https://api.github.com/users/troglodyne/followers","following_url":"https://api.github.com/users/troglodyne/following{/other_user}","gists_url":"https://api.github.com/users/troglodyne/gists{/gist_id}","starred_url":"https://api.github.com/users/troglodyne/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/troglodyne/subscriptions","organizations_url":"https://api.github.com/users/troglodyne/orgs","repos_url":"https://api.github.com/users/troglodyne/repos","events_url":"https://api.github.com/users/troglodyne/events{/privacy}","received_events_url":"https://api.github.com/users/troglodyne/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/troglodyne/SSP","description":"System Status Probe","fork":true,"url":"https://api.github.com/repos/troglodyne/SSP","forks_url":"https://api.github.com/repos/troglodyne/SSP/forks","keys_url":"https://api.github.com/repos/troglodyne/SSP/keys{/key_id}","collaborators_url":"https://api.github.com/repos/troglodyne/SSP/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/troglodyne/SSP/teams","hooks_url":"https://api.github.com/repos/troglodyne/SSP/hooks","issue_events_url":"https://api.github.com/repos/troglodyne/SSP/issues/events{/number}","events_url":"https://api.github.com/repos/troglodyne/SSP/events","assignees_url":"https://api.github.com/repos/troglodyne/SSP/assignees{/user}","branches_url":"https://api.github.com/repos/troglodyne/SSP/branches{/branch}","tags_url":"https://api.github.com/repos/troglodyne/SSP/tags","blobs_url":"https://api.github.com/repos/troglodyne/SSP/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/troglodyne/SSP/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/troglodyne/SSP/git/refs{/sha}","trees_url":"https://api.github.com/repos/troglodyne/SSP/git/trees{/sha}","statuses_url":"https://api.github.com/repos/troglodyne/SSP/statuses/{sha}","languages_url":"https://api.github.com/repos/troglodyne/SSP/languages","stargazers_url":"https://api.github.com/repos/troglodyne/SSP/stargazers","contributors_url":"https://api.github.com/repos/troglodyne/SSP/contributors","subscribers_url":"https://api.github.com/repos/troglodyne/SSP/subscribers","subscription_url":"https://api.github.com/repos/troglodyne/SSP/subscription","commits_url":"https://api.github.com/repos/troglodyne/SSP/commits{/sha}","git_commits_url":"https://api.github.com/repos/troglodyne/SSP/git/commits{/sha}","comments_url":"https://api.github.com/repos/troglodyne/SSP/comments{/number}","issue_comment_url":"https://api.github.com/repos/troglodyne/SSP/issues/comments{/number}","contents_url":"https://api.github.com/repos/troglodyne/SSP/contents/{+path}","compare_url":"https://api.github.com/repos/troglodyne/SSP/compare/{base}...{head}","merges_url":"https://api.github.com/repos/troglodyne/SSP/merges","archive_url":"https://api.github.com/repos/troglodyne/SSP/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/troglodyne/SSP/downloads","issues_url":"https://api.github.com/repos/troglodyne/SSP/issues{/number}","pulls_url":"https://api.github.com/repos/troglodyne/SSP/pulls{/number}","milestones_url":"https://api.github.com/repos/troglodyne/SSP/milestones{/number}","notifications_url":"https://api.github.com/repos/troglodyne/SSP/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/troglodyne/SSP/labels{/name}","releases_url":"https://api.github.com/repos/troglodyne/SSP/releases{/id}","deployments_url":"https://api.github.com/repos/troglodyne/SSP/deployments","created_at":"2018-06-20T19:21:40Z","updated_at":"2018-06-20T19:21:42Z","pushed_at":"2018-06-21T14:11:50Z","git_url":"git://github.com/troglodyne/SSP.git","ssh_url":"git@github.com:troglodyne/SSP.git","clone_url":"https://github.com/troglodyne/SSP.git","svn_url":"https://github.com/troglodyne/SSP","homepage":null,"size":887,"stargazers_count":0,"watchers_count":0,"language":"Perl","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"other","name":"Other","spdx_id":null,"url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"CpanelInc:master","ref":"master","sha":"bd8e5de0973169da0fbf757a8a893c98ad1878fc","user":{"login":"CpanelInc","id":549400,"node_id":"MDEyOk9yZ2FuaXphdGlvbjU0OTQwMA==","avatar_url":"https://avatars3.githubusercontent.com/u/549400?v=4","gravatar_id":"","url":"https://api.github.com/users/CpanelInc","html_url":"https://github.com/CpanelInc","followers_url":"https://api.github.com/users/CpanelInc/followers","following_url":"https://api.github.com/users/CpanelInc/following{/other_user}","gists_url":"https://api.github.com/users/CpanelInc/gists{/gist_id}","starred_url":"https://api.github.com/users/CpanelInc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CpanelInc/subscriptions","organizations_url":"https://api.github.com/users/CpanelInc/orgs","repos_url":"https://api.github.com/users/CpanelInc/repos","events_url":"https://api.github.com/users/CpanelInc/events{/privacy}","received_events_url":"https://api.github.com/users/CpanelInc/received_events","type":"Organization","site_admin":false},"repo":{"id":5688504,"node_id":"MDEwOlJlcG9zaXRvcnk1Njg4NTA0","name":"SSP","full_name":"CpanelInc/SSP","owner":{"login":"CpanelInc","id":549400,"node_id":"MDEyOk9yZ2FuaXphdGlvbjU0OTQwMA==","avatar_url":"https://avatars3.githubusercontent.com/u/549400?v=4","gravatar_id":"","url":"https://api.github.com/users/CpanelInc","html_url":"https://github.com/CpanelInc","followers_url":"https://api.github.com/users/CpanelInc/followers","following_url":"https://api.github.com/users/CpanelInc/following{/other_user}","gists_url":"https://api.github.com/users/CpanelInc/gists{/gist_id}","starred_url":"https://api.github.com/users/CpanelInc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CpanelInc/subscriptions","organizations_url":"https://api.github.com/users/CpanelInc/orgs","repos_url":"https://api.github.com/users/CpanelInc/repos","events_url":"https://api.github.com/users/CpanelInc/events{/privacy}","received_events_url":"https://api.github.com/users/CpanelInc/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/CpanelInc/SSP","description":"System Status Probe","fork":true,"url":"https://api.github.com/repos/CpanelInc/SSP","forks_url":"https://api.github.com/repos/CpanelInc/SSP/forks","keys_url":"https://api.github.com/repos/CpanelInc/SSP/keys{/key_id}","collaborators_url":"https://api.github.com/repos/CpanelInc/SSP/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/CpanelInc/SSP/teams","hooks_url":"https://api.github.com/repos/CpanelInc/SSP/hooks","issue_events_url":"https://api.github.com/repos/CpanelInc/SSP/issues/events{/number}","events_url":"https://api.github.com/repos/CpanelInc/SSP/events","assignees_url":"https://api.github.com/repos/CpanelInc/SSP/assignees{/user}","branches_url":"https://api.github.com/repos/CpanelInc/SSP/branches{/branch}","tags_url":"https://api.github.com/repos/CpanelInc/SSP/tags","blobs_url":"https://api.github.com/repos/CpanelInc/SSP/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/CpanelInc/SSP/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/CpanelInc/SSP/git/refs{/sha}","trees_url":"https://api.github.com/repos/CpanelInc/SSP/git/trees{/sha}","statuses_url":"https://api.github.com/repos/CpanelInc/SSP/statuses/{sha}","languages_url":"https://api.github.com/repos/CpanelInc/SSP/languages","stargazers_url":"https://api.github.com/repos/CpanelInc/SSP/stargazers","contributors_url":"https://api.github.com/repos/CpanelInc/SSP/contributors","subscribers_url":"https://api.github.com/repos/CpanelInc/SSP/subscribers","subscription_url":"https://api.github.com/repos/CpanelInc/SSP/subscription","commits_url":"https://api.github.com/repos/CpanelInc/SSP/commits{/sha}","git_commits_url":"https://api.github.com/repos/CpanelInc/SSP/git/commits{/sha}","comments_url":"https://api.github.com/repos/CpanelInc/SSP/comments{/number}","issue_comment_url":"https://api.github.com/repos/CpanelInc/SSP/issues/comments{/number}","contents_url":"https://api.github.com/repos/CpanelInc/SSP/contents/{+path}","compare_url":"https://api.github.com/repos/CpanelInc/SSP/compare/{base}...{head}","merges_url":"https://api.github.com/repos/CpanelInc/SSP/merges","archive_url":"https://api.github.com/repos/CpanelInc/SSP/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/CpanelInc/SSP/downloads","issues_url":"https://api.github.com/repos/CpanelInc/SSP/issues{/number}","pulls_url":"https://api.github.com/repos/CpanelInc/SSP/pulls{/number}","milestones_url":"https://api.github.com/repos/CpanelInc/SSP/milestones{/number}","notifications_url":"https://api.github.com/repos/CpanelInc/SSP/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/CpanelInc/SSP/labels{/name}","releases_url":"https://api.github.com/repos/CpanelInc/SSP/releases{/id}","deployments_url":"https://api.github.com/repos/CpanelInc/SSP/deployments","created_at":"2012-09-05T14:38:25Z","updated_at":"2018-06-11T17:26:48Z","pushed_at":"2018-06-21T14:19:14Z","git_url":"git://github.com/CpanelInc/SSP.git","ssh_url":"git@github.com:CpanelInc/SSP.git","clone_url":"https://github.com/CpanelInc/SSP.git","svn_url":"https://github.com/CpanelInc/SSP","homepage":null,"size":784,"stargazers_count":7,"watchers_count":7,"language":"Perl","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":19,"mirror_url":null,"archived":false,"open_issues_count":1,"license":{"key":"other","name":"Other","spdx_id":null,"url":null,"node_id":"MDc6TGljZW5zZTA="},"forks":19,"open_issues":1,"watchers":7,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/CpanelInc/SSP/pulls/57"},"html":{"href":"https://github.com/CpanelInc/SSP/pull/57"},"issue":{"href":"https://api.github.com/repos/CpanelInc/SSP/issues/57"},"comments":{"href":"https://api.github.com/repos/CpanelInc/SSP/issues/57/comments"},"review_comments":{"href":"https://api.github.com/repos/CpanelInc/SSP/pulls/57/comments"},"review_comment":{"href":"https://api.github.com/repos/CpanelInc/SSP/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/CpanelInc/SSP/pulls/57/commits"},"statuses":{"href":"https://api.github.com/repos/CpanelInc/SSP/statuses/909e7e283e0ecea203947a3625fcb5039ed59806"}},"author_association":"NONE"}}
{"actor":{"display_login":"cPanelChrisD"}}