#TIL : Improve apt package manager speed by changi by changing source list mirrors

Sometimes, you ran into issues that apt update package list so slow or even can not connect to the destination server.

You can change the default list into new one near your country. Get example : if you live in ASIA, choose the Singapore or Japan mirror instead of main mirror. you just change the “archive.ubuntu.com” and “security.ubuntu.com” to “[country_code].archive.ubuntu.com” in file list /etc/apt/sources.list

Then run sudo apt update to test your new mirror :)

TIP : you can generate the new source list file based on this online tool : https://repogen.simplylinux.ch/generate.php

#TIL : Improve apt package manager speed by changing source list mirrors

Sometimes, you ran into issues that apt update package list so slow or even can not connect to the destination server.

You can change the default list into new one near your country. Get example : if you live in ASIA, choose the Singapore or Japan mirror instead of main mirror. you just change the “archive.ubuntu.com” and “security.ubuntu.com” to “[country_code].archive.ubuntu.com” in file list /etc/apt/sources.list

Then run sudo apt update to test your new mirror :)

TIP : you can generate the new source list file based on this online tool : https://repogen.simplylinux.ch/generate.php

#TIL : Run shell command in all hosts

To run a shell command in all hosts, you can use the module name raw and provide shell command to module args.

Example:

  • To list all CPU model name of hosts
1
$ ansible all -m raw -a "cat /proc/cpuinfo | grep 'model name'"

#TIL : Build lightweight image by using multistage

Docker is great tool to build a pull-n-run application. But sometimes, your image will be large if you build image from a big base image which has heavy compliling toolbox.

Ex:

One-stage build

1
2
3
4
5
FROM golang:1.9.2
WORKDIR /go/src/github.com/khanhicetea/test/
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build .
ENTRYPOINT ["/go/src/github.com/khanhicetea/test/test"]

Multi-stage builds

1
2
3
4
5
6
7
8
FROM golang:1.9.2
WORKDIR /go/src/github.com/khanhicetea/test/
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build .

FROM scratch
COPY --from=0 /go/src/github.com/khanhicetea/test/test .
ENTRYPOINT ["/test"]

So final image will only contains /test excutable file. COPY --from=0 means COPY from build has index 0 in Dockerfile.

#TIL : Ansible running host pattern

Ansible supports pattern to select and filter running hosts from all hosts. Here is some common pattern

  • * : wildcard, standalone mean all
  • group1,group2 : run hosts belong to group1 or group2
  • *.web : run hosts belongs to group matches pattern. Ex: backend.web, frontend.web
  • all:!abc : run all hosts exclude hosts belongs to group abc

Infrastructure by code ;)

#TIL : Reduce init time MySQL docker image

Original MySQL docker image uses a script to generate ssl certificates for service. Sometime we don’t really need it (connect via a docker network link or need a fast enough database service to build a automated test).

We can reduce init time by removing the script from original Docker image

1
2
3
4
FROM mysql:5.7

# Remove mysql_ssl_rsa_setup to ignore setup SSL certs
RUN rm -f /usr/bin/mysql_ssl_rsa_setup

FAST as a FEATURE !!! 🚀

#TIL : Using netcat to wait a TCP service

When doing a CI/CD testing, you would need to connect a external service (RDBMS, HTTP server or generic TCP server service). So you need waiting the service before running your test app.

One way to do right waiting instead of sleep for a specified time is using netcat tool

1
$ while ! echo -e '\x04' | nc [service_host] [service_port]; do sleep 1; done;

Examples

  • MySQL service on port 3306
1
2
$ while ! echo -e '\x04' | nc 127.0.0.1 3306; do sleep 1; done;
$ ./run_test.sh

Explanation :

echo -e '\x04' will send an EOT (End Of Transmission) to the TCP every second to check if it’s ready !