Skip to content

Commit

Permalink
Fix #90: Initial work for kapitan init
Browse files Browse the repository at this point in the history
  • Loading branch information
uberspot committed Feb 18, 2019
1 parent a446a8b commit 3aa3884
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ Targets can also be defined inside the `inventory`.

# Usage

Use `kapitan init --init-directory <directory>` to populate the
directory with the recommended kapitan folder structure in new projects.

For other available options use:

```
$ kapitan -h
usage: kapitan [-h] [--version]
Expand All @@ -272,14 +277,16 @@ Generic templated configuration management for Kubernetes, Terraform and other
things
positional arguments:
{eval,compile,inventory,searchvar,secrets,lint}
{eval,compile,inventory,searchvar,secrets,lint,init}
commands
eval evaluate jsonnet file
compile compile targets
inventory show inventory
searchvar show all inventory files where var is declared
secrets manage secrets
lint linter for inventory and secrets
init initialize a directory with the recommended kapitan
project skeleton.
optional arguments:
-h, --help show this help message and exit
Expand Down
9 changes: 9 additions & 0 deletions kapitan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from kapitan.resources import search_imports, resource_callbacks, inventory_reclass
from kapitan.version import PROJECT_NAME, DESCRIPTION, VERSION
from kapitan.lint import start_lint
from kapitan.initialiser import initialise_skeleton

from kapitan.refs.base import RefController, Revealer
from kapitan.refs.secrets.gpg import GPGSecret
Expand Down Expand Up @@ -210,6 +211,11 @@ def main():
default=from_dot_kapitan('lint', 'inventory-path', './inventory'),
help='set inventory path, default is "./inventory"')

init_parser = subparser.add_parser('init', help='initialize a directory with the recommended kapitan project skeleton.')
init_parser.add_argument('--init-directory',
default=from_dot_kapitan('init', 'init-directory', '.'),
help='set path, in which to generate the project skeleton, assumes directory already exists. default is "./"')

args = parser.parse_args()

logger.debug('Running with args: %s', args)
Expand Down Expand Up @@ -287,6 +293,9 @@ def _search_imports(cwd, imp):
elif cmd == 'lint':
start_lint(args.fail_on_warning, args.skip_class_checks, args.inventory_path, args.search_secrets, args.secrets_path, args.compiled_path)

elif cmd == 'init':
initialise_skeleton(args.init_directory)

elif cmd == 'secrets':
ref_controller = RefController(args.secrets_path)

Expand Down
51 changes: 51 additions & 0 deletions kapitan/initialiser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
#
# Copyright 2019 The Kapitan Authors
#
# 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.

"initialiser module"

import logging
import os
import sys

logger = logging.getLogger(__name__)


def initialise_skeleton(init_directory):
""" Initialises a directory with a recommended skeleton structure
Args:
init_directory (string): path which to initialise, directory is assumed to exist
"""
if not os.path.isdir(init_directory):
logger.info("Provided path is not a directory, or doesn't exist. Exiting...")
sys.exit(1)

dir_structure = [
"compiled",
"components",
"templates",
"templates/scripts",
"templates/docs",
"inventory",
"inventory/classes",
"inventory/targets",
"lib",
"secrets",
]

for path in dir_structure:
computed_path = os.path.join(init_directory, path)
logger.info("Creating {}".format(computed_path))
os.makedirs(computed_path, exist_ok=True)

0 comments on commit 3aa3884

Please sign in to comment.