# Node Setup Guide

* Jenkins Plugins

  * pipeline
  * node and label
  * gradle
  * maven

* ubuntu [locale](https://unix.stackexchange.com/questions/303712/how-can-i-enable-utf-8-support-in-the-linux-console)

  ```sh
  $ apt-get install locales
  
  # conigure to en_US.UT-8
  $ sudo dpkg-reconfigure locales
  
  # show locale setting (after reboot)
  $ locale
  
  LANG=en_US.UTF-8
  LANGUAGE=
  LC_CTYPE="en_US.UTF-8"
  LC_NUMERIC="en_US.UTF-8"
  LC_TIME="en_US.UTF-8"
  LC_COLLATE="en_US.UTF-8"
  LC_MONETARY="en_US.UTF-8"
  LC_MESSAGES="en_US.UTF-8"
  LC_PAPER="en_US.UTF-8"
  LC_NAME="en_US.UTF-8"
  LC_ADDRESS="en_US.UTF-8"
  LC_TELEPHONE="en_US.UTF-8"
  LC_MEASUREMENT="en_US.UTF-8"
  LC_IDENTIFICATION="en_US.UTF-8"
  LC_ALL=
  ```

  

* Java compiler

  * Java 8

    ```sh
    # install full jdk8 packge
    # openjdk-8-jdk-headless exclude some jar file (eg /lib/jconsole.jar)
    # which is need by alibaba druid dependencies package
    $ apt install openjdk-8-jdk
    ```

    

* Slave Agent

  * Create a slave VM. Username as `jenkins`

  * Set to sudo user

    ```sh
    # add jenkins user
    $ adduser jenkins
    
    # change jenkins to sudo group
    $ usermod -aG sudo jenkins
    
    # check the user group
    $ groups jenkins
    
    # verify sudo access
    $ su - jenkins
    
    
    ```

  * Optional, add `jenkins` as `sudoer`

  * [reference](https://www.maketecheasier.com/edit-sudoers-file-linux/)

    ```sh
    # add jenkins as sudoer
    $ visudo
    
    
    # User privilege specification
    # username hostlist = (userlist) commandlist
    # eg. Only allow alex to run apt-get update
    # alex ALL=(ALL) /usr/bin/apt-get update
    root    ALL=(ALL:ALL) ALL
    jenkins ALL=(ALL:ALL) ALL
    ```

  * Run jenkins node agent

  ```sh
  # download agent.jar file from jenkins server
  $ wget https://192.168.0.119/jnlpJars/agent.jar --no-check-certificate
  
  # run the agent to connect to server
  $ sudo java -jar agent.jar -jnlpUrl https://192.168.0.119/computer/jenkins.slave/jenkins-agent.jnlp -noCertificateCheck -secret 39694868bc66890cd5eb6c04b0a51692403ab957ceb1d1b258c353a031334733 -workDir "/home/jenkins"
  ```

  ## Run agent as service

* [reference](https://www.baeldung.com/linux/run-java-application-as-service)

* create a script file `startagent.sh`

  ```sh
  #!/bin/bash
  
  JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
  WORKDIR=/home/jenkins/scripts
  APP_OPTIONS=" -jnlpUrl https://192.168.0.119/computer/jenkins.slave/jenkins-agent.jnlp -noCertificateCheck -secret @secret-file -workDir \"/home/jenkins\""
  
  "${JAVA_HOME}/bin/java" -jar agent.jar $APP_OPTIONS
  
  ```

* create the `secret-file`

  ```sh
  $ echo 39694868bc66890cd5eb6c04b0a51692403ab957ceb1d1b258c353a031334733 > secret-file
  ```

  

* test the script

  ```sh
  $ chmod +x startagent.sh
  
  # start at background
  $ ./startagent.sh &
  
  # make foreground
  $ fg
  ```

* create a service

  * Create the service unit file `jenkinsagent.service`

    ```ini
    [Unit]
    Description=Jenkins Agent service
    
    [Service]
    SuccessExitStatus=143
    User=jenkins
    Group=sudo
    
    WorkingDirectory=/home/jenkins/scripts
    
    ExecStart=/home/jenkins/scripts/startagent.sh
    
    
    [Install]
    WantedBy=multi-user.target
    ```

  * copy the service unit file to system `/etc/systemd/system`

  ```sh
  $ sudo cp ./jenkinsagent.service /etc/systemd/system/jenkinsagent.service
  
  # reload unit files
  $ sudo systemctl daemon-reload
  
  # enable the service (after tested ok). service will start upon every PC restart
  $ sudo systemctl enable jenkinsagent.service
  
  Created symlink /etc/systemd/system/multi-user.target.wants/jenkinsagent.service -> /etc/systemd/system/jenkinsagent.service.
  
  # start the service
  $ sudo systemctl start jenkinsagent.service
  
  # check status o service
  $ sudo systemctl status jenkinsagent.service
  
  # stop the service
  $ sudo systemctl stop jenkinsagent.service
  
  ```

  