backy/pkg/backy/types.go
Andrew 059f4c0097 added some features
- Added `cron` command to run lists with `cron` time specifed
- Changed `-c` flag to `-f` flag for passing config file
- Modified some config keys
  - cmdArgs -> Args
  - Got rid of `hosts.config`
- better SSH handling
  - respects values in config file
2023-02-01 23:54:48 -06:00

169 lines
4.3 KiB
Go

// types.go
// Copyright (C) Andrew Woodlee 2023
// License: Apache-2.0
package backy
import (
"bytes"
"github.com/kevinburke/ssh_config"
"github.com/nikoksr/notify"
"github.com/rs/zerolog"
"github.com/spf13/viper"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"golang.org/x/crypto/ssh"
)
type CmdConfigSchema struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
CmdList []string `bson:"command-list,omitempty"`
Name string `bson:"name,omitempty"`
}
type CmdSchema struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Cmd string `bson:"cmd,omitempty"`
Args []string `bson:"args,omitempty"`
Host string `bson:"host,omitempty"`
Dir string `bson:"dir,omitempty"`
}
type Schemas struct {
CmdConfigSchema
CmdSchema
}
// Host defines a host to which to connect.
// If not provided, the values will be looked up in the default ssh config files
type Host struct {
ConfigFilePath string `yaml:"configfilepath,omitempty"`
Host string `yaml:"host,omitempty"`
HostName string `yaml:"hostname,omitempty"`
KnownHostsFile string `yaml:"knownhostsfile,omitempty"`
ClientConfig *ssh.ClientConfig
SSHConfigFile sshConfigFile
Port uint16 `yaml:"port,omitempty"`
JumpHost string `yaml:"jumphost,omitempty"`
Password string `yaml:"password,omitempty"`
PrivateKeyPath string `yaml:"privatekeypath,omitempty"`
PrivateKeyPassword string `yaml:"privatekeypassword,omitempty"`
UseConfigFiles bool `yaml:"use_config_files,omitempty"`
useDefaultConfig bool
User string `yaml:"user,omitempty"`
// ProxyHost holds the configuration for a JumpHost host
ProxyHost *Host
}
type sshConfigFile struct {
SshConfigFile *ssh_config.Config
DefaultUserSettings *ssh_config.UserSettings
}
type Command struct {
// Remote bool `yaml:"remote,omitempty"`
Output BackyCommandOutput `yaml:"-"`
// command to run
Cmd string `yaml:"cmd"`
// host on which to run cmd
Host *string `yaml:"host,omitempty"`
/*
Shell specifies which shell to run the command in, if any.
Not applicable when host is defined.
*/
Shell string `yaml:"shell,omitempty"`
RemoteHost *Host `yaml:"-"`
// Args is an array that holds the arguments to cmd
Args []string `yaml:"Args,omitempty"`
/*
Dir specifies a directory in which to run the command.
Ignored if Host is set.
*/
Dir *string `yaml:"dir,omitempty"`
// Env points to a file containing env variables to be used with the command
Env string `yaml:"env,omitempty"`
// Environment holds env variables to be used with the command
Environment []string `yaml:"environment,omitempty"`
}
type BackyOptionFunc func(*BackyConfigOpts)
type CmdList struct {
Name string `yaml:"name,omitempty"`
Cron string `yaml:"cron,omitempty"`
Order []string `yaml:"order,omitempty"`
Notifications []string `yaml:"notifications,omitempty"`
NotifyConfig *notify.Notify
// NotificationsConfig map[string]*NotificationsConfig
// NotifyConfig map[string]*notify.Notify
}
type BackyConfigFile struct {
// Cmds holds the commands for a list.
// Key is the name of the command,
Cmds map[string]*Command `yaml:"commands"`
// CmdConfigLists holds the lists of commands to be run in order.
// Key is the command list name.
CmdConfigLists map[string]*CmdList `yaml:"cmd-configs"`
// Hosts holds the Host config.
// key is the host.
Hosts map[string]*Host `yaml:"hosts"`
// Notifications holds the config for different notifications.
Notifications map[string]*NotificationsConfig
Logger zerolog.Logger
}
type BackyConfigOpts struct {
// Global log level
BackyLogLvl *string
// Holds config file
ConfigFile *BackyConfigFile
// Holds config file
ConfigFilePath string
Schemas
DB *mongo.Database
// use command lists using cron
useCron bool
// Holds commands to execute for the exec command
executeCmds []string
// Holds commands to execute for the exec command
executeLists []string
viper *viper.Viper
}
type NotificationsConfig struct {
Config *viper.Viper
Enabled bool
}
type CmdOutput struct {
Err error
Output bytes.Buffer
}
type BackyCommandOutput interface {
Error() error
GetOutput() CmdOutput
}
type environmentVars struct {
file string
env []string
}