-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,500 additions
and
756 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/usr/bin/env python3 | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import argparse | ||
import logging | ||
from typing import List | ||
|
||
import click | ||
|
||
from . import nomadlib | ||
from .common import argument_alloc, common_options, complete_job, mynomad | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def gen_alloc(alloc: nomadlib.Alloc) -> List[str]: | ||
out = [] | ||
log.debug(f"Parsing {alloc.ID}") | ||
for i in alloc["AllocatedResources"]["Shared"]["Ports"]: | ||
params = dict( | ||
**{ | ||
k: v | ||
for k, v in alloc.items() | ||
if isinstance(v, str) and isinstance(k, str) | ||
}, | ||
label=i["Label"], | ||
host=i["HostIP"], | ||
port=i["Value"], | ||
) | ||
log.debug(f"Found") | ||
out += [args.format % params] | ||
return out | ||
|
||
|
||
def output(out: List[str]): | ||
if out: | ||
print(*out, sep=args.separator) | ||
|
||
|
||
@click.group() | ||
@click.option( | ||
"-f", "--format", default="%(Name)s %(label)s %(host)s:%(port)s", show_default=True | ||
) | ||
@click.option("-s", "--separator", default="\n", show_default=True) | ||
@click.option("-v", "--verbose", count=True, help="Be more verbose.") | ||
@common_options() | ||
def cli(**kwargs): | ||
global args | ||
args = argparse.Namespace(**kwargs) | ||
logging.basicConfig( | ||
level=( | ||
logging.DEBUG | ||
if args.verbose > 0 | ||
else logging.INFO | ||
if args.verbose == 0 | ||
else logging.WARN | ||
if args.verbose == 1 | ||
else logging.ERROR | ||
), | ||
) | ||
|
||
|
||
@cli.command("alloc", help="Show ports of a single allocation.") | ||
@argument_alloc() | ||
def mode_alloc(alloc: nomadlib.Alloc): | ||
out = gen_alloc(alloc) | ||
output(out) | ||
|
||
|
||
@cli.command("job", help="Show ports of all running or pending job allocations.") | ||
@click.option( | ||
"--all", | ||
help="Show all allocation ports, not only running or pending.", | ||
is_flag=True, | ||
) | ||
@click.argument( | ||
"jobid", | ||
shell_complete=complete_job(), | ||
) | ||
def mode_job(jobid, all): | ||
jobid = mynomad.find_job(jobid) | ||
out = [] | ||
for alloc in mynomad.get(f"job/{jobid}/allocations"): | ||
if all or nomadlib.Alloc(alloc).is_pending_or_running(): | ||
# job/*/allocations does not have AllocatedResources information. | ||
alloc = nomadlib.Alloc(mynomad.get(f"allocation/{alloc['ID']}")) | ||
out += gen_alloc(alloc) | ||
output(out) | ||
|
||
|
||
if __name__ == "__main__": | ||
cli() |
Oops, something went wrong.