-
Notifications
You must be signed in to change notification settings - Fork 55
Add task to report on code synchronization status #196
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
Merged
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
b9f449d
(SOLARCH-558) checking in start of work
davidsandilands 3a6129e
(SOLARCH-558) still work in progress needing to get ruby params and s…
davidsandilands ce7757e
(SOLARCH-558) testing as a task before refactoring
davidsandilands 60fe75e
(SOLARCH-558) missing end
davidsandilands 12af77e
(SOLARCH-558) missed out stdin on json
davidsandilands 58cb4f6
(SOLARCH-558) corrected parameter names
davidsandilands 24357f1
(SOLARCH-558) correcting variable declaration
davidsandilands 1ceb66e
(SOLARCH-558) changing to logging and returning json output
davidsandilands bfec889
(SOLARCH-558) trying this
davidsandilands 9821930
(SOLARCH-558) debugging
davidsandilands 4ac4e6b
(SOLARCH-558) missing an equals
davidsandilands 6db14f3
(SOLARCH-558) mistake in variable
davidsandilands a66d01f
(SOLARCH-558) removed debugging messages
davidsandilands 6aaf7c3
(SOLARCH-558) adjusted json output
davidsandilands df248cf
(SOLARCH-558) correcting default
davidsandilands 38eb413
(SOLARCH-558) changing from logger to raise
davidsandilands 7ee85fa
(SOLARCH-558) adding error handling for when server doesnt have envir…
davidsandilands f4638bb
(SOLARCH-558) removing more trouble than its worth
davidsandilands e180955
(SOLARCH-558) correcting linting errors
davidsandilands 3d43590
(SOLARCH-558) refactored on advice from Reid as per code review
davidsandilands 5b4e33f
(SOLARCH-558) correcting pdk issues and a missing end
davidsandilands 531e538
(SOLARCH-558) Class had a capital and fixing some other linting issues
davidsandilands 6a509c9
(SOLARCH-558) require puppet missing
davidsandilands cebf697
(SOLARCH-558) missing a /
davidsandilands 1b0e008
(SOLARCH-558) was json parsing twice creating an error and changing v…
davidsandilands 02e1386
(SOLARCH-558) correcting some further linting errors
davidsandilands 39d32a1
(SOLARCH-558) wasn't calling the global params
davidsandilands a2a8bd7
(SOLARCH-538) old params refrences from before refactor left in
davidsandilands 24b453c
(SOLARCH-558) missed inialising results hash
davidsandilands b7355a4
(SOLARCH-558) variable with wrong name
davidsandilands b98edb2
(SOLARCH-538) removing left in body method from refactor
davidsandilands 7850575
(SOLARCH-558) missing quotes on hash key
davidsandilands cb11238
(SOLARCH-558) wrong variable on hash
davidsandilands 2855a08
(SOLARCH-558) commenting out temporarily
davidsandilands d06e3ea
(SOLARCH-538) missing return on checkenvironmentcode
davidsandilands ec8c344
(SOLARCH-558) adding sync status check back in
davidsandilands f734050
(SOLARCH-558) making sync output consistent
davidsandilands bfea621
(SOLARCH-558) adding to check each environment
davidsandilands 5a5b314
(SOLARCH-558) adding extra structure to make matchin easier
davidsandilands 0289343
(SLOARCH-558) renaming https to https_client to avoid any method conf…
davidsandilands 659d3cb
(SOLARCH-558) changing https to client to avoid confusion
davidsandilands d1c098c
(SOLARCH-558) changing naming to better pracitce underscore
davidsandilands 43deea0
(SOLARCH-558) making rest of methods in class private for better prot…
davidsandilands 894dedf
(SOLARCH-558) fixed trailing white space
davidsandilands 5702d6d
(SOLARCH-558) Update to split last section rather than count chars
davidsandilands c8ebc2f
(SOLARCH-558) updated to use a split instead of a char count for comm…
davidsandilands d329877
(SOLARCH-558) fixing indentation
davidsandilands File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"puppet_task_version": 1, | ||
"supports_noop": false, | ||
"description": "A task to confirm code is in sync accross the cluster for clusters with code manager configured", | ||
"parameters": { | ||
"environments": { | ||
"type": "Array", | ||
"description": "A list of environments to check, pass a single value of all for all", | ||
"default": ["all"] | ||
} | ||
}, | ||
"input_method": "stdin" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#!/opt/puppetlabs/puppet/bin/ruby | ||
|
||
# Puppet Task Name: code_sync_status | ||
require 'net/https' | ||
require 'uri' | ||
require 'json' | ||
require 'puppet' | ||
|
||
# CodeSyncStatus task class | ||
class CodeSyncStatus | ||
def initialize(params) | ||
@params = params | ||
end | ||
|
||
def execute! | ||
puts sync_status.to_json | ||
end | ||
|
||
private | ||
|
||
def https_client | ||
client = Net::HTTP.new('localhost', '8140') | ||
client.use_ssl = true | ||
client.cert = @cert ||= OpenSSL::X509::Certificate.new(File.read(Puppet.settings[:hostcert])) | ||
client.key = @key ||= OpenSSL::PKey::RSA.new(File.read(Puppet.settings[:hostprivkey])) | ||
client.verify_mode = OpenSSL::SSL::VERIFY_NONE | ||
client | ||
end | ||
|
||
def api_status | ||
status = https_client | ||
# Only debug level includes code sync details | ||
status_request = Net::HTTP::Get.new('/status/v1/services?level=debug') | ||
JSON.parse(status.request(status_request).body) | ||
end | ||
|
||
def check_environment_list(environments, request_environments) | ||
environmentstocheck = [] | ||
# If all was passed as an argument we check all visible environments | ||
if request_environments.any? { |s| s.casecmp('all') == 0 } | ||
environmentstocheck = environments | ||
# Else check each requested environment to confirm its a visible environment | ||
else | ||
request_environments.each do |environment| | ||
environments.any? { |s| s.casecmp(environment.to_s) == 0 } || raise("Environment #{environment} is not visible and will not be checked") | ||
davidsandilands marked this conversation as resolved.
Show resolved
Hide resolved
|
||
environmentstocheck << environment | ||
end | ||
end | ||
environmentstocheck | ||
end | ||
|
||
def check_environment_code(environment, servers, status_call) | ||
# Find the commit ID of the environment according to the file sync service note expected message is of the format | ||
# code-manager deploy signature: '93027145096d9f1e0b716b20b8129618d0a2c7e2' | ||
primarycommit = status_call.dig('file-sync-storage-service', | ||
'status', | ||
'repos', | ||
'puppet-code', | ||
'submodules', | ||
environment.to_s, | ||
'latest_commit', | ||
'message').split("'").last | ||
results = {} | ||
results['latest_commit'] = primarycommit | ||
results['servers'] = {} | ||
servers.each do |server| | ||
results['servers'][server] = {} | ||
# Find the commit ID of the server we are checking for this environment note expected message is of the format | ||
# code-manager deploy signature: '93027145096d9f1e0b716b20b8129618d0a2c7e2' | ||
servercommit = status_call.dig('file-sync-storage-service', | ||
'status', | ||
'clients', | ||
server.to_s, | ||
'repos', | ||
'puppet-code', | ||
'submodules', | ||
environment.to_s, | ||
'latest_commit', | ||
'message').split("'").last | ||
results['servers'][server]['commit'] = servercommit | ||
# Check if it matches and if not mark the environment not in sync on an environment | ||
results['servers'][server]['sync'] = servercommit == primarycommit | ||
end | ||
results['sync'] = results['servers'].all? { |_k, v| v['sync'] == true } | ||
results | ||
end | ||
|
||
def sync_status | ||
status_call = api_status | ||
# Get list of servers from filesync service | ||
servers = status_call['file-sync-storage-service']['status']['clients'].keys | ||
# Get list of environments from filesync service | ||
environments = status_call['file-sync-storage-service']['status']['repos']['puppet-code']['submodules'].keys | ||
# Process this list of environments and validate against visible environments | ||
environmentstocheck = check_environment_list(environments, @params['environments']) | ||
results = {} | ||
# For each environment get the syncronisation information of the servers | ||
environmentstocheck.each do |environment| | ||
results[environment] = check_environment_code(environment, servers, status_call) | ||
end | ||
# Confirm are all environments being checked in sync | ||
results['sync'] = results.all? { |_k, v| v['sync'] == true } | ||
results | ||
end | ||
end | ||
# Run the task unless an environment flag has been set, signaling not to. The | ||
# environment flag is used to disable auto-execution and enable Ruby unit | ||
# testing of this task. | ||
unless ENV['RSPEC_UNIT_TEST_MODE'] | ||
Puppet.initialize_settings | ||
task = CodeSyncStatus.new(JSON.parse(STDIN.read)) | ||
task.execute! | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.