Skip to content

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 47 commits into from
Aug 22, 2021
Merged
Show file tree
Hide file tree
Changes from 19 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 Aug 10, 2021
3a6129e
(SOLARCH-558) still work in progress needing to get ruby params and s…
davidsandilands Aug 11, 2021
ce7757e
(SOLARCH-558) testing as a task before refactoring
davidsandilands Aug 12, 2021
60fe75e
(SOLARCH-558) missing end
davidsandilands Aug 12, 2021
12af77e
(SOLARCH-558) missed out stdin on json
davidsandilands Aug 12, 2021
58cb4f6
(SOLARCH-558) corrected parameter names
davidsandilands Aug 12, 2021
24357f1
(SOLARCH-558) correcting variable declaration
davidsandilands Aug 13, 2021
1ceb66e
(SOLARCH-558) changing to logging and returning json output
davidsandilands Aug 13, 2021
bfec889
(SOLARCH-558) trying this
davidsandilands Aug 13, 2021
9821930
(SOLARCH-558) debugging
davidsandilands Aug 13, 2021
4ac4e6b
(SOLARCH-558) missing an equals
davidsandilands Aug 13, 2021
6db14f3
(SOLARCH-558) mistake in variable
davidsandilands Aug 13, 2021
a66d01f
(SOLARCH-558) removed debugging messages
davidsandilands Aug 13, 2021
6aaf7c3
(SOLARCH-558) adjusted json output
davidsandilands Aug 13, 2021
df248cf
(SOLARCH-558) correcting default
davidsandilands Aug 13, 2021
38eb413
(SOLARCH-558) changing from logger to raise
davidsandilands Aug 13, 2021
7ee85fa
(SOLARCH-558) adding error handling for when server doesnt have envir…
davidsandilands Aug 13, 2021
f4638bb
(SOLARCH-558) removing more trouble than its worth
davidsandilands Aug 13, 2021
e180955
(SOLARCH-558) correcting linting errors
davidsandilands Aug 13, 2021
3d43590
(SOLARCH-558) refactored on advice from Reid as per code review
davidsandilands Aug 20, 2021
5b4e33f
(SOLARCH-558) correcting pdk issues and a missing end
davidsandilands Aug 20, 2021
531e538
(SOLARCH-558) Class had a capital and fixing some other linting issues
davidsandilands Aug 20, 2021
6a509c9
(SOLARCH-558) require puppet missing
davidsandilands Aug 20, 2021
cebf697
(SOLARCH-558) missing a /
davidsandilands Aug 20, 2021
1b0e008
(SOLARCH-558) was json parsing twice creating an error and changing v…
davidsandilands Aug 20, 2021
02e1386
(SOLARCH-558) correcting some further linting errors
davidsandilands Aug 20, 2021
39d32a1
(SOLARCH-558) wasn't calling the global params
davidsandilands Aug 20, 2021
a2a8bd7
(SOLARCH-538) old params refrences from before refactor left in
davidsandilands Aug 20, 2021
24b453c
(SOLARCH-558) missed inialising results hash
davidsandilands Aug 20, 2021
b7355a4
(SOLARCH-558) variable with wrong name
davidsandilands Aug 20, 2021
b98edb2
(SOLARCH-538) removing left in body method from refactor
davidsandilands Aug 20, 2021
7850575
(SOLARCH-558) missing quotes on hash key
davidsandilands Aug 20, 2021
cb11238
(SOLARCH-558) wrong variable on hash
davidsandilands Aug 20, 2021
2855a08
(SOLARCH-558) commenting out temporarily
davidsandilands Aug 20, 2021
d06e3ea
(SOLARCH-538) missing return on checkenvironmentcode
davidsandilands Aug 20, 2021
ec8c344
(SOLARCH-558) adding sync status check back in
davidsandilands Aug 20, 2021
f734050
(SOLARCH-558) making sync output consistent
davidsandilands Aug 20, 2021
bfea621
(SOLARCH-558) adding to check each environment
davidsandilands Aug 20, 2021
5a5b314
(SOLARCH-558) adding extra structure to make matchin easier
davidsandilands Aug 20, 2021
0289343
(SLOARCH-558) renaming https to https_client to avoid any method conf…
davidsandilands Aug 20, 2021
659d3cb
(SOLARCH-558) changing https to client to avoid confusion
davidsandilands Aug 20, 2021
d1c098c
(SOLARCH-558) changing naming to better pracitce underscore
davidsandilands Aug 20, 2021
43deea0
(SOLARCH-558) making rest of methods in class private for better prot…
davidsandilands Aug 20, 2021
894dedf
(SOLARCH-558) fixed trailing white space
davidsandilands Aug 20, 2021
5702d6d
(SOLARCH-558) Update to split last section rather than count chars
davidsandilands Aug 22, 2021
c8ebc2f
(SOLARCH-558) updated to use a split instead of a char count for comm…
davidsandilands Aug 22, 2021
d329877
(SOLARCH-558) fixing indentation
davidsandilands Aug 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions tasks/code_sync_status.json
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"
}
77 changes: 77 additions & 0 deletions tasks/code_sync_status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/opt/puppetlabs/puppet/bin/ruby

# Puppet Task Name: code_sync_status
require 'net/https'
require 'uri'
require 'json'

# Parameters expected:
# Hash
# Array requestedenvironments
params = JSON.parse(STDIN.read)

# Only debug level includes code sync details
uri = URI.parse('https://localhost:8140/status/v1/services?level=debug')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.request(Net::HTTP::Get.new(uri.request_uri))
# Fail with return code
raise "API failure https://localhost:8140/status/v1/services returns #{response.code}" unless response.is_a? Net::HTTPSuccess
# Get list of servers from filesync service
servers = JSON.parse(response.body)['file-sync-storage-service']['status']['clients'].keys
# Get list of environments from filesync service
environments = JSON.parse(response.body)['file-sync-storage-service']['status']['repos']['puppet-code']['submodules'].keys
environmentstocheck = []

# If all was passed as an argument we check all visible environments
if params['environments'].any? { |s| s.casecmp('all') == 0 }
environmentstocheck = environments
# Else check each requested environment to confirm its a visible environment
else
params['environments'].each do |environment|
environments.any? { |s| s.casecmp(environment.to_s) == 0 } || raise("Environment #{environment} is not visible and will not be checked")
environmentstocheck << environment
end
end
results = {}
# Run status of the script assume its good until it we hit a failure
scriptstatus = true
environmentstocheck.each do |environment|
results[environment] = {}
# The status of this environment assume its good until we hit a failure
environmentmatch = true
# Find the commit ID of the environment accroding to the file sync service
primarycommit = JSON.parse(response.body)['file-sync-storage-service']['status']['repos']['puppet-code']['submodules'][environment.to_s]['latest_commit']['message'][32..71]
results[environment]['latest_commit'] = primarycommit
servers.each do |server|
results[environment][server] = {}
# Find the commit ID of the server we are checking for this environment
servercommit = JSON.parse(response.body)['file-sync-storage-service']['status']['clients'][server.to_s]['repos']['puppet-code']['submodules'][environment.to_s]['latest_commit']['message'][32..71]
results [environment][server]['commit'] = servercommit
# Check if it matches and if not mark the environment and script as having a server not in sync on an environment
if servercommit == primarycommit
results [environment][server]['sync'] = true
else
results [environment][server]['sync'] = false
environmentmatch = false
scriptstatus = false
end
end
# write to the result json if its a match for the environment
results [environment]['in_sync'] = if environmentmatch
true
else
false
end
end
# Write to the result json if for all environments checked if its a match
if scriptstatus
results['in_sync'] = true
puts results.to_json
exit 0
else
results['in_sync'] = false
puts results.to_json
exit 1
end