Skip to content

Commit f1b6a5a

Browse files
committed
Improves parrotserver Docker container
1 parent a893931 commit f1b6a5a

File tree

5 files changed

+51
-11
lines changed

5 files changed

+51
-11
lines changed

parrot/.goreleaser.yaml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
version: 2
33
project_name: parrot
44

5+
dist: parrot/dist
6+
57
monorepo:
68
tag_prefix: parrot/
79
dir: parrot
@@ -20,8 +22,7 @@ builds:
2022
goarch:
2123
- amd64
2224
- arm64
23-
ldflags:
24-
- '-s -w'
25+
binary: parrot
2526

2627
archives:
2728
- formats: ['binary']
@@ -32,6 +33,7 @@ dockers:
3233
goarch: amd64
3334
image_templates:
3435
- '{{ .Env.IMG_PRE }}/parrot:{{ .Tag }}-amd64'
36+
- '{{ .Env.IMG_PRE }}/parrot:latest-amd64'
3537
build_flag_templates:
3638
- --platform=linux/amd64
3739
- --pull
@@ -44,6 +46,7 @@ dockers:
4446
goarch: arm64
4547
image_templates:
4648
- '{{ .Env.IMG_PRE }}/parrot:{{ .Tag }}-arm64'
49+
- '{{ .Env.IMG_PRE }}/parrot:latest-arm64'
4750
build_flag_templates:
4851
- --platform=linux/arm64
4952
- --pull
@@ -57,7 +60,11 @@ docker_manifests:
5760
image_templates:
5861
- '{{ .Env.IMG_PRE }}/parrot:{{ .Tag }}-amd64'
5962
- '{{ .Env.IMG_PRE }}/parrot:{{ .Tag }}-arm64'
63+
- name_template: '{{ .Env.IMG_PRE }}/parrot:latest'
64+
image_templates:
65+
- '{{ .Env.IMG_PRE }}/parrot:latest-amd64'
66+
- '{{ .Env.IMG_PRE }}/parrot:latest-arm64'
6067

6168
before:
6269
hooks:
63-
- sh -c "cd parrot && go mod tidy"
70+
- sh -c "cd parrot && go mod tidy"

parrot/Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
FROM scratch
2+
23
COPY parrot /parrot
3-
ENTRYPOINT [ "parrot", "-port", "9090", "-t" ]
4+
ENV PARROT_PORT 80
5+
ENV PARROT_TRACE true
6+
EXPOSE 80
7+
ENTRYPOINT [ "/parrot" ]

parrot/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ fuzz:
3636
.PHONY: build
3737
build:
3838
cd .. && goreleaser release --snapshot --clean -f ./parrot/.goreleaser.yaml
39+
echo "Build done, check in parrot/dist for binaries"
3940

parrot/cmd/main.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"os"
66
"os/signal"
7+
"strconv"
8+
"strings"
79
"syscall"
810
"time"
911

@@ -26,6 +28,31 @@ func main() {
2628
rootCmd := &cobra.Command{
2729
Use: "parrot",
2830
Short: "A server that can register and parrot back dynamic requests",
31+
PreRun: func(cmd *cobra.Command, args []string) {
32+
// Check environment variables if flags are not set
33+
if !cmd.Flags().Changed("port") {
34+
if envPort, err := strconv.Atoi(os.Getenv("PARROT_PORT")); err == nil {
35+
port = envPort
36+
}
37+
}
38+
if !cmd.Flags().Changed("debug") {
39+
debug = os.Getenv("PARROT_DEBUG") == "true"
40+
}
41+
if !cmd.Flags().Changed("trace") {
42+
trace = os.Getenv("PARROT_TRACE") == "true"
43+
}
44+
if !cmd.Flags().Changed("silent") {
45+
silent = os.Getenv("PARROT_SILENT") == "true"
46+
}
47+
if !cmd.Flags().Changed("json") {
48+
json = os.Getenv("PARROT_JSON") == "true"
49+
}
50+
if !cmd.Flags().Changed("recorders") {
51+
if envRecorders := os.Getenv("PARROT_RECORDERS"); envRecorders != "" {
52+
recorders = strings.Split(envRecorders, ",")
53+
}
54+
}
55+
},
2956
RunE: func(cmd *cobra.Command, args []string) error {
3057
options := []parrot.ServerOption{parrot.WithPort(port)}
3158
logLevel := zerolog.InfoLevel
@@ -63,12 +90,12 @@ func main() {
6390
},
6491
}
6592

66-
rootCmd.Flags().IntVarP(&port, "port", "p", 0, "Port to run the parrot on")
67-
rootCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable debug output")
68-
rootCmd.Flags().BoolVarP(&trace, "trace", "t", false, "Enable trace and debug output")
69-
rootCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Disable all output")
70-
rootCmd.Flags().BoolVarP(&json, "json", "j", false, "Output logs in JSON format")
71-
rootCmd.Flags().StringSliceVarP(&recorders, "recorders", "r", nil, "Existing recorders to use")
93+
rootCmd.Flags().IntVarP(&port, "port", "p", 0, "Port to run the parrot on (env: PARROT_PORT)")
94+
rootCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable debug output (env: PARROT_DEBUG)")
95+
rootCmd.Flags().BoolVarP(&trace, "trace", "t", false, "Enable trace and debug output (env: PARROT_TRACE)")
96+
rootCmd.Flags().BoolVarP(&silent, "silent", "s", false, "Disable all output (env: PARROT_SILENT)")
97+
rootCmd.Flags().BoolVarP(&json, "json", "j", false, "Output logs in JSON format (env: PARROT_JSON)")
98+
rootCmd.Flags().StringSliceVarP(&recorders, "recorders", "r", nil, "Existing recorders to use (env: PARROT_RECORDERS)")
7299

73100
if err := rootCmd.Execute(); err != nil {
74101
log.Error().Err(err).Msg("error executing command")

parrot/parrot.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ func (p *Server) Healthy() error {
247247
ResponseStatusCode: http.StatusOK,
248248
}
249249

250+
p.log.Info().Msg("Checking Parrot health")
250251
err := p.Register(healthCheckRoute)
251252
if err != nil {
252253
return newDynamicError(ErrServerUnhealthy, fmt.Sprintf("%s: unable to register routes", err.Error()))
@@ -263,7 +264,7 @@ func (p *Server) Healthy() error {
263264

264265
p.Delete(healthCheckRoute)
265266

266-
p.log.Debug().Msg("Parrot is healthy")
267+
p.log.Info().Msg("Parrot healthy")
267268
return nil
268269
}
269270

0 commit comments

Comments
 (0)