Mentoring SysAdmin Session 2

Usefull commands

Package managers

Most used inside Dockerfile when you need to install specific packages on the container image.

1
2
3
4
FROM debian:latest

RUN apt-get -y install \
            package-name

Depending on the docker base image you need to use different package managers and also in some cases the package-name is different, so you need to check the distribution documentation to know the exact name of the package.

The most used package managers are:

  • apt or apt-get - Debian derivated distributions (like ubuntu)
  • pkg - Alpine Linux

Each package manager has his own implementation and flags, you can use the manpages command man as you can do with other system tools to know exactly which actions and flags you need to use.

Bash scripting

We use bash to run some tasks on pipelines, deployments and administration tasks, even thougt if the deployment is using cloud native applications in many cases we need to write bash scripts to do some things.

Bash scripting is the sequence of commands you want to execute and this have also the power to execute some transformations to the state of the code.

Use case

In a client pipeline running on buildkite we needed to run some scripts to build the docker images and calculate the version number using the git tag (if is present), the branch name or the first 8 characters of the commit hash. To do so we made an script like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash

IMAGE = "demo-application"
TAG = ${BUILDKITE_COMMIT:0:8} # this notation allows you to slice the hash string to the first 8 characters

if [[ -z $BUILDKITE_TAG ]]; then
  TAG=${BUILDKITE_TAG}
fi

if [[ $BUILDKITE_BRANCH == 'master' ]]; then
  IMAGES="-t ${IMAGE}:${TAG} -t ${IMAGE}:${BUILDKITE_BRANCH}"
fi

docker build ${IMAGES} .
docker push ${IMAGE}

As you can see is like a programming language, it has variables, operators, loops, etc. But can be runned on the host system without installing or compiling anything.

Exercise time

Excercise #1

On your home folder there is a bash script called sample.sh for printing "Hello World" and the current date, try to execute it, is there something wrong? can you fix it? (Please remember the previous session)

Excercise #2

On the home user path of your application, write a simple script to rename all the files in the ~/excersise folder to be on this format:

file-{current name}-test.txt

Going forward

Please do the first 4 excercises on this web:

  • https://exercism.io/tracks/bash