use json config and enable arguments
This commit is contained in:
parent
c7e61f33a1
commit
48aeb14a85
15 changed files with 100 additions and 75 deletions
|
@ -1,4 +0,0 @@
|
||||||
ADMIN=gempir
|
|
||||||
IRCUSER=gempbot
|
|
||||||
IRCTOKEN=oauth:123123123
|
|
||||||
CHANNELS=gempir,pajlada
|
|
11
Dockerfile
11
Dockerfile
|
@ -1,11 +0,0 @@
|
||||||
FROM golang:latest
|
|
||||||
WORKDIR /go/src/github.com/gempir/gempbotgo
|
|
||||||
COPY . .
|
|
||||||
RUN go get ./...
|
|
||||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
|
|
||||||
|
|
||||||
FROM alpine:latest
|
|
||||||
RUN apk --no-cache add ca-certificates
|
|
||||||
COPY --from=0 /go/src/github.com/gempir/gempbotgo/app .
|
|
||||||
CMD ["./app"]
|
|
||||||
EXPOSE 8025
|
|
2
LICENSE
2
LICENSE
|
@ -1,6 +1,6 @@
|
||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2017 gempir
|
Copyright (c) 2018 gempir
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
---
|
---
|
||||||
api_domain: api2.gempir.com
|
api_domain: justlog.gempir.com
|
||||||
email: !vault |
|
email: !vault |
|
||||||
$ANSIBLE_VAULT;1.1;AES256
|
$ANSIBLE_VAULT;1.1;AES256
|
||||||
65373739353434323038366266343639376262633166356330336339396664326264343334633864
|
65373739353434323038366266343639376262633166356330336339396664326264343334633864
|
||||||
6164633261343062623666613536343265663038343330360a313637663739343464396133366565
|
6164633261343062623666613536343265663038343330360a313637663739343464396133366565
|
||||||
35666538386231343533333231646131383734663834663039636235313237636366343535376366
|
35666538386231343533333231646131383734663834663039636235313237636366343535376366
|
||||||
3438353430666537630a323338663565366439353163646435633738653563393330613064363163
|
3438353430666537630a323338663565366439353163646435633738653563393330613064363163
|
||||||
66356363623063373532626262343361663933623132653162633034396131623038
|
66356363623063373532626262343361663933623132653162633034396131623038
|
||||||
|
admin: gempir
|
||||||
|
channels: "gempir,pajlada"
|
|
@ -5,9 +5,6 @@
|
||||||
|
|
||||||
roles:
|
roles:
|
||||||
- {role: 'common', tags: ['common']}
|
- {role: 'common', tags: ['common']}
|
||||||
- {role: 'geerlingguy.docker', tags: ['docker']}
|
|
||||||
- {role: 'traefik', tags: ['traefik']}
|
|
||||||
- {role: 'portainer', tags: ['portainer']}
|
|
||||||
- {role: 'bot', tags: ['bot']}
|
- {role: 'bot', tags: ['bot']}
|
||||||
|
|
||||||
handlers:
|
handlers:
|
||||||
|
|
|
@ -1,16 +1,23 @@
|
||||||
---
|
---
|
||||||
- name: create directory
|
- name: create log directory
|
||||||
file:
|
file:
|
||||||
path: /var/twitch_logs
|
path: /var/justlog
|
||||||
state: directory
|
state: directory
|
||||||
owner: root
|
owner: justlog
|
||||||
group: docker
|
group: justlog
|
||||||
mode: 0740
|
mode: 0740
|
||||||
|
|
||||||
- name: create channels file
|
- name: create env file
|
||||||
template:
|
template:
|
||||||
src: templates/channels.j2
|
src: templates/env.j2
|
||||||
dest: /var/logstv_channels
|
dest: /etc/justlog.env
|
||||||
owner: root
|
owner: justlog
|
||||||
group: docker
|
group: justlog
|
||||||
mode: 0640
|
mode: 0640
|
||||||
|
|
||||||
|
- name: install systemd unit file
|
||||||
|
template: src=myservice.j2 dest=/etc/systemd/system/myservice.service
|
||||||
|
|
||||||
|
- name: reload units
|
||||||
|
systemd:
|
||||||
|
daemon_reload: yes
|
|
@ -1,5 +0,0 @@
|
||||||
gempir
|
|
||||||
pajlada
|
|
||||||
forsen
|
|
||||||
nymn
|
|
||||||
nani
|
|
8
ansible/roles/bot/templates/config.j2
Executable file
8
ansible/roles/bot/templates/config.j2
Executable file
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"admin: "{{ admin }}",
|
||||||
|
"logDirectory": "/var/justlog"
|
||||||
|
"channels": [
|
||||||
|
"gempir",
|
||||||
|
"pajlada"
|
||||||
|
]
|
||||||
|
}
|
11
ansible/roles/bot/templates/service.j2
Normal file
11
ansible/roles/bot/templates/service.j2
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
[Unit]
|
||||||
|
Description=justlog service
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
ExecStart=/home/justlog/justlog
|
||||||
|
TimeoutStopSec=5
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
|
@ -14,3 +14,15 @@
|
||||||
|
|
||||||
- name: install ufw firewall
|
- name: install ufw firewall
|
||||||
include: ufw.yml
|
include: ufw.yml
|
||||||
|
|
||||||
|
- name: Ensure group "justlog" exists
|
||||||
|
group:
|
||||||
|
name: justlog
|
||||||
|
state: present
|
||||||
|
|
||||||
|
- name: Create justlog user
|
||||||
|
user:
|
||||||
|
name: justlog
|
||||||
|
shell: /bin/bash
|
||||||
|
groups: justlog
|
||||||
|
append: yes
|
5
config.json
Normal file
5
config.json
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"admin": "gempir",
|
||||||
|
"logsDirectory": "./logs",
|
||||||
|
"channels": ["gempir", "pajlada"]
|
||||||
|
}
|
|
@ -1,11 +0,0 @@
|
||||||
version: "2.1"
|
|
||||||
services:
|
|
||||||
gempbotgo:
|
|
||||||
build: ./
|
|
||||||
restart: always
|
|
||||||
ports:
|
|
||||||
- "8025:8025"
|
|
||||||
env_file:
|
|
||||||
- .env
|
|
||||||
volumes:
|
|
||||||
- ./logs:/var/twitch_logs
|
|
|
@ -13,9 +13,9 @@ type Logger struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFileLogger create file logger
|
// NewFileLogger create file logger
|
||||||
func NewFileLogger() Logger {
|
func NewFileLogger(logPath string) Logger {
|
||||||
return Logger{
|
return Logger{
|
||||||
logPath: "/var/twitch_logs",
|
logPath: logPath,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
55
main.go
55
main.go
|
@ -1,8 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -13,26 +14,33 @@ import (
|
||||||
"github.com/gempir/gempbotgo/humanize"
|
"github.com/gempir/gempbotgo/humanize"
|
||||||
"github.com/gempir/go-twitch-irc"
|
"github.com/gempir/go-twitch-irc"
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
LogsDirectory string `json:"logsDirectory"`
|
||||||
|
Admin string `json:"admin"`
|
||||||
|
Channels []string `json:"channels"`
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
admin string
|
cfg config
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
admin = os.Getenv("ADMIN")
|
|
||||||
|
configFile := flag.String("config", "config.json", "json config file")
|
||||||
|
flag.Parse()
|
||||||
|
cfg = loadConfiguration(*configFile)
|
||||||
|
|
||||||
apiServer := api.NewServer()
|
apiServer := api.NewServer()
|
||||||
go apiServer.Init()
|
go apiServer.Init()
|
||||||
|
|
||||||
twitchClient := twitch.NewClient(os.Getenv("IRCUSER"), os.Getenv("IRCTOKEN"))
|
twitchClient := twitch.NewClient("justinfan123123", "oauth:123123123")
|
||||||
|
fileLogger := filelog.NewFileLogger(cfg.LogsDirectory)
|
||||||
|
|
||||||
fileLogger := filelog.NewFileLogger()
|
for _, channel := range cfg.Channels {
|
||||||
|
|
||||||
channels := strings.Split(os.Getenv("CHANNELS"), ",")
|
|
||||||
for _, channel := range channels {
|
|
||||||
fmt.Println("Joining " + channel)
|
fmt.Println("Joining " + channel)
|
||||||
twitchClient.Join(channel)
|
twitchClient.Join(channel)
|
||||||
apiServer.AddChannel(channel)
|
apiServer.AddChannel(channel)
|
||||||
|
@ -43,20 +51,20 @@ func main() {
|
||||||
go func() {
|
go func() {
|
||||||
err := fileLogger.LogMessageForUser(channel, user, message)
|
err := fileLogger.LogMessageForUser(channel, user, message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := fileLogger.LogMessageForChannel(channel, user, message)
|
err := fileLogger.LogMessageForChannel(channel, user, message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if user.Username == admin && strings.HasPrefix(message.Text, "!status") {
|
if user.Username == cfg.Admin && strings.HasPrefix(message.Text, "!status") {
|
||||||
uptime := humanize.TimeSince(startTime)
|
uptime := humanize.TimeSince(startTime)
|
||||||
twitchClient.Say(channel, admin+", uptime: "+uptime)
|
twitchClient.Say(channel, cfg.Admin+", uptime: "+uptime)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -65,17 +73,34 @@ func main() {
|
||||||
go func() {
|
go func() {
|
||||||
err := fileLogger.LogMessageForUser(channel, user, message)
|
err := fileLogger.LogMessageForUser(channel, user, message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := fileLogger.LogMessageForChannel(channel, user, message)
|
err := fileLogger.LogMessageForChannel(channel, user, message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
})
|
})
|
||||||
|
|
||||||
twitchClient.Connect()
|
twitchClient.Connect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadConfiguration(file string) config {
|
||||||
|
log.Info("Loading config from " + file)
|
||||||
|
var cfg config
|
||||||
|
configFile, err := os.Open(file)
|
||||||
|
defer configFile.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonParser := json.NewDecoder(configFile)
|
||||||
|
jsonParser.Decode(&cfg)
|
||||||
|
|
||||||
|
cfg.LogsDirectory = strings.TrimSuffix(cfg.LogsDirectory, "/")
|
||||||
|
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
11
prod.yml
11
prod.yml
|
@ -1,11 +0,0 @@
|
||||||
version: "3"
|
|
||||||
services:
|
|
||||||
gempbotgo:
|
|
||||||
image: gempir/gempbotgo
|
|
||||||
restart: always
|
|
||||||
ports:
|
|
||||||
- "8025:8025"
|
|
||||||
env_file:
|
|
||||||
- .env
|
|
||||||
volumes:
|
|
||||||
- /var/twitch_logs:/var/twitch_logs
|
|
Loading…
Add table
Reference in a new issue