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

Develop #9

Merged
merged 6 commits into from
Jul 6, 2024
Merged
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
17 changes: 17 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,28 @@ jobs:
- name: run integration tests
run: ./integration_tests.sh --durations=10 --cov=nomad_tools -n 3

test_ssl:
name: Test ssl
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v4
- name: upgrade pip
run: pip install --upgrade pip
- name: install editable package
run: pip install -e .
- name: install nomad server
run: bash ./tests/provision.sh nomad_install 1.8.0
- name: run nomad server tls
run: bash ./tests/provision.sh nomad_start_tls
- name: test tls connection
run: ./tests/tls_env.bash testall

pypi-publish:
name: Upload release to PyPI
needs:
- test
- test_ssl
if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
#if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
Expand Down
13 changes: 13 additions & 0 deletions jobs/test-forever.nomad.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
job "test-forever" {
# meta { uuid = uuidv4() }
group "test-forever" {
task "test-forever" {
driver = "docker"
config {
image = "busybox:stable"
command = "sleep"
args = ["3600h"]
}
}
}
}
26 changes: 26 additions & 0 deletions jobs/test-sigterm.nomad.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
job "test-sigterm" {
type = "batch"
meta {
uuid = uuidv4()
}
group "example" {
task "example" {
driver = "docker"
config {
image = "bash"
args = [
"sh",
"-xc",
<<EOF
trap 'echo SIGTERM; wait' SIGTERM
trap 'echo EXIT' EXIT
sleep infinity &
wait
EOF
]
}
kill_timeout = "20s"
shutdown_delay = "10s"
}
}
}
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ clickdc>=0.0.5
clickforward>=0.0.1
python-dotenv>=0.21.1
packaging>=16.1
tabulate
tabulate>=0.9.0
requests-toolbelt>=1.0.0
81 changes: 81 additions & 0 deletions src/nomad_tools/entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env python3
import click
import clickforward
from click.shell_completion import BashComplete

from . import (
entry_constrainteval,
entry_go,
entry_cp,
entry_dockers,
entry_downloadrelease,
entry_gitlab_runner,
entry_port,
entry_vardir,
entry_watch,
)
from .common_click import EPILOG, common_options, main_options
from .common_nomad import namespace_option

clickforward.init()

# Fix bash splitting completion on colon.
# Use __reassemble_comp_words_by_ref from bash-completion.
# Pass COMP_POINT as environment variable.
BashComplete.source_template = r"""\
%(complete_func)s() {
local cword words=()
if [[ $(type -t __reassemble_comp_words_by_ref) == function ]]; then
__reassemble_comp_words_by_ref "=:" words cword
else
words=("${COMP_WORDS[@]}")
cword=${COMP_CWORD}
fi
local IFS=$'\n'
response=$(COMP_POINT=$COMP_POINT COMP_WORDS="${words[*]}" COMP_CWORD="$cword" %(complete_var)s=bash_complete $1)
for completion in $response; do
IFS=',' read type value <<< "$completion"
case $type in
dir) COMPREPLY=(); compopt -o dirnames; ;;
file) COMPREPLY=(); compopt -o default; ;;
plain) COMPREPLY+=("$value"); ;;
nospace) compopt -o nospace; ;;
esac
done
}
%(complete_func)s_setup() {
complete -o nosort -F %(complete_func)s %(prog_name)s
}
%(complete_func)s_setup;
"""


@click.group(
"nomadtools",
help="Collection of useful tools for HashiCorp Nomad.",
epilog=EPILOG,
)
@namespace_option()
@common_options()
@main_options()
def cli():
pass


cli.add_command(entry_constrainteval.cli)
cli.add_command(entry_go.cli)
cli.add_command(entry_cp.cli)
cli.add_command(entry_dockers.cli)
cli.add_command(entry_downloadrelease.cli)
cli.add_command(entry_gitlab_runner.cli)
cli.add_command(entry_port.cli)
cli.add_command(entry_vardir.cli)
cli.add_command(entry_watch.cli)


def main():
cli(max_content_width=9999)


if __name__ == "__main__":
main()
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import click
import yaml

from . import nomad_watch, nomadlib, taskexec
from . import entry_watch, nomadlib, taskexec
from .common import (
cached_property,
common_options,
Expand All @@ -38,7 +38,7 @@


def get_gitlab_runner_package_script(file: str):
return get_package_file(f"nomad_gitlab_runner/{file}")
return get_package_file(f"entry_gitlab_runner/{file}")


def get_CUSTOM_ENV() -> Dict[str, str]:
Expand Down Expand Up @@ -536,12 +536,12 @@ def get_nomad_job(self) -> Job:
###############################################################################


def run_nomad_watch(cmd: str):
def run_entry_watch(cmd: str):
cmdarr = ["-T", *split(cmd)]
cmd = quotearr(cmdarr)
log.debug(f"+ nomad-watch {cmd}")
try:
return nomad_watch.cli.main(cmdarr, standalone_mode=False)
return entry_watch.cli.main(cmdarr, standalone_mode=False)
except SystemExit as e:
if not (e.code is None or e.code == 0):
raise
Expand All @@ -556,7 +556,7 @@ def purge_previous_nomad_job(jobname: str):
assert (
job.Stop is True or job.Status == "dead"
), f"Job {job.description()} already exists and is not stopped or not dead. Bailing out"
run_nomad_watch(f"--purge -xn0 stop {quote(jobname)}")
run_entry_watch(f"--purge -xn0 stop {quote(jobname)}")


###############################################################################
Expand Down Expand Up @@ -759,7 +759,7 @@ def mode_prepare():
with tempfile.NamedTemporaryFile("w+") as f:
f.write(jobjson)
f.flush()
run_nomad_watch(f"--json start {f.name}")
run_entry_watch(f"--json start {f.name}")


@cli.command("run", help="https://docs.gitlab.com/runner/executors/custom.html#run")
Expand Down Expand Up @@ -797,7 +797,7 @@ def mode_run(script: str, stage: str):
@executor_exit
def mode_cleanup():
je = Jobenv()
run_nomad_watch(
run_entry_watch(
f" {'--purge' if config.purge else ''}"
f" {'--purge-successful' if config.purge_successful and config.purge is None else ''}"
f" -x -n0 stop {quote(je.jobname)}"
Expand Down
8 changes: 4 additions & 4 deletions src/nomad_tools/entry_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import clickforward
import dotenv

from . import nomad_watch, taskexec
from . import entry_watch, taskexec
from .common_base import NOMAD_NAMESPACE, quotearr
from .common_click import EPILOG, common_options
from .common_nomad import namespace_option
Expand Down Expand Up @@ -636,9 +636,9 @@ def __thread(self):
@common_options()
@namespace_option()
@clickdc.adddc("args", Args)
@clickdc.adddc("notifyargs", nomad_watch.NotifyOptions)
@clickdc.adddc("notifyargs", entry_watch.NotifyOptions)
@click.option("-v", "--verbose", is_flag=True)
def cli(args: Args, notifyargs: nomad_watch.NotifyOptions, verbose: bool):
def cli(args: Args, notifyargs: entry_watch.NotifyOptions, verbose: bool):
global ARGS
ARGS = args
if args.command[0].startswith("-"):
Expand Down Expand Up @@ -674,7 +674,7 @@ def cli(args: Args, notifyargs: nomad_watch.NotifyOptions, verbose: bool):
cmd = [f"--notifyfdstarted={wfd}", *cmd]
with redirect_stdin_str(jobjson):
log.debug(f"+ {quotearr(cmd)}")
nomad_watch.cli.main(args=cmd)
entry_watch.cli.main(args=cmd)


if __name__ == "__main__":
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
121 changes: 50 additions & 71 deletions src/nomad_tools/entrypoint.py
Original file line number Diff line number Diff line change
@@ -1,82 +1,61 @@
#!/usr/bin/env python3

import click
import clickforward
from click.shell_completion import BashComplete

from . import (
entry_constrainteval,
entry_go,
nomad_cp,
nomad_dockers,
nomad_downloadrelease,
nomad_gitlab_runner,
nomad_port,
nomad_vardir,
nomad_watch,
)
from .common_click import EPILOG, common_options, main_options
from .common_nomad import namespace_option

clickforward.init()

# Fix bash splitting completion on colon.
# Use __reassemble_comp_words_by_ref from bash-completion.
# Pass COMP_POINT as environment variable.
BashComplete.source_template = r"""\
%(complete_func)s() {
local cword words=()
if [[ $(type -t __reassemble_comp_words_by_ref) == function ]]; then
__reassemble_comp_words_by_ref "=:" words cword
else
words=("${COMP_WORDS[@]}")
cword=${COMP_CWORD}
fi
local IFS=$'\n'
response=$(COMP_POINT=$COMP_POINT COMP_WORDS="${words[*]}" COMP_CWORD="$cword" %(complete_var)s=bash_complete $1)
for completion in $response; do
IFS=',' read type value <<< "$completion"
case $type in
dir) COMPREPLY=(); compopt -o dirnames; ;;
file) COMPREPLY=(); compopt -o default; ;;
plain) COMPREPLY+=("$value"); ;;
nospace) compopt -o nospace; ;;
esac
done
}
%(complete_func)s_setup() {
complete -o nosort -F %(complete_func)s %(prog_name)s
}
%(complete_func)s_setup;
import os
import sys

NOMAD_HELP = """
Usage: nomad [-version] [-help] [-autocomplete-(un)install] <command> [args]

Common commands:
run Run a new job or update an existing job
stop Stop a running job
status Display the status output for a resource
alloc Interact with allocations
job Interact with jobs
node Interact with nodes
agent Runs a Nomad agent

Other commands:
acl Interact with ACL policies and tokens
action Run a pre-defined command from a given context
agent-info Display status information about the local agent
config Interact with configurations
deployment Interact with deployments
eval Interact with evaluations
exec Execute commands in task
fmt Rewrites Nomad config and job files to canonical format
license Interact with Nomad Enterprise License
login Login to Nomad using an auth method
monitor Stream logs from a Nomad agent
namespace Interact with namespaces
operator Provides cluster-level tools for Nomad operators
plugin Inspect plugins
quota Interact with quotas
recommendation Interact with the Nomad recommendation endpoint
scaling Interact with the Nomad scaling endpoint
sentinel Interact with Sentinel policies
server Interact with servers
service Interact with registered services
setup Interact with setup helpers
system Interact with the system API
tls Generate Self Signed TLS Certificates for Nomad
ui Open the Nomad Web UI
var Interact with variables
version Prints the Nomad version
volume Interact with volumes
"""


@click.group(
"nomadtools",
help="Collection of useful tools for HashiCorp Nomad.",
epilog=EPILOG,
)
@namespace_option()
@common_options()
@main_options()
def cli():
pass


cli.add_command(entry_constrainteval.cli)
cli.add_command(entry_go.cli)
cli.add_command(nomad_cp.cli)
cli.add_command(nomad_dockers.cli)
cli.add_command(nomad_downloadrelease.cli)
cli.add_command(nomad_gitlab_runner.cli)
cli.add_command(nomad_port.cli)
cli.add_command(nomad_vardir.cli)
cli.add_command(nomad_watch.cli)
def get_nomad_subcommands():
return set(x.split()[0] for x in NOMAD_HELP.splitlines() if x.startswith(" "))


def main():
cli(max_content_width=9999)
if len(sys.argv) > 1:
if sys.argv[1] in get_nomad_subcommands():
os.execvp("nomad", ["nomad", *sys.argv[1:]])
from . import entry

entry.main()

if __name__ == "__main__":
main()
Loading