Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #12211 - Improve DHCP subnets parsing to get more informations … #330

Conversation

bagasse
Copy link
Member

@bagasse bagasse commented Oct 20, 2015

…from DHCP server

Add ability to parse some subnet options and provides them to the foreman to enhance subnet imports.

This modify the dhcp smart-proxy API (adding options hash).

case option
when /option\s+routers\s+[\d\.]+/
# get the first router match and use it for gateway
options[:gateway] = option.scan(/option\s+routers\s+([\d\.]+)/).first.first
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not keep the whole list?

@dmitri-d
Copy link
Member

@bagasse: cheers for the PR!

I feel that in the data we return we should keep option names and format of their values close to the original ones (if not the same): gateway would become :routers and contain the whole list, :dns_primary and :dns_secondary would become :domain_name_servers and keep all of the servers, etc.

Another issue that I see is that dhcp config file may contain global options applicable to all subnets (and potentially overridden in individual subnet configs). I we should support this if we are going to return these additional options.

@bagasse
Copy link
Member Author

bagasse commented Oct 20, 2015

@witlessbird

I changed the options names to map names used on the foreman side. I will update the PR to keep names like in the dhcp server config, and keep all values too.
For the global options, i didn't take a look at it because you can have global options, shared networks blocks and group block (with some nested ones) that overrides some of the options. I wanted to keep this simple at this time and just take local subnet options. Parsing dhcpd config with regex with this kind of russian dolls config blocks was out of the scope of this PR (and testing all cases is not trivial), but if it's a requirement for you, i will take a look at it when i will have more time.

@dmitri-d
Copy link
Member

@bagasse: let's wait with the renaming until we hear something from the foreman side (although I think it makes sense to rename attribute names there and keep them close to original here).

Yeah, I agree that parsing global options makes things more complicated (and possibly warrants a real parser, not regexes). I'm ok with postponing this bit, as long as we can get the functionality in not too distant future.

@bagasse bagasse force-pushed the Fixes_12211_-_Improve_DHCP_subnets_parsing_to_get_more_informations_from_DHCP_server branch from 5839bd7 to e19101a Compare November 4, 2015 15:37
@dLobatog
Copy link
Member

dLobatog commented Nov 5, 2015

[test]

@dLobatog
Copy link
Member

@bagasse One of the tests you modified (get_root) is failing. see Jenkins or test locally with rake test. Rubocop is also throwing a few warnings, please fix them & update the PR.

@bagasse bagasse force-pushed the Fixes_12211_-_Improve_DHCP_subnets_parsing_to_get_more_informations_from_DHCP_server branch from e19101a to 0977f1c Compare November 11, 2015 09:43
@bagasse
Copy link
Member Author

bagasse commented Nov 11, 2015

@dLobatog: I've fixed rubocop warnings, sorry i forgot to run rubocop before on this PR. For the get_root of dhcp_api_test, all test are green in my dev env (CentOS 7, ruby 1.9.3). I seen in jenkins that this test only fail with ruby 1.8.7. I don't know why, with this ruby version, networks presents in the list are not in the same order than in others ruby versions. I will take a look at this more deeply tomorrow.

@dmitri-d
Copy link
Member

[test]

@@ -288,8 +298,7 @@ def read_config file, ignore_includes=false
logger.debug "Reading config file #{file}"
config = []
File.readlines(file).each do |line|

line.strip! # remove left and right whitespace
line = line.split('#').first.strip # remove comments, left and right whitespace
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? The next line used to skip over comments (and isn't needed with this change).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my understanding, the next line remove lines that starts with comment, this remove comments at the end of lines. But if you want i can remove this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once you call String#split all instances of the symbol you used to split the string will be removed, which makes https://github.com/bagasse/smart-proxy/blob/Fixes_12211_-_Improve_DHCP_subnets_parsing_to_get_more_informations_from_DHCP_server/modules/dhcp/providers/server/isc.rb#L302 redundant.

I'm ok with this change if you remove code on line 302.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 302 removed.

@bagasse bagasse force-pushed the Fixes_12211_-_Improve_DHCP_subnets_parsing_to_get_more_informations_from_DHCP_server branch from 0977f1c to c28d12b Compare November 11, 2015 16:25
@bagasse
Copy link
Member Author

bagasse commented Nov 11, 2015

@witlessbird: changed, arrays to sets, thanks for pointing this out to me.

@dLobatog
Copy link
Member

[test]

@@ -82,8 +82,24 @@ def test_poap_quirks
def test_loadSubnets_loads_managed_subnets
subnets = @dhcp.loadSubnets
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that this was here before, but it wasn't a very good test: loadSubnets returns subnets that were processed, not the data that was stored.

Another issue is that this tests too many things; which will make it harder to pinpoint an issue should code change. Could you break this up to test specific cases, for example handling of domain_name_server option when it's not present, when only one server is present, when both are present, etc.

It is probably easier to work directly with subnet_service (see @subnet_service in setup -- you can query it for relevant subnets, as opposed to parsing the hash returned by @dhcp.subnets.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my understanding, loadSubnets implementation of Proxy::DHCP::Server::ISC load subnets only from config files and Proxy::DHCP::SubnetService is only here to store loaded data, so i don't see the problem to use loadSubnets here. For the other point, i've separated tests in several methods.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nods,that's correct. However, currently these tests verify the behaviour of parse_config_for_subnets method: so either calls to loadSubnetsshould be replaced with calls to parse_config_for_subnets, or the assertions should be made against Proxy::DHCP::SubnetService (the main side-effect of loadSubnets is initialization of SubnetService).

@dmitri-d
Copy link
Member

@bagasse: Looks pretty good, only needs some changes in tests around added functionality.

@domcleal
Copy link
Contributor

Thanks for your PRs and work on this feature @bagasse, and welcome to our GitHub organisation. If you can set your membership to "public" on this page then Jenkins will automatically test any updates you make.

@bagasse bagasse force-pushed the Fixes_12211_-_Improve_DHCP_subnets_parsing_to_get_more_informations_from_DHCP_server branch from c28d12b to ec1d84d Compare November 12, 2015 08:31
@domcleal
Copy link
Contributor

[test] one last time :)

@bagasse
Copy link
Member Author

bagasse commented Nov 12, 2015

@domcleal Glad to join and thank you for the invitation :)

@bagasse bagasse force-pushed the Fixes_12211_-_Improve_DHCP_subnets_parsing_to_get_more_informations_from_DHCP_server branch from ec1d84d to d2938d9 Compare November 12, 2015 09:01
ret_val << Proxy::DHCP::Subnet.new(match[0], match[1])
@config.scan(/subnet\s+([\d\.]+)\s+netmask\s+([\d\.]+)\s*{([\w|;|\.|,|\-|\s]*)}/) do |match|
options = {}
if match[2]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind to extract this logic to a method or something similar? Maybe write a comment / save match[0] match[1] match[2] to named variables? It's a bit difficult to understand without being well acquainted with the format of the conf file.

@dLobatog
Copy link
Member

@bagasse I only have 2 comments about the regexes & maintainability, but overall great job 👍. IMO if you could fix that that should be it. In any case @domcleal and @witlessbird feel free to merge if you think it's fine already, you are the ones that work on this code the most.

@bagasse bagasse force-pushed the Fixes_12211_-_Improve_DHCP_subnets_parsing_to_get_more_informations_from_DHCP_server branch from d2938d9 to 424b1b6 Compare November 12, 2015 09:37
@bagasse
Copy link
Member Author

bagasse commented Nov 12, 2015

@dLobatog you are right, it's not really readable, i've extracted range regexp too and used named vars.

@@ -409,4 +421,14 @@ def poap_options_statements(options)
statements
end
end

# Get all IPv4 addresses provided by the ISC DHCP config line following the pattern "my-config-option-or-directive IPv4_ADDR[, IPv4_ADDR] [...];" and return an array.
def get_ip_list_from_config_line(option_line)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for this method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was already there, but i've added a test for get_range_from_config_line that i've extracted too. I changed loadSubnets calls to parse_config_for_subnets as suggested.

@bagasse bagasse force-pushed the Fixes_12211_-_Improve_DHCP_subnets_parsing_to_get_more_informations_from_DHCP_server branch from 424b1b6 to 3761282 Compare November 12, 2015 10:39
@dmitri-d
Copy link
Member

[test]

@dmitri-d
Copy link
Member

Waiting for tests to pass before merging.

@dmitri-d
Copy link
Member

merged in at e2a21ec. Thanks, @bagasse!

@dmitri-d dmitri-d closed this Nov 12, 2015
@bagasse
Copy link
Member Author

bagasse commented Nov 12, 2015

@witlessbird @dLobatog, thank you for pointers and re (re re re)-reviews :)

@bagasse bagasse deleted the Fixes_12211_-_Improve_DHCP_subnets_parsing_to_get_more_informations_from_DHCP_server branch June 1, 2016 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants