Merge remote-tracking branch 'puppetlabs/3.11.x' into 3.13.x
* puppetlabs/3.11.x:
(packaging) Updating the FACTER.pot file
(packaging) Bump to version '3.11.11' [no-promote]
(maint) Test Java integration in Travis CI
(FACT-2004) use also nm lease files for azure fact
# Conflicts:
# CMakeLists.txt
# lib/Doxyfile
# locales/FACTER.pot
mihaibuzgau
4 years ago
7 | 7 | script: |
8 | 8 | - > |
9 | 9 | docker run -v `pwd`:/facter gcr.io/cpp-projects/cpp-ci:1 /bin/bash -c " |
10 | apk add --no-cache openjdk8 && | |
11 | export JAVA_HOME=/usr/lib/jvm/default-jvm && | |
10 | 12 | wget https://github.com/puppetlabs/leatherman/releases/download/${LEATHERMAN_VERSION}/leatherman-dynamic.tar.gz && |
11 | 13 | tar xzvf leatherman-dynamic.tar.gz --strip 1 -C / && |
12 | 14 | wget https://github.com/puppetlabs/cpp-hocon/releases/download/${CPPHOCON_VERSION}/cpp-hocon-dynamic.tar.gz && |
289 | 289 | # JDK versions after 9 don't provide javah. Use javac in these cases |
290 | 290 | |
291 | 291 | if(Java_VERSION VERSION_LESS "10") |
292 | set(JAVAH_COMMAND javah) | |
292 | set(JAVAH_COMMAND ${Java_JAVAH_EXECUTABLE}) | |
293 | 293 | set(JAVAH_ARG -classpath facter.jar -d "${CMAKE_CURRENT_LIST_DIR}/src/java" com.puppetlabs.Facter) |
294 | 294 | else() |
295 | set(JAVAH_COMMAND javac) | |
295 | set(JAVAH_COMMAND ${Java_JAVAC_EXECUTABLE}) | |
296 | 296 | set(JAVAH_ARG -h "${CMAKE_CURRENT_LIST_DIR}/src/java" com/puppetlabs/Facter.java) |
297 | 297 | endif() |
298 | 298 |
29 | 29 | |
30 | 30 | /** |
31 | 31 | * Gets whether the machine is running in Azure. |
32 | * @param facts The fact collection that is resolving facts. | |
33 | * @param leases_file The location of where the leases file exists. | |
34 | 32 | * @return Returns "azure" if running on azure, otherwise an empty string. |
35 | 33 | */ |
36 | static std::string get_azure(collection& facts, std::string const& leases_file = "/var/lib/dhcp/dhclient.eth0.leases"); | |
34 | static std::string get_azure(); | |
35 | ||
36 | /** | |
37 | * Checks whether the azure dhcp option (245) is present in leases_file file. | |
38 | * @param leases_file The file containing leases information. | |
39 | * @return Returns "azure" if found, otherwise an empty string. | |
40 | */ | |
41 | static std::string get_azure_from_leases_file(std::string leases_file); | |
37 | 42 | |
38 | 43 | private: |
39 | 44 | static std::string get_cgroup_vm(); |
4 | 4 | #include <facter/facts/fact.hpp> |
5 | 5 | #include <facter/facts/vm.hpp> |
6 | 6 | #include <leatherman/file_util/file.hpp> |
7 | #include <leatherman/file_util/directory.hpp> | |
7 | 8 | #include <leatherman/util/regex.hpp> |
9 | #include <leatherman/logging/logging.hpp> | |
8 | 10 | #include <boost/filesystem.hpp> |
9 | 11 | #include <boost/algorithm/string.hpp> |
10 | 12 | |
22 | 24 | |
23 | 25 | string virtualization_resolver::get_cloud_provider(collection& facts) |
24 | 26 | { |
25 | // Check for Azure | |
26 | std::string provider = get_azure(facts); | |
27 | ||
27 | std::string provider = get_azure(); | |
28 | 28 | return provider; |
29 | 29 | } |
30 | 30 | |
31 | string virtualization_resolver::get_azure(collection& facts, string const& leases_file) | |
31 | string virtualization_resolver::get_azure() | |
32 | 32 | { |
33 | 33 | std::string provider; |
34 | if (boost::filesystem::exists(leases_file)) | |
35 | { | |
36 | lth_file::each_line(leases_file, [&](string& line) { | |
37 | // Search for DHCP option 245. This is an accepted method of determining | |
38 | // whether a machine is running inside Azure. Source: | |
39 | // https://social.msdn.microsoft.com/Forums/azure/en-US/f7fbbee6-370a-41c2-a384-d14ab2a0ac12/what-is-the-correct-method-in-linux-on-azure-to-test-if-you-are-an-azure-vm-?forum=WAVirtualMachinesforWindows | |
40 | if (line.find("option 245") != std::string::npos || | |
41 | line.find("option unknown-245") != std::string::npos) { | |
42 | provider = "azure"; | |
43 | return false; | |
44 | } | |
45 | return true; | |
46 | }); | |
47 | } | |
34 | static vector<string> const dhclient_search_directories = { | |
35 | "/var/lib/dhcp", | |
36 | "/var/lib/NetworkManager" | |
37 | }; | |
38 | ||
39 | for (auto const& dir : dhclient_search_directories) { | |
40 | LOG_DEBUG("searching \"{1}\" for dhclient lease files.", dir); | |
41 | lth_file::each_file(dir, [&](string const& leases_file) { | |
42 | LOG_DEBUG("reading \"{1}\" for dhclient lease azure information.", leases_file); | |
43 | provider = get_azure_from_leases_file(leases_file); | |
44 | return provider.empty(); | |
45 | }, "^dhclient.*lease.*$"); | |
46 | if (!provider.empty()) | |
47 | break; | |
48 | } | |
49 | return provider; | |
50 | } | |
51 | ||
52 | // Search for DHCP option 245. This is an accepted method of determining | |
53 | // whether a machine is running inside Azure. Source: | |
54 | // https://social.msdn.microsoft.com/Forums/azure/en-US/f7fbbee6-370a-41c2-a384-d14ab2a0ac12/what-is-the-correct-method-in-linux-on-azure-to-test-if-you-are-an-azure-vm-?forum=WAVirtualMachinesforWindows | |
55 | string virtualization_resolver::get_azure_from_leases_file(string leases_file) | |
56 | { | |
57 | string provider; | |
58 | lth_file::each_line(leases_file, [&](string& line) { | |
59 | if (line.find("option 245") != std::string::npos || line.find("option unknown-245") != std::string::npos) { | |
60 | LOG_DEBUG("found azure option in \"{1}\" lease file.", leases_file); | |
61 | provider = "azure"; | |
62 | return false; | |
63 | } | |
64 | return true; | |
65 | }); | |
48 | 66 | return provider; |
49 | 67 | } |
50 | 68 |
11 | 11 | |
12 | 12 | struct peek_resolver : linux::virtualization_resolver |
13 | 13 | { |
14 | using virtualization_resolver::get_azure; | |
14 | using virtualization_resolver::get_azure_from_leases_file; | |
15 | 15 | }; |
16 | 16 | |
17 | 17 | SCENARIO("azure") { |
18 | 18 | collection_fixture facts; |
19 | 19 | |
20 | 20 | WHEN("leases file does not exist") { |
21 | auto result = peek_resolver::get_azure(facts, "does-not-exist"); | |
21 | auto result = peek_resolver::get_azure_from_leases_file("does-not-exist"); | |
22 | 22 | THEN("azure is empty") { |
23 | 23 | REQUIRE(result == ""); |
24 | 24 | } |
25 | 25 | } |
26 | 26 | |
27 | 27 | WHEN("leases file contains 'option 245'") { |
28 | auto result = peek_resolver::get_azure(facts, string(LIBFACTER_TESTS_DIRECTORY) + "/fixtures/facts/linux/cloud/azure"); | |
28 | auto result = peek_resolver::get_azure_from_leases_file(string(LIBFACTER_TESTS_DIRECTORY) + "/fixtures/facts/linux/cloud/azure"); | |
29 | 29 | THEN("it reports azure") { |
30 | 30 | REQUIRE(result == "azure"); |
31 | 31 | } |
32 | 32 | } |
33 | 33 | |
34 | 34 | WHEN("leases file contains 'option unknown-245'") { |
35 | auto result = peek_resolver::get_azure(facts, string(LIBFACTER_TESTS_DIRECTORY) + "/fixtures/facts/linux/cloud/azure-unknown"); | |
35 | auto result = peek_resolver::get_azure_from_leases_file(string(LIBFACTER_TESTS_DIRECTORY) + "/fixtures/facts/linux/cloud/azure-unknown"); | |
36 | 36 | THEN("it reports azure") { |
37 | 37 | REQUIRE(result == "azure"); |
38 | 38 | } |
39 | 39 | } |
40 | 40 | |
41 | 41 | WHEN("leases file does not contain correct option") { |
42 | auto result = peek_resolver::get_azure(facts, string(LIBFACTER_TESTS_DIRECTORY) + "/fixtures/facts/linux/cloud/not-azure"); | |
42 | auto result = peek_resolver::get_azure_from_leases_file(string(LIBFACTER_TESTS_DIRECTORY) + "/fixtures/facts/linux/cloud/not-azure"); | |
43 | 43 | THEN("it does not report azure") { |
44 | 44 | REQUIRE(result == ""); |
45 | 45 | } |
429 | 429 | "mismatched processor frequencies found; facter will only report one of them" |
430 | 430 | msgstr "" |
431 | 431 | |
432 | #. debug | |
432 | #. warning | |
433 | 433 | #: lib/src/facts/aix/serial_number_resolver.cc |
434 | 434 | msgid "" |
435 | 435 | "Could not retrieve serial number: sys0 systemid did not match the expected " |
458 | 458 | |
459 | 459 | #. debug |
460 | 460 | #: lib/src/facts/bsd/networking_resolver.cc |
461 | #: lib/src/facts/linux/virtualization_resolver.cc | |
461 | 462 | msgid "searching \"{1}\" for dhclient lease files." |
462 | 463 | msgstr "" |
463 | 464 | |
843 | 844 | msgstr "" |
844 | 845 | |
845 | 846 | #. debug |
847 | #: lib/src/facts/linux/virtualization_resolver.cc | |
848 | msgid "reading \"{1}\" for dhclient lease azure information." | |
849 | msgstr "" | |
850 | ||
851 | #. debug | |
852 | #: lib/src/facts/linux/virtualization_resolver.cc | |
853 | msgid "found azure option in \"{1}\" lease file." | |
854 | msgstr "" | |
855 | ||
856 | #. debug | |
846 | 857 | #: lib/src/facts/map_value.cc |
847 | 858 | msgid "null value cannot be added to map." |
848 | 859 | msgstr "" |