dockron/main.go

109 lines
2.9 KiB
Go
Raw Normal View History

2018-08-01 15:56:13 +00:00
package main
import (
2018-08-04 00:45:53 +00:00
"flag"
2018-08-01 15:56:13 +00:00
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/robfig/cron"
"golang.org/x/net/context"
"strings"
"time"
)
2018-08-04 00:30:17 +00:00
// WatchInterval is the duration we should sleep until polling Docker
2018-08-04 00:45:53 +00:00
var DefaultWatchInterval = (1 * time.Minute)
2018-08-03 01:56:02 +00:00
2018-08-04 00:30:17 +00:00
// SchedLabel is the string label to search for cron expressions
2018-08-04 00:45:53 +00:00
var SchedLabel = "dockron.schedule"
2018-08-04 00:30:17 +00:00
// ContainerStartJob represents a scheduled container task
// It contains a reference to a client, the schedule to run on, and the
// ID of that container that should be started
2018-08-01 15:56:13 +00:00
type ContainerStartJob struct {
Client *client.Client
ContainerID string
Context context.Context
Name string
Schedule string
}
2018-08-04 00:30:17 +00:00
// Run is executed based on the ContainerStartJob Schedule and starts the
// container
2018-08-01 15:56:13 +00:00
func (job ContainerStartJob) Run() {
2018-08-03 01:56:02 +00:00
fmt.Println("Starting:", job.Name)
2018-08-01 15:56:13 +00:00
err := job.Client.ContainerStart(job.Context, job.ContainerID, types.ContainerStartOptions{})
if err != nil {
panic(err)
}
}
2018-08-04 00:30:17 +00:00
// QueryScheduledJobs queries Docker for all containers with a schedule and
// returns a list of ContainerStartJob records to be scheduled
2018-08-03 01:56:02 +00:00
func QueryScheduledJobs(cli *client.Client) (jobs []ContainerStartJob) {
fmt.Println("Scanning containers for new schedules...")
2018-08-01 15:56:13 +00:00
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
if err != nil {
panic(err)
}
for _, container := range containers {
2018-08-04 00:30:17 +00:00
if val, ok := container.Labels[SchedLabel]; ok {
2018-08-01 15:56:13 +00:00
jobName := strings.Join(container.Names, "/")
2018-08-01 15:59:52 +00:00
jobs = append(jobs, ContainerStartJob{
2018-08-01 15:56:13 +00:00
Schedule: val,
Client: cli,
ContainerID: container.ID,
Context: context.Background(),
Name: jobName,
})
}
}
2018-08-03 01:56:02 +00:00
return
}
2018-08-01 15:59:52 +00:00
2018-08-04 00:30:17 +00:00
// ScheduleJobs accepts a Cron instance and a list of jobs to schedule.
// It then schedules the provided jobs
2018-08-03 01:56:02 +00:00
func ScheduleJobs(c *cron.Cron, jobs []ContainerStartJob) {
2018-08-01 15:59:52 +00:00
for _, job := range jobs {
2018-08-03 01:56:02 +00:00
fmt.Printf("Scheduling %s (%s) with schedule '%s'\n", job.Name, job.ContainerID[:10], job.Schedule)
2018-08-01 15:59:52 +00:00
c.AddJob(job.Schedule, job)
}
2018-08-03 01:56:02 +00:00
}
func main() {
2018-08-04 00:30:17 +00:00
// Get a Docker Client
2018-08-03 01:56:02 +00:00
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
2018-08-01 15:59:52 +00:00
2018-08-04 00:45:53 +00:00
// Read interval for polling Docker
var watchInterval time.Duration
flag.DurationVar(&watchInterval, "watch", DefaultWatchInterval, "Interval used to poll Docker for changes")
flag.Parse()
2018-08-04 00:30:17 +00:00
// Create a Cron
2018-08-03 01:56:02 +00:00
c := cron.New()
2018-08-01 15:56:13 +00:00
// Start the loop
for {
2018-08-03 01:56:02 +00:00
// HACK: This is risky as it could fall on the same interval as a task and that task would get skipped
// It would be best to manage a ContainerID to Job mapping and then remove entries that are missing
// in the new list and add new entries. However, cron does not support this yet.
// Stop and create a new cron
c.Stop()
c = cron.New()
// Schedule jobs again
jobs := QueryScheduledJobs(cli)
ScheduleJobs(c, jobs)
c.Start()
// Sleep until the next query time
2018-08-04 00:45:53 +00:00
time.Sleep(watchInterval)
2018-08-01 15:56:13 +00:00
}
}