ZSI / Automatizácia

Automatizácia

Základy softvérového inžinierstva

Sergej Chodarev (sergejx.net)

We are programmers

Our job is to automate

Don't Use Manual Procedures

A computer will execute the same instructions, in the same order, time after time.

The Pragmatic Programmer

Shell scripts

Shebang

#!/bin/sh
echo "Hello!"

Different languages

#!/bin/bash
#!/usr/bin/env python
#!/usr/bin/env python3

Shell programming

Use the Power of Command Shells

Use the shell when graphical user interfaces don’t cut it.

The Pragmatic Programmer

Just make it!

make

Makefile

targets: prerequisites
	recipe
	recipe…

Running

make target

Phony targets – not files

.PHONY: clean
clean:
        rm *.o temp

Automatic variables

Dependencies

Package management

Dependencies

Getting started

Dependencies

Virtualization

Containers

Containers

Containers — standardized units for development, shipment and deployment

Docker

Concepts

Example — src/index.php

<?php
echo("Hello world!");

Dependencies:

Dockerfile — instructions for building image

FROM php:7.2-apache-stretch
COPY src /var/www/html

Build image and start container

$ docker build  -t hello-php  .
$ docker run  -p 8000:80  --name running-hello  hello-php

Other commands

Layers

Persistent data storage — volumes

Dockerfile

FROM php:7.2-apache-stretch
COPY src /var/www/html
VOLUME /var/www/html/uploads

In development

Bind mount

docker run -p 8000:80 -v "$(pwd)"/src:/var/www/html hello-php
docker run -p 8000:80 -v d:/hello/src:/var/www/html hello-php

Let’s connect to database

<?php
$conn = new mysqli(
    "db-server", "user", "my-password", "hello");
if ($conn->connect_error)
    die("Oh, no! Connection failed!");
echo "Hello MariaDB!";

Dockerfile

FROM php:7.2-apache-stretch
RUN docker-php-ext-install mysqli
COPY src /var/www/html

We need to run a database server

Docker Compose

compose.yaml

services:
    web:
        build: .
        image: hello-php
        ports:
            - "8000:80"
        volumes:
            - ./src:/var/www/html
    db-server:
        image: mariadb:10.3
        environment:
            MYSQL_ROOT_PASSWORD: my-root-pwd
            MYSQL_DATABASE: hello
            MYSQL_USER: user
            MYSQL_PASSWORD: my-password

Up!

$ docker-compose build web
$ docker-compose up
$ docker-compose down

Examples

Continuous integration

Continuous Delivery

CI/CD server

CI/CD server

Use Version Control to Drive Builds, Tests, and Releases

Use commits or pushes to trigger builds, tests, releases. Use a version control tag to deploy to production.

The Pragmatic Programmer

Examples