From 48aeb14a85336f0ed23bc63434bdccb3d11ce61f Mon Sep 17 00:00:00 2001 From: gempir Date: Thu, 29 Nov 2018 22:20:57 +0100 Subject: [PATCH] use json config and enable arguments --- .env.dist | 4 -- Dockerfile | 11 ----- LICENSE | 2 +- ansible/group_vars/all.yml | 6 ++- ansible/playbook.yml | 3 -- ansible/roles/bot/tasks/main.yml | 27 +++++++----- ansible/roles/bot/templates/channels.j2 | 5 --- ansible/roles/bot/templates/config.j2 | 8 ++++ ansible/roles/bot/templates/service.j2 | 11 +++++ ansible/roles/common/tasks/main.yml | 12 ++++++ config.json | 5 +++ docker-compose.yml | 11 ----- filelog/userlog.go | 4 +- main.go | 55 ++++++++++++++++++------- prod.yml | 11 ----- 15 files changed, 100 insertions(+), 75 deletions(-) delete mode 100644 .env.dist delete mode 100644 Dockerfile delete mode 100755 ansible/roles/bot/templates/channels.j2 create mode 100755 ansible/roles/bot/templates/config.j2 create mode 100644 ansible/roles/bot/templates/service.j2 create mode 100644 config.json delete mode 100644 docker-compose.yml delete mode 100644 prod.yml diff --git a/.env.dist b/.env.dist deleted file mode 100644 index 3b51d1b..0000000 --- a/.env.dist +++ /dev/null @@ -1,4 +0,0 @@ -ADMIN=gempir -IRCUSER=gempbot -IRCTOKEN=oauth:123123123 -CHANNELS=gempir,pajlada \ No newline at end of file diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 50657ea..0000000 --- a/Dockerfile +++ /dev/null @@ -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 diff --git a/LICENSE b/LICENSE index a88bf05..38c7d39 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2017 gempir +Copyright (c) 2018 gempir Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/ansible/group_vars/all.yml b/ansible/group_vars/all.yml index 9c4b506..57db1f6 100755 --- a/ansible/group_vars/all.yml +++ b/ansible/group_vars/all.yml @@ -1,9 +1,11 @@ --- -api_domain: api2.gempir.com +api_domain: justlog.gempir.com email: !vault | $ANSIBLE_VAULT;1.1;AES256 65373739353434323038366266343639376262633166356330336339396664326264343334633864 6164633261343062623666613536343265663038343330360a313637663739343464396133366565 35666538386231343533333231646131383734663834663039636235313237636366343535376366 3438353430666537630a323338663565366439353163646435633738653563393330613064363163 - 66356363623063373532626262343361663933623132653162633034396131623038 \ No newline at end of file + 66356363623063373532626262343361663933623132653162633034396131623038 +admin: gempir +channels: "gempir,pajlada" \ No newline at end of file diff --git a/ansible/playbook.yml b/ansible/playbook.yml index 137c83b..c078580 100755 --- a/ansible/playbook.yml +++ b/ansible/playbook.yml @@ -5,9 +5,6 @@ roles: - {role: 'common', tags: ['common']} - - {role: 'geerlingguy.docker', tags: ['docker']} - - {role: 'traefik', tags: ['traefik']} - - {role: 'portainer', tags: ['portainer']} - {role: 'bot', tags: ['bot']} handlers: diff --git a/ansible/roles/bot/tasks/main.yml b/ansible/roles/bot/tasks/main.yml index 2f2e532..c1b4cad 100755 --- a/ansible/roles/bot/tasks/main.yml +++ b/ansible/roles/bot/tasks/main.yml @@ -1,16 +1,23 @@ --- -- name: create directory +- name: create log directory file: - path: /var/twitch_logs + path: /var/justlog state: directory - owner: root - group: docker + owner: justlog + group: justlog mode: 0740 -- name: create channels file +- name: create env file template: - src: templates/channels.j2 - dest: /var/logstv_channels - owner: root - group: docker - mode: 0640 \ No newline at end of file + src: templates/env.j2 + dest: /etc/justlog.env + owner: justlog + group: justlog + mode: 0640 + +- name: install systemd unit file + template: src=myservice.j2 dest=/etc/systemd/system/myservice.service + +- name: reload units + systemd: + daemon_reload: yes \ No newline at end of file diff --git a/ansible/roles/bot/templates/channels.j2 b/ansible/roles/bot/templates/channels.j2 deleted file mode 100755 index 3f8f538..0000000 --- a/ansible/roles/bot/templates/channels.j2 +++ /dev/null @@ -1,5 +0,0 @@ -gempir -pajlada -forsen -nymn -nani \ No newline at end of file diff --git a/ansible/roles/bot/templates/config.j2 b/ansible/roles/bot/templates/config.j2 new file mode 100755 index 0000000..ea721e9 --- /dev/null +++ b/ansible/roles/bot/templates/config.j2 @@ -0,0 +1,8 @@ +{ + "admin: "{{ admin }}", + "logDirectory": "/var/justlog" + "channels": [ + "gempir", + "pajlada" + ] +} \ No newline at end of file diff --git a/ansible/roles/bot/templates/service.j2 b/ansible/roles/bot/templates/service.j2 new file mode 100644 index 0000000..f0af4b5 --- /dev/null +++ b/ansible/roles/bot/templates/service.j2 @@ -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 \ No newline at end of file diff --git a/ansible/roles/common/tasks/main.yml b/ansible/roles/common/tasks/main.yml index 4fc6775..095a278 100755 --- a/ansible/roles/common/tasks/main.yml +++ b/ansible/roles/common/tasks/main.yml @@ -14,3 +14,15 @@ - name: install ufw firewall 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 \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..3eec956 --- /dev/null +++ b/config.json @@ -0,0 +1,5 @@ +{ + "admin": "gempir", + "logsDirectory": "./logs", + "channels": ["gempir", "pajlada"] +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index b2de9ef..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,11 +0,0 @@ -version: "2.1" -services: - gempbotgo: - build: ./ - restart: always - ports: - - "8025:8025" - env_file: - - .env - volumes: - - ./logs:/var/twitch_logs \ No newline at end of file diff --git a/filelog/userlog.go b/filelog/userlog.go index accab23..2d2b4f9 100644 --- a/filelog/userlog.go +++ b/filelog/userlog.go @@ -13,9 +13,9 @@ type Logger struct { } // NewFileLogger create file logger -func NewFileLogger() Logger { +func NewFileLogger(logPath string) Logger { return Logger{ - logPath: "/var/twitch_logs", + logPath: logPath, } } diff --git a/main.go b/main.go index 6d9bef7..176bd6b 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,9 @@ package main import ( + "encoding/json" + "flag" "fmt" - "log" "os" "time" @@ -13,26 +14,33 @@ import ( "github.com/gempir/gempbotgo/humanize" "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 ( - admin string + cfg config ) func main() { startTime := time.Now() - admin = os.Getenv("ADMIN") + + configFile := flag.String("config", "config.json", "json config file") + flag.Parse() + cfg = loadConfiguration(*configFile) apiServer := api.NewServer() 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() - - channels := strings.Split(os.Getenv("CHANNELS"), ",") - for _, channel := range channels { + for _, channel := range cfg.Channels { fmt.Println("Joining " + channel) twitchClient.Join(channel) apiServer.AddChannel(channel) @@ -43,20 +51,20 @@ func main() { go func() { err := fileLogger.LogMessageForUser(channel, user, message) if err != nil { - log.Println(err.Error()) + log.Error(err.Error()) } }() go func() { err := fileLogger.LogMessageForChannel(channel, user, message) 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) - twitchClient.Say(channel, admin+", uptime: "+uptime) + twitchClient.Say(channel, cfg.Admin+", uptime: "+uptime) } }) @@ -65,17 +73,34 @@ func main() { go func() { err := fileLogger.LogMessageForUser(channel, user, message) if err != nil { - log.Println(err.Error()) + log.Error(err.Error()) } }() go func() { err := fileLogger.LogMessageForChannel(channel, user, message) if err != nil { - log.Println(err.Error()) + log.Error(err.Error()) } }() }) 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 +} diff --git a/prod.yml b/prod.yml deleted file mode 100644 index 3b8a8d5..0000000 --- a/prod.yml +++ /dev/null @@ -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