workshops/slides/makefiles/PS/general_make.md

120 lines
2.3 KiB
Markdown
Raw Permalink Normal View History

2024-11-18 17:03:49 +00:00
# Generalizing Makefiles for All Occasions
- Rules are bad,
- standards are good,
- habits are better.
***
# General Standards
- `make help`
- `make check`
- `sudo make install`
**NB:** we separate `make` from `make install` so that the files are not build with the user `root` as the owner.
***
# Service Standards
- `make backup.tgz`
- `make install`
## Service Backups in Two Files
- `configs`: configuration files, all tracked in git.
- `store`: passwords and binary files, not tracked in git.
## New Abilities Unlocked
- Open Access Administration.
- Sharing admin configs with hacker spaces.
***
## BEHOLD: The Everything Makefile
***
```make
CONFIGS = $(patsubst /%,%,$(shell cat configs))
STORE = $(patsubst /%,%,$(shell cat store))
CP = mkdir -p $(dir $@) && cp -a
IGNORE_FILE = $(shell test -d .git/info && echo .git/info/exclude || echo .gitignore)
GIT_COMMIT = git commit -m"add $@" --no-gpg-sign --no-signoff
SELECTOR != command -v sk || command -v fzy || command -f fzf
.PHONY: all
all: init backup.tgz $(IGNORE_FILE)
backup.tgz: $(CONFIGS) $(STORE)
tar czf $@ $^
$(IGNORE_FILE): store
echo $(STORE) backup.tgz | tr ' ' '\n' > $@
.PHONY: init
init: configs store
configs store:
while con="$$(find /var /etc/ /sys/ -maxdepth 2 -mindepth 1 -type f 2>/dev/null | $(SELECTOR) -p "Select files for $@\nPress Ctrl+d once done")"; do \
echo "$$con"; \
done > $@
$(CONFIGS): %: /%
$(CP) $< $@
git add $@
$(GIT_COMMIT)
$(info made git commit for $@)
$(STORE): %: /%
$(CP) $< $@
clean:
$(RM) -r $(CONFIGS) $(STORE) backup.tgz
```
***
## Example: Soft Serve Makefile
<br>
The `configs` file:
```
/etc/soft-serve.conf
/var/lib/soft-serve/data/config.yaml
/var/lib/soft-serve/data/file
/etc/nginx/sites-enabled/soft.dmz.rs
/etc/nginx/sites-available/soft.dmz.rs
```
<br>
The `store` file:
```
/var/lib/soft-serve/data/lfs
/var/lib/soft-serve/data/repo
/var/lib/soft-serve/data/soft
/etc/letsencrypt/archive/soft
/etc/letsencrypt/live/soft.dm
/var/lib/soft-serve/data/ssh/
```
***
# Other Build Systems
> "*What if I want to use this other thing, instead of a makefile?"
***
# Other Build Systems (Sans Heresy)
> "*What if I want to use this other thing, instead of a makefile?"
- Easy: just use a Makefile.
```make
[ ... ]
public/: src/
python build_pages.py
```