Skip to content

Instantly share code, notes, and snippets.

@cvuorinen
Last active February 3, 2018 20:40
Show Gist options
  • Save cvuorinen/4a5b18fea1f4a65cea95 to your computer and use it in GitHub Desktop.
Save cvuorinen/4a5b18fea1f4a65cea95 to your computer and use it in GitHub Desktop.
Deploy Button on Olimex A20-OLinuXino-LIME with Jenkins build status and capistrano deploy. More information here: http://cvuorinen.net/2015/10/building-a-deploy-button/
#!/usr/bin/env python
import os
import time
from pyA20Lime.gpio import gpio
from pyA20Lime.gpio import port
from autojenkins import Jenkins
### configration parameters
JENKINS_URL = 'http://your.jenkins.host'
JENKINS_USER = 'username'
JENKINS_PASSWORD = 'password-or-apikey'
JENKINS_JOB = 'jenkins-job-name'
CHECK_INTERVAL = 120
PROJECT_PATH = '/path/to/project'
DEPLOY_COMMAND = 'git pull && cap deploy:migrations'
RED_LED = port.PG9
GREEN_LED = port.PC7
BUTTON = port.PE11
### end configuration
def main():
init()
build = Build(
Jenkins(JENKINS_URL, auth=(JENKINS_USER, JENKINS_PASSWORD))
)
blinkOn = False
while True:
if buttonPressed() and build.success():
deploy()
build.check()
blinkOn = setLeds(build, blinkOn)
time.sleep(1)
def setLeds(build, blinkOn):
if build.success():
onLed = GREEN_LED
offLed = RED_LED
else:
onLed = RED_LED
offLed = GREEN_LED
ledOff(offLed)
# blink the LED when build in progress
if build.building() and blinkOn:
ledOff(onLed)
else:
ledOn(onLed)
return not blinkOn
def deploy():
# button must be pressed for two seconds to start deploy
log("deploy.sleep")
time.sleep(2)
if not buttonPressed():
log("deploy.return")
return
log("deploy.start")
ledOn(RED_LED)
ledOn(GREEN_LED)
os.system(DEPLOY_COMMAND)
ledOff(RED_LED)
ledOff(GREEN_LED)
def init():
log('init')
gpio.init()
gpio.setcfg(RED_LED, gpio.OUTPUT)
gpio.pullup(RED_LED, gpio.PULLUP)
gpio.setcfg(GREEN_LED, gpio.OUTPUT)
gpio.pullup(GREEN_LED, gpio.PULLUP)
gpio.setcfg(BUTTON, gpio.INPUT)
gpio.pullup(BUTTON, gpio.PULLUP)
# set LEDs on for one second to signal everything is OK
ledOn(RED_LED)
ledOn(GREEN_LED)
time.sleep(1)
ledOff(RED_LED)
ledOff(GREEN_LED)
os.chdir(PROJECT_PATH)
def buttonPressed():
return not gpio.input(BUTTON) == 1
def ledOn(ledPort):
gpio.output(ledPort, gpio.HIGH)
def ledOff(ledPort):
gpio.output(ledPort, gpio.LOW)
def log(message):
print time.strftime('[%Y-%m-%d %H:%M:%S] ') + message
class Build(object):
def __init__(self, jenkins):
self._jenkins = jenkins
self._lastCheck = 0
self._lastResult = {'result': '', 'building': False}
def check(self):
if self._shouldCheck():
self._checkBuild()
def success(self):
return self._lastResult['result'] == 'SUCCESS'
def building(self):
return self._lastResult['building']
def _shouldCheck(self):
return (time.time() - self._lastCheck) > CHECK_INTERVAL
def _checkBuild(self):
log("Build._checkBuild")
self._lastCheck = time.time()
try:
self._lastResult = self._jenkins.last_result(JENKINS_JOB)
except Exception:
self._lastResult = {'result': 0, 'building': True}
log("Build._lastResult: " + str(self._lastResult))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment