Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Goryudyuma authored Sep 8, 2023
0 parents commit b27b424
Show file tree
Hide file tree
Showing 13 changed files with 2,311 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/on-created.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: On Created
on:
push:
branches:
- master
jobs:
on-created:
name: on-created
runs-on: ubuntu-latest
if: ${{ github.repository != 'bgpat/isucon-boilerplate' }}
steps:
- uses: actions/checkout@v1
- name: config git
run: |
git config user.name $GITHUB_ACTOR
git config user.email $GITHUB_ACTOR@users.noreply.github.com
git remote set-url origin "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git checkout master
- name: replace
run: |
sed -i "s/ isucon-boilerplate/ ${GITHUB_REPOSITORY#*/}/" README.md
sed -i "s!\\\$GITHUB_REPOSITORY!${GITHUB_REPOSITORY}!" README.md
sed -i 's/create a new repository/~\0~ already done/' README.md
- name: commit
run: git commit -am 'Replace $GITHUB_REPOSITORY'
- name: delete this script
run: |
rm -f .github/workflows/on-created.yml
git commit -am 'Delete on-created workflow'
- name: push
run: git push -f origin master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50 changes: 50 additions & 0 deletions .github/workflows/update-hosts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Update Hosts
on:
workflow_dispatch:
push:
branches:
- master
paths:
- files/hosts/*
jobs:
update-hosts:
name: update-hosts
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: generate config for prometheus
run: |
jq -sR '
[
.
| split("\n")[]
| split(" ")
| select(length == 2)
| {
targets: [
"\(.[1]):9100",
"\(.[1]):9256"
],
labels: {
hostname: .[0],
ip: .[1]
}
}
]
' files/hosts/* > etc/prometheus/hosts.json
- name: config git
run: |
git config user.name $GITHUB_ACTOR
git config user.email $GITHUB_ACTOR@users.noreply.github.com
git remote set-url origin "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git checkout master
- name: git commit
run: |
git add -f etc/prometheus/hosts.json
git commit -m 'Update config for prometheus'
- name: git push
run: |
git pull origin master
git push origin master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
73 changes: 73 additions & 0 deletions .github/workflows/update-sshkeys.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Update SSH Keys
on:
workflow_dispatch:
push:
branches:
- master
paths:
- files/pubkey/*
jobs:
update-sshkeys:
name: update-sshkeys
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: ensure ssh directory
run: mkdir -p root/.ssh
- name: replace ssh keys
uses: actions/github-script@v3
with:
script: |
require("fs").writeFileSync(
"root/.ssh/authorized_keys",
[
...(await Promise.all(
await github.repos.listCollaborators(context.repo).then((resp) =>
resp.data.map((c) =>
github.users
.getByUsername({ username: c.login })
.then((resp) => ({
...c,
email: resp.data.email || `${c.login}@users.noreply.github.com`,
}))
.then((user) =>
github.users
.listPublicKeysForUser({ username: user.login })
.then((resp) =>
resp.data.map(({ key }) =>
[
Object.entries({
GIT_AUTHOR_NAME: user.login,
GIT_AUTHOR_EMAIL: user.email,
})
.map(([k, v]) => `environment="${k}=${v}"`)
.join(","),
key,
].join(" ")
)
)
)
)
)
).then((users) => [].concat(...users))),
"",
].join("\n")
);
- name: add host keys
run: cat files/pubkey/* >> root/.ssh/authorized_keys || true
- name: config git
run: |
git config user.name $GITHUB_ACTOR
git config user.email $GITHUB_ACTOR@users.noreply.github.com
git remote set-url origin "https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git checkout master
- name: git commit
run: |
git add -f root/.ssh/authorized_keys
git commit -m 'Update ssh keys'
- name: git push
run: |
git pull origin master
git push origin master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Atsushi Tanaka

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
171 changes: 171 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
HOSTNAME := $(shell hostname)

.PHONY: all
all: git ssh commit /home/isucon/.bashrc /files/hosts/$(HOSTNAME)
@echo "\n\n\n\n"
@echo "Open \e[4mhttps://github.com/$$(\
git config --get remote.origin.url | sed -r 's/^.*?:(.*)\.git$$/\1/' \
)/settings/keys/new\e[0m and paste pubkey:\n"
@cat /root/.ssh/id_rsa.pub
@echo

.PHONY: commit
commit:
git add -A
git commit -m "Configure $(HOSTNAME)"

.PHONY: git
git: /.gitignore /usr/local/bin/git-preserve-permissions /root/.vimrc /home/isucon/.vimrc
git config --get remote.origin.url \
| grep '^https://' \
&& git config --get remote.origin.url \
| sed -r 's%^https://github.com/([a-zA-Z0-9_-]*/[a-zA-Z0-9_-]*).*$$%git@github.com:\1.git%' \
| xargs git remote set-url origin \
|| true

.PHONY: ssh
ssh: /root/.ssh/authorized_keys /files/pubkey/$(HOSTNAME) sshd

.PHONY: sshd
sshd: /etc/sudoers /etc/ssh/sshd_config

.PHONY: /etc/sudoers
/etc/sudoers: /etc/sudoers.bak
echo 'Defaults env_keep+="GIT_AUTHOR_NAME"' >> $@
echo 'Defaults env_keep+="GIT_AUTHOR_EMAIL"' >> $@

/etc/sudoers.bak:
cp -p /etc/sudoers $@

.PHONY: /etc/ssh/sshd_config
/etc/ssh/sshd_config: /etc/ssh/sshd_config.bak ssh_host_key
sed -i '/^PermitRootLogin no/d' $@
echo 'PermitUserEnvironment yes' >> $@
echo 'PermitRootLogin without-password' >> $@
sshd -t
systemctl reload sshd

/etc/ssh/sshd_config.bak:
cp -p /etc/ssh/sshd_config $@

.PHONY: clean
clean:
rm -rf /root/.ssh /.gitignore /root/.vimrc /home/isucon/.vimrc /root/.ssh/id_rsa /root/.gitconfig
test -e /etc/sudoers.bak && cp -fp /etc/sudoers.bak /etc/sudoers
test -e /etc/ssh/sshd_config.bak && cp -fp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config

/root/.ssh:
mkdir -p /root/.ssh
chmod 700 /root/.ssh

/root/.ssh/authorized_keys: /root/.ssh
touch $@
chmod 600 $@
git add -f $@

/.gitignore: files/gitignore
cp -n $< $@

/root/.vimrc: files/vimrc
cp -f $< $@

/home/isucon/.vimrc: files/vimrc
cp -f $< $@

/root/.ssh/id_rsa: /root/.ssh
yes | ssh-keygen -f $@ -t rsa -N "" -b 4096 > /dev/null

/root/.ssh/id_rsa.pub: /root/.ssh/id_rsa

/files/pubkey/$(HOSTNAME): /root/.ssh/id_rsa.pub
mkdir -p /files/pubkey
cp -f $< $@
git add -f $@

/root/.gitconfig:
git config --global user.email "anonymous@example.com"
git config --global user.name "anonymous"
git config --global preserve-permissions.user "true"
git config --global preserve-permissions.group "true"

.PHONY: ssh_host_key
ssh_host_key:
ssh-keygen -A

/usr/local/bin/git-preserve-permissions: /git-preserve-permissions /root/.gitconfig
cp -f $</git-preserve-permissions $@
cp -f $</post-checkout $</post-merge $</pre-commit /.git/hooks/
-git preserve-permissions --save

/git-preserve-permissions:
git clone --depth=1 https://github.com/dr4Ke/git-preserve-permissions.git

/home/isucon:
mkdir -p $@

/home/isucon/.bashrc.backup: /home/isucon
cp -f /home/isucon/.bashrc $@
/files/gitignore.sh $@

/home/isucon/.bashrc: /home/isucon/.bashrc.backup
ifeq ($(shell git status --ignored --short /home/isucon/.bashrc),)
@echo skip $@
else
/files/gitignore.sh $@
echo 'alias git="sudo git"' >> $@
git add -f $@
git commit -m 'Add alias for git'
endif

/files/hosts/$(HOSTNAME):
mkdir -p /files/hosts
echo "$(shell hostname -s) $(shell hostname -i)" > $@
-git add -f $@
-git commit -m 'Add host'

.PHONY: monitoring
monitoring: prometheus node_exporter process-exporter grafana-server

.PHONY: prometheus
prometheus: /usr/local/bin/prometheus
-pkill $@
nohup $@ --config.file=/etc/prometheus/prometheus.yml &

/usr/local/bin/prometheus:
curl -sL https://github.com/prometheus/prometheus/releases/download/v2.21.0-rc.1/prometheus-2.21.0-rc.1.linux-amd64.tar.gz | tar xzv --strip-components 1 -C /usr/local/bin/ prometheus-2.21.0-rc.1.linux-amd64/prometheus

.PHONY: node_exporter
node_exporter: /usr/local/bin/node_exporter
-pkill $@
nohup $@ &

/usr/local/bin/node_exporter:
curl -sL https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz | tar xzv --strip-components 1 -C /usr/local/bin/ node_exporter-1.0.1.linux-amd64/node_exporter

.PHONY: process-exporter
process-exporter: /usr/local/bin/process-exporter /etc/process-exporter/all.yaml
-pkill $@
nohup $@ -config.path /etc/process-exporter/all.yaml &

/usr/local/bin/process-exporter:
curl -sL https://github.com/ncabatoff/process-exporter/releases/download/v0.7.2/process-exporter-0.7.2.linux-amd64.tar.gz | tar xzv --strip-components 1 -C /usr/local/bin/ process-exporter-0.7.2.linux-amd64/process-exporter

/etc/process-exporter/all.yaml: /etc/process-exporter
curl -sL https://raw.githubusercontent.com/ncabatoff/process-exporter/v0.7.2/packaging/conf/all.yaml > $@

/etc/process-exporter:
mkdir -p $@

.PHONY: grafana-server
grafana-server: /usr/local/bin/grafana-server
-pkill $@
GF_SERVER_HTTP_PORT=3999 GF_AUTH_ANONYMOUS_ENABLED=true GF_AUTH_ANONYMOUS_ORG_ROLE=Admin nohup $@ -homepath /opt/grafana &

/usr/local/bin/grafana-server: /opt/grafana
ln -sf $</bin/grafana-server $@

/opt/grafana:
mkdir -p $@
curl -sL https://dl.grafana.com/oss/release/grafana-7.1.5.linux-amd64.tar.gz | tar xzv --strip-components 1 -C $@
cp -f /files/grafana/datasources.yml $@/conf/provisioning/datasources/datasources.yml
cp -f /files/grafana/dashboards.yml $@/conf/provisioning/dashboards/dashboards.yml
Loading

0 comments on commit b27b424

Please sign in to comment.