-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: some clean ups mentioned in 2do.md
- Loading branch information
Showing
18 changed files
with
255 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: main | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
|
||
jobs: | ||
build-and-release: | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Login to Docker | ||
uses: docker/login-action@v1 | ||
with: | ||
registry: docker.io | ||
username: ${{ secrets.DOCKER_USERNAME }} | ||
password: ${{ secrets.DOCKER_PASSWORD }} | ||
|
||
- name: Build and push Docker image | ||
uses: docker/build-push-action@v2 | ||
with: | ||
context: . | ||
push: true | ||
tags: arshamalh/dockeroller:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.env | ||
log.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
- [ ] Use Cobra as explained in Readme. | ||
- [x] Add logging | ||
- [x] Dockerize, Makefile, Basic CI/CD | ||
- [x] Telegram is at the presentation layer, it doesn't need to be abstracted behind an interface. | ||
- [x] What is tools/get_token.go? there is no need for that. (UPDATE: Removed) | ||
- [x] Use Cobra as explained in Readme. | ||
- [x] ChatID whitelisting (has a TODO, middleware whitelisting) for security concerns | ||
- [ ] Remove session which is used as a one size fits all! | ||
- [ ] Should we replace docker by contract and use it directly as we will never replace it? Not actually, as we may want to plug a mock instead or wrap its functions | ||
- [ ] Load yaml config | ||
- [ ] Add logging | ||
- [ ] Define semi-hardcoded buttons! (button unique identifiers all totally hard coded in two places which lead to confusion, one when defining and one when putting in the handler, it's better to define button somehow else) | ||
- [ ] What is tools/get_token.go? there is no need for that. | ||
- [ ] Implement web hook and let the user decide for it. | ||
- [ ] Dockerize, Makefile and make it ready to be installed using go install, apt install, etc. | ||
- [ ] ChatID whitelisting for security concerns | ||
- [ ] Telegram is at the presentation layer, it doesn't need to be abstracted behind an interface. | ||
- [ ] Make it ready to be installed using go install, apt install, etc. | ||
- [ ] Should we replace docker by contract and use it directly as we will never replace it? Not actually, as we may want to plug a mock instead or wrap its functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM golang:1.21-alpine3.18 AS builder | ||
WORKDIR /app | ||
COPY go.mod go.sum ./ | ||
RUN go mod download | ||
COPY . ./ | ||
RUN CGO_ENABLED=0 GOOS=linux go build -a -o dclr . | ||
|
||
FROM alpine:3.18 AS production | ||
COPY --from=builder /app/dclr . | ||
ENTRYPOINT [ "./dclr" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Default values, but should be overriden like `make run File=data/another.json | ||
version=latest | ||
name=dockeroller | ||
|
||
help: # Generate list of targets with descriptions | ||
@grep '^.*\:\s#.*' Makefile | sed 's/\(.*\) # \(.*\)/\1 \2/' | column -t -s ":" | ||
|
||
build: # Build the binary for current local system | ||
go build -o dockeroller . | ||
|
||
build-docker: # Build the docker image | ||
docker build -t ${name}:${version} . | ||
|
||
sample-docker: # Run docker with sample start command | ||
docker run --rm ${name}:${version} start --token="something" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/arshamalh/dockeroller/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Execute() { | ||
var root = &cobra.Command{ | ||
Use: "dockeroller", | ||
Short: "ChatOps application for controlling docker daemon through messengers such as Telegram", | ||
} | ||
|
||
registerStart(root) | ||
if err := root.Execute(); err != nil { | ||
log.Gl.Fatal(err.Error()) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/arshamalh/dockeroller/contracts" | ||
"github.com/arshamalh/dockeroller/docker" | ||
"github.com/arshamalh/dockeroller/log" | ||
"github.com/arshamalh/dockeroller/pkg/session" | ||
tpkg "github.com/arshamalh/dockeroller/telegram" | ||
"github.com/arshamalh/dockeroller/telegram/handlers" | ||
"github.com/joho/godotenv" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/telebot.v3" | ||
"gopkg.in/telebot.v3/middleware" | ||
) | ||
|
||
func registerStart(root *cobra.Command) { | ||
var token string | ||
whitelistedIDS := make([]int64, 0) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "start", | ||
Short: "starting telegram bot", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
start(token, whitelistedIDS) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&token, "token", "t", "", "input your telegram token") | ||
cmd.Flags().Int64SliceVarP(&whitelistedIDS, "whitelisted-ids", "w", whitelistedIDS, "a comma separated list of ids which are allowed to use this bot") | ||
root.AddCommand(cmd) | ||
} | ||
|
||
func start(token string, whitelistedIDs []int64) { | ||
if err := godotenv.Load(); err != nil { | ||
log.Gl.Error(err.Error()) | ||
} | ||
|
||
docker := docker.New() | ||
|
||
// apiSrv := api.New(docker) | ||
if token == "" { | ||
if os.Getenv("TOKEN") != "" { | ||
token = os.Getenv("TOKEN") | ||
} else { | ||
log.Gl.Error("telegram can't start because no token is provided") | ||
} | ||
} | ||
startTelegram(docker, token, whitelistedIDs) | ||
} | ||
|
||
func startTelegram(docker contracts.Docker, token string, whitelistedIDs []int64) { | ||
bot, err := telebot.NewBot(telebot.Settings{ | ||
Token: token, | ||
Poller: &telebot.LongPoller{Timeout: 10 * time.Second}, | ||
}) | ||
if err != nil { | ||
log.Gl.Error(err.Error()) | ||
} | ||
session := session.New() | ||
handlers.Register(bot, docker, session) | ||
// Middlewares | ||
bot.Use(middleware.Whitelist(whitelistedIDs...)) | ||
// TODO: Disabled logger middleware for now. | ||
// bot.Use(LoggerMiddleware(log.Gl)) | ||
if err := bot.SetCommands(tpkg.Commands); err != nil { | ||
log.Gl.Error(err.Error()) | ||
} | ||
bot.Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package log | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// Gl is the global logger | ||
var Gl *zap.Logger | ||
|
||
func init() { | ||
config := zap.NewProductionEncoderConfig() | ||
config.EncodeTime = zapcore.ISO8601TimeEncoder | ||
fileEncoder := zapcore.NewJSONEncoder(config) | ||
consoleEncoder := zapcore.NewConsoleEncoder(config) | ||
logFile, err := os.OpenFile("log.json", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
writer := zapcore.AddSync(logFile) | ||
defaultLogLevel := zapcore.DebugLevel | ||
core := zapcore.NewTee( | ||
zapcore.NewCore(fileEncoder, writer, defaultLogLevel), | ||
zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), defaultLogLevel), | ||
) | ||
Gl = zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/arshamalh/dockeroller/cmd" | ||
) | ||
|
||
func main() { | ||
cmd.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.