Codebase list facter / 2056db8
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
6 changed file(s) with 65 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
77 script:
88 - >
99 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 &&
1012 wget https://github.com/puppetlabs/leatherman/releases/download/${LEATHERMAN_VERSION}/leatherman-dynamic.tar.gz &&
1113 tar xzvf leatherman-dynamic.tar.gz --strip 1 -C / &&
1214 wget https://github.com/puppetlabs/cpp-hocon/releases/download/${CPPHOCON_VERSION}/cpp-hocon-dynamic.tar.gz &&
289289 # JDK versions after 9 don't provide javah. Use javac in these cases
290290
291291 if(Java_VERSION VERSION_LESS "10")
292 set(JAVAH_COMMAND javah)
292 set(JAVAH_COMMAND ${Java_JAVAH_EXECUTABLE})
293293 set(JAVAH_ARG -classpath facter.jar -d "${CMAKE_CURRENT_LIST_DIR}/src/java" com.puppetlabs.Facter)
294294 else()
295 set(JAVAH_COMMAND javac)
295 set(JAVAH_COMMAND ${Java_JAVAC_EXECUTABLE})
296296 set(JAVAH_ARG -h "${CMAKE_CURRENT_LIST_DIR}/src/java" com/puppetlabs/Facter.java)
297297 endif()
298298
2929
3030 /**
3131 * 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.
3432 * @return Returns "azure" if running on azure, otherwise an empty string.
3533 */
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);
3742
3843 private:
3944 static std::string get_cgroup_vm();
44 #include <facter/facts/fact.hpp>
55 #include <facter/facts/vm.hpp>
66 #include <leatherman/file_util/file.hpp>
7 #include <leatherman/file_util/directory.hpp>
78 #include <leatherman/util/regex.hpp>
9 #include <leatherman/logging/logging.hpp>
810 #include <boost/filesystem.hpp>
911 #include <boost/algorithm/string.hpp>
1012
2224
2325 string virtualization_resolver::get_cloud_provider(collection& facts)
2426 {
25 // Check for Azure
26 std::string provider = get_azure(facts);
27
27 std::string provider = get_azure();
2828 return provider;
2929 }
3030
31 string virtualization_resolver::get_azure(collection& facts, string const& leases_file)
31 string virtualization_resolver::get_azure()
3232 {
3333 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 });
4866 return provider;
4967 }
5068
1111
1212 struct peek_resolver : linux::virtualization_resolver
1313 {
14 using virtualization_resolver::get_azure;
14 using virtualization_resolver::get_azure_from_leases_file;
1515 };
1616
1717 SCENARIO("azure") {
1818 collection_fixture facts;
1919
2020 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");
2222 THEN("azure is empty") {
2323 REQUIRE(result == "");
2424 }
2525 }
2626
2727 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");
2929 THEN("it reports azure") {
3030 REQUIRE(result == "azure");
3131 }
3232 }
3333
3434 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");
3636 THEN("it reports azure") {
3737 REQUIRE(result == "azure");
3838 }
3939 }
4040
4141 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");
4343 THEN("it does not report azure") {
4444 REQUIRE(result == "");
4545 }
429429 "mismatched processor frequencies found; facter will only report one of them"
430430 msgstr ""
431431
432 #. debug
432 #. warning
433433 #: lib/src/facts/aix/serial_number_resolver.cc
434434 msgid ""
435435 "Could not retrieve serial number: sys0 systemid did not match the expected "
458458
459459 #. debug
460460 #: lib/src/facts/bsd/networking_resolver.cc
461 #: lib/src/facts/linux/virtualization_resolver.cc
461462 msgid "searching \"{1}\" for dhclient lease files."
462463 msgstr ""
463464
843844 msgstr ""
844845
845846 #. 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
846857 #: lib/src/facts/map_value.cc
847858 msgid "null value cannot be added to map."
848859 msgstr ""