Last active
August 29, 2015 14:23
-
-
Save manuwell/ecaef60510867969e9d4 to your computer and use it in GitHub Desktop.
[Git-Hook] - Missing Deis Env Vars
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
#!/usr/bin/env ruby | |
# An example hook script to verify what is about to be pushed. Called by "git | |
# push" after it has checked the remote status, but before anything has been | |
# pushed. If this script exits with a non-zero status nothing will be pushed. | |
# This script shows a disclaimer with unsetted env vars on your deis cluster, | |
# only when you do a "git push deis master" | |
# | |
# It tries to read a ".env.sample" file to diff with your current set of env | |
# var on your cluster | |
class DotEnvParser | |
def extract(raw_input) | |
env_vars = {} | |
raw_input.each_line do |line| | |
key, value = line.split("=") | |
env_vars[key.gsub(/export/, "").strip] = value | |
end | |
env_vars | |
end | |
end | |
class DeisEnvParser | |
ENV_VAR_EXTRACT_RE = /^([A-Z_]+?)\s*([^\s]+?)$/ | |
def extract(raw_input) | |
env_vars = {} | |
each_var_from(raw_input) do |key, value| | |
env_vars[key] = value | |
end | |
env_vars | |
end | |
protected | |
def each_var_from(raw_input) | |
raw_input.scan(ENV_VAR_EXTRACT_RE) do |content_vars| | |
yield(content_vars[0], content_vars[1]) | |
end | |
end | |
end | |
class EnvDiff | |
attr_reader :source, :template | |
def initialize(source, template) | |
@source = source | |
@template = template | |
end | |
def show_disclaimer | |
unsetted_vars_disclaimer = unsetted_vars.map do |env_var| | |
"#{env_var} #{"\t"*5}# example #{template[env_var]}" | |
end.join("") | |
puts(<<-TEXT) | |
******************************** | |
Hi! You have this vars unsetted on your deis cluster" | |
#{unsetted_vars_disclaimer} | |
Let's set them now? | |
******************************** | |
TEXT | |
end | |
def has_unsetted_vars? | |
!unsetted_vars.empty? | |
end | |
protected | |
def unsetted_vars | |
template.keys - source.keys | |
end | |
end | |
########### MAIN | |
$remote_name = ARGV[0] # from git input | |
$remote_url = ARGV[1] # from git input | |
CONFIG_FILE_PATH = ".env.sample" | |
exit unless File.exists?(CONFIG_FILE_PATH) | |
exit unless $remote_name == "deis" | |
deis_env_raw = %x(deis config) | |
deis_vars = DeisEnvParser.new.extract(deis_env_raw) | |
dot_env_raw = File.read(CONFIG_FILE_PATH) | |
vars_template = DotEnvParser.new.extract(dot_env_raw) | |
diff = EnvDiff.new(deis_vars, vars_template) | |
if diff.has_unsetted_vars? | |
diff.show_disclaimer | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment