Shell Script Moderno - Aurelio Jargas
Shell Script Moderno - Aurelio Jargas
Moderno
Aurelio Jargas
http://aurelio.net
@oreio
Sysadmin anos 90
Sysadmin hoje
Foto: caminholivre.wordpress.com
Sysadmin dos anos 90
CD do Linux
Instala a distro
Recompila o kernel
Instala pacotes
Inicia serviços
Feito!
Sysadmin dos anos 90
CD do Linux
Instala a distro
Recompila o kernel
Instala pacotes
Inicia serviços
Feito!
} install.sh
VM
Cloud
Docker
Sysadmin / DevOps moderno
Jenkins
Travis CI / GitLab CI
Docker
Meu precioso servidor,
criado “na mão”
com muito ♥
Hoje eu lanço e destruo servidores.
Com um clique.
Sem pensar muito.
E o shell?
Ambientes de execução de scripts
Linha de comando
$ meu-script.sh foo
Cron
$ ls /path/to/my/repo/.git/hooks/
applypatch-msg.sample pre-push.sample
commit-msg.sample pre-rebase.sample
post-update.sample prepare-commit-msg.sample
pre-applypatch.sample update.sample
pre-commit.sample
$
Git hooks
$ cat app.bare.git/hooks/post-receive
#!/bin/bash -exu
Travis, GitLab
Travis CI
language: bash
before_install:
- url_base="https://raw.githubusercontent.com/aureliojargas"
- curl -sOL "${url_base}/clitest/master/clitest"
- chmod +x clitest
- mv clitest testador
script:
- cd testador
- ./run
Travis CI
language: bash
before_install:
- url_base="https://raw.githubusercontent.com/aureliojargas"
- curl -sOL "${url_base}/clitest/master/clitest"
- chmod +x clitest
- mv clitest testador
script:
- cd testador
- ./run
GitLab CI
code_lint:
script:
- date
- uname -a
- env
- ls -la
- cd wp-content/plugins/foo-setup
- phpcs .
- cd -
- cd wp-content/themes/foo
- phpcs --ignore=bootstrap,inc,js,css .
- cd -
unit_tests:
script:
- cd wp-content/plugins/foo-setup
- WP_TESTS_DIR=/opt/wp-tests/wp-tests-lib phpunit
GitLab CI
code_lint:
script:
- date
- uname -a
- env
- ls -la
- cd wp-content/plugins/foo-setup
- phpcs .
- cd -
- cd wp-content/themes/foo
- phpcs --ignore=bootstrap,inc,js,css .
- cd -
unit_tests:
script:
- cd wp-content/plugins/foo-setup
- WP_TESTS_DIR=/opt/wp-tests/wp-tests-lib phpunit
Puppet
Puppet
FROM debian:jessie
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y curl
COPY . /app
WORKDIR /app
FROM debian:jessie
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y curl
COPY . /app
WORKDIR /app
FROM debian:jessie
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y curl
COPY . /app
WORKDIR /app
FROM debian:jessie
cp -r . /app
ARG DEBIAN_FRONTEND=noninteractive
cd
RUN apt-get update/a&&
pp apt-get install -y curl
COPY . /app
WORKDIR /app
FROM debian:jessie
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y curl
COPY . /app
WORKDIR /app
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y curl
FROM debian:jessie
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y curl
if test $# -gt 0
COPY . /app then
WORKDIR /app bash funcoeszz "$@"
else
ENV PATH /app:$PATH bash funcoeszz --help
fi
ENV ZZPATH /app/funcoeszz
ENV ZZDIR /app/zz
$ date
$ uname -a
$ env | sort
$ whoami
$ groups
$ ls -la
$ ps auxw
$ git branch
$ git status
$ docker info
$ docker ps
Use funções para organizar o script
#!/bin/bash
build() {
docker build ...
}
publish() {
docker push ...
docker rmi ...
}
run() {
docker run ...
}
build
publish
run
Crie uma pasta “scripts” no repositório
$ ls -1 scripts/
_lib.sh
build.sh
cleanup.sh
database-reset.sh
deploy.sh
merge-branch.sh
publish.sh
test.sh
$
Pratique POSIX
#!/bin/sh
Evite arrays
Evite ${variavel/isso/aquilo}
Evite ${variavel:1:3}
Evite [[
Evite $FUNCNAME
Evite for(;;)
Evite $'…'
Evite comando < <(comando)
Seja --verbose, mande tudo para STDOUT
echo
echo "Iniciando o backup"
tar cvzf meuapp-$timestamp.tgz "$destino"
#!/bin/bash
# Modo strict
set -euo pipefail
#!/bin/bash
# Modo strict
set -euo pipefail
#!/bin/bash
Para executar:
$ ./meu-script.sh # sem debug
$ DEBUG=1 ./meu-script.sh # com debug
Ligue o debug com $DEBUG - strict
#!/bin/bash
$ shellcheck meu-script.sh
Line 3:
for f in $(ls *.txt)
^-- SC2045: Iterating over ls output is fragile.
Line 5:
grep -i nofx.*mp3 $f
^-- SC2062: Quote the grep pattern so the shell won't
interpret it.
^-- SC2086: Double quote to prevent globbing and
word splitting.
jq - Parser de JSON
https://stedolan.github.io/jq/
$ cat foo.json
{ "foo": { "bar": { "baz": 123 } } }
$ cat foo.json | jq .
{
"foo": {
"bar": {
"baz": 123
}
}
}
$ cat foo.json | jq .foo.bar.baz
123
$
curl, sed
AWS aws
https://aws.amazon.com/cli/
Azure azure
https://github.com/Azure/azure-xplat-cli
DigitalOcean doctl
https://github.com/digitalocean/doctl
Rackspace rack
https://developer.rackspace.com/docs/rack-cli/
Perguntas?
@oreio
$ __