blob: d9e0ede4b9b53f7b674cdddbbbd50f00533d68ed [file] [log] [blame]
#!/usr/bin/env python3
# Copyright 2019 Fairphone B.V.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Deploy device-related configurations to a LAVA instance.
configure_lava allows to deploy configurations of device tags, devices types and
devices to a LAVA instance. It uses a set of YAML files as input and makes
required calls to the LAVA API through lavacli to configure the LAVA instance as
requested.
Prerequisites for using this tool:
* Set up a lavacli identity. This is required in order to connect and
authenticate at a LAVA instance. Consult the LAVA API and lavacli
documentation for instructions on how to setup lavacli.
* Write LAVA device administration configurations. This tool takes a root YAML
file as input, that defines a set of input files for device tags, device types
and devices that are to be configured in the selected LAVA instance.
"""
import argparse
from pathlib import Path
from lava_config_definitions import LAVAInstanceConfigDeployer
import lava_config_def_generator
def deploy(args: argparse.Namespace) -> None:
"""Entry point of the deploy subcommand."""
deployer = LAVAInstanceConfigDeployer(
instance_definition_file=args.instance_definition_file,
lavacli_identity=args.identity,
)
deployer.configure()
def dump_devices(args: argparse.Namespace) -> None:
"""Entry point of the dump devices subcommand."""
lava_config_def_generator.dump_devices(
lavacli_identity=args.identity, dump_all=args.all
)
def build_cmdline_arguments() -> argparse.ArgumentParser:
"""Parse the command line arguments.
Returns:
argparse Namespace containing the parsed command line arguments of this
script.
"""
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"-i",
"--identity",
required=True,
type=str,
help=(
"Identity in lavacli. Authentication in lavacli must be set up "
"before running this tool. It will be assumed that the specified "
"identity has necessary permissions to execute requested actions."
),
)
sub_parsers = parser.add_subparsers(title="subcommands")
parser_deploy = sub_parsers.add_parser(
"deploy", help="Deploy configurations to a LAVA instance."
)
parser_deploy.set_defaults(func=deploy)
parser_deploy.add_argument(
"instance_definition_file",
type=Path,
help=(
"Path to the YAML file that defines the LAVA instance "
"configuration that will be deployed."
),
)
dump_parser = sub_parsers.add_parser(
"dump",
help=(
"Helper for dumping information from an existing LAVA instance to "
"a format expected by the `deploy` subcommand."
),
)
dump_sub_parsers = dump_parser.add_subparsers(title="subcommands")
parser_dump_devices = dump_sub_parsers.add_parser(
"devices", help="Extract the list of devices from a LAVA instance."
)
parser_dump_devices.set_defaults(func=dump_devices)
parser_dump_devices.add_argument(
"-a",
"--all",
action="store_true",
help="List all devices. Retired devices are excluded by default.",
)
return parser
def main() -> None:
"""Entry point into the configure_lava command line tool."""
parser = build_cmdline_arguments()
args = parser.parse_args()
if "func" in args:
args.func(args)
else:
parser.error("Select the subcommand to execute.")
if __name__ == "__main__":
main()