Browse Source

[scripts] upload-env

Gabriel Corona 8 years ago
parent
commit
3447cfbba9
2 changed files with 71 additions and 5 deletions
  1. 19 5
      README.md
  2. 52 0
      scripts/upload-env

+ 19 - 5
README.md

@@ -50,11 +50,6 @@ Install the modules:
 
     r10k puppetfile install Puppetfile
 
-Commit and push:
-
-    git commit -am"[vpn] Do something on the VPN"
-    git push origin HEAD
-
 Update the puppet from the repository and apply:
 
     # Update the puppet master from the git repo:
@@ -62,3 +57,22 @@ Update the puppet from the repository and apply:
     
     # Apply the rules from the puppet master on some server:
     ssh foo.newldn sudo puppet agent -t
+
+## Development
+
+Don't forget to install the modules:
+
+    r10k puppetfile install Puppetfile
+
+Upload the current directory as as `test` environment on the Puppet master:
+
+    ./scripts/upload-env test
+
+Apply the `test` environment on some server:
+
+    ssh foo.newldn sudo puppet agent -t --environment=test
+
+Commit and push our modifications:
+
+    git commit -am"[vpn] Do something on the VPN"
+    git push origin HEAD

+ 52 - 0
scripts/upload-env

@@ -0,0 +1,52 @@
+#!/bin/sh
+
+set -e
+
+# ##### Basic functions
+
+log() {
+  echo "$@" >&2
+}
+
+die() {
+  echo "$1" >&2
+  exit "$2"
+}
+
+# Check that a value matched a given regex.
+# Usage: validate '^[a-zA-Z][-a-zA-Z0-9_]$' foo
+validate_grep() {
+    local value
+    local pattern
+    pattern="$1"
+    value="$2"
+    printf '%s\n' "$value" | wc -l | grep ^1$ > /dev/null || return 1
+    printf '%s\n' "$value" | grep -- "$pattern" > /dev/null || return 1
+    return 0
+}
+
+# ##### Argument processing
+
+if [ "$1" = "--help" ]; then
+  cat <<EOF
+Upload the working directory as an environment in the puppet server
+Usage: ./script upload-env my_environment
+EOF
+  exit 0
+fi
+
+env="$1"
+validate_grep '^[a-zA-Z][-a-zA-Z0-9_]*$' "$env" || die "Invalid environment name" 1
+test "$env" != "production" || die "Please, do not use with the production environment." 1
+
+# ##### Upload the stuff on the server
+
+sources="Gemfile Gemfile.lock modules environment.conf manifests"
+path=/srv/puppet/$env/
+rsync_opt="-rl --perms --chmod=u+rwX,go+rX --delete --exclude .git*"
+puppet_host=zoidberg.newldn
+
+log "Create env on puppet server ($path)"
+ssh "$puppet_host" sudo mkdir -p "$path"
+ssh $puppet_host 'user=$(whoami) && group=$(id -gn) && sudo chown $user:$group' $path
+rsync $rsync_opt $sources "$puppet_host:${path}$dest"