Skip to content
This repository has been archived by the owner on Nov 26, 2018. It is now read-only.

[WIP] Docker / go 1.6 updates #43

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 5 additions & 32 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,36 +1,9 @@
FROM ubuntu:14.04
FROM golang:1.6.0
MAINTAINER Yann Malet <[email protected]>

RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
ENV PATH /usr/src/go/bin:$PATH
ENV GOPATH /go
ENV PATH /go/bin:$PATH
ENV GOLANG_VERSION 1.3.1
ADD . /go/src/github.com/BotBotMe/botbot-bot/
WORKDIR /go/src/github.com/BotBotMe/botbot-bot/

RUN go get -u github.com/kardianos/govendor

# SCMs for "go get", gcc for cgo
RUN DEBIAN_FRONTEND=noninteractive apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y \
ca-certificates curl gcc libc6-dev \
bzr git mercurial
RUN rm -rf /var/lib/apt/lists/*
RUN curl -sSL http://golang.org/dl/go$GOLANG_VERSION.src.tar.gz | tar -v -C /usr/src -xz

RUN cd /usr/src/go/src && ./make.bash --no-clean 2>&1

RUN mkdir -p /go/src
WORKDIR /go

ENV GOPACKAGE github.com/BotBotMe/botbot-bot
# Copy the local package files to the container's workspace.
ADD . /go/src/$GOPACKAGE

# Build the $GOPACKAGE command inside the container.
# (You may fetch or manage dependencies here,
# either manually or with a tool like "godep".)
RUN go get $GOPACKAGE

ENTRYPOINT /go/bin/botbot-bot -logtostderr=true
CMD go run main.go -logtostderr=true
8 changes: 4 additions & 4 deletions botbot.go → botbot/botbot.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package botbot

import (
"strconv"
Expand Down Expand Up @@ -52,7 +52,7 @@ func NewBotBot(storage common.Storage, queue common.Queue) *BotBot {
}

// Listen for incoming commands
func (bot *BotBot) listen(queueName string) {
func (bot *BotBot) Listen(queueName string) {

var msg []byte
var err error
Expand All @@ -71,7 +71,7 @@ func (bot *BotBot) listen(queueName string) {
}
}

func (bot *BotBot) mainLoop() {
func (bot *BotBot) MainLoop() {
// TODO (yml) comment out bot.recordUserCounts because I think it is
// leaking postgres connection.
//go bot.recordUserCounts()
Expand Down Expand Up @@ -169,6 +169,6 @@ func (bot *BotBot) recordUserCounts() {
}

// Stop
func (bot *BotBot) shutdown() {
func (bot *BotBot) Shutdown() {
bot.netMan.Shutdown()
}
13 changes: 12 additions & 1 deletion common/queue.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"fmt"
"net"
"net/url"
"os"
Expand Down Expand Up @@ -109,7 +110,17 @@ type RedisQueue struct {
func NewRedisQueue() Queue {
redisUrlString := os.Getenv("REDIS_PLUGIN_QUEUE_URL")
if redisUrlString == "" {
glog.Fatal("REDIS_PLUGIN_QUEUE_URL cannot be empty.\nexport REDIS_PLUGIN_QUEUE_URL=redis://host:port/db_number")

// try to connect via docker links if they exist.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not sure how I feel about having Docker related stuff in the code. It's easy enough to build up the environment variable when starting the containers, right?

if os.Getenv("REDIS_PORT_6379_TCP_ADDR") != "" {
redisUrlString = fmt.Sprintf("redis://%s:%s/0",
os.Getenv("REDIS_PORT_6379_TCP_ADDR"),
os.Getenv("REDIS_PORT_6379_TCP_PORT"),
)
} else {
glog.Fatal("REDIS_PLUGIN_QUEUE_URL cannot be empty.\nexport REDIS_PLUGIN_QUEUE_URL=redis://host:port/db_number")

}
}
redisUrl, err := url.Parse(redisUrlString)
if err != nil {
Expand Down
17 changes: 16 additions & 1 deletion common/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"database/sql"
"fmt"
"os"
"time"

Expand Down Expand Up @@ -56,11 +57,25 @@ type PostgresStorage struct {
// Connect to the database.
func NewPostgresStorage() *PostgresStorage {
postgresUrlString := os.Getenv("STORAGE_URL")

if glog.V(2) {
glog.Infoln("postgresUrlString: ", postgresUrlString)
}
if postgresUrlString == "" {
glog.Fatal("STORAGE_URL cannot be empty.\nexport STORAGE_URL=postgres://user:password@host:port/db_name")

// Try to make the connection string from docker links
if os.Getenv("DB_ENV_POSTGRES_USER") != "" {
glog.Infoln("STORAGE_URL not in the environment. Try to create from docker links.")

postgresUrlString = fmt.Sprintf("postgres://%s:%s@%s:%s/botbot",
os.Getenv("DB_ENV_POSTGRES_USER"),
os.Getenv("DB_ENV_POSTGRES_PASSWORD"),
os.Getenv("DB_PORT_5432_TCP_ADDR"),
os.Getenv("DB_PORT_5432_TCP_PORT"),
)
} else {
glog.Fatal("STORAGE_URL cannot be empty.\nexport STORAGE_URL=postgres://user:password@host:port/db_name")
}
}
dataSource, err := pq.ParseURL(postgresUrlString)
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
bot:
build: .
links:
- redis
- db
volumes:
- ./:/go/src/github.com/BotBotMe/botbot-bot/

redis:
image: redis:latest
ports:
- 6379

db:
image: postgres:9.5
ports:
- "5432:5432"
volumes:
- ./init-db:/docker-entrypoint-initdb.d/
- ./sql:/pg-tmp/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't persist the DB, right? Would people want to use docker beyond just testing?

Can we throw all Docker related stuff into a parent folder so it isn't cluttering the root of the repo?

environment:
POSTGRES_PASSWORD: docker
POSTGRES_USER: postgres
13 changes: 13 additions & 0 deletions init-db/init-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A short comment on what this file does and why it is necessary would be nice.

set -e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I typically use set -euf to make scripts a little more bulletproof https://sipb.mit.edu/doc/safe-shell/



psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE DATABASE botbot;
GRANT ALL PRIVILEGES ON DATABASE botbot TO postgres;
EOSQL


psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" botbot -c 'CREATE EXTENSION hstore;'
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" botbot < /pg-tmp/schema.sql
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"os/signal"
"syscall"

"github.com/BotBotMe/botbot-bot/botbot"
"github.com/BotBotMe/botbot-bot/common"
"github.com/golang/glog"
_ "net/http/pprof"

)

const (
Expand All @@ -29,13 +29,13 @@ func main() {

queue := common.NewRedisQueue()

botbot := NewBotBot(storage, queue)
bot := botbot.NewBotBot(storage, queue)

// Listen for incoming commands
go botbot.listen(LISTEN_QUEUE_PREFIX)
go bot.Listen(LISTEN_QUEUE_PREFIX)

// Start the main loop
go botbot.mainLoop()
go bot.MainLoop()

// Start and http server to serve the stats from expvar
log.Fatal(http.ListenAndServe(":3030", nil))
Expand All @@ -48,7 +48,7 @@ func main() {
for {
<-kill
glog.Infoln("Graceful shutdown")
botbot.shutdown()
bot.Shutdown()
break
}

Expand Down
9 changes: 5 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/BotBotMe/botbot-bot/botbot"
"github.com/BotBotMe/botbot-bot/common"
"github.com/golang/glog"
)
Expand Down Expand Up @@ -41,9 +42,9 @@ func TestBotBotIRC(t *testing.T) {

// Run BotBot
time.Sleep(time.Second) // Sleep of one second to avoid the 5s backoff
botbot := NewBotBot(storage, queue)
go botbot.listen("testcmds")
go botbot.mainLoop()
bot := botbot.NewBotBot(storage, queue)
go bot.Listen("testcmds")
go bot.MainLoop()
waitForServer(server, 4)

// this sleep allow us to keep the answer in the right order
Expand Down Expand Up @@ -84,7 +85,7 @@ func TestBotBotIRC(t *testing.T) {

// test shutdown - should probably be separate test

botbot.shutdown()
bot.Shutdown()

tries = 0
val := 5
Expand Down
1 change: 1 addition & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker-compose run bot go test -v ./...
7 changes: 5 additions & 2 deletions sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ CREATE TABLE "bots_chatbot" (
"server_password" varchar(100),
"nick" varchar(64) NOT NULL,
"password" varchar(100),
"real_name" varchar(250) NOT NULL
"real_name" varchar(250) NOT NULL,
"server_identifier" varchar(164)
)
;
CREATE TABLE "bots_channel" (
Expand All @@ -18,7 +19,9 @@ CREATE TABLE "bots_channel" (
"password" varchar(250),
"is_public" boolean NOT NULL,
"is_active" boolean NOT NULL,
"is_featured" boolean NOT NULL
"is_featured" boolean NOT NULL,
"fingerprint" varchar(36),
"status" varchar(20)
)
;
CREATE TABLE "bots_usercount" (
Expand Down
Loading