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 2 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
60 changes: 60 additions & 0 deletions templates/nomad_bootstrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import argparse
import textwrap
import fileinput
import sys
from string import Template
Rehzende marked this conversation as resolved.
Show resolved Hide resolved

def server(bootstrap_expect, retry_join, nomad_path):
retry_join = f'"{retry_join}"'
Rehzende marked this conversation as resolved.
Show resolved Hide resolved
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 = f'"{retry_join}"'
render_config(f"{nomad_path}client.hcl.tpl", "<RETRY_JOIN>", retry_join)

def both(bootstrap_expect, retry_join, nomad_path):
retry_join = f'"{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

if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent("""
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"
"""))
Rehzende marked this conversation as resolved.
Show resolved Hide resolved

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):
exit(1)

if args.mode == 'server':
server(args.bootstrap_expect, args.retry_join, args.nomad_path)
elif args.mode == 'client':
client(args.retry_join, args.nomad_path)
else:
both(args.bootstrap_expect, args.retry_join, args.nomad_path)
Rehzende marked this conversation as resolved.
Show resolved Hide resolved

97 changes: 0 additions & 97 deletions templates/nomad_bootstrap.sh

This file was deleted.