Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 2a4f4616432f7ba3261d0592347301ce8e6d9a15 0 parents
Miguel Jacq authored
1  .forward
@@ -0,0 +1 @@
+"| /usr/local/bin/jenkinsmail.py"
68 Dummy_Deploy_Job.config.xml
@@ -0,0 +1,68 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<project>
+ <actions/>
+ <description></description>
+ <keepDependencies>false</keepDependencies>
+ <properties/>
+ <scm class="hudson.plugins.git.GitSCM">
+ <configVersion>1</configVersion>
+ <remoteRepositories>
+ <org.spearce.jgit.transport.RemoteConfig>
+ <string>origin</string>
+ <int>5</int>
+ <string>fetch</string>
+ <string>+refs/heads/*:refs/remotes/origin/*</string>
+ <string>receivepack</string>
+ <string>git-upload-pack</string>
+ <string>uploadpack</string>
+ <string>git-upload-pack</string>
+ <string>url</string>
+ <string>git@dummyrepo.com:dummy</string>
+ <string>tagopt</string>
+ <string></string>
+ </org.spearce.jgit.transport.RemoteConfig>
+ </remoteRepositories>
+ <branches>
+ <hudson.plugins.git.BranchSpec>
+ <name>dummybranch</name>
+ </hudson.plugins.git.BranchSpec>
+ </branches>
+ <mergeOptions/>
+ <recursiveSubmodules>false</recursiveSubmodules>
+ <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
+ <authorOrCommitter>false</authorOrCommitter>
+ <clean>false</clean>
+ <wipeOutWorkspace>false</wipeOutWorkspace>
+ <pruneBranches>false</pruneBranches>
+ <buildChooser class="hudson.plugins.git.util.DefaultBuildChooser"/>
+ <gitTool>Default</gitTool>
+ <submoduleCfg class="list"/>
+ <relativeTargetDir></relativeTargetDir>
+ <excludedRegions></excludedRegions>
+ <excludedUsers></excludedUsers>
+ <skipTag>false</skipTag>
+ </scm>
+ <canRoam>true</canRoam>
+ <disabled>true</disabled>
+ <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
+ <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
+ <triggers class="vector">
+ <hudson.triggers.SCMTrigger>
+ <spec>* * * * *</spec>
+ </hudson.triggers.SCMTrigger>
+ </triggers>
+ <concurrentBuild>false</concurrentBuild>
+ <builders>
+ <hudson.tasks.Shell>
+ <command>/usr/local/bin/deployment.sh dummyaegirserver ${JOB_NAME} dummyprofile dummywebserver dummydbserver dummymakefile ${BUILD_NUMBER}</command>
+ </hudson.tasks.Shell>
+ </builders>
+ <publishers>
+ <hudson.tasks.Mailer>
+ <recipients>you@example.com</recipients>
+ <dontNotifyEveryUnstableBuild>false</dontNotifyEveryUnstableBuild>
+ <sendToIndividuals>false</sendToIndividuals>
+ </hudson.tasks.Mailer>
+ </publishers>
+ <buildWrappers/>
+</project>
46 deployment.sh
@@ -0,0 +1,46 @@
+#!/bin/bash
+#
+# Wrapper script for our fabfile, to be called from Jenkins
+#
+
+# Where our fabfile is
+FABFILE=/usr/local/bin/fabfile.py
+
+HOST=$1
+SITE=$2
+PROFILE=$3
+WEBSERVER=$4
+DBSERVER=$5
+MAKEFILE=$6
+BUILD_NUMBER=$7
+DATE=`date +%Y%m%d%H%M%S`
+
+if [[ -z $HOST ]] || [[ -z $SITE ]] || [[ -z $PROFILE ]] || [[ -z $WEBSERVER ]] || [[ -z $DBSERVER ]] || [[ -z $MAKEFILE ]] || [[ -z $BUILD_NUMBER ]]
+then
+ echo "Missing args! Exiting"
+ exit 1
+fi
+
+
+# Array of tasks - these are actually functions in the fabfile, as an array here for the sake of abstraction
+if [ $BUILD_NUMBER -eq "1" ]; then
+ # This is a first-time ever build. Let's install a site instead of migrate it
+ TASKS=(
+ build_platform
+ save_alias
+ install_site
+ )
+else
+ TASKS=(
+ build_platform
+ migrate_site
+ save_alias
+ import_site
+ )
+fi
+
+# Loop over each 'task' and call it as a function via the fabfile,
+# with some extra arguments which are sent to this shell script by Jenkins
+for task in ${TASKS[@]}; do
+ fab -f $FABFILE -H $HOST $task:site=$SITE,profile=$PROFILE,webserver=$WEBSERVER,dbserver=$DBSERVER,makefile=$MAKEFILE,build=$DATE || exit 1
+done
42 fabfile.py
@@ -0,0 +1,42 @@
+from fabric.api import *
+import time
+
+env.user = 'aegir'
+
+# Download and import a platform using Drush Make
+def build_platform(site, profile, webserver, dbserver, makefile, build):
+ print "===> Building the platform..."
+ run("drush make %s /var/aegir/platforms/%s" % (makefile, build), pty=True )
+ run("drush --root='/var/aegir/platforms/%s' provision-save '@platform_%s' --context_type='platform'" % (build, build), pty=True)
+ run("drush @hostmaster hosting-import '@platform_%s'" % build, pty=True)
+ run("drush @hostmaster hosting-dispatch", pty=True)
+
+# Install a site on a platform, and kick off an import of the site
+def install_site(site, profile, webserver, dbserver, makefile, build):
+ print "===> Installing the site for the first time..."
+ run("drush @%s provision-install" % site, pty=True)
+ run("drush @hostmaster hosting-task @platform_%s verify" % build, pty=True)
+ time.sleep(5)
+ run("drush @hostmaster hosting-dispatch", pty=True)
+ time.sleep(5)
+ run("drush @hostmaster hosting-task @%s verify" % site, pty=True)
+
+# Migrate a site to a new platform
+def migrate_site(site, profile, webserver, dbserver, makefile, build):
+ print "===> Migrating the site to the new platform"
+ run("drush @%s provision-migrate '@platform_%s'" % (site, build), pty=True)
+
+# Save the Drush alias to reflect the new platform
+def save_alias(site, profile, webserver, dbserver, makefile, build):
+ print "===> Updating the Drush alias for this site"
+ run("drush provision-save @%s --context_type=site --uri=%s --platform=@platform_%s --server=@server_%s --db_server=@server_%s --profile=%s --client_name=admin" % (site, site, build, webserver, dbserver, profile), pty=True)
+
+# Import a site into the frontend, so that Aegir learns the site is now on the new platform
+def import_site(site, profile, webserver, dbserver, makefile, build):
+ print "===> Refreshing the frontend to reflect the site under the new platform"
+ run("drush @hostmaster hosting-import @%s" % site, pty=True)
+ run("drush @hostmaster hosting-task @platform_%s verify" % build, pty=True)
+ # Seem to need to run hosting-import twice, otherwise the site's profile is 'n/a'
+ run("drush @hostmaster hosting-import @%s" % site, pty=True)
+ run("drush @hostmaster hosting-task @%s verify" % site, pty=True)
+
46 jenkinsmail.py
@@ -0,0 +1,46 @@
+#! /usr/bin/env python
+
+import sys
+import string
+import os
+from fabric.api import *
+
+# Add a new site deployment job to Jenkins
+def jenkins_project_deployment(filename):
+ email = open(filename, 'r')
+ args = [ ]
+ for line in email:
+ for key in [ "site", "install_profile", "aegirserver", "webserver", "dbserver", "makefile", "repo", "branch" ]:
+ if key in line:
+ splitter = string.split(line, "=")
+ splitter = string.split(splitter[1], ": ")
+ splitter = string.split(splitter[1], "\n")
+ args.append(splitter[0])
+
+ arguments = string.join(args, ' ')
+
+ # Kick off the new deploy job in Jenkins
+ os.system('/usr/local/bin/newjenkinsjob %s' % arguments)
+
+# Let's test to see if the request has come in via email or from CLI
+def process_request():
+ if not sys.stdin.isatty():
+
+ rawmessage = sys.stdin.read()
+
+ filename = '/tmp/rawmessage.%s.txt' % os.getpid()
+ logfile = open(filename, 'w')
+ logfile.write(rawmessage)
+ logfile.close()
+
+ logfile = open(filename, 'r')
+ for line in logfile:
+ if 'Subject' in line:
+ _split_arg = string.split(line, ":")
+ subject = _split_arg[1]
+ if "New Aegir Site deployment" in subject:
+ jenkins_project_deployment(filename)
+ logfile.close()
+
+if __name__ == "__main__":
+ process_request()
88 newjenkinsjob
@@ -0,0 +1,88 @@
+#!/bin/bash
+
+# Script to copy a dummy Jenkins job and build a new one.
+# Takes a set of arguments to build the new job with relevant parameters
+
+TO_PROJECT=$1
+PROFILE=$2
+AEGIRSERVER=$3
+WEBSERVER=$4
+DBSERVER=$5
+MAKEFILE=$6
+REPO=$7
+BRANCH=$8
+
+
+if [[ -z $TO_PROJECT ]] || [[ -z $PROFILE ]] || [[ -z $AEGIRSERVER ]] || [[ -z $WEBSERVER ]] || [[ -z $WEBSERVER ]] || [[ -z $DBSERVER ]] || [[ -z $MAKEFILE ]] || [[ -z $REPO ]] || [[ -z $BRANCH ]]
+then
+ echo "Missing args!"
+ exit 1
+fi
+
+JENKINS_URL="http://example.com:8080"
+USERNAME="jenkinsbot"
+PASSWORD="changeme"
+
+TMP_DIR="/tmp/jenkinsJobs"
+CURL="curl --user $USERNAME:$PASSWORD -s "
+
+FROM_PROJECT="Dummy_Deploy_Job"
+
+FROM_CONFIG_URL="$JENKINS_URL/job/$FROM_PROJECT/config.xml"
+TO_CONFIG_URL="$JENKINS_URL/job/$TO_PROJECT/config.xml"
+
+FROM_CONFIG_XML="$TMP_DIR/$FROM_PROJECT-config.xml"
+TO_CONFIG_XML="$TMP_DIR/$TO_PROJECT-config.xml"
+
+# Make our temp dir
+mkdir -p $TMP_DIR
+
+# check for existing job
+`$CURL -f -o /dev/null $TO_CONFIG_URL`
+if [ $? -eq 0 ]; then
+ echo "WARN: $TO_PROJECT already exists on the jenkins server. No action taken."
+ exit 1
+fi
+
+# download the config.xml for the job, continue if it doesn't exist
+`$CURL -o $FROM_CONFIG_XML $FROM_CONFIG_URL`
+grep "Error 404" "$FROM_CONFIG_XML" > /dev/null
+if [ $? -eq 0 ]; then
+ echo "FAIL: $FROM_CONFIG_URL is not a valid project url"
+ exit 1
+fi
+
+# replace some details
+# Aegir server
+sed "s/dummyaegirserver/$AEGIRSERVER/g" "$FROM_CONFIG_XML" > "$TO_CONFIG_XML"
+
+# Profile
+sed -i "s/dummyprofile/$PROFILE/g" $TO_CONFIG_XML
+
+# Web server
+sed -i "s/dummywebserver/$WEBSERVER/g" "$TO_CONFIG_XML"
+
+# DB server
+sed -i "s/dummydbserver/$DBSERVER/g" "$TO_CONFIG_XML"
+
+# Makefile
+sed -i "s/dummymakefile/$(echo $MAKEFILE | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/&/\\\&/g')/g" "$TO_CONFIG_XML"
+
+# Repo name
+sed -i "s/git\@dummyrepo\.com\:dummy/$(echo $REPO | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/&/\\\&/g')/g" "$TO_CONFIG_XML"
+
+# Branch name
+sed -i "s/dummybranch/$BRANCH/g" "$TO_CONFIG_XML"
+
+# create a new job based on the existing job
+`$CURL -o /dev/null --data " " "$JENKINS_URL/createItem?name=${TO_PROJECT}&mode=copy&from=${FROM_PROJECT}"`
+
+# upload new job data
+`$CURL -o /dev/null --data "@$TO_CONFIG_XML" "$JENKINS_URL/job/$TO_PROJECT/config.xml"`
+
+# enable the job, there seems to be a bug where we need to first disable and then enable
+`$CURL -o /dev/null --data disable "$JENKINS_URL/job/$TO_PROJECT/disable"`
+`$CURL -o /dev/null --data enable "$JENKINS_URL/job/$TO_PROJECT/enable"`
+
+# Remove our temp dir
+rm -rf $TMP_DIR
Please sign in to comment.
Something went wrong with that request. Please try again.