From 985423e981e7ad80b3748ad19e32f9fecc3d3467 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 6 Sep 2019 13:35:52 +0900 Subject: [PATCH] Improved build scripts --- bots/build.sh | 12 +++- jobs/build.sh | 12 +++- makefile | 63 ++++++++++--------- patches/build.sh | 12 +++- .../animenotifier.service | 7 +-- services/build.sh | 14 +++++ services/discord.service | 13 ++++ services/imageserver.service | 0 services/japanese.service | 0 services/jobs.service | 13 ++++ 10 files changed, 103 insertions(+), 43 deletions(-) rename systemd.service => services/animenotifier.service (57%) create mode 100755 services/build.sh create mode 100644 services/discord.service create mode 100644 services/imageserver.service create mode 100644 services/japanese.service create mode 100644 services/jobs.service diff --git a/bots/build.sh b/bots/build.sh index 1d3dd214..78fd06fb 100755 --- a/bots/build.sh +++ b/bots/build.sh @@ -1,4 +1,10 @@ #!/bin/sh -MYDIR="$(dirname "$(readlink -f "$0")")" -cd "$MYDIR" -for dir in ./*; do ([ -d "$dir" ] && cd "$dir" && echo "Building bots/$dir" && go build); done \ No newline at end of file +SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" +cd "$SCRIPTPATH" + +for dir in *; do + [ -d "$SCRIPTPATH/$dir" ] && + cd "$SCRIPTPATH/$dir" && + echo "Building bots/$dir" && + go build +done \ No newline at end of file diff --git a/jobs/build.sh b/jobs/build.sh index 47cf0d48..f954f5a5 100755 --- a/jobs/build.sh +++ b/jobs/build.sh @@ -1,5 +1,11 @@ #!/bin/sh -MYDIR="$(dirname "$(readlink -f "$0")")" -cd "$MYDIR" +SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" +cd "$SCRIPTPATH" go build -for dir in ./*; do ([ -d "$dir" ] && cd "$dir" && echo "Building jobs/$dir" && go build); done \ No newline at end of file + +for dir in *; do + [ -d "$SCRIPTPATH/$dir" ] && + cd "$SCRIPTPATH/$dir" && + echo "Building jobs/$dir" && + go build +done \ No newline at end of file diff --git a/makefile b/makefile index a98584c7..e9706be8 100644 --- a/makefile +++ b/makefile @@ -4,7 +4,6 @@ GOTEST=@./utils/test/go-test-color.sh PACK=$(shell command -v pack 2> /dev/null) RUN=$(shell command -v run 2> /dev/null) -SERVICEFILE=/etc/systemd/system/animenotifier.service # Determine the name of the platform OSNAME= @@ -21,45 +20,30 @@ else endif endif -# Build targets +# builds the server executable server: @go build -v -deps: - go mod download -jobs: - @./jobs/build.sh -bots: - @./bots/build.sh -patches: - @./patches/build.sh -test: - $(GOTEST) github.com/animenotifier/notify.moe -v -cover -bench: - $(GOTEST) -bench . -pack: - go install github.com/aerogo/pack/... -run: - go install github.com/aerogo/run/... + +# installs development tools tools: ifeq ($(OSNAME),OSX) brew install coreutils endif - @make pack - @make run -service: - sudo cp systemd.service $(SERVICEFILE) - sudo sed -i "s|MAKEFILE_USER|$(USER)|g" $(SERVICEFILE) - sudo sed -i "s|MAKEFILE_PWD|$(PWD)|g" $(SERVICEFILE) - sudo sed -i "s|MAKEFILE_EXEC|$(PWD)/notify.moe|g" $(SERVICEFILE) - sudo systemctl daemon-reload - @echo -e "\nYou can now start the service using:\n\nsudo systemctl start animenotifier.service" + go install github.com/aerogo/pack/... + go install github.com/aerogo/run/... + +# compiles assets for the server assets: @tsc @pack + +# cleans all binaries and generated files clean: find . -type f | xargs file | grep "ELF.*executable" | awk -F: '{print $1}' | xargs rm find . -type f | grep /scripts/ | grep .js | xargs rm rm -rf ./components + +# forwards local ports 80 and 443 to 4000 and 4001 ports: ifeq ($(OSNAME),LINUX) @sudo iptables -t nat -A OUTPUT -o lo -p tcp --dport 80 -j REDIRECT --to-port 4000 @@ -70,6 +54,29 @@ endif ifeq ($(OSNAME),OSX) @echo "rdr pass inet proto tcp from any to any port 443 -> 127.0.0.1 port 4001" | sudo pfctl -ef - endif + +# installs systemd service files for all required services +services: + @./services/build.sh + +# builds all background jobs +jobs: + @./jobs/build.sh + +# builds all bots +bots: + @./bots/build.sh + +# builds all patches +patches: + @./patches/build.sh + +test: + $(GOTEST) github.com/animenotifier/notify.moe -v -cover + +bench: + $(GOTEST) -run=^$ -bench . + all: tools assets server bots jobs patches -.PHONY: tools assets server bots jobs patches ports clean versions +.PHONY: tools assets server bots jobs patches services ports clean diff --git a/patches/build.sh b/patches/build.sh index 7bd5ae41..01c023e7 100755 --- a/patches/build.sh +++ b/patches/build.sh @@ -1,4 +1,10 @@ #!/bin/sh -MYDIR="$(dirname "$(readlink -f "$0")")" -cd "$MYDIR" -for dir in ./*; do ([ -d "$dir" ] && cd "$dir" && echo "Building patches/$dir" && go build); done \ No newline at end of file +SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" +cd "$SCRIPTPATH" + +for dir in *; do + [ -d "$SCRIPTPATH/$dir" ] && + cd "$SCRIPTPATH/$dir" && + echo "Building patches/$dir" && + go build +done \ No newline at end of file diff --git a/systemd.service b/services/animenotifier.service similarity index 57% rename from systemd.service rename to services/animenotifier.service index 6b63e3e3..be1d3849 100644 --- a/systemd.service +++ b/services/animenotifier.service @@ -5,14 +5,9 @@ After=network.target [Service] Type=simple User=MAKEFILE_USER -ExecStart=MAKEFILE_EXEC +ExecStart=MAKEFILE_PWD/notify.moe WorkingDirectory=MAKEFILE_PWD Restart=on-failure -#Nice=-19 -#CPUSchedulingPolicy=fifo -#CPUSchedulingPriority=99 -#IOSchedulingClass=realtime -#IOSchedulingPriority=0 [Install] WantedBy=multi-user.target \ No newline at end of file diff --git a/services/build.sh b/services/build.sh new file mode 100755 index 00000000..a7488e83 --- /dev/null +++ b/services/build.sh @@ -0,0 +1,14 @@ +#!/bin/sh +INSTALLPATH="/etc/systemd/system/" +cd $(dirname $0) + +for service in *; do + [ -f "$service" ] && + echo "Installing services/$service" && + sudo cp "services/$service" "$INSTALLPATH/$service" && + sudo sed -i "s|MAKEFILE_USER|$USER|g" "$INSTALLPATH/$service" && + sudo sed -i "s|MAKEFILE_PWD|$PWD|g" "$INSTALLPATH/$service" +done + +sudo systemctl daemon-reload +echo -e "\nYou can now start the service using:\n\nsudo systemctl start animenotifier.service" \ No newline at end of file diff --git a/services/discord.service b/services/discord.service new file mode 100644 index 00000000..14cbd971 --- /dev/null +++ b/services/discord.service @@ -0,0 +1,13 @@ +[Unit] +Description=Discord Bot +After=animenotifier.service + +[Service] +Type=simple +User=MAKEFILE_USER +ExecStart=MAKEFILE_PWD/bots/discord/discord +WorkingDirectory=MAKEFILE_PWD +Restart=on-failure + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/services/imageserver.service b/services/imageserver.service new file mode 100644 index 00000000..e69de29b diff --git a/services/japanese.service b/services/japanese.service new file mode 100644 index 00000000..e69de29b diff --git a/services/jobs.service b/services/jobs.service new file mode 100644 index 00000000..6536aa0d --- /dev/null +++ b/services/jobs.service @@ -0,0 +1,13 @@ +[Unit] +Description=Background Jobs +After=animenotifier.service + +[Service] +Type=simple +User=MAKEFILE_USER +ExecStart=MAKEFILE_PWD/jobs/jobs +WorkingDirectory=MAKEFILE_PWD +Restart=on-failure + +[Install] +WantedBy=multi-user.target \ No newline at end of file