Skip to content

Instantly share code, notes, and snippets.

@orangewolf
Last active September 1, 2020 17:51
Show Gist options
  • Save orangewolf/cd4c84307a801b6eac3dd2b360f164f9 to your computer and use it in GitHub Desktop.
Save orangewolf/cd4c84307a801b6eac3dd2b360f164f9 to your computer and use it in GitHub Desktop.
Solr in Helm
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd $DIR/../../chart
echo $DIR
REPO=$(basename $(git config --get remote.origin.url))
NAMESPACE=${REPO%.git}
if [ -z "$1" ]
then
echo './chart/bin/decrypt ENVIRONMENT'
exit 1
fi
keybase decrypt -i $1-values.yaml.enc -o $1-values.yaml
#!/usr/bin/env ruby
require 'yaml'
require 'rainbow'
def blank?(string)
# rubocop:disable Rails/Blank
!string || string.empty?
# rubocop:enable Rails/Blank
end
def environment_name
@environment_name ||= ARGV[0]&.downcase
end
def tag_name
@tag_name ||= ARGV[1]
end
def config_filename
@config_filename = if blank?(ARGV[2])
config_dir = File.join(__dir__, '..')
File.join(config_dir, "#{environment_name}-values.yaml")
else
ARGV[2]
end
end
def namespace
config = YAML.load_file(config_filename)
@namespace = config['env']['configmap']['NAMESPACE']
end
def check_config
if blank?(environment_name) || blank?(tag_name) || ['-h', '--help'].include?(environment_name)
puts './chart/bin/deploy ENVIRONMENT TAG (CONFIG_FILE)'
exit 1
end
if !File.exists?(config_filename)
puts Rainbow('Missing config file: ').red + config_filename
end
if blank?(namespace)
puts Rainbow('Missing NAMESPACE key in config: ').red + 'Configure under env.configmap.NAMESPACE'
end
end
Dir.chdir(File.join(__dir__, '..', '..', 'chart')) do
check_config
command = "helm upgrade --install --namespace #{namespace} #{environment_name} . -f #{config_filename} --set rails.image.tag=#{tag_name}"
puts Rainbow('Deploying ').blue + Rainbow(tag_name).cyan + Rainbow(' to ').blue + Rainbow(environment_name).cyan + Rainbow(' ...').blue
puts Rainbow('Executing: ').blue + command
case system(command)
when nil
puts Rainbow('Error: ').red + 'Command not found. Is helm installed?'
when false
puts Rainbow('Error: ').red + "Command exited with status code: #{$?.exitstatus}"
else
puts Rainbow('Deployment complete').green
end
end
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd $DIR/../../chart
REPO=$(basename $(git config --get remote.origin.url))
NAMESPACE=${REPO%.git}
if [ -z "$1" ] || [ -z "$2" ]
then
echo './chart/bin/encrypt ENVIRONMENT TEAM'
exit 1
fi
keybase encrypt -i $1-values.yaml -o $1-values.yaml.enc --team $2
#!/usr/bin/env ruby
require 'yaml'
require 'rainbow'
def blank?(string)
!string || string.length == 0
end
Dir.chdir(File.join(__dir__, '..', '..', 'chart')) do
environment_name = ARGV[0]&.downcase
if blank?(environment_name) || ['-h', '--help'].include?(environment_name)
puts './chart/bin/rollback ENVIRONMENT (CONFIG_FILE)'
exit 1
end
if blank?(ARGV[1])
config_dir = File.join(__dir__, '..')
config_filename = File.join(config_dir, "#{environment_name}-values.yaml")
else
config_filename = ARGV[1]
end
if !File.exists?(config_filename)
puts Rainbow('Missing config file: ').red + config_filename
end
config = YAML::load_file(config_filename)
namespace = config['env']['configmap']['NAMESPACE']
if blank?(namespace)
puts Rainbow('Missing NAMESPACE key in config: ').red + 'Configure under env.configmap.NAMESPACE'
end
command = "helm rollback --namespace #{namespace} #{environment_name}"
puts Rainbow('Rolling Back ').blue + Rainbow(tag_name).cyan + Rainbow(' to ').blue + Rainbow(environment_name).cyan + Rainbow(' ...').blue
puts Rainbow('Executing: ').blue + command
case system(command)
when nil
puts Rainbow('Error: ').red + 'Command not found. Is helm installed?'
when false
puts Rainbow('Error: ').red + "Command exited with status code: #{$?.exitstatus}"
else
puts Rainbow('Rollback complete').green
end
end
# !/usr/bin/env ruby
# frozen_string_literal: true
puts 'Installing ZK and Zookeper gems'
system('CFLAGS=-Wno-error=format-overflow gem install zookeeper --version 1.4.11')
system('CFLAGS=-Wno-error=format-overflow gem install zk')
# Create a new Solr collection
class CreateSolrCollection
require 'faraday'
require 'json'
def initialize(name)
@name = name
end
def create_solr_collection
return if collection_exists?
response = Faraday.get collections_url, collection_options
return response.status
end
private
def collections_url
"#{ENV.fetch('SOLR_BASE_URL')}/admin/collections"
end
def collection_options
{
'collection.configName' => @name,
'numShards' => 1,
action: 'CREATE',
name: @name
}
end
def collection_exists?
response = Faraday.get collections_url, action: 'LIST'
collections = JSON.parse(response.body)['collections']
collections.include? @name
end
end
# Upload solr configuration from the local filesystem into the zookeeper configs path for solr
class SolrConfigUploader
attr_reader :collection_path
require 'zk'
##
# Build a new SolrConfigUploader using the application-wide settings
def self.default
new(ENV.fetch('SOLR_CONFIGSET'))
end
def initialize(collection_path)
@collection_path = collection_path
end
def upload(upload_directory)
with_client do |zk|
salient_files(upload_directory).each do |file|
zk.create(zookeeper_path_for_file(file), file.read, or: :set)
end
end
end
def get(path)
with_client do |zk|
zk.get(zookeeper_path(path)).first
end
end
private
def zookeeper_path_for_file(file)
zookeeper_path(File.basename(file.path))
end
def zookeeper_path(*path)
"/#{([collection_path] + path).compact.join('/')}"
end
def salient_files(config_dir)
return to_enum(:salient_files, config_dir) unless block_given?
Dir.new(config_dir).each do |file_name|
full_path = File.expand_path(file_name, config_dir)
next unless File.file? full_path
yield File.new(full_path)
end
end
def with_client(&block)
ensure_chroot!
::ZK.open(connection_str, &block)
end
def connection_str
"#{ENV.fetch('ZOOKEEPER_ENDPOINT')}/configs"
end
def ensure_chroot!
raise ArgumentError, 'Zookeeper connection string must include a chroot path' unless connection_str =~ %r{/[^/]}
end
end
puts 'Loading the Solr Config to Zookeeper'
SolrConfigUploader.default.upload(ENV.fetch('SOLR_CONFIGSET_SOURCE_PATH'))
puts 'Creating the Solr Collection'
puts CreateSolrCollection.new(ENV.fetch('SOLR_CONFIGSET')).create_solr_collection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment