Browse Source

[ADD] cruft & cookiecutter

Signed-off-by: Justin Lecher <justin@jlec.de>
master
Justin Lecher 3 days ago
parent
commit
090b63ed97
Signed by: justin GPG Key ID: E9402A79B03529A2
  1. 15
      .cruft.json
  2. 4
      .pre-commit-config.yaml
  3. 38
      .yamllint
  4. 31
      Makefile
  5. 15
      README.md
  6. 20
      hooks/post_gen_project.py
  7. 6
      hooks/pre_gen_project.py
  8. 15
      tests/test_bake_project.py
  9. 8
      {{ cookiecutter.role_name }}/meta/main.yml
  10. 2
      {{ cookiecutter.role_name }}/molecule/default/converge.yml
  11. 1
      {{ cookiecutter.role_name }}/tests/inventory
  12. 2
      {{ cookiecutter.role_name }}/tests/test.yml

15
.cruft.json

@ -0,0 +1,15 @@
{
"template": "git@git.ercpe.de:justin/cookiecutter-cookie.git",
"commit": "f0580cea84d52371582980b2bd3a46089009aadb",
"context": {
"cookiecutter": {
"full_name": "Justin Lecher",
"email": "justin@jlec.de",
"project_name": "Ansible Role Cookiecutter",
"project_slug": "cookiecutter_ansible_role",
"project_short_description": "Cookiecutter for Ansible roles",
"_template": "git@git.ercpe.de:justin/cookiecutter-cookie.git"
}
},
"directory": null
}

4
.pre-commit-config.yaml

@ -24,3 +24,7 @@ repos:
hooks:
- id: black
args: ["--line-length", "128"]
- repo: https://github.com/jlec/pre-commit-cruft
rev: v0.1.1
hooks:
- id: cruft

38
.yamllint

@ -0,0 +1,38 @@
---
# Based on ansible-lint config
extends: default
yaml-files:
- '*.yaml'
- '*.yml'
rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
colons:
max-spaces-after: -1
level: error
commas:
max-spaces-after: -1
level: error
comments: disable
comments-indentation: disable
document-start: disable
empty-lines:
max: 3
level: error
hyphens:
level: error
indentation: disable
key-duplicates: enable
new-line-at-end-of-file: disable
new-lines:
type: unix
trailing-spaces: disable
truthy: disable
line-length:
max: 128

31
Makefile

@ -54,19 +54,35 @@ publish:
git push -v --progress
git push -v --progress --tags
################################################################################
#
# Cookies options
#
################################################################################
.PHONY: cruft-update
cruft-update: setup-dev.done
git stash
$(PIPENV) cruft update --allow-untracked-files
git stash pop
################################################################################
#
# Checking and Linting
#
################################################################################
.PHONY: check lint linting pytest test
.PHONY: check cruft-check lint linting pytest test
check: lint test
check: cruft-check lint test
test: pytest
cruft-check: setup-dev.done
$(PIPENV) cruft check
$(PIPENV) cruft diff
test: cruft-check pytest
pytest: setup-dev.done
$(PIPENV) pytest ${PYTESTARGS} .
$(PIPENV) pytest ${PYTESTARGS} tests
pre-commit: setup-dev.done
$(PIPENV) pre-commit run --all-files
@ -79,12 +95,7 @@ linting: pre-commit
# Custom options
#
################################################################################
.PHONY: clean cruft-update
cruft-update: setup-dev.done
git stash
$(PIPENV) cruft update --allow-untracked-files
git stash pop
.PHONY: clean
clean:
rm -rvf tmp setup*done

15
README.md

@ -1,3 +1,14 @@
# cookiecutter-ansible-role
# cookiecutter_ansible_collection
=========
Create your ansible role from it
Cookiecutter for Ansible collections
License
-------
Apache-2.0
Author Information
------------------
* Justin Lecher <justin@jlec.de>

20
hooks/post_gen_project.py

@ -5,6 +5,15 @@ import shutil
PROJECT_DIRECTORY = os.path.realpath(os.path.curdir)
PROJECT_FOLDERS = (
"defaults",
"files",
"handlers",
"meta",
"tasks",
"templates",
"vars",
)
def create_folder(filepath):
@ -22,16 +31,7 @@ def remove_file_or_folder(filepath):
def gen_role_folders():
ansible_role_folders = (
"defaults",
"files",
"handlers",
"meta",
"tasks",
"templates",
"vars",
)
for d in ansible_role_folders:
for d in PROJECT_FOLDERS:
create_folder(d)

6
hooks/pre_gen_project.py

@ -5,10 +5,10 @@ import sys
ROLE_REGEX = r"^[_a-zA-Z][_a-zA-Z0-9]+$"
role_name = "{{ cookiecutter.role_name }}"
ANSIBLE_ROLE = "{{ cookiecutter.role_name }}"
if not re.match(ROLE_REGEX, role_name):
print("ERROR: %s is not a valid Python module name!" % role_name)
if not re.match(ROLE_REGEX, ANSIBLE_ROLE):
print(f"ERROR: '{ANSIBLE_ROLE}' is not a valid Python module name!")
# exits with status 1 to indicate failure
sys.exit(1)

15
tests/test_bake_project.py

@ -10,7 +10,9 @@ from contextlib import contextmanager
from cookiecutter.utils import rmtree
ANSIBLE_ROLE_DIRS = (
FILES = (".gitignore", ".pre-commit-config.yaml", ".yamllint", "Makefile", "Pipfile", "README.md")
DIRS = (
"defaults",
"files",
"handlers",
@ -21,7 +23,6 @@ ANSIBLE_ROLE_DIRS = (
"templates",
"vars",
)
ANSIBLE_ROLE_FILES = (".yamllint", "Makefile", "README.md")
@contextmanager
@ -68,7 +69,7 @@ def check_output_inside_dir(command, dirpath):
return subprocess.check_output(shlex.split(command))
def check_project_content(result, files, dirs):
def check_project_content(result, files=(), dirs=()):
assert result.exit_code == 0
found_toplevel_files = [f.basename for f in result.project.listdir()]
@ -112,13 +113,11 @@ def test_make_help(cookies):
assert b"Make targets" in output
def test_bake_role_invalid_name(cookies):
# with pytest.raises(ZeroDivisionError):
# bake_in_temp_dir(cookies, extra_context={"role_name": "foo-bar"})
def test_bake_invalid_name(cookies):
bake_in_temp_dir(cookies, extra_context={"role_name": "foo-bar"})
def test_bake_role(cookies):
def test_bake(cookies):
with bake_in_temp_dir(cookies) as result:
assert result.exit_code == 0
check_project_content(result, ANSIBLE_ROLE_FILES, ANSIBLE_ROLE_DIRS)
check_project_content(result, FILES, DIRS)

8
{{ cookiecutter.role_name }}/meta/main.yml

@ -1,10 +1,10 @@
---
galaxy_info:
author: {{ cookiecutter.full_name }}
description: {{ cookiecutter.role_short_description }}
author: "{{ cookiecutter.full_name }}"
description: "{{ cookiecutter.role_short_description }}"
role_name: {{ cookiecutter.role_name }}
namespace: {{ cookiecutter.role_namespace }}
role_name: "{{ cookiecutter.role_name }}"
namespace: "{{ cookiecutter.role_namespace }}"
license: Apache-2.0
min_ansible_version: 2.1

2
{{ cookiecutter.role_name }}/molecule/default/converge.yml

@ -2,7 +2,7 @@
- name: Converge
hosts: all
collections:
- {{ cookiecutter.ansible_namespace }}.{{ cookiecutter.ansible_collection }}
- "{{ cookiecutter.ansible_namespace }}.{{ cookiecutter.ansible_collection }}"
tasks:
- name: "Include {{ cookiecutter.role_name }} role"
include_role:

1
{{ cookiecutter.role_name }}/tests/inventory

@ -1,2 +1 @@
localhost

2
{{ cookiecutter.role_name }}/tests/test.yml

@ -2,6 +2,6 @@
- hosts: localhost
remote_user: root
collections:
- {{ cookiecutter.ansible_namespace }}.{{ cookiecutter.ansible_collection }}
- "{{ cookiecutter.ansible_namespace }}.{{ cookiecutter.ansible_collection }}"
roles:
- role: "{{ cookiecutter.role_name }}"
Loading…
Cancel
Save