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 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 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"
}
113 changes: 113 additions & 0 deletions tasks/code_sync_status.rb
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")
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