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

Rewrite updated_ood_portal to be Ruby #108

Merged
merged 24 commits into from
Nov 12, 2019
Merged

Rewrite updated_ood_portal to be Ruby #108

merged 24 commits into from
Nov 12, 2019

Conversation

treydock
Copy link
Contributor

@treydock treydock commented Nov 7, 2019

Should resolve #100.

This changes checksum to be based on non-comments and also replacing simple cmp with diff that ignores comments.

Just FYI the way OSC deploys ood-portal.conf will NOT ignore comments because we generate /etc/ood/config/ood-portal.conf not using update_ood_portal and then we use Puppet to put the file into correct location and no way for it to ignore comments.

One flaw with this approach is changes to the comments won't even get written to as .new file. If I keep cmp instead of using diff then get something like this when comments change:

/opt/ood/ood-portal-generator/sbin/update_ood_portal 
Generating Apache config using YAML config: '/etc/ood/config/ood_portal.yml'
WARNING: Checksum of /opt/rh/httpd24/root/etc/httpd/conf.d/ood-portal.conf does not match previous value, not replacing.
Generating new Apache config at: '/opt/rh/httpd24/root/etc/httpd/conf.d/ood-portal.conf.new'

[root@53cfead15bf4 ondemand]# diff -u /opt/rh/httpd24/root/etc/httpd/conf.d/ood-portal.conf /opt/rh/httpd24/root/etc/httpd/conf.d/ood-portal.conf.new
--- /opt/rh/httpd24/root/etc/httpd/conf.d/ood-portal.conf	2019-11-07 17:41:58.487211000 +0000
+++ /opt/rh/httpd24/root/etc/httpd/conf.d/ood-portal.conf.new	2019-11-07 17:42:59.970782000 +0000
@@ -31,6 +31,12 @@
 #      sudo service httpd24-httpd condrestart
 #      sudo service httpd24-htcacheclean condrestart
 #
+#      # For CentOS 7
+#      sudo systemctl try-restart httpd24-httpd.service httpd24-htcacheclean.service
+#
+#      # For CentOS 8
+#      sudo systemctl try-restart httpd.service htcacheclean.service
+#

@ericfranz
Copy link
Contributor

How does this achieve the goal?

The checksum is used to verify that the administrator has not hand-modified the ood-portal.conf Apache config. If the administrator never hand-modifies the config, the checksum will ensure that REPLACE=true every time this script is run, regardless of whether or not the checksum is of the ood-portal.conf or of ood-portal.conf without comments.

Note: because the checksum file is being made of ood-portal.conf without comments, but then is being used to check a file ood-portal.conf that contains comments, won't this new checksum always fail? So REPLACE=false will now always be the case.

Regardless, since we are now sticking with if ! cmp -s "${NEW_APACHE}" "${APACHE}" and the comparison is not excluding differences in comments, if somehow REPLACE=true the Apache config would get replaced and Apache would be restarted.

@ericfranz
Copy link
Contributor

Also, https://github.com/OSC/ondemand/blob/master/ood-portal-generator/ is getting more complex, and I wonder if it makes more sense to move some of this logic into the Ruby code itself. Or at least, write some minitest tests that exercise this script with different inputs and outputs to verify the desired functionality. Happy to help with test scaffolding.

…ly support replacing update_ood_portal with Application module
@treydock
Copy link
Contributor Author

treydock commented Nov 8, 2019

@ericfranz I agree, this is getting complex enough this should be in Ruby. I am working on moving update_ood_portal to Ruby code and will use RSpec because I'm more familiar with RSpec than other testing tools for Ruby.

@treydock
Copy link
Contributor Author

treydock commented Nov 8, 2019

Completely refactored update_ood_portal to be Ruby.

Example output:

[root@58a65dd0c11b /]# /opt/ood/ood-portal-generator/sbin/update_ood_portal --detailed-exitcode 
Generating Apache config using YAML config: '/etc/ood/config/ood_portal.yml'
WARNING: Checksum of /opt/rh/httpd24/root/etc/httpd/conf.d/ood-portal.conf does not match previous value, not replacing.
Generating new Apache config at: '/opt/rh/httpd24/root/etc/httpd/conf.d/ood-portal.conf.new'
[root@58a65dd0c11b /]# echo $?
4
[root@58a65dd0c11b /]# /opt/ood/ood-portal-generator/sbin/update_ood_portal --detailed-exitcode -f
Generating Apache config using YAML config: '/etc/ood/config/ood_portal.yml'
Backing up previous Apache config to: '/opt/rh/httpd24/root/etc/httpd/conf.d/ood-portal.conf.20191108T174136'
Generating new Apache config at: '/opt/rh/httpd24/root/etc/httpd/conf.d/ood-portal.conf'
Generating Apache config checksum file: '/etc/ood/config/ood_portal.sha256sum'
Completed successfully!

Restart the httpd24-httpd service now.

Suggested command:
    sudo systemctl try-restart httpd24-httpd.service httpd24-htcacheclean.service

[root@58a65dd0c11b /]# echo $?
3

@treydock treydock requested a review from johrstrom November 8, 2019 18:00
@treydock treydock changed the title Improved checksum logic to ignore comments Rewrite updated_ood_portal to be Ruby Nov 8, 2019
@@ -12,5 +12,10 @@ class << self
def root
Pathname.new(__dir__).dirname
end

def scl_apache
Copy link
Contributor

Choose a reason for hiding this comment

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

def scl_apache?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed made.

end
end

def sum
Copy link
Contributor

Choose a reason for hiding this comment

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

def sum_path for clarity?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change made.

Copy link
Contributor

@ericfranz ericfranz left a comment

Choose a reason for hiding this comment

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

Added comments but it was all minor code quality suggestions or test improvements. So if you are happy and/or don't have time to make those changes I say just merge and we can make notes to improve later.

@treydock
Copy link
Contributor Author

treydock commented Nov 8, 2019

The changes are simple except the one for tests. I'll make them Tuesday at latest. The unit test either has to mock because can't assume write access to like /opt and /etc or has to be run as system tests in a container. For unit I have to at least mock file paths with Tempfile to avoid writing to inaccessible places but system tests could better stress this inside docker where we have root.

@ericfranz
Copy link
Contributor

I didn't communicate my idea for the tests well. What I was thinking was a set of tests that exercised the outside boundary of the script, without mocking any parts inside of the script. If all of the system paths can be modified using env vars (CONFIG, SUM, APACHE) the set of tests could use https://github.com/thoughtbot/climate_control and follow the pattern:

it 'does not replace if no changes detected' do
  # 1. copy fixtures to tmp so that sum, apache, and config are in a tmpdir structure
  # 2. use climate control to set CONFIG, SUM, APACHE in block
  # 3. execute OodPortalGenerator::Application.start("update_ood_portal") with the CLI arguments
  # 4. check exit status
end

@ericfranz
Copy link
Contributor

Following up on the idea I posed with using climate control, you would also add argv as an argument to start i.e. start(mode, argv=ARGV) and then in the test you could do

OodPortalGenerator::Application.start("update_ood_portal", ["--detailed-exitcodes"])

@treydock treydock requested a review from ericfranz November 11, 2019 19:19
@treydock
Copy link
Contributor Author

@ericfranz Look now. I added spec/update_ood_portal_spec.rb that only changes environment variables to point at fixtures using Tempfile paths. Avoiding the function that calls exit! is ideal for other spec tests because if something goes wrong and exit! is called due to an exception caught in start then the rest of the tests are skipped. The only place this can happen now is in spec/update_ood_portal_spec.rb.

@treydock
Copy link
Contributor Author

Added Rake tasks to aid in running tests from root of this project without ugly "cd" type Travis scripts

@treydock
Copy link
Contributor Author

Rewrote travis.yml to use stages so that unit tests happen before system tests and only running tests on master, tags and pull requests to avoid double testing during pull requests when the branch is on this repo.

@treydock treydock force-pushed the portal.conf-diff branch 2 times, most recently from 38f061c to 2e36e49 Compare November 12, 2019 15:54
@treydock
Copy link
Contributor Author

Looks like getting this in Travis sh: 1: source: not found. I will investigate.

@treydock
Copy link
Contributor Author

Looks like source was not present in Travis because default shell is not /bin/bash. To avoid this issue when people run ood-portal-generator I moved the environment parsing of /etc/os-release to use dotenv.

@ericfranz
Copy link
Contributor

@treydock I'm happy with the changes. Ready to merge or did you want to do anything else on this first?

@treydock
Copy link
Contributor Author

@ericfranz This PR is ready to merge.

@ericfranz ericfranz merged commit 115450c into master Nov 12, 2019
@ericfranz ericfranz deleted the portal.conf-diff branch November 12, 2019 19:35
@johrstrom johrstrom added this to the OOD1.7 milestone Mar 6, 2020
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.

Ignore comments for checksum for Apache config?
3 participants