Jenkins – Add Color to Console Output

Jenkins console output is place where you can spend decent amount of time trying to figure out what went wrong (or perhaps right?).

AnsiColor plugins gives you opportunity to color monochromatic Jenkins console output.

1. Install AnsiColor plugin
On Jenkins:
Manage Jenkins > Manage Plugins > Available
> search and install ‘Ansi Color’

2. Configure your build/job
Under Build Environment section check Color ANSI Console Output and select xterm

TesterFenster-Jenkins-Build-Environment-Color-ANSI
3. Inside Execute shell step add something like:

set +x
info() {
echo "\033[1;33m[Info]    \033[0m $1"
}

error() {
echo "\033[1;31m[Error]   \033[0m $1"
}

success() {
echo "\033[1;32m[Success] \033[0m $1"
}

info "This is information message"
error "Houston we have a problem"
success "Great!!!"

echo "Foreground colors"
echo "\033[31m Red \033[0m"
echo "\033[32m Green \033[0m"
echo "\033[33m Yellow \033[0m"
echo "\033[34m Blue \033[0m"
echo "\033[35m Magneta \033[0m"
echo "\033[36m Cyan \033[0m"
echo "Background colors"
echo "\033[41m Red \033[0m"
echo "\033[42m Green \033[0m"
echo "\033[43m Yellow \033[0m"
echo "\033[44m Blue \033[0m"
echo "\033[45m Magneta \033[0m"
echo "\033[46m Cyan \033[0m"
echo "Different combinations"
echo "\033[1;31m Red \033[0m"
echo "\033[1;4;37;42m Green \033[0m"
echo "\033[1;43m Yellow \033[0m"
set -x

4. On console output

Screenshot_2017-04-13_11-13-22

Done!!!

How to stop Jenkins CI build by extract a value from a JSON endpoint in bash using Curl

Curl the URL to get the JSON response and pipe it to the python tool. The python code will extract the value mapped to the given key (number in this case).

machine='dbelab04'
machineport=8080
jobname='mkat_fpga_tests_4k'
buildnumber=$(curl --silent -X POST http://$machine:$machineport/job/$jobname/lastBuild/api/json?tree=number | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["'number'"]';)
curl -X POST http://$machine:$machineport/job/$jobname/$number/stop

Note: the above command actually works

How to make Git commit to trigger a job/build in Jenkins CI

In this blog post I will show you how to configure a post commit hook on Git that notifies a Jenkins CI server about repository changes. This means that whenever a new commit is pushed to the Git repository, the cURL command line tool will be launched to initiate a Http request telling Jenkins to trigger a job.

1. Execute Jenkins build with Curl

In this setup, there is no authentication at Jenkins, considering that I will be responsible for the repository and jenkins server.

To test, I had to execute jenkins with curl


(Test)mmphego@dbelab04:~/src/mkat_fpga_tests/.git/hooks (GIT_DIR!)
|_ [2017-04-05 11:58:47] $ >>> curl -D - -X POST http://dbelab04:8080/job/mkat_fpga_tests_4k/build

HTTP/1.1 201 Created
Date: Wed, 05 Apr 2017 09:59:23 GMT
X-Content-Type-Options: nosniff
Location: http://dbelab04:8080/queue/item/387/
Content-Length: 0
Server: Jetty(9.2.z-SNAPSHOT)

2. Configure a hook of repository

Create a file in .git/hooks called post-commit and do not forget to make it executable.

$ nano $(pwd)/.git/hooks/post-commit

#!/bin/bash
# Jenkins-Git build trigger
# Mpho Mphego

machine=$(uname -n)
echo 'Notifying Jenkins Server to execute a Build on' $machine
curl -X POST http://$machine:8080/job/mkat_fpga_tests_4k/build

Screenshot_2017-04-05_12-57-44

Assumptions:
Git version > 2.6
Jenkins version > 2.0
Git Plugin (Jenkins)
Gitlab hook (Jenkins)

3. Configure Jenkins 

Configure your Jenkins job to be able to “Trigger builds remotely”

Screenshot_2017-04-05_13-18-19.png

Enable SCM polling for each project you want to notify:

  1. Go to the Dashboard.
  2. Click on your project.
  3. Click Configure.
  4. Under Build Triggers check the box for Poll SCM.
  5. Repeat for any other projects.

The notification you send tells Jenkins to poll the repository, so projects will only respond if SCM polling is enabled.

 

Now Jenkins will runs automatically when I/we commit and push using Git via CLI.

Done.

Allow users of a certain group to run a command without sudo passwd

Time to time I usually find myself typing sudo to execute commands requiring sudo rights, and this is repetitive which simply means one gets weary of typing sudo password every time, hence this blog post. Reason for this is to remind me how to do it the next time I am faced with such conundrum(s).

Suppose I wanted to add a group of users who are allowed to run mount and umount without passwords. So I first want to add a group called “staff”

sudo groupadd staff

Next we need to edit the /etc/group and add the users

staff:407:

will be present ,hence append users you want to add the users separated by commas.

staff:x:407:user1,user2,...

Now we need to configure sudo to allow members of the “staff” group to actually invoke the mount and umount commands.

You just need to add the following lines to /etc/sudoers or execute sudo visudo

%staff ALL=NOPASSWD: /sbin/mount, /sbin/umount

Now sudo mount won’t ask password but since it is a pain in the butt typing sudo all the time, we can avoid it by doing the following:

I can create the following script called “/usr/bin/mount” (and similar script for umount)

#! /bin/sh
sudo /sbin/mount $*

To make this slightly more secure, We might want to change the ownership of these scripts to the “staff” group.

chgrp staff /usr/bin/mount /usr/bin/umount

and then make them executable only for the group “staff”

chmod g+x /usr/bin/mount  /usr/bin/umount

Note:Depending on the OS you are using please check where mount and umount commands are located. It might be in /bin/ instead of /sbin.
So you might have to make necessary changes

 

Done

Python’s Virtualenv working with Jenkins

If you use virtualenv to isolate your python project’s environment, and want your code tested automatically — read on, else ignore.

virtualenv isolates your project’s python environment

virtualenv makes sure you lock down your project’s main directory and all subdirectories of it. This ‘lockdown’ means that you never touches your global python binary, or any globally installed libraries (like “sudo pip install ipython” ).

Once locked down, you install all packages again, even those you have globally installed. This enables you to have one version of flask globally installed, but another version in your project. All dependencies can be listed in a separate file and validate a precise environment for you to work with. Tightly controlled dependencies is key to a deployment without surprises.

Jenkins checks the health of your project for each change

Jenkins is a CI server which means it does a lot of repeating stuff so you can focus on doing more important stuff. More specifically, it listens for changes to your project’s version control system (like git).

When changes are detected, the project is built and the test suite is executed. If any step fails, the CI server tells you that it did.

Setup Jenkins, and make it use virtualenv

Jenkins needs some massaging before it handles the hijacked environment of virtualenv. This is how I did it for my local git repository:

  • Download and install Jenkins
  • Start it, it should be up on http://localhost:8080
  • Install the Git Plugin
  • Setup a new project with these properties:
    • Source Code Management: add the URI to your local repository,
    • /Users/you/Sites/asdf in my case. Make sure the jenkins user can read this directory, otherwise the Jenkins GUI will tell you something random about invalid git repo, without a hint about a permissions error.
    • Build Triggers: Poll SCM (with an interval like 0 * * * *). This is needed because
      you’re too lazy to build manually; and
      you can not trigger builds with a git post-commit hook otherwise
    • Build > Execute shell. I’ve used two steps, one for setting up the environment and one for the actual tests:

# Setup a proper path, I call my virtualenv dir "venv" and
# I've got the virtualenv command installed in /usr/local/bin
PATH=$WORKSPACE/venv/bin:/usr/local/bin:$PATH
if [ ! -d "venv" ]; then
        virtualenv venv
fi
. venv/bin/activate
pip install -r requirements.txt --download-cache=/tmp/$JOB_NAME

and


. venv/bin/activate
which python

# reply should be
/home/user/.virtualenv/bin/python

Smart Doorbell using Raspberry Pi

About a year ago, I had to do a project titled Smart Home Automation using a Raspberry Pi and Arduino, part of the project  incorporated a doorbell but not just any doorbell but a Smart doorbell.

Why do I call it a Smart Doorbell?

A classic doorbell can be defined as a signalling device typically placed near an entry door to a building. When a visitor presses a button the bell rings inside the building, alerting the occupant to the presence of the visitor.

The smart doorbell, works in that manner however when a visitor presses the doorbell button it notifies the occupant with an images of the visitor and also sends an email, sms and push notification (to an smart device) to the occupant in case they are not able to hear the bell ringing.

If the button is pressed a pre-recorded voice notification is played for the occupant to check the door as there might be someone, this happens concurrently with an sms notification while a picture of the visitor is captured and sent to both email and push notification. Initially there was a feature to enable 2-way real-time video communication but due to network latency and high resource usage the feature was deprecated.

Click For A Demo

Comment if you need the code

See code snippets(Initial Revision): https://mmphego.wordpress.com/2015/01/11/smart-doorbell-using-rpi-with-voice-and-email-notification/

Getting started with Nodemcu in Arduino IDE

On this blog I will introduce and demonstrate how to use NodeMCU in Arduino IDE

What is NodeMCU or ESP8266 ?

The ESP8266 is a low-cost Wi-Fi chip with full TCP/IP stack and MCU (Micro Controller Unit) capability produced by Espressif Systems.

Whereas NodeMCU is an open source firmware and development kit that helps us to create IoT platform. It includes firmware which runs on the ESP8266 WiFi SoC from Espressif Systems and hardware which is based on the ESP-12 module. NodeMCU can be directly programmed using Arduino Software.

300px-nodemcu_devkit_1-0

P.S: With NodeMCU in hand, you do not need to have Arduino Boards. You can simply program NodeMCU board using Arduino Software using USB cable. However, Small changes in Arduino Software needs to be done so that NodeMCU Module can be programmed using Arduino Software. NodeMCU is very cheap and can be bought in less than $5. Only.  This is a very good solution for IoT application in a very cheap price. Buy it here NodeMCU WiFi Development Board.

This assumes you have the latest Arduino IDE.

1. Adding NodeMCU on board management.

Open Arduino IDE, go to Files > Preferences 

Copy URL below and paste it on Additional Boards Manager URLs as per image below.

http://arduino.esp8266.com/stable/package_esp8266com_index.json

screenshot_2017-03-02_14-06-56

More Info can be found on https://github.com/esp8266/Arduino

2. Installing NodeMCU board

This assumes you are connected to the Internet, else how did you even get to this blog post.

Goto Tools > Board > Boards Manager, This will download additional board information from the url you added in step 1.

board-manager

Navigate and select esp8266, and install.

board-manager-open

Close and re-open the Arduino IDE after installation is successful.

Connect NodeMCU and select Tools > port  and select correct port.

3. LED Blinking Program on NodeMCU Board

After following steps given above, now your NodeMCU board can be used like an Arduino board and you can program it using Arduino Software.

This is a very basic program for blinking of an LED.

You can copy and paste program from here.

Compile it and Upload it on NodeMCU Board and observe the output on pin  D1.

// Program starts here
#define LED_PIN D1

void setup() {
// put your setup code here, to run once:
pinMode( LED_PIN, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED_PIN, HIGH); // Turn on the LED.
delay(1000); // Wait 1 second
digitalWrite(LED_PIN, LOW); // Turn off the LED.
delay(1000);
}
// Program ends here

I will compile a video soon and upload.