Created
September 19, 2014 01:09
-
-
Save philchristensen/98b9fc6bd3a8e48eba79 to your computer and use it in GitHub Desktop.
Script to checkout/refresh per-environment puppet modules
This file contains 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 python | |
import sys | |
import os | |
import os.path | |
import logging | |
import subprocess | |
ENV_DIR = "environments" | |
MODULES = ['core', 'client'] | |
logging.basicConfig(format="%(message)s", level='INFO') | |
log = logging.getLogger('puppet-refresh') | |
def needs_pull(path, branch='master'): | |
output = subprocess.check_output(['git', '--git-dir=.git', 'ls-remote', '--heads', 'origin', branch]).strip() | |
latest_commit, branchinfo = output.split() | |
current_commit = subprocess.check_output(['git', '--git-dir=.git', 'ls-remote', '--heads', 'origin', branch]).strip() | |
return current_commit != latest_commit | |
def clone(url, path): | |
try: | |
output = subprocess.check_output(['git', 'clone', url, path]) | |
log.debug(output) | |
except subprocess.CalledProcessError, e: | |
log.error("Error in git clone: %s" % e.output) | |
sys.exit(1) | |
def checkout(path, branch): | |
try: | |
output = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], cwd=path).strip() | |
if(output != branch): | |
output = subprocess.check_output(['git', 'checkout', branch], cwd=path) | |
log.debug(output) | |
except subprocess.CalledProcessError, e: | |
log.error("Error in git checkout: %s" % e.output) | |
sys.exit(1) | |
def pull(path): | |
try: | |
output = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], cwd=path).strip() | |
if(needs_pull(output)): | |
output = subprocess.check_output(['git', 'pull'], cwd=path) | |
log.debug(output) | |
except subprocess.CalledProcessError, e: | |
log.error("Error in git pull: %s" % e.output) | |
sys.exit(1) | |
def fetch(path): | |
try: | |
output = subprocess.check_output(['git', 'fetch'], cwd=path) | |
log.debug(output) | |
except subprocess.CalledProcessError, e: | |
log.error("Error in git fetch: %s" % e.output) | |
sys.exit(1) | |
def main(): | |
selected_branch, selected_module = None, None | |
if(len(sys.argv) == 2): | |
selected_branch = sys.argv[1] | |
elif(len(sys.argv) == 3): | |
selected_branch, selected_module = sys.argv[1:] | |
for environment in os.listdir(ENV_DIR): | |
if(environment == 'common'): | |
continue | |
elif(not os.path.isdir(os.path.join(ENV_DIR, environment))): | |
continue | |
elif(selected_branch and selected_branch != environment): | |
continue | |
log.info("Updating module %s for %s..." % (selected_module, environment)) | |
for module in MODULES: | |
if(selected_module and selected_module != module): | |
continue | |
module_path = os.path.join(ENV_DIR, environment, module) | |
if(os.path.exists(module_path)): | |
log.info("Refreshing %s" % module) | |
fetch(module_path) | |
checkout(module_path, environment) | |
pull(module_path) | |
else: | |
log.info("Cloning %s" % module) | |
clone("git@repository-prodtools.fu-prod.net:infrastructure/%s-puppet.git" % module, module_path) | |
checkout(module_path, environment) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment