Skip to content

Instantly share code, notes, and snippets.

View trickyearlobe's full-sized avatar

Richard Nixon trickyearlobe

View GitHub Profile
@trickyearlobe
trickyearlobe / cloud-init-aws.yaml
Created September 13, 2021 12:27
Cloud Init for AWS
#cloud-init
chef:
install_type: "omnibus"
omnibus_url: "https://omnitruck.chef.io/install.sh" (or https://omnitruck.chef.io/install.ps1 for Win)
force_install: false
server_url: "https://manage.chef.io/organizations/thenixons"
node_name: "testnode001"
validation_name: "thenixons-validator"
validation_key: |
@trickyearlobe
trickyearlobe / analyse_inspec_report.rb
Created April 13, 2021 09:05
Analyse size of Inspec JSON report
#!/usr/bin/env ruby
require 'json'
require 'objspace'
puts "Checking #{ARGV[0]}"
all_controls = {}
report=JSON.parse(File.read(ARGV[0]))
report['profiles'].each do |profile|
profile['controls'].map { |control|
curl -T myBackup.tgz https://chef-success-customer-write-only.s3.amazonaws.com/<mybucket>/mybackup.tgz
@trickyearlobe
trickyearlobe / count_chef_guids.rb
Created April 20, 2020 14:06
Script to count duplicate chef client GUIDs
# Execute with `knife exec count_chef_guids.rb`
guids = {}
total_nodes=0
nodes.all.each do |mynode|
total_nodes=total_nodes+1
guids[mynode['chef_guid']] = (guids[mynode['chef_guid']] ||0) +1 if mynode['chef_guid']
end
require 'aws-sdk-secretsmanager'
require 'base64'
def get_aws_secret(secret_name,region_name)
client = Aws::SecretsManager::Client.new(region: region_name)
begin
get_secret_value_response = client.get_secret_value(secret_id: secret_name)
rescue Aws::SecretsManager::Errors::DecryptionFailure => e
raise
#!/bin/env ruby
# Load the native Ruby HTTP libraries
require 'net/http'
require 'json'
# Get an API key to access our vault(s)
# Note that the machine must have been granted access to the vault using managed identity
apikey_uri = URI('http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://vault.azure.net')
req = Net::HTTP::Get.new(apikey_uri)
@trickyearlobe
trickyearlobe / chef_ha_backend_resync.md
Last active May 8, 2019 14:45
Forced resync on Chef HA Backend
# If you already tried removing and re-adding a failed backend
# As per https://getchef.zendesk.com/hc/en-us/articles/360003035092-Chef-Backend-Cluster-2-0-1-Full-follower-recovery

# and it's failing with missing WAL problems like this
# FATAL:  could not receive data from WAL stream: ERROR:  requested WAL segment 0000003E000008B3000000CC has already been removed

# TRY this on the failed backend (after re-adding it)

#!/bin/env bash
# This script converts user public keys stored in certs on older
# chef servers into pure public keys so that Vault works properly
# NOTE: Back up Postgres before running this as it doesnt error check
function sql_exec {
/opt/opscode/embedded/bin/psql -d opscode_chef -qtA -c "$1"
}
@trickyearlobe
trickyearlobe / client.rb.monkeypatch.gem.rb
Last active March 14, 2018 16:03
Monkey patch Chef Cookbook Metadata Gem Installer in client.rb
class MonkeyPatcher < Chef::Handler
puts "MONKEY PATCH GemInstaller"
def report
Chef::Cookbook::GemInstaller.send(:define_method,'install') do
cookbook_gems = Hash.new { |h, k| h[k] = [] }
cookbook_collection.each do |cookbook_name, cookbook_version|
cookbook_version.metadata.gems.each do |args|
cookbook_gems[args.first] += args[1..-1]
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string"
},
"virtualMachineName": {
"type": "string"
},