diff --git a/.gitignore b/.gitignore index f0ed4b8..32b37ef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ .env *.deb +*.snap +parts/ +stage/ +snap/ +packages/ \ No newline at end of file diff --git a/DEBIAN/control b/DEBIAN/control index 97c3cd3..24ba033 100644 --- a/DEBIAN/control +++ b/DEBIAN/control @@ -1,5 +1,11 @@ Package: shreyam-do -Version: 1.0 +Version: 1.0.1 Architecture: all Maintainer: Shreyam Maity Description: A package that contains my personal server setup scripts +Homepage: https://shreyam.dev +Source: shreyam-do +Section: misc +Priority: optional +Depends: make +Pre-Depends: make diff --git a/DEBIAN/postinst b/DEBIAN/postinst index 408477f..186e09e 100644 --- a/DEBIAN/postinst +++ b/DEBIAN/postinst @@ -2,14 +2,6 @@ set -e -if [ "$1" = "configure" ]; then - # Check if make is installed - if ! dpkg -s make >/dev/null 2>&1; then - # Install make - apt-get update - apt-get install -y make - fi +chmod +x /usr/bin/shreyam-do - # Run make - make -f /usr/share/shreyam-do/Makefile $2 -fi +exit 0 diff --git a/Dockerfile b/Dockerfile index ca81f40..f9e68b2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,24 +1,15 @@ FROM ubuntu:22.04 -RUN apt-get update && \ - apt-get clean && \ - # apt-get install -y snapcraft dpkg-dev build-essential && \ - rm -rf /var/lib/apt/lists/* - WORKDIR /pkg COPY . . ARG PACKAGE_NAME ARG PACKAGE_VERSION +ENV SNAPCRAFT_DISABLE_REMOTE_PARTS=true RUN chmod 755 /pkg/DEBIAN/postinst RUN mkdir -p /packages && \ - dpkg-deb --build /pkg /packages/${PACKAGE_NAME}_${PACKAGE_VERSION}.deb - # && \ - # cd /pkg && \ - # snapcraft + dpkg-deb --build /pkg /packages/${PACKAGE_NAME}_${PACKAGE_VERSION}.deb -CMD cp /packages/*.deb /build -# && \ -# cp /pkg/*.snap /build +CMD cp /packages/*.deb /build diff --git a/Makefile b/Makefile index 7c0348a..0fa16ea 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,13 @@ up: - docker-compose --env-file=.env up -d --build \ No newline at end of file + docker-compose --env-file=.env up -d --build + docker-compose down +test: + docker-compose --profile test --env-file=.env up -d --build +version: + @if [ -z "$(word 2, $(MAKECMDGOALS))" ]; then echo "Usage: make version "; exit 1; fi + @if [ ! -f DEBIAN/control ]; then echo "Error: DEBIAN/control does not exist"; exit 1; fi + @echo "Current version: $(grep -oP 'Version: \K.*' DEBIAN/control | cut -d' ' -f1)" + @echo "Setting version $(word 2, $(MAKECMDGOALS))" + @sed -i "s/Version:.*/Version: $(word 2, $(MAKECMDGOALS))/" DEBIAN/control + @echo "New version: $(grep -oP 'Version: \K.*' DEBIAN/control | cut -d' ' -f1)" + @echo "Done" \ No newline at end of file diff --git a/README.md b/README.md index 26de575..f1d708f 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,30 @@ ## `TLDR:` This tool is for me and it might not be useful for you. But if you want to use it, you can. +--- + +### Options +```dotnetcli +shreyam-do - Shreyam's setup scripts + +Usage: shreyam-do [options] + +Options: + [command] Run a specific setup script + -h, --help Display this help message + -v, --version Display version information + -s, --setup Set up the system with all the required packages + -r, --remove Remove all installed packages +``` +--- +### How to use + +- `shreyam-do -s` - installs all the required packages on a fresh vm and configure with npm, zsh, autosuggestions etc. +- `shreyam-do -r` - removes all the packages installed by the tool +- `shreyam-do -v` - displays the version of the tool +- `shreyam-do -h` - displays the help message and the options available +- `shreyam-do ` - runs the specific command + --- ### Commands Available @@ -24,4 +48,17 @@ - `shreyam-do ssl-nginx ` - generates ssl certificate for the domain from letsencrypt and automatically configures nginx to serve the domain with ssl - `shreyam-do ssl-cert ` - generates ssl certificate for the domain from letsencrypt and saves it in `/etc/letsencrypt/live/` - `shreyam-do open-ports` - opens necessary ports for nginx on firewall -- `shreyam-do symLink ` - creates a symlink of the conf file in nginx \ No newline at end of file +- `shreyam-do symLink ` - creates a symlink of the conf file in nginx + +--- +### How to contribute +- Create an issue if you find any bug or want to suggest a feature +- Fork the repo and create a PR if you want to contribute + +--- +### License +#### Apache License 2.0 +--- +### Author +#### [Shreyam Maity](https://shreyam.dev) +--- \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index b278736..925aaf0 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,6 +1,6 @@ version: "3" services: - build: + shreyam-do: build: context: . args: @@ -8,3 +8,28 @@ services: PACKAGE_VERSION: ${VERSION:-1.0.0} volumes: - ./build:/build + tests: + image: ubuntu:22.04 + volumes: + - ./build:/build + environment: + - LC_ALL=C.UTF-8 + - LANG=C.UTF-8 + - PACKAGE_NAME=${PACKAGE_NAME:-shreyam-do} + - VERSION=${VERSION:-1.0.0} + working_dir: /tmp + depends_on: + - shreyam-do + profiles: + - test + command: sh -c ' + set -e; + apt-get update; + apt-get install -y make; + cd /build; + ls -l; + dpkg -i /build/${PACKAGE_NAME}_${VERSION}.deb; + shreyam-do -v; + shreyam-do -h; + shreyam-do test; + shreyam-do' diff --git a/serve/install.sh b/serve/install.sh index 2e2ecf6..53a72dc 100644 --- a/serve/install.sh +++ b/serve/install.sh @@ -16,6 +16,12 @@ PACKAGE_VERSION=${TAG#"v"} # Download the package curl -sSLO "https://github.com/${GITHUB_USERNAME}/${GITHUB_REPO}/releases/download/${TAG}/${PACKAGE_NAME}_${PACKAGE_VERSION}.deb" +# Install dependencies +if ! dpkg-query -W -f='${Status}' make 2>/dev/null | grep -c "ok installed" >/dev/null; then + sudo apt-get -y update && sudo apt-get -y install make +fi + + # Install the package sudo dpkg -i ${PACKAGE_NAME}_${PACKAGE_VERSION}.deb diff --git a/snapcraft.yaml b/snapcraft.yaml index 0c26f9a..ecd198b 100644 --- a/snapcraft.yaml +++ b/snapcraft.yaml @@ -27,12 +27,8 @@ parts: stage-packages: - make make: - plugin: nil + plugin: autotools source: https://ftp.gnu.org/gnu/make/make-4.3.tar.gz source-type: tar build-packages: - gettext - override-build: | - ./configure - make - make install diff --git a/usr/bin/shreyam-do b/usr/bin/shreyam-do index 3b09626..0e51901 100644 --- a/usr/bin/shreyam-do +++ b/usr/bin/shreyam-do @@ -1,3 +1,67 @@ #!/bin/bash -make -f /usr/share/shreyam-do/Makefile "$@" +set -e + +function print_usage { + make -s -f /usr/share/shreyam-do/Makefile help +} + +function print_version { + echo "shreyam-do version 1.0.1" +} + +function setup_all { + echo "Setting up all packages" + sudo apt-get update + sudo apt-get install -y git + sudo apt-get install -y vim + sudo apt-get install -y curl + sudo apt-get install -y zsh + sudo apt-get install -y tmux + sudo apt-get install -y npm + chsh -s $(which zsh) + which $SHELL + /usr/bin/zsh + git clone https://github.com/zsh-users/zsh-autosuggestions ~/.zsh/zsh-autosuggestions + source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh + echo "Setup complete" +} + +if [ $# -eq 0 ]; then + print_usage + exit 1 +fi + +while [[ $# -gt 0 ]]; do + key="$1" + + case $key in + -h | --help) + print_usage + exit 0 + ;; + -v | --version) + print_version + exit 0 + ;; + -s | --setup) + setup_all + exit 0 + ;; + -r | --remove) + echo "Removing all installed packages" + sudo apt-get remove -y tmux + sudo apt-get remove -y npm + exit 0 + ;; + *) + # if the argument doesn't match any flag, execute the make command + make -s -f /usr/share/shreyam-do/Makefile "$@" + exit 0 + ;; + esac + shift +done + +print_usage +exit 1 diff --git a/usr/share/shreyam-do/Makefile b/usr/share/shreyam-do/Makefile index 7c12307..7a7d6f2 100644 --- a/usr/share/shreyam-do/Makefile +++ b/usr/share/shreyam-do/Makefile @@ -1,5 +1,32 @@ -#setts up system env for docker +.DEFAULT_GOAL := help + +.PHONY: help + +help: + @#Help: prints this help message + @echo "shreyam-do - Shreyam's setup scripts" + @echo "Usage: shreyam-do [options]" + @echo "Options:" + @echo " [command] Run a specific setup script" + @echo " -h, --help Display this help message" + @echo " -v, --version Display version information" + @echo " -s, --setup Set up the system with all the required packages" + @echo " -r, --remove Remove all installed packages" + @echo "Available commands:" + @printf "%-20s %s\n" "Commands" "Description" + @printf "%-20s %s\n" "------" "-----------" + @awk -F'[ :]' '!/^all:/ && /^([A-z_-]+):/ {print $$1}' $(lastword $(MAKEFILE_LIST)) \ + | sort \ + | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' \ + | xargs -I _ sh -c 'printf "%-20s " _; make -f $(lastword $(MAKEFILE_LIST)) _ -nB | (grep -i "^# Help:" || echo "") | tail -1 | sed "s/^# Help: //g"' + +%: + @echo "Error: Command $@ not available." + @echo "Run 'shreyam-do -h' to see available commands." + +#sets up system env for docker doker-setup: + @# Help: sets up system env for docker echo "Setting up env" && \ sudo apt-get update && \ curl -sSL https://get.docker.com/ | sh && \ @@ -21,6 +48,7 @@ doker-setup: #enables overcommit which helps with memory issues on virtual machines enable-overcommit: + @# Help: enables overcommit which helps with memory issues on virtual machines echo "Enabling overcommit" && \ sudo sysctl vm.overcommit_memory=1 && \ echo "vm.overcommit_memory = 1" | sudo tee -a /etc/sysctl.conf && \ @@ -28,18 +56,21 @@ enable-overcommit: #installs certbot and obtains ssl certificates for the domain names passed as arguments ssl-cert: + @# Help: installs certbot and obtains ssl certificates for the domain names passed as arguments echo "Setting up ssl using certbot for $(subst ssl-cert,,$(MAKECMDGOALS))" && \ sudo apt-get update && \ sudo snap install --classic certbot && \ sudo certbot certonly $(addprefix -d ,$(subst ssl-cert,,$(MAKECMDGOALS))) && \ echo "SSL setup done for $(subst ssl-cert,,$(MAKECMDGOALS)) and certificated stored in /etc/letsencrypt/live/$(subst ssl-cert,,$(MAKECMDGOALS))" -#fixes sentry issues on memory profiler +##fixes sentry issues on memory profiler sentry-fix: + @# Help: fixes sentry issues on memory profiler sudo apt-get update && sudo apt-get upgrade libstdc++6 -y #opens ports for http, https open-ports: + @# Help: opens ports for http, https echo "Opening ports" && \ sudo apt-get update && \ sudo apt install -y firewalld && \ @@ -51,11 +82,11 @@ open-ports: sudo firewall-cmd --zone=public --permanent --add-port=443/tcp && \ sudo firewall-cmd --reload && \ sudo firewall-cmd --list-all && \ - sudo systemctl restart docker && \ echo "Ports opened" #installs certbot and obtains ssl certificates for the domain names passed as arguments for nginx ssl-nginx: + @# Help: installs certbot and obtains ssl certificates for the domain names passed as arguments for nginx echo "Setting up ssl using certbot for $(subst ssl-nginx,,$(MAKECMDGOALS))" && \ sudo apt-get update && \ sudo snap install --classic certbot && \ @@ -65,6 +96,7 @@ ssl-nginx: #installs nginx nginx-install: + @# Help: installs nginx echo "Installing nginx" && \ sudo apt-get update && \ sudo apt-get install nginx -y && \ @@ -72,6 +104,7 @@ nginx-install: #installs fpm a dpkg package manager for non-Debian systems fpm-install: + @# Help: installs fpm a dpkg package manager for non-Debian systems echo "Installing fpm" && \ sudo gem install fpm && \ brew install rpm squashfs && \ @@ -80,6 +113,7 @@ fpm-install: #builds dpkg package for the package name and version passed as arguments build-dpkg: + @# Help: builds dpkg package for the package name and version passed as arguments @if [ -z "$(word 2, $(MAKECMDGOALS))" ] || [ -z "$(word 3, $(MAKECMDGOALS))" ]; then \ echo "Usage: make build-dpkg " ; \ else \ @@ -88,8 +122,9 @@ build-dpkg: echo "Dpkg built" ; \ fi -# creates a symlink for the file name passed as argument in nginx sites-enabled +#creates a symlink for the file name passed as argument in nginx sites-enabled symLink: + @# Help: creates a symlink for the file name passed as argument in nginx sites-enabled @if [ -z "$(word 2, $(MAKECMDGOALS))" ]; then \ echo "Usage: make symLink " ; \ else \