Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
iranzo committed Nov 6, 2024
1 parent af9856b commit 6445f9a
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 163 deletions.
36 changes: 16 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
Fast Assembler for Bootc
===
# Fast Assembler for Bootc

`fab` is a somewhat opinionated build pipeline for [bootc](https://github.com/containers/bootc), allowing the modularization
of Containerfiles/bootc image building.

Fabfiles and module definitions
---
## Fabfiles and module definitions

`fab` is structured as a main descriptive file for the final image, currently called "Fabfile". A Fabfile example looks like this:
`fab` is structured as a main descriptive file for the final image, currently called "`Fabfile`". A `Fabfile` example looks like this:

```
```yaml
---
metadata:
name: fabrules
Expand All @@ -25,14 +23,14 @@ buildargs:
The fields are somewhat self-explanatory:
- `metadata`: just metadata about the bootc image
- `from`: base image (i.e. the first `FROM` in the pipeline)
- `include`: list of modules to include, in order in the bootc image
- `buildargs`: list of buildargs (variables) used in the build process
- `metadata`: just metadata about the bootc image
- `from`: base image (i.e. the first `FROM` in the pipeline)
- `include`: list of modules to include, in order in the bootc image
- `buildargs`: list of buildargs (variables) used in the build process

For each module, there is a short descriptive file with the module definition (see `modules/` directory for examples):

```
```yaml
---
metadata:
name: dnf-install
Expand All @@ -45,27 +43,25 @@ buildargs:

Like with the top level definition, the fields should be easy to understand:

- `metadata`: module level metadata
- `containerfile`: filename for the Containerfile to use
- `buildargs`: simple list of expected buildargs (without values!)
- `metadata`: module level metadata
- `containerfile`: filename for the `Containerfile` to use
- `buildargs`: simple list of expected buildargs (without values!)

Installation
---
## Installation

```
```sh
$ git clone [email protected]:kwozyman/fab.git
$ cd fab
$ python3 -m pip install --requirement requirements.txt
```

Usage
---
## Usage

A simple `python3 -m fab --help` will show the full command line arguments.

In order to build the container from `Fabfile.example`:

```
```sh
$ python3 -m fab build --fabfile Fabfile.example
```

Expand Down
3 changes: 1 addition & 2 deletions fab/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3

from fab.cli import FabCli
from fab.module import FabModule

if __name__ == '__main__':
if __name__ == "__main__":
FabCli()
135 changes: 77 additions & 58 deletions fab/cli.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import logging
from fab.fab import Fab

class FabCli():

class FabCli:
"""
Fab command line
"""

def __init__(self, **kwargs):
self.loglevel = 'info'
self.log_format = '%(asctime)19s - %(levelname)8s - %(message)s'
self.log_datefmt = '%d-%m-%Y %H:%M:%S'
self.loglevel = "info"
self.log_format = "%(asctime)19s - %(levelname)8s - %(message)s"
self.log_datefmt = "%d-%m-%Y %H:%M:%S"
self.logmap = {
'info': logging.INFO,
'warning': logging.WARN,
'warn': logging.WARN,
'debug': logging.DEBUG
"info": logging.INFO,
"warning": logging.WARN,
"warn": logging.WARN,
"debug": logging.DEBUG,
}
config = {**self._cmdargs(), **kwargs}
for k in config:
Expand All @@ -22,85 +24,102 @@ def __init__(self, **kwargs):
self.loglevel = self.log_level
self.set_loglevel(self.loglevel)

if config['command'] == 'build':
self.fab = Fab(config['fabfile'],
container_tool=config['container_tool'],
tool_args=config['container_tool_extra_args'])
if config["command"] == "build":
self.fab = Fab(
config["fabfile"],
container_tool=config["container_tool"],
tool_args=config["container_tool_extra_args"],
)
self.fab.build()

def _basic_logging(self):
logging.basicConfig(level=self.logmap[self.loglevel],
format=self.log_format,
datefmt=self.log_datefmt)
logging.basicConfig(
level=self.logmap[self.loglevel],
format=self.log_format,
datefmt=self.log_datefmt,
)

def set_loglevel(self, level):
logging.getLogger().setLevel(self.logmap[level])
logging.debug('DEBUG mode is enabled')
logging.debug("DEBUG mode is enabled")

def _cmdargs(self):
"""
Parse command line arguments and read config files (if module exists)
"""
description = 'Fast Assembly Bootc'
description = "Fast Assembly Bootc"
try:
import configargparse

parser = configargparse.ArgParser(
default_config_files=[
'fab.config',
'/etc/fab/config',
'~/.config/fab/config'],
description=description
"fab.config",
"/etc/fab/config",
"~/.config/fab/config",
],
description=description,
)
except ModuleNotFoundError:
logging.debug('Could not find module "configparse"')
import argparse
parser = argparse.ArgumentParser(
description=description
)

parser.add_argument('--log-level', '--loglevel',
choices=self.logmap.keys(),
type=str.lower,
default=self.loglevel,
help='Logging level',
)
parser.add_argument('--log-format',
type=str,
default=self.log_format,
help='Python Logger() compatible format string')
parser.add_argument('--log-datefmt',
type=str,
default=self.log_datefmt,
help='Python Logger() compatible date format str')
parser.add_argument('--container-tool',
help='What container tool to use',
default='/usr/bin/podman')
parser.add_argument('--container-tool-extra-args',
help='container tool extra arguments',
default=[])
parser = argparse.ArgumentParser(description=description)

subparsers = parser.add_subparsers(dest='command')
parser.add_argument(
"--log-level",
"--loglevel",
choices=self.logmap.keys(),
type=str.lower,
default=self.loglevel,
help="Logging level",
)
parser.add_argument(
"--log-format",
type=str,
default=self.log_format,
help="Python Logger() compatible format string",
)
parser.add_argument(
"--log-datefmt",
type=str,
default=self.log_datefmt,
help="Python Logger() compatible date format str",
)
parser.add_argument(
"--container-tool",
help="What container tool to use",
default="/usr/bin/podman",
)
parser.add_argument(
"--container-tool-extra-args",
help="container tool extra arguments",
default=[],
)

subparsers = parser.add_subparsers(dest="command")
subparsers.required = False
parser_build = subparsers.add_parser('build',
help='Build bootc container')
parser_build.add_argument('--fabfile',
help='path to fabfile',
default='Fabfile')
parser_build = subparsers.add_parser("build", help="Build bootc container")
parser_build.add_argument(
"--fabfile", help="path to fabfile", default="Fabfile"
)

try:
import argcomplete
from os.path import basename
parser.add_argument('--bash-completion',
action='store_true',
help='Dump bash completion file.'
' Activate with "eval '
'$({} --bash-completion)"'.format(basename(__file__)))

parser.add_argument(
"--bash-completion",
action="store_true",
help="Dump bash completion file."
' Activate with "eval '
'$({} --bash-completion)"'.format(basename(__file__)),
)
argcomplete.autocomplete(parser)
except ModuleNotFoundError:
logging.debug('argcomplete module not found, no bash completion available')
logging.debug("argcomplete module not found, no bash completion available")

args = parser.parse_args()
if 'bash_completion' in args:
if "bash_completion" in args:
if args.bash_completion:
print(argcomplete.shellcode(basename(__file__), True, 'bash'))
print(argcomplete.shellcode(basename(__file__), True, "bash"))
return vars(args)
Loading

0 comments on commit 6445f9a

Please sign in to comment.