Introduction

DevOps engineers must be comfortable with Linux but also with softwares and programming languages like Python, NodeJS and Java. It’s important to understand how applications are built, deployed and how to troubleshoot, in order to be able to setup CI/CD.

In this article, we will see together 3 of the most used programming languages used.

Java

Introduction

Java is a compiled programming language. Indeed, they are first developped, then compiled and finally executed. Writing Java applets and applications needs development tools like JDK. In the JDK you can find :

  • jdb : to debug your application
  • javadoc: to document your application
  • javac : to build your application
  • jar : helps archive the code into a single jar file.

To run your application anywhere, you need the JRE (Java Runtime Environment)

How to install java 13 :

sudo curl https://download.java.net/java/GA/jdk13.0.2/d4173c853231432d94f001e99d882ca7/8/GPL/openjdk-13.0.2_linux-x64_bin.tar.gz --output /opt/openjdk-13.0.2_linux-
x64_bin.tar.gz

sudo tar -xf /opt/openjdk-13.0.2_linux-x64_bin.tar.gz -C /opt/

# To make easier to use, add the binary in the path
export PATH=$PATH:/opt/jdk-13.0.2/bin

# To check
java -version

Code example :

class Simple{  
    public static void main(String args[]){  
     System.out.println("Hello Java");  
    }  
} 

Compilation :

javac Simple.java # The file Simple.class is generated

Execution :

java Simple

Build and Packaging – JARs

As explained previously, you need to compile your code in order to use it. When you have multiple files, you can create a JAR archive which contains all of them with their librairies and packages. Moreover, if you need to add extra html or images to your JAR, you will create a WAR (Web Archive.)

# To create a jar file
jar cf MyApp.jar MyClass.class MyService1.class ...

# To run a jar file
java -jar MyApp.jar

# Generate javadoc
javadoc -d doc MyClass.java

The problem with creating jar files is when you have multiple developpers working on the same project. It’s become really complex. That’s where build tools like Maven Gradle or ANT can help you. They can manage a project’s build, reporting and documentation from a central piece of information.

NodeJS

Introduction

NodeJS is an interpreted language. It means that it doesn’t need to be compiled ! Node.js is an open-source and cross-platform JavaScript runtime environment. It is a popular tool for almost any kind of project ! A Node.js app is run in a single process, without creating a new thread for every request. So it handles thousands of concurrent connections with a single server without introducing the burden of managing thread concurrency, which could be a significant source of bugs.

Installation :

curl -sL https://rpm.nodesource.com/setup_13.x | bash -
yum install nodejs

node -v # To check if it has been correctly installed

Code example :

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

Execution :

node app.js

NPM

Node Package Manager, is two things: first and foremost, it is an online repository for the publishing of open-source Node.js projects; second, it is a command-line utility for interacting with said repository that aids in package installation, version management, and dependency management. It allows developpers to develop new reusable packages or modules and share it on the public repository so other developpers can use it.

npm -v # get npm version

npm search file # search for a package

npm install file # install a package

Python

Introduction

Python is an interpreted, high-level and general-purpose programming language.

Check all my articles on Python for beginners : here

Installation

Installing Python is generally easy, and nowadays many Linux and UNIX distributions include a recent Python. Even some Windows computers (notably those from HP) now come with Python already installed. If you do need to install Python and aren’t confident about the task you can find a few notes on the BeginnersGuide/Download wiki page, but installation is unremarkable on most platforms.

Code example :

def print_message():
    print("Hello World")

if __name__ == '__main__':
    print_message()

Execution :

python main.py

PIP

PIP is the package installer for Python. You can use pip to install packages from the Python Package Index and other indexes. When you install python, pip is installed at the same time. Here are some basics commands :

# To install a package (flask is a famous framework for python)
pip install flask

# To get information on an installed package
pip show flask

Name: Flask
Version: 1.1.
Summary: A simple framework fo building complex web applications.
Home-page: https://palletsprojects.com/p/flask
Author: Armin Ronacher
Author-email: armin.ronacher@active-4.com
Licence: BSD-3-Clause
Location: /usr/lib64/python2.7/site-packages
Requires: Werkzeug, click, Jinja2, itsdangerous

# Import a package and see it location
python2 -c "import sys; print(sys.path)"


# Install multiple packages from a requirements.txt file.
# It's a good practice to specify in your file packages version like :
# flask==0.10.1
# jinja2==2.7.3

pip install -r requirements.txt


# Upgrade a package
pip install flask --upgrade

# Remove a package
pip uninstall flask

We have seen previously in java how to create jar packages. A similar way to do it in Python is to use Setuptools in order to create egg (or wheel) files. It is a package development process library designed to facilitate packaging Python projects by enhancing the Python standard library distutils (distribution utilities). It includes:

  • Python package and module definitions
  • Distribution package metadata
  • Test hooks
  • Project installation
  • Platform-specific details
  • Python 3 support

That’s all for today !

Leave a Comment

Your email address will not be published. Required fields are marked *