Compare commits

..

No commits in common. "5429a4147e83c310982f87c85a40430c4bf13d3f" and "a08f9ce5a090545dbe5e30813697c039447cdc68" have entirely different histories.

9 changed files with 73 additions and 306 deletions

3
.gitmodules vendored
View file

@ -1,3 +0,0 @@
[submodule "makeshift.wiki"]
path = makeshift.wiki
url = https://git.towk2.me/towk/makeshift.wiki.git

View file

@ -1,27 +1,14 @@
FROM alpine:latest AS builder FROM cgr.dev/chainguard/wolfi-base
RUN apk add go git gcc binutils bash RUN apk add --no-cache tini bash
RUN mkdir -p /configurator
WORKDIR /tmp # nobody 65534:65534
RUN git clone https://git.towk2.me/towk/makeshift.git USER 65534:65534
WORKDIR /tmp/makeshift # copy the binary and all of the default plugins
COPY configurator /configurator/configurator
RUN go mod tidy && \ CMD ["/configurator/configurator"]
go build && \
mkdir tmp && \
./makeshift plugins compile ./pkg/plugins/jinja2/jinja2.go -o ./tmp/plugins/jinja.so
ENTRYPOINT [ "/sbin/tini", "--" ]
FROM alpine:latest
COPY --from=builder /tmp/makeshift/makeshift /usr/local/bin
COPY --from=builder /tmp/makeshift/tmp/plugins/* /makeshift/server/plugins/
RUN chmod +x /usr/local/bin/makeshift
RUN mkdir -p /makeshift/logs && \
touch /makeshift/logs/makeshift.log
ENTRYPOINT ["/usr/local/bin/makeshift"]

View file

@ -1,115 +0,0 @@
package cmd
import (
"os"
"git.towk2.me/towk/makeshift/pkg/util"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
const CONFIG_FILE = `---
#
# Makeshift Config
#
# Repository: https://git.towk2.me/towk/makeshift
#
# Default configuration file for 'makeshift' CLI and service.
# This file was autogenerated using 'makeshift config new' command.
#
# Set the service host.
host: http://localhost:5050
# Set the path to the file or directory to download.
path: help.txt
# Set the log file path. Logs will be written to this location.
log-file: logs/makeshift.log
# Set the log level. Possible values include 'debug', 'info', 'warn',
# 'error', 'disabled', and 'trace'.
log-level: info
# Set the plugins to use when downloading files and/or directories.
plugins:
- smd
- jinja2
# Set the positional arguments to pass to ALL plugins when downloading
# files and directories. NOTE: These arguments may be ignored or not
# used within certain plugins.
plugin-args:
# Set the key-word arguments stored in the data that is passed to ALL
# plugins when downloading files and directories. NOTE: These arguments
# may be ignored or not used within certain plugins.
plugin-kwargs:
# Set the profiles to use when downloading files and/or directories.
profiles:
- default
# Set whether to extract the archive when downloading directories.
extract: false
# Set whether to remove an archive after downloading and extracting.
# This requires that the 'extract' flag be set to 'true'.
remove-archive: false
# Set the path to a CA certificate.
cacert: ""
# Set the path to a CA key file.
keyfile: ""
`
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage makeshift config file",
}
var configNewCmd = &cobra.Command{
Use: "new [path]",
Example: `
# create a new default config at specified path
makeshift config new configs/makeshift.yaml
`,
Args: cobra.ExactArgs(1),
Short: "Create a new config file",
Run: func(cmd *cobra.Command, args []string) {
for _, path := range args {
var (
overwrite, _ = cmd.Flags().GetBool("overwrite")
exists bool
err error
)
if exists, err = util.PathExists(path); err != nil {
log.Error().
Err(err).
Str("path", path).
Msg("failed to determine if path exists")
} else {
if exists && !overwrite {
log.Error().Msg("file exists and '--overwrite' flag not passed")
return
}
err := os.WriteFile(path, []byte(CONFIG_FILE), 0o644)
if err != nil {
log.Error().
Err(err).
Str("path", path).
Msg("failed to write diefault config file to path")
continue
}
}
}
},
}
func init() {
configNewCmd.Flags().BoolP("overwrite", "f", false, "Set whether to overwrite an existing file")
configCmd.AddCommand(configNewCmd)
rootCmd.AddCommand(configCmd)
}

View file

@ -49,7 +49,6 @@ var downloadCmd = cobra.Command{
host, _ = cmd.Flags().GetString("host") host, _ = cmd.Flags().GetString("host")
path, _ = cmd.Flags().GetString("path") path, _ = cmd.Flags().GetString("path")
outputPath, _ = cmd.Flags().GetString("output") outputPath, _ = cmd.Flags().GetString("output")
configPath, _ = cmd.Flags().GetString("config")
cacertPath, _ = cmd.Flags().GetString("cacert") cacertPath, _ = cmd.Flags().GetString("cacert")
pluginNames, _ = cmd.Flags().GetStringSlice("plugins") pluginNames, _ = cmd.Flags().GetStringSlice("plugins")
profileIDs, _ = cmd.Flags().GetStringSlice("profiles") profileIDs, _ = cmd.Flags().GetStringSlice("profiles")
@ -63,7 +62,6 @@ var downloadCmd = cobra.Command{
err error err error
) )
// build download query URL
query = fmt.Sprintf("/download/%s?", path) query = fmt.Sprintf("/download/%s?", path)
if len(pluginNames) > 0 { if len(pluginNames) > 0 {
query += "plugins=" + url.QueryEscape(strings.Join(pluginNames, ",")) query += "plugins=" + url.QueryEscape(strings.Join(pluginNames, ","))
@ -81,7 +79,6 @@ var downloadCmd = cobra.Command{
log.Debug(). log.Debug().
Str("host", host). Str("host", host).
Str("path", path). Str("path", path).
Str("config", configPath).
Str("query", query). Str("query", query).
Str("output", outputPath). Str("output", outputPath).
Strs("profiles", profileIDs). Strs("profiles", profileIDs).

View file

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strings" "strings"
logger "git.towk2.me/towk/makeshift/pkg/log" logger "git.towk2.me/towk/makeshift/pkg/log"
@ -21,14 +20,14 @@ var (
var rootCmd = cobra.Command{ var rootCmd = cobra.Command{
Use: "makeshift", Use: "makeshift",
Short: "Extensible file cobbler", Short: "Extensible file cobbler",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { PersistentPreRun: func(cmd *cobra.Command, args []string) {
var ( var (
logFile, _ = cmd.Flags().GetString("log-file") logFile string
configPath, _ = cmd.Flags().GetString("config")
err error err error
) )
// initialize the logger // initialize the logger
logFile, _ = cmd.Flags().GetString("log-file")
err = logger.InitWithLogLevel(loglevel, logFile) err = logger.InitWithLogLevel(loglevel, logFile)
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to initialize logger") log.Error().Err(err).Msg("failed to initialize logger")
@ -36,13 +35,12 @@ var rootCmd = cobra.Command{
} }
// You can bind cobra and viper in a few locations, but PersistencePreRunE on the root command works well // You can bind cobra and viper in a few locations, but PersistencePreRunE on the root command works well
return initConfig(cmd, configPath) err = initializeConfig(cmd)
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// try and set flags using env vars // try and set flags using env vars
setenv(cmd, "log-file", "MAKESHIFT_LOG_FILE") setenv(cmd, "log-file", "MAKESHIFT_LOG_FILE")
setenv(cmd, "log-level", "MAKESHIFT_LOG_LEVEL") setenv(cmd, "log-level", "MAKESHIFT_LOG_LEVEL")
setenv(cmd, "config", "MAKESHIFT_CONFIG_FILE")
if len(args) == 0 { if len(args) == 0 {
err := cmd.Help() err := cmd.Help()
if err != nil { if err != nil {
@ -56,7 +54,6 @@ var rootCmd = cobra.Command{
err := logger.LogFile.Close() err := logger.LogFile.Close()
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to close log file") log.Error().Err(err).Msg("failed to close log file")
os.Exit(1)
} }
}, },
} }
@ -74,83 +71,8 @@ func init() {
initLogger, initLogger,
) )
// initialize the config a single time // initialize the config a single time
rootCmd.PersistentFlags().VarP(&loglevel, "log-level", "l", "Set the log level output (can be set with MAKESHIFT_LOG_LEVEL)") rootCmd.PersistentFlags().VarP(&loglevel, "log-level", "l", "Set the log level output")
rootCmd.PersistentFlags().String("log-file", "", "Set the log file path (can be set with MAKESHIFT_LOG_FILE)") rootCmd.PersistentFlags().String("log-file", "", "Set the log file path (can be set with MAKESHIFT_LOG_FILE)")
rootCmd.PersistentFlags().StringP("config", "c", "", "Set the config file path (can be set with MAKESHIFT_CONFIG_FILE)")
}
func initLogger() {
// initialize the logger
logfile, _ := rootCmd.PersistentFlags().GetString("log-file")
err := logger.InitWithLogLevel(loglevel, logfile)
if err != nil {
log.Error().Err(err).Msg("failed to initialize logger")
os.Exit(1)
}
}
func initConfig(cmd *cobra.Command, path string) error {
// Dissect the path to separate config name from its directory
var (
isFlagSet = cmd.Flags().Changed("config")
filename = filepath.Base(path)
ext = filepath.Ext(filename)
directory = filepath.Dir(path)
v = viper.New()
)
// The 'config' flag not set, so don't continue
if !isFlagSet {
return nil
}
// Only use specified YAML file from --config or -c flag
v.SetConfigName(strings.TrimSuffix(filename, ext))
v.SetConfigType("yaml")
v.AddConfigPath(directory)
// Attempt to read the config file. Return an error if we cannot parse
// the config file or if it is not found.
if err := v.ReadInConfig(); err != nil {
if isFlagSet {
// It's okay if there isn't a config file when no path is provided
// if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
// return err
// }
switch err.(type) {
case viper.ConfigFileNotFoundError:
log.Error().
Err(err).
Str("path", path).
Msg("failed to read config")
os.Exit(1)
default:
log.Error().Err(err).Msg("failed to read config")
os.Exit(1)
}
}
}
// When we bind flags to environment variables expect that the
// environment variables are prefixed, e.g. a flag like --number
// binds to an environment variable STING_NUMBER. This helps
// avoid conflicts.
v.SetEnvPrefix("MAKESHIFT")
// Environment variables can't have dashes in them, so bind them to their equivalent
// keys with underscores, e.g. --favorite-color to STING_FAVORITE_COLOR
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
// Bind to environment variables
// Works great for simple config names, but needs help for names
// like --favorite-color which we fix in the bindFlags function
v.AutomaticEnv()
// Bind the current command's flags to viper
bindFlags(cmd, v)
return nil
} }
func setenv(cmd *cobra.Command, varname string, envvar string) { func setenv(cmd *cobra.Command, varname string, envvar string) {
@ -173,6 +95,16 @@ func setenvp(cmd *cobra.Command, varname string, envvar string) {
} }
} }
func initLogger() {
// initialize the logger
logfile, _ := rootCmd.PersistentFlags().GetString("log-file")
err := logger.InitWithLogLevel(loglevel, logfile)
if err != nil {
log.Error().Err(err).Msg("failed to initialize logger")
os.Exit(1)
}
}
func handleResponseError(res *http.Response, host, query string, err error) { func handleResponseError(res *http.Response, host, query string, err error) {
if err != nil { if err != nil {
log.Error().Err(err). log.Error().Err(err).
@ -215,3 +147,44 @@ func bindFlags(cmd *cobra.Command, v *viper.Viper) {
} }
}) })
} }
func initializeConfig(cmd *cobra.Command) error {
v := viper.New()
// Set the base name of the config file, without the file extension.
v.SetConfigName("makeshift")
// Set as many paths as you like where viper should look for the
// config file. We are only looking in the current working directory.
v.AddConfigPath(".")
// Attempt to read the config file, gracefully ignoring errors
// caused by a config file not being found. Return an error
// if we cannot parse the config file.
if err := v.ReadInConfig(); err != nil {
// It's okay if there isn't a config file
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return err
}
}
// When we bind flags to environment variables expect that the
// environment variables are prefixed, e.g. a flag like --number
// binds to an environment variable STING_NUMBER. This helps
// avoid conflicts.
v.SetEnvPrefix("MAKESHIFT")
// Environment variables can't have dashes in them, so bind them to their equivalent
// keys with underscores, e.g. --favorite-color to STING_FAVORITE_COLOR
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
// Bind to environment variables
// Works great for simple config names, but needs help for names
// like --favorite-color which we fix in the bindFlags function
v.AutomaticEnv()
// Bind the current command's flags to viper
bindFlags(cmd, v)
return nil
}

View file

@ -19,18 +19,17 @@ var serveCmd = &cobra.Command{
makeshift serve --root ./test --init -l debug makeshift serve --root ./test --init -l debug
`, `,
Args: cobra.NoArgs, Args: cobra.NoArgs,
// PreRun: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
// setenv(cmd, "host", "MAKESHIFT_HOST") setenv(cmd, "host", "MAKESHIFT_HOST")
// setenv(cmd, "root", "MAKESHIFT_ROOT") setenv(cmd, "root", "MAKESHIFT_ROOT")
// setenv(cmd, "timeout", "MAKESHIFT_TIMEOUT") setenv(cmd, "timeout", "MAKESHIFT_TIMEOUT")
// setenv(cmd, "cacert", "MAKESHIFT_CACERT") setenv(cmd, "cacert", "MAKESHIFT_CACERT")
// setenv(cmd, "keyfile", "MAKESHIFT_KEYFILE") setenv(cmd, "keyfile", "MAKESHIFT_KEYFILE")
// }, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var ( var (
host, _ = cmd.Flags().GetString("host") host, _ = cmd.Flags().GetString("host")
rootPath, _ = cmd.Flags().GetString("root") rootPath, _ = cmd.Flags().GetString("root")
configPath, _ = cmd.Flags().GetString("config")
cacertPath, _ = cmd.Flags().GetString("cacert") cacertPath, _ = cmd.Flags().GetString("cacert")
keyfile, _ = cmd.Flags().GetString("keyfile") keyfile, _ = cmd.Flags().GetString("keyfile")
timeout, _ = cmd.Flags().GetInt("timeout") timeout, _ = cmd.Flags().GetInt("timeout")
@ -61,7 +60,6 @@ var serveCmd = &cobra.Command{
Str("host", parsed.Host). Str("host", parsed.Host).
Any("paths", map[string]string{ Any("paths", map[string]string{
"root": rootPath, "root": rootPath,
"config": configPath,
"cacert": cacertPath, "cacert": cacertPath,
"keyfile": keyfile, "keyfile": keyfile,
"data": server.PathForData(), "data": server.PathForData(),

View file

@ -1,15 +0,0 @@
services:
makeshift:
# build: ./Dockerfile
image: makeshift:latest
container_name: makeshift
network:
- internal
volumes:
- /tmp/makeshift/makeshift:/usr/local/bin
- /tmp/makeshift/plugins/*:/makeshift/server/plugins
ports:
- 5050:5050
networks:
internal:

View file

@ -1,54 +0,0 @@
---
#
# Makeshift Config
#
# Repository: https://git.towk2.me/towk/makeshift
#
# Default configuration file for 'makeshift' CLI and service.
# This file was autogenerated using 'makeshift config new' command.
#
# Set the service host.
host: http://localhost:5050
# Set the path to the file or directory to download.
path: help.txt
# Set the log file path. Logs will be written to this location.
log-file: logs/makeshift.log
# Set the log level. Possible values include 'debug', 'info', 'warn',
# 'error', 'disabled', and 'trace'.
log-level: info
# Set the plugins to use when downloading files and/or directories.
plugins:
- smd
- jinja2
# Set the positional arguments to pass to ALL plugins when downloading
# files and directories. NOTE: These arguments may be ignored or not
# used within certain plugins.
plugin-args:
# Set the key-word arguments stored in the data that is passed to ALL
# plugins when downloading files and directories. NOTE: These arguments
# may be ignored or not used within certain plugins.
plugin-kwargs:
# Set the profiles to use when downloading files and/or directories.
profiles:
- default
# Set whether to extract the archive when downloading directories.
extract: false
# Set whether to remove an archive after downloading and extracting.
# This requires that the 'extract' flag be set to 'true'.
remove-archive: false
# Set the path to a CA certificate.
cacert: ""
# Set the path to a CA key file.
keyfile: ""

@ -1 +0,0 @@
Subproject commit 64090892b46ffea8d1775cc37b995182aeacf31d