cronicle

package
v0.3.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2022 License: MIT Imports: 38 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrBranchAndCommitGiven is thrown because commit and branch are mutually exclusive to identify a repo
	ErrBranchAndCommitGiven = errors.New("branch and commit can not both be populated")
	//ErrRepoNotGiven is thrown because a git repo is not given, for the case where Checkout or other git
	//specific methods are called
	ErrRepoNotGiven = errors.New("git repo has not been given")
	//ErrIfRepoGivenAndPathNotGiven is thrown because a repo was given but the path to the local repo has not been provided
	ErrIfRepoGivenAndPathNotGiven = errors.New("if repo is populated, path must also be given at runtime")
	//ErrScheduleNameEmpty is thrown because schedule.Name == "", hcl can not be given with schedule "" {}
	ErrScheduleNameEmpty = errors.New("schedule name can not be an empty string")
	//ErrTaskNameEmpty is thrown because task.Name == "", hcl can not be given with task "" {}
	ErrTaskNameEmpty = errors.New("task name can not be an empty string")
	//ErrRepoGivenAndURLNotGiven is thrown because task.Name == "", hcl can not be given with task "" {}
	ErrRepoGivenAndURLNotGiven = errors.New("if repo is populated, it must have an assoicated url")
)
View Source
var (
	// CommandEvalContext hcl.EvalContext evaluates the "${date}" argument and carries it through
	// as a string of the same form that will be used as an arugment later in the code.
	CommandEvalContext = hcl.EvalContext{
		Variables: map[string]cty.Value{
			"date":      cty.StringVal("${date}"),
			"datetime":  cty.StringVal("${datetime}"),
			"timestamp": cty.StringVal("${timestamp}"),
		},
	}

	// TimeArgumentFormatMap maps the CommandEvalContext arguments to time.Format strings for reforamting
	// arguments given in hcl to timestamps.
	// ${date}: 		"2006-01-02"
	// ${datetime}: 	"2006-01-02T15:04:05Z07:00"
	// ${timestamp}: 	"2006-01-02 15:04:05Z07:00"
	TimeArgumentFormatMap = map[string]string{
		"${date}":      "2006-01-02",
		"${datetime}":  time.RFC3339,
		"${timestamp}": "2006-01-02 15:04:05Z07:00",
	}
)

Functions

func Commit

func Commit(worktreeDir string, msg string)

Commit does a git commit on the repository at worktree

func ConsumeSchedule

func ConsumeSchedule(queue <-chan []byte, path string, wg *sync.WaitGroup)

ConsumeSchedule consumes the byte array of a schedule from the message queue for execution

func DirExists

func DirExists(dirname string) bool

DirExists checks that a directory exists.

func ExecTasks

func ExecTasks(cronicleFile string, taskName string, scheduleName string, now time.Time)

ExecTasks parses the cronicle.hcl config, filters for a specified task and executes the task

func GetRepos

func GetRepos(conf *Config) map[string]bool

GetRepos collects the set of repos associated to a given config

func Init

func Init(croniclePath string, cloneRepo string, deployKey string, defaultConf Config)

Init initializes a default croniclePath with a .git repository, Basic schedule as code in a cronicle.hcl file and a repos folder.

func LoadCron

func LoadCron(cronicleFile string, c *cron.Cron, queue chan<- []byte, force bool)

LoadCron exeutes GetConfig(cronicleFile) to load the current config from file, checks the given config against the global confPrior, and if there is a change, stops the cron, removes all of the confPrior cron entries and adds the new conf schedules to the cron.

func LocalRepoDir

func LocalRepoDir(croniclePath string, repoURL string) (string, error)

LocalRepoDir takes a cronicle.hcl path and a github repo URL and converts it to the local clone of that repo

func MakeViceTransport

func MakeViceTransport(queueType string, addr string) vice.Transport

MakeViceTransport creates a vice.Transport interface from the given queue field in the config

func MarshallHcl

func MarshallHcl(conf Config, path string) string

MarshallHcl writes a given Config to an hcl file at path

func ProduceSchedule

func ProduceSchedule(schedule Schedule, queue chan<- []byte) func()

ProduceSchedule produces the json of a schdule to the message queue for consumption

func Run

func Run(cronicleFile string, runOptions RunOptions)

Run is the main function of the cron package

func StartCron

func StartCron(cronicleFile string, queue chan<- []byte)

StartCron pushes all schedules in the given config to the cron scheduler starts the cron scheduler which publishes the serialzied schedules to the message queue for execution.

func StartWorker

func StartWorker(path string, runOptions RunOptions)

StartWorker listens to a vice transport queue for schedules produced by cronicle run

Types

type Config

type Config struct {
	// Repo repository containing version controlled cronicle.hcl
	Repo *Repo `hcl:"repo,block"`
	// Cron expression that specifies the cronicle heartbeat and cronicle.hcl refresh
	Heartbeat string `hcl:"heartbeat,optional"`
	// Repos points at external dependent repos that maintain their own schedules remotly.
	Repos []string `hcl:"repos,optional"`
	// Timezone Location to run cron in. i.e. "America/New_York" [IANA Time Zone database]
	// https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
	Timezone string `hcl:"timezone,optional"`
	// GitRemote *GitRemote `hcl:"git,block"`
	Queue     *Queue     `hcl:"queue,block"`
	Schedules []Schedule `hcl:"schedule,block"`
}

Config is the configuration structure for the cronicle checker. https://raw.githubusercontent.com/mitchellh/golicense/master/config/config.go TODO: Add Version string `hcl:"version,optional"`

func Default

func Default() Config

Default returns a basic default Config it includes a single schedule that runs every 5 seconds and a single "Hello World" task.

func GetConfig

func GetConfig(cronicleFile string) (*Config, error)

GetConfig returns the Config specified by the given cronicle.hcl file Including any Cronicle files specified by in the repos directory.

func ParseFile added in v0.2.0

func ParseFile(cronicleFile string, parser *hclparse.Parser) (*Config, hcl.Diagnostics)

ParseFile parses a given hcl file into a Config

func (Config) Hcl

func (conf Config) Hcl() HclWriteFile

Hcl returns a hcl File object from a given Config

func (*Config) Init

func (conf *Config) Init(croniclePath string) error

Init populates task repo path, runs git clone for any sub repos, and assigns Git meta data to the task

func (Config) JSON

func (conf Config) JSON() []byte

JSON method returns a json []byte array of the struct

func (*Config) PropigateTaskProperties

func (conf *Config) PropigateTaskProperties(croniclePath string)

PropigateTaskProperties pushes schedule.Name, schedule.Repo and the repo path down to the task values. It also populates task.Git.ReferenceName with task.Branch or HEAD.

func (*Config) ScheduleMap added in v0.3.1

func (conf *Config) ScheduleMap() ScheduleMap

ScheduleMap exports a ScheduleMap all schedules in a given config

func (*Config) TaskArray

func (conf *Config) TaskArray() TaskArray

TaskArray exports a TaskArray all tasks in a given config, additionally, it ensures that task.ScheduleName is propigated

func (*Config) Validate

func (conf *Config) Validate() error

Validate checks that schedule.Name is not empty and assigns task.ScheduleName on a whole config struct.

type Git

type Git struct {
	Worktree      *git.Worktree
	Repository    *git.Repository
	Head          *plumbing.Reference
	Hash          *plumbing.Hash
	Commit        *object.Commit
	ReferenceName plumbing.ReferenceName
	// contains filtered or unexported fields
}

Git is the struct which associates common data structures from the go-git library.

func Clone

func Clone(worktreeDir string, url string, auth *transport.AuthMethod) (Git, error)

Clone checks for the existance of worktreeDir/.git and clones if it does not exist then executes Git = GetGit(worktreeDir)

func (*Git) Checkout

func (g *Git) Checkout(branch string, commit string) error

Checkout does a git fetch for task.Repo and does a git checkout for the given task.Branch or task.Commit. Note: Only one can be given, branch or commit. Checkout requires task.Repo to be given

func (*Git) Open

func (g *Git) Open(worktreePath string) error

Open populates a git struct for the given worktreePath

type HclWriteFile

type HclWriteFile struct {
	// File is the hclwrite.File encoded by gohcl.EncodeIntoBody
	File hclwrite.File

	// Bytes is the byte array with deduped $$
	Bytes []byte
}

HclWriteFile contains the encoded hclwrite.File and the byte array of the file with the $$ deduped. This is due the the effect that when writing template arguments with the hcl library an extra $ will be added automatically. i.e. "${date}" becomes "$${date}"

type Queue

type Queue struct {
	//Type names the message queue technology to be used
	//options are nsq and redis
	Type string `hcl:"type,optional"`
	//host:port of nsqd/nsqlookupd/redis queue service
	Addr string `hcl:"addr,optional"`
}

Queue is the metadata associated to the message queue for distributed operation. Cronicle uses vice to communicate with queues via channels. https://github.com/matryer/vice

type Repo added in v0.2.0

type Repo struct {
	// URL is the remote git repository, a local path to git repository
	URL string `hcl:"url,optional"`
	// DeployKey is the path to the rsa private key that enables pull access to a
	// private remote repository.
	DeployKey string `hcl:"key,optional"`
	Commit    string `hcl:"branch,optional"`
	Branch    string `hcl:"commit,optional"`
}

Repo is the structure that defines a git repository

func (*Repo) Auth added in v0.2.0

func (repo *Repo) Auth() (transport.AuthMethod, error)

Auth authroizes a repository if from a local rsa key

type Retry

type Retry struct {
	//Count: Number of retry attemts to make after first attempt
	Count int `hcl:"count,optional"`
	//Seconds: Number of seconds to wait between retry attempts
	Seconds int `hcl:"seconds,optional"`
	//Minutes: Number of minutes to wait between retry attempts
	Minutes int `hcl:"minutes,optional"`
	//Hours: Number of hours to wait between retry attempts
	Hours int `hcl:"hours,optional"`
}

Retry defines the retry count and delay in number and seconds.

type RunOptions

type RunOptions struct {
	RunWorker bool
	QueueType string
	QueueName string
	Addr      string
	LogToFile bool
}

RunOptions enables the runtime configuration of the distributed message queue

type Schedule

type Schedule struct {
	// Cron is the schedule interval. The field accepts standard cron
	// and other configurations listed here https://godoc.org/gopkg.in/robfig/cron.v2
	// i.e. ["@hourly", "@every 1h30m", "0 30 * * * *", "TZ=Asia/Tokyo 30 04 * * * *"]
	Name string `hcl:"name,label"`
	Cron string `hcl:"cron,optional"`
	// Timezone Location to run cron in. i.e. "America/New_York" [IANA Time Zone database]
	// https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
	Timezone  string `hcl:"timezone,optional"`
	StartDate string `hcl:"start_date,optional"`
	EndDate   string `hcl:"end_date,optional"`
	Repo      *Repo  `hcl:"repo,block"`
	Tasks     []Task `hcl:"task,block"`
	//Now is the execution time of the given schedule that will be used to
	//fill variable task command ${datetime}. The cron scheduler generally provides
	//the value.
	Now time.Time
	//repo given at the config level, will be overridden by repo given at schedule or task level.
	CronicleRepo *Repo
}

Schedule is the configuration structure that defines a cron job consisting of tasks.

func (*Schedule) CleanGit

func (schedule *Schedule) CleanGit()

CleanGit nulls non-serlizable properties of a schedule task.Git = Git{}

func (Schedule) ExecuteTasks

func (schedule Schedule) ExecuteTasks()

ExecuteTasks handels the execution of all tasks in a given schedule. The execution walks over a DAG[Directed Acyclic Graph] to determine execution order, which will default to parallel unless task.depends is specified.

func (Schedule) JSON

func (schedule Schedule) JSON() []byte

JSON method returns a json []byte array of the struct

func (*Schedule) PropigateTaskProperties

func (schedule *Schedule) PropigateTaskProperties(croniclePath string)

PropigateTaskProperties pushes schedule.Name, schedule.Repo and the repo path down to the task values. It also populates task.Git.ReferenceName with task.Branch or HEAD.

func (*Schedule) TaskMap

func (schedule *Schedule) TaskMap() TaskMap

TaskMap exports a TaskMap all tasks in a given config,

type ScheduleMap added in v0.3.1

type ScheduleMap map[string]Schedule

ScheduleMap is an map of key=schedul.Name: value=Schedule struct,

type TZFormatter added in v0.2.2

type TZFormatter struct {
	log.Formatter
	// contains filtered or unexported fields
}

TZFormatter enables timezone specifc logrus formatting Example: loc, _ = time.LoadLocation("America/Los_Angeles")

log.SetFormatter(TZFormatter{Formatter: &log.TextFormatter{
	FullTimestamp: true,
	}, loc: loc})

func (TZFormatter) Format added in v0.2.2

func (u TZFormatter) Format(e *log.Entry) ([]byte, error)

Format sets the timezone for the given loc *time.Timezone

type Task

type Task struct {
	Name         string   `hcl:"name,label"`
	Command      []string `hcl:"command,optional"`
	Depends      []string `hcl:"depends,optional"`
	Repo         *Repo    `hcl:"repo,block"`
	Retry        *Retry   `hcl:"retry,block"`
	Env          []string `hcl:"env,optional"`
	Path         string
	CronicleRepo *Repo
	CroniclePath string
	Git          Git
	ScheduleName string
}

Task is the configuration structure that defines a task (i.e., a command)

func (*Task) CleanGit

func (task *Task) CleanGit()

CleanGit nulls non-serlizable properties of a task task.Git = Git{}

func (*Task) Exec

func (task *Task) Exec(t time.Time) exec.Result

Exec executes task.Command at task.Path and returns the exec.Result struct prior to execution, the command will replace any ${date}, ${datetime}, ${timestamp} with time t given in the bash command

func (*Task) Execute

func (task *Task) Execute(t time.Time) (exec.Result, error)

Execute does a git pull, git checkout and exec's the given command

func (Task) Hcl

func (task Task) Hcl() HclWriteFile

Hcl returns a hcl File object from a given task

func (Task) JSON

func (task Task) JSON() []byte

JSON method returns a json []byte array of the struct

func (*Task) Log

func (task *Task) Log(res exec.Result)

Log logs the exit status, stderr, git commit and other logging data.

func (*Task) Validate

func (task *Task) Validate() error

Validate validates the fields and sets the default values.

type TaskArray

type TaskArray []Task

TaskArray is an array of Task structs, calling config.TaskArray() ensures that each task.ScheduleName is filled

func (TaskArray) FilterTasks

func (t TaskArray) FilterTasks(taskName string, scheduleName string) TaskArray

FilterTasks returns a task array where only matching task.Name = taskName and schedule.Name=scheduleName if taskName = "" and scheduleName = "" then all tasks will be returned empty strings are intrepreted as no filtering requested.

type TaskMap

type TaskMap map[string]Task

TaskMap is an map of key=task.Name: value=Task struct,

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL