Skip to content

Commit

Permalink
Merge pull request #24 from luniehq/aleksei/workflow-setup
Browse files Browse the repository at this point in the history
Aleksei/workflow setup
  • Loading branch information
colw authored Oct 19, 2019
2 parents de38551 + 642ef01 commit f8c4d75
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 40 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Update existing droplet in Digital Ocean

on:
push:
branches:
- develop
jobs:
test:
name: Compile
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Use Node.js
uses: actions/setup-node@v1
- name: npm install
run: npm install
- name: Build image
run: docker build -t lunieapi .
- name: Save file
run: docker save lunieapi | gzip > lunieapi.tgz
- name: prepare to push to Digital Ocean
run: mkdir -p ~/.ssh && echo "${{ secrets.DO_PRIVATE_KEY }}" | tr -d '\r' > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa && eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_rsa && ssh-keyscan -H 157.245.121.175 >> ~/.ssh/known_hosts
- name: push to Digital Ocean
run: rsync -4 lunieapi.tgz [email protected]:~/ && rsync -4 pm2metrics.pl [email protected]:~/ && rsync -4 docker-compose.yml [email protected]:~/ && rsync -4 Caddyfile [email protected]:~/
- name: Installing Docker Compose
run: ssh [email protected] "sudo curl -L \"https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)\" -o /usr/local/bin/docker-compose; sudo chmod +x /usr/local/bin/docker-compose"
- name: Run on Digital Ocean
run: ssh [email protected] "docker image prune -a -f; docker load < lunieapi.tgz; docker container stop lunieapi;docker container stop caddy; docker container stop node_exporter; docker container prune -f; export HASURA_URL="https://staging-db.lunie.io/v1/graphql"; export HASURA_ADMIN_KEY="${{ secrets.LUNIE_STAGING_DB_KEY }}"; docker-compose -f docker-compose.yml up --build -d"
- name: Setting up cron job for pm2 metrics export
run: ssh [email protected] "mkdir /logs -p; touch /logs/show; line='*/1 * * * * docker exec lunieapi pm2 show 0 > /logs/show; perl /root/pm2metrics.pl'; crontab -l | grep -q 'lunieapi pm2' && true || (crontab -l; echo "$line" ) | crontab -"
16 changes: 16 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: AlertOnPR

on:
pull_request:
types: [opened]
jobs:
test:
name: Compile
runs-on: ubuntu-latest
steps:
- uses: rtCamp/action-slack-notify@master
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
SLACK_CHANNEL: 'pr_alerts'
SLACK_USERNAME: 'luniebot'
SLACK_MESSAGE: 'New pull request to lunie-api'
30 changes: 30 additions & 0 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Update existing droplet in Digital Ocean

on:
push:
branches:
- master
jobs:
test:
name: Compile
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Use Node.js
uses: actions/setup-node@v1
- name: npm install
run: npm install
- name: Build image
run: docker build -t lunieapi .
- name: Save file
run: docker save lunieapi | gzip > lunieapi.tgz
- name: prepare to push to Digital Ocean
run: mkdir -p ~/.ssh && echo "${{ secrets.DO_PRIVATE_KEY }}" | tr -d '\r' > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa && eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_rsa && ssh-keyscan -H 167.71.107.214 >> ~/.ssh/known_hosts
- name: push to Digital Ocean
run: rsync -4 lunieapi.tgz [email protected]:~/ && rsync -4 pm2metrics.pl [email protected]:~/ && rsync -4 docker-compose.prod.yml [email protected]:~/ && rsync -4 Caddyfile.prod [email protected]:~/
- name: Installing Docker Compose
run: ssh [email protected] "sudo curl -L \"https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)\" -o /usr/local/bin/docker-compose; sudo chmod +x /usr/local/bin/docker-compose"
- name: Run on Digital Ocean
run: ssh [email protected] "docker image prune -a -f; docker load < lunieapi.tgz; docker container stop lunieapi;docker container stop caddy; docker container stop node_exporter; docker container prune -f;export HASURA_URL="https://production-db.lunie.io/v1/graphql"; export HASURA_ADMIN_KEY="${{ secrets.LUNIE_PRODUCTION_DB_KEY }}"; docker-compose -f docker-compose.yml up --build -d"
- name: Setting up cron job for pm2 metrics export
run: ssh [email protected] "mkdir /logs -p; touch /logs/show; line='*/1 * * * * docker exec lunieapi pm2 show 0 > /logs/show; perl /root/pm2metrics.pl'; crontab -l | grep -q 'lunieapi pm2' && true || (crontab -l; echo "$line" ) | crontab -"
29 changes: 0 additions & 29 deletions .github/workflows/test.yml

This file was deleted.

5 changes: 5 additions & 0 deletions Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
staging-api.lunie.io {
proxy / lunieapi:4000 {
websocket
}
}
5 changes: 5 additions & 0 deletions Caddyfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
production-api.lunie.io {
proxy / lunieapi:4000 {
websocket
}
}
32 changes: 21 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
FROM node:10-alpine
WORKDIR /app
COPY ./data /app/data
COPY ./lib /app/lib
COPY package.json /app/package.json
COPY package-lock.json /app/package-lock.json
COPY index.js /app/index.js
COPY config.js /app/config.js
RUN npm install
EXPOSE 4000
CMD ["node", "index.js"]
FROM keymetrics/pm2:latest-alpine

WORKDIR /var/www/server

COPY ./node_modules ./node_modules

COPY ./lib ./lib

COPY ./data ./data

COPY package.json .

COPY config.js .

COPY pm2.json .

COPY index.js .

CMD [ "pm2-runtime", "start", "pm2.json", "--format"]

EXPOSE 4200
59 changes: 59 additions & 0 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
version: '3.6'
services:
lunieapi:
image: lunieapi:latest
container_name: lunieapi
restart: on-failure
volumes:
- /logs:/var/www/server/logs
networks:
- general
ports:
- "4000:4000"
deploy:
replicas: 1
environment:
- HASURA_URL=$HASURA_URL
- HASURA_ADMIN_KEY=$HASURA_ADMIN_KEY
node-exporter:
image: prom/node-exporter
container_name: node_exporter
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /root/textfile_collector:/var/www
- /:/rootfs:ro
command:
- --path.procfs=/host/proc
- --path.sysfs=/host/sys
- --collector.textfile.directory=/var/www
- --collector.filesystem.ignored-mount-points
- ^/(sys|proc|dev|host|etc|rootfs/var/lib/docker/containers|rootfs/var/lib/docker/overlay2|rootfs/run/docker/netns|rootfs/var/lib/docker/aufs)($$|/)
ports:
- 9100:9100
restart: always
networks:
- general
deploy:
mode: global
caddy:
image: abiosoft/caddy:0.11.0
depends_on:
- "lunieapi"
restart: on-failure
container_name: caddy
networks:
- general
deploy:
replicas: 1
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile.prod:/etc/Caddyfile
- ./caddy_certs:/root/.caddy
volumes:
caddy_certs:
networks:
general:

59 changes: 59 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
version: '3.6'
services:
lunieapi:
image: lunieapi:latest
container_name: lunieapi
restart: on-failure
volumes:
- /logs:/var/www/server/logs
networks:
- general
ports:
- "4000:4000"
deploy:
replicas: 1
environment:
- HASURA_URL=$HASURA_URL
- HASURA_ADMIN_KEY=$HASURA_ADMIN_KEY
node-exporter:
image: prom/node-exporter
container_name: node_exporter
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /root/textfile_collector:/var/www
- /:/rootfs:ro
command:
- --path.procfs=/host/proc
- --path.sysfs=/host/sys
- --collector.textfile.directory=/var/www
- --collector.filesystem.ignored-mount-points
- ^/(sys|proc|dev|host|etc|rootfs/var/lib/docker/containers|rootfs/var/lib/docker/overlay2|rootfs/run/docker/netns|rootfs/var/lib/docker/aufs)($$|/)
ports:
- 9100:9100
restart: always
networks:
- general
deploy:
mode: global
caddy:
image: abiosoft/caddy:0.11.0
depends_on:
- "lunieapi"
restart: on-failure
container_name: caddy
networks:
- general
deploy:
replicas: 1
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/Caddyfile
- ./caddy_certs:/root/.caddy
volumes:
caddy_certs:
networks:
general:

9 changes: 9 additions & 0 deletions pm2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "lunietest",
"script": "npm",
"instances": "1",
"args": ["run", "start"],
"error_file": "/var/www/server/logs/error.log",
"combine_logs": true,
"time": true
}
61 changes: 61 additions & 0 deletions pm2metrics.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/perl
my $filename = "/root/textfile_collector/pm2errors.prom";
my $logfile = "/logs/show";
my $parameters = 0;
sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };

open(my $fh, '<', $logfile);
open(my $fw, '>', $filename);
# fetching log file
while (my $row = <$fh>) {
chomp $row;
if($parameters && index($row, '─────') == -1){
my @params = split("", $row);
my $pname = lc trim(@params[1]);
$pname =~ s/ /_/ig;
my $pvalue = trim(@params[2]);
# special parsing for uptime and status
if($pname eq 'uptime'){
my $pvalue_digit = $pvalue;
$pvalue_digit =~ s/[^0-9]//g;
#print($pvalue);
if(index($pvalue, 'h') != -1){
$pvalue = $pvalue_digit * 60;
}
if(index($pvalue, 'd') != -1){
$pvalue = $pvalue_digit * 60 * 24;
}
if(index($pvalue, 'm') != -1){
$pvalue = $pvalue_digit;
}
if(index($pvalue, 's') != -1){
$pvalue = $pvalue_digit / 60;
}
}
if($pname eq 'status'){
if($pvalue eq 'online'){
$pvalue = 1;
}else{
$pvalue = 0;
}
}
#print($pvalue =~ qr/^[0-9\.]+$/);
my $dotscount = () = $pvalue =~ /\./g;
# only numerical values goes to metrics
if($pname && $pvalue =~ qr/^[0-9\.]+$/ && $dotscount < 2){
print $fw 'pm2_'.$pname . ' ' . $pvalue ."\n";
}

}
if(index($row, '────') != -1){
if($paremeters){
$parameters = 0;
}else{
$parameters = 1;
}
}

}
close $fw;
close $fh;
exit 0;

0 comments on commit f8c4d75

Please sign in to comment.