diff --git a/.ansible-lint b/.ansible-lint index 3575254..c8005d2 100644 --- a/.ansible-lint +++ b/.ansible-lint @@ -3,6 +3,7 @@ exclude_paths: - .github/ - .cache/ - molecule/default + - templates/ offline: false use_default_rules: true parseable: true diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index de8a9ea..e20add9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,10 +19,11 @@ jobs: run: | python3 -m pip install --upgrade pip python3 -m pip install -r requirements.txt + python3 -m pip install pylint - name: Run Linters run: | ansible-lint - shellcheck ./templates/nomad_bootstrap.sh + pylint ./templates/nomad_bootstrap.py molecule: runs-on: ubuntu-18.04 strategy: diff --git a/tasks/main.yml b/tasks/main.yml index ccef14a..0b9e408 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -31,8 +31,8 @@ - server.hcl - name: "Criando script de inicialização" template: - src: nomad_bootstrap.sh - dest: /usr/local/bin/nomad_bootstrap.sh + src: nomad_bootstrap.py + dest: /usr/local/bin/nomad_bootstrap.py mode: 0755 # CNI-plugin diff --git a/templates/nomad_bootstrap.py b/templates/nomad_bootstrap.py new file mode 100644 index 0000000..513df2d --- /dev/null +++ b/templates/nomad_bootstrap.py @@ -0,0 +1,72 @@ +import argparse +import fileinput +import sys + + +def server(bootstrap_expect, retry_join, nomad_path): + retry_join = retry_join + render_config(f"{nomad_path}server.hcl.tpl", + "", bootstrap_expect) + render_config(f"{nomad_path}server.hcl.tpl", "", retry_join) + + +def client(retry_join, nomad_path): + retry_join = retry_join + render_config(f"{nomad_path}client.hcl.tpl", "", retry_join) + + +def both(bootstrap_expect, retry_join, nomad_path): + retry_join = retry_join + render_config(f"{nomad_path}server.hcl.tpl", + "", bootstrap_expect) + render_config(f"{nomad_path}server.hcl.tpl", "", retry_join) + render_config(f"{nomad_path}client.hcl.tpl", "", retry_join) + + +def render_config(file, searchExp, replaceExp): + for line in fileinput.input(file, inplace=1): + line = line.replace(searchExp, str(replaceExp)) + sys.stdout.write(line) + + +def validate_args(args): + if (args.mode == "server" or args.mode == "both") and args.bootstrap_expect is None: + print("Parâmetro 'bootstrap_expect' não informado.") + return False + return True + + +EPILOG = """ +Exemplos: + Iniciar cluster local com client e servidor: + nomad_boostrap.py both -b 1 + + Iniciar cluster no GCP com cloud auto-join: + nomad_bootstrap.py server -b 3 -r "provider=gce project_name=meu-projeto tag_value=nomad-server" + nomad_bootstrap.py client -r "provider=gce project_name=meu-projeto tag_value=nomad-server" +""" + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + formatter_class=argparse.RawDescriptionHelpFormatter, epilog=EPILOG) + + parser.add_argument('mode', help='Modo de execução do nomad', choices=[ + 'server', 'client', 'both']) + parser.add_argument('--bootstrap-expect', '-b', + help='Número de nodes do servidor a aguardar antes de inicializar', type=int) + parser.add_argument('--retry-join', '-r', help='', + type=str, default='127.0.0.1') + parser.add_argument('--nomad_path', help='Caminho do nomad', + type=str, default='/etc/nomad.d/') + args = parser.parse_args() + + if not validate_args(args): + sys.exit(1) + + if args.mode == 'server': + + server(args.bootstrap_expect, f'"{args.retry_join}"', args.nomad_path) + elif args.mode == 'client': + client(f'"{args.retry_join}"', args.nomad_path) + else: + both(args.bootstrap_expect, f'"{args.retry_join}"', args.nomad_path) diff --git a/templates/nomad_bootstrap.sh b/templates/nomad_bootstrap.sh deleted file mode 100644 index be3902f..0000000 --- a/templates/nomad_bootstrap.sh +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env bash - -nomad_config_path=/etc/nomad.d - -help() { - echo " -uso: nomad_boostrap.sh [-h|--help] mode [bootstrap_expect] [rety_join] - -Inicializa os arquivos de configuração do Nomad e habilita a unidade do Nomad no systemd. - -argumentos: - mode modo em que o Nomad vai rodar. Valores possíveis: server, client ou both. - -argumentos opcionais: - --help, -h, help imprime essa mensagem de ajuda. - bootstrap_expect número de servidores no cluster. - retry_join lista de IPs ou configuração do cloud auto-join. - - -exemplos: - Iniciar cluster local com client e servidor: - nomad_boostrap.sh both 1 - - Iniciar cluster no GCP com cloud auto-join: - nomad_boostrap.sh server 3 '\"provider=gce project_name=meu-projeto tag_value=nomad-server\"' - nomad_boostrap.sh client '\"provider=gce project_name=meu-projeto tag_value=nomad-server\"' -" -} - -main() { - local mode="$1" - - case "${mode}" in - server) - render_server_config "${@:2}" - ;; - client) - render_client_config "${@:2}" - ;; - both) - render_both_config "${@:2}" - ;; - help | --help | -h) - help - exit 0 - ;; - *) - echo "Parâmetro 'mode' invalido." - help - exit 1 - ;; - esac - - echo "Habilitando e iniciando a unidade do Nomad no systemd..." - systemctl enable nomad - systemctl start nomad - - echo "Finalizado." - exit 0 -} - -render_server_config() { - local bootstrap_expect="$1" - local retry_join="${2:-\"127.0.0.1\"}" - - echo "Renderizando arquivo de configuração do server..." - - if [[ -z "${bootstrap_expect}" ]]; then - echo "Parâmetro 'bootstrap_expect' não informado." - exit 1 - fi - - sed --expression " - s//${bootstrap_expect}/ - s//${retry_join}/ - " "${nomad_config_path}/server.hcl.tpl" > "${nomad_config_path}/server.hcl" -} - -render_client_config() { - local retry_join="${1:-\"127.0.0.1\"}" - - echo "Renderizando arquivo de configuração do client..." - - sed --expression " - s//${retry_join}/ - " "${nomad_config_path}/client.hcl.tpl" > "${nomad_config_path}/client.hcl" -} - -render_both_config() { - local bootstrap_expect="$1" - local retry_join="${2:-\"127.0.0.1\"}" - - render_server_config "${bootstrap_expect}" "${retry_join}" - render_client_config "${retry_join}" -} - -main "$@"