$30 off During Our Annual Pro Sale. View Details »

PHP and IoT: Creating a Deployment Device

Ivan
May 28, 2017

PHP and IoT: Creating a Deployment Device

Setup Jenkins on a raspberry pi, connect it to a GitHub project and trigger the build remotely via gesture sensor on NodeMCU.

Ivan

May 28, 2017
Tweet

Other Decks in Technology

Transcript

  1. IoT Jenkins Deployment Device
    Petar Slović && Ivan Ćirić
    Quantox Technology

    View Slide

  2. View Slide

  3. View Slide

  4. View Slide

  5. View Slide

  6. View Slide

  7. View Slide

  8. View Slide

  9. Continuous Integration
    Continuous Integration (CI) is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect
    problems early. By integrating regularly, you can detect errors quickly, and locate them more easily. Because you’re integrating so frequently, there is significantly less back-tracking to discover where things went wrong, so you
    can spend more time building features. Continuous Integration is cheap. Not integrating continuously is expensive. If you don’t follow a continuous approach, you’ll have longer periods between integrations. This makes it
    exponentially more difficult to find and fix problems. Such integration problems can easily knock a project off-schedule, or cause it to fail altogether. Continuous Integration brings multiple benefits to your organization: Say
    goodbye to long and tense integrations, Increase visibility enabling greater communication, Catch issues early and nip them in the bud, Spend less time debugging and more time adding features, Build a solid foundation, Stop
    waiting to find out if your code’s going to work, Reduce integration problems allowing you to deliver software more rapidly. The main aim of CI is to prevent integration problems, referred to as "integration hell" in early descriptions
    of XP. CI is not universally accepted as an improvement over frequent integration, so it is important to distinguish between the two as there is disagreement about the virtues of each.[citation needed] In XP, CI was intended to be used
    in combination with automated unit tests written through the practices of test-driven development. Initially this was conceived of as running all unit tests in the developer's local environment and verifying they all passed before
    committing to the mainline. This helps avoid one developer's work-in-progress breaking another developer's copy. If necessary, partially complete features can be disabled before commit, such as by using feature toggles. Later
    elaborations of the concept introduced build servers, which automatically ran the unit tests periodically or even after every commit and reported the results to the developers. The use of build servers (not necessarily running unit
    tests) had already been practiced by some teams outside the XP community. Nowadays, many organizations have adopted CI without adopting all of XP. In addition to automated unit tests, organizations using CI typically use a
    build server to implement continuous processes of applying quality control in general — small pieces of effort, applied frequently. In addition to running the unit and integration tests, such processes run additional static and
    dynamic tests, measure and profile performance, extract and format documentation from the source code and facilitate manual QA processes. This continuous application of quality control aims to improve the quality of software,
    and to reduce the time taken to deliver it, by replacing the traditional practice of applying quality control after completing all development. This is very similar to the original idea of integrating more frequently to make integration
    easier, only applied to QA processes. In the same vein, the practice of continuous delivery further extends CI by making sure the software checked in on the mainline is always in a state that can be deployed to users and makes
    the actual deployment process very rapid. When embarking on a change, a developer takes a copy of the current code base on which to work. As other developers submit changed code to the source code repository, this copy
    gradually ceases to reflect the repository code. Not only can the existing code base change, but new code can be added as well as new libraries, and other resources that create dependencies, and potential conflicts. The longer
    a branch of code remains checked out, the greater the risk of multiple integration conflicts and failures when the developer branch is reintegrated into the main line. When developers submit code to the repository they must first
    update their code to reflect the changes in the repository since they took their copy. The more changes the repository contains, the more work developers must do before submitting their own changes. Eventually, the repository
    may become so different from the developers' baselines that they enter what is sometimes referred to as "merge hell", or "integration hell",[3] where the time it takes to integrate exceeds the time it took to make their original
    changes. When embarking on a change, a developer takes a copy of the current code base on which to work. As other developers submit changed code to the source code repository, this copy gradually ceases to reflect the
    repository code. Not only can the existing code base change, but new code can be added as well as new libraries, and other resources that create dependencies, and potential conflicts. The longer a branch of code remains
    checked out, the greater the risk of multiple integration conflicts and failures when the developer branch is reintegrated into the main line. When developers submit code to the repository they must first update their code to reflect
    the changes in the repository since they took their copy. The more changes the repository contains, the more work developers must do before submitting their own changes. Eventually, the repository may become so different
    from the developers' baselines that they enter what is sometimes referred to as "merge hell", or "integration hell",[3] where the time it takes to integrate exceeds the time it took to make their original changes. Continuous
    integration involves integrating early and often, so as to avoid the pitfalls of "integration hell". The practice aims to reduce rework and thus reduce cost and time.[4] A complementary practice to CI is that before submitting work,
    each programmer must do a complete build and run (and pass) all unit tests. Integration tests are usually run automatically on a CI server when it detects a new commit. In 1994, Grady Booch used the phrase continuous
    integration in Object-Oriented Analysis and Design with Applications (2nd edition)[5] to explain how, when developing using micro processes, "internal releases represent a sort of continuous integration of the system, and exist to
    force closure of the micro process." In 1997, Kent Beck and Ron Jeffries invented Extreme Programming (XP) while on the Chrysler Comprehensive Compensation System project, including continuous integration.[6] Beck
    published about continuous integration in 1998, emphasising the importance of face-to-face communication over technological support.[7] In 1999, Beck elaborated more in his first full book on Extreme Programming.[8]
    CruiseControl was released in 2001. When embarking on a change, a developer takes a copy of the current code base on which to work. As other developers submit changed code to the source code repository, this copy
    gradually ceases to reflect the repository code. Not only can the existing code base change, but new code can be added as well as new libraries, and other resources that create dependencies, and potential conflicts. The longer
    a branch of code remains checked out, the greater the risk of multiple integration conflicts and failures when the developer branch is reintegrated into the main line. When developers submit code to the repository they must first
    update their code to reflect the changes in the repository since they took their copy. The more changes the repository contains, the more work developers must do before submitting their own changes. Eventually, the repository
    may become so different from the developers' baselines that they enter what is sometimes referred to as "merge hell", or "integration hell",[3] where the time it takes to integrate exceeds the time it took to make their original
    changes. Continuous integration involves integrating early and often, so as to avoid the pitfalls of "integration hell". The practice aims to reduce rework and thus reduce cost and time.[4] A complementary practice to CI is that
    before submitting work, each programmer must do a complete build and run (and pass) all unit tests. Integration tests are usually run automatically on a CI server when it detects a new commit. In 1994, Grady Booch used the
    phrase continuous integration in Object-Oriented Analysis and Design with Applications (2nd edition)[5] to explain how, when developing using micro processes, "internal releases represent a sort of continuous integration of the
    system, and exist to force closure of the micro process." In 1997, Kent Beck and Ron Jeffries invented Extreme Programming (XP) while on the Chrysler Comprehensive Compensation System project, including continuous
    integration.[6] Beck published about continuous integration in 1998, emphasising the importance of face-to-face communication over technological support.[7] In 1999, Beck elaborated more in his first full book on Extreme
    Programming.[8] CruiseControl was released in 2001.

    View Slide

  10. View Slide

  11. Continuous Integration
    ● Push code often
    ● Build/test
    ● Repeat

    View Slide

  12. Continuous Delivery
    Software is always in a deliverable
    state, ready to be deployed on
    demand.
    Continuous Deployment
    Every change that passes automated testing
    is automatically deployed to specific
    servers.

    View Slide

  13. Tools

    View Slide

  14. ● Has a bunch of plugins
    ● All the cool kids are using it

    View Slide

  15. View Slide

  16. View Slide

  17. View Slide

  18. Here we go!

    View Slide

  19. Requirements
    A Laptop
    GIT installed on your machine
    GitHub account
    Composer
    Open the readme.md github.com/phpsrb/phpiot

    View Slide

  20. Connect to this WiFi in order to connect to devices
    SSID: PHPSrb2017booth
    Password: php_serbia_conference_2017

    View Slide

  21. - Create a GitHub repo
    - Create a Laravel application
    - Connect with Raspberry Pi
    - Install Jenkins on Raspberry Pi
    - Setup a Jenkins job
    - Setup Jenkins GitHub credentials
    - Test our deployment tool
    Steps

    View Slide

  22. > Create a GitHub repo
    1. Create an account on GitHub
    2. Create a repo called ‘phpiot’

    View Slide

  23. > Create a Laravel application
    $ composer create-project --prefer-dist laravel/laravel
    phpsrb
    $ cd phpsrb

    View Slide

  24. phpsrb$ git init
    phpsrb$ git add .
    phpsrb$ git remote add origin https://github.com/{username}/phpiot.git
    phpsrb$ git commit -m ‘Initial commit’
    phpsrb$ git push -u origin master

    View Slide

  25. Create a GitHub repo
    > Connect with Raspberry Pi
    Install Jenkins on Raspberry Pi
    Setup a Jenkins job
    Setup Jenkins GitHub credentials
    Test our deployment tool

    View Slide

  26. How do you connect with
    your Raspberry Pi?
    ● Go camping together
    ● Watch a movie
    ● Go out and get drunk
    ● ...

    View Slide

  27. Connect the RPi
    USB
    microUSB

    View Slide

  28. Wait a bit until Raspberry Pi
    boots...

    View Slide

  29. View Slide

  30. $ ssh [email protected]
    Password: jenkinsX

    View Slide

  31. Create a GitHub repo
    Connect with Raspberry Pi
    > Install Jenkins on Raspberry Pi
    Setup a Jenkins job
    Setup Jenkins GitHub credentials
    Test our deployment tool

    View Slide

  32. $ sudo update-alternatives --config java (8)
    $ mkdir jenkins
    $ cd jenkins
    $ wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key
    add -
    $ sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ >
    /etc/apt/sources.list.d/jenkins.list'
    $ sudo apt-get update
    $ sudo apt-get install jenkins
    $ sudo /etc/init.d/jenkins start

    View Slide

  33. http://jenkinsX.local:8080

    View Slide

  34. View Slide

  35. Install suggested plugins...

    View Slide

  36. Create a new Jenkins User
    Username: admin
    Password: jenkins

    View Slide

  37. Create a GitHub repo
    Connect with Raspberry Pi
    Install Jenkins on Raspberry Pi
    > Setup a Jenkins job
    Setup Jenkins GitHub credentials
    Test our deployment tool

    View Slide

  38. Create a Jenkins Job
    - Name: buildpi
    - Freestyle Job

    View Slide

  39. Create a Jenkins Job
    General => GitHub Project [x]
    Project Url:
    https://github.com/{username}/phpiot/

    View Slide

  40. Source Code Management
    => Git [x]
    Repository Url:
    https://github.com/{username}/phpiot.git

    View Slide

  41. Create a GitHub repo
    Connect with Raspberry Pi
    Install Jenkins on Raspberry Pi
    Setup a Jenkins job
    > Setup Jenkins GitHub credentials
    Test our deployment tool

    View Slide

  42. Add Credentials to Jenkins
    Connect your GitHub
    account using username and
    password

    View Slide

  43. Build Triggers => Trigger
    Builds Remotely [x]
    Authentication Token:
    php-srb-2017

    View Slide

  44. Configure the Jenkins job
    to have a Build Step =>
    Execute Shell
    Copy the script from
    README.md

    View Slide

  45. python green-light.py
    python red-light.py

    View Slide

  46. class ExampleTest extends TestCase
    {
    /**
    * A basic test example.
    *
    * @return void
    */
    public function testBasicTest()
    {
    $response = $this->get('/');
    $response->assertStatus(418); // I’m a teapot
    }
    }

    View Slide

  47. Create a GitHub repo
    Connect with Raspberry Pi
    Install Jenkins on Raspberry Pi
    Setup a Jenkins job
    Setup Jenkins GitHub credentials
    > Test our deployment tool

    View Slide

  48. Wave left!
    (and wait a few seconds)

    View Slide

  49. 1. sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
    2. Add/modify the ‘network’ element:
    Changing/adding WiFi networks
    to Rpi via CLI
    network={
    ssid="PHPSrb2017booth"
    psk="php_serbia_conference_2017"
    key_mgmt=WPA-PSK
    }

    View Slide

  50. Fixed IP address
    Your pi’s have fixed IP addresses.
    In order to remove this setting, open up the
    sudo nano /etc/dhcpcd.conf
    file and remove or edit the following:
    interface wlan0
    static ip_address=192.168.0.200/24
    static routers=192.168.0.1
    static domain_name_servers=192.168.0.1

    View Slide

  51. Remember to check this repo for details
    https://github.com/phpsrb/phpiot/blob/master/readme.md

    View Slide

  52. https://joind.in/talk/b4103

    View Slide

  53. Questions?
    Thank you!

    View Slide