Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reescreve o nomad_bootstrap.sh em pyhton #18

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ exclude_paths:
- .github/
- .cache/
- molecule/default
- templates/
offline: false
use_default_rules: true
parseable: true
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 72 additions & 0 deletions templates/nomad_bootstrap.py
Original file line number Diff line number Diff line change
@@ -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>", bootstrap_expect)
render_config(f"{nomad_path}server.hcl.tpl", "<RETRY_JOIN>", retry_join)


def client(retry_join, nomad_path):
retry_join = retry_join
render_config(f"{nomad_path}client.hcl.tpl", "<RETRY_JOIN>", retry_join)


def both(bootstrap_expect, retry_join, nomad_path):
retry_join = retry_join
render_config(f"{nomad_path}server.hcl.tpl",
"<BOOTSTRAP_EXPECT>", bootstrap_expect)
render_config(f"{nomad_path}server.hcl.tpl", "<RETRY_JOIN>", retry_join)
render_config(f"{nomad_path}client.hcl.tpl", "<RETRY_JOIN>", 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)
97 changes: 0 additions & 97 deletions templates/nomad_bootstrap.sh

This file was deleted.