Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a feature to run existing, but stopped containers #42

Merged
merged 4 commits into from
Jun 21, 2018
Merged
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ sudo: false

go:
- 1.9.x
- 1.10.x

script:
- make test-coverage
Expand Down
21 changes: 14 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
FROM golang:1.7
MAINTAINER Máximo Cuadros <mcuadros@gmail.com>
FROM golang:1.10.0 AS builder

ADD . ${GOPATH}/src/github.com/mcuadros/ofelia
WORKDIR ${GOPATH}/src/github.com/mcuadros/ofelia
COPY . ${GOPATH}/src/github.com/mcuadros/ofelia

RUN go get -v ./... \
&& go install -v ./... \
&& rm -rf $GOPATH/src/
ENV CGO_ENABLED 0
ENV GOOS linux

RUN go get -v ./...
RUN go build -a -installsuffix cgo -ldflags '-w -extldflags "-static"' -o /go/bin/ofelia .

FROM scratch

COPY --from=builder /go/bin/ofelia /usr/bin/ofelia

VOLUME /etc/ofelia/
CMD ["ofelia", "daemon", "--config", "/etc/ofelia/config.ini"]
ENTRYPOINT ["/usr/bin/ofelia"]

CMD ["daemon", "--config", "/etc/ofelia/config.ini"]
47 changes: 34 additions & 13 deletions core/runjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,36 @@ func init() {

type RunJob struct {
BareJob
Client *docker.Client `json:"-"`
User string `default:"root"`
TTY bool `default:"false"`
Delete bool `default:"true"`
Image string
Network string
Client *docker.Client `json:"-"`
User string `default:"root"`
TTY bool `default:"false"`
Delete bool `default:"true"`
Image string
Network string
Container string
}

func NewRunJob(c *docker.Client) *RunJob {
return &RunJob{Client: c}
}

func (j *RunJob) Run(ctx *Context) error {
if err := j.pullImage(); err != nil {
return err
}
var container *docker.Container
var err error
if j.Image != "" && j.Container == "" {
if err = j.pullImage(); err != nil {
return err
}

container, err := j.buildContainer()
if err != nil {
return err
container, err = j.buildContainer()
if err != nil {
return err
}
} else {
container, err = j.getContainer(j.Container)
if err != nil {
return err
}
}

if err := j.startContainer(ctx.Execution, container); err != nil {
Expand All @@ -46,7 +56,10 @@ func (j *RunJob) Run(ctx *Context) error {
return err
}

return j.deleteContainer(container.ID)
if j.Container == "" {
return j.deleteContainer(container.ID)
}
return nil
}

func (j *RunJob) pullImage() error {
Expand Down Expand Up @@ -98,6 +111,14 @@ func (j *RunJob) startContainer(e *Execution, c *docker.Container) error {
return j.Client.StartContainer(c.ID, &docker.HostConfig{})
}

func (j *RunJob) getContainer(id string) (*docker.Container, error) {
container, err := j.Client.InspectContainer(id)
if err != nil {
return nil, err
}
return container, nil
}

const (
watchDuration = time.Millisecond * 100
maxProcessDuration = time.Hour * 24
Expand Down
13 changes: 13 additions & 0 deletions test/run-job/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "3"

services:
ofelia:
build:
dockerfile: Dockerfile
context: ../../.
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./ofelia.ini:/etc/ofelia/config.ini

job:
image: hello-world
3 changes: 3 additions & 0 deletions test/run-job/ofelia.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[job-run"job"]
schedule = "*/1 * * * *"
container = ofelia_job_1