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

Devel #35

Merged
merged 18 commits into from
Aug 23, 2024
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
certs/**
!certs/*.go
config.yaml
config.yml
packagelock.pid
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RUN go mod download
# Build
ARG APP_VERSION="v0.1.0+hotfixes"
RUN \
CGO_ENABLED=0 GOOS=linux go build -ldflags "-X 'main.AppVersion=$APP_VERSION'" -o /app/packagelock
CGO_ENABLED=0 GOOS=linux go build -ldflags "-X 'main.AppVersion=$APP_VERSION'" -o /packagelock

# Optional:
# To bind to a TCP port, runtime parameters must be supplied to the docker command.
Expand All @@ -28,4 +28,5 @@ RUN \
EXPOSE 8080

# Run
CMD ["/app/packagelock start"]
CMD ["/packagelock", "start"]

15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ TODO: explain usage
- [ ] Check Vars and Func-Names for naming convention
- [ ] persistent storage
- [ ] implement interfaces for external functions for easier mocking in tests
- [ ] systemd service start/stop/enable/disable
- [ ] copy app file (.deb/rpm/binary) via SFTP to host and start stop
- [ ] binary self-Update
- [ ] agent can run docker/podman containers
- [ ] agent fetches running docker/podman containers, updates, restarts etc
- [ ] user management & SSH keys
- [ ] system definition in mpackagelock file for easy recovery & scaling
- [ ] CLI-Commands to add:
- [ ] sync now|timestamp - force sync the server with the Agents
- [ ] logs -s (severity) info|warning|error -d (date to start) 2024-08-23-10-00-00 (date-time)
- [ ] backup - Creates a backup from server, server config, database
- [ ] generate certs letsencrypt - lets encrypt certs
- [ ] generate certs letsencrypt renew - renews
- [ ] test - runs healthchecks on server
- [ ] test agents - runs healthchecks on agents



Expand Down
12 changes: 0 additions & 12 deletions config.yaml

This file was deleted.

22 changes: 11 additions & 11 deletions config/conf-init.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ func CreateDefaultConfig(config ConfigProvider) {
// TODO: Add default config
yamlExample := []byte(`
general:
debug: True
production: False
Port: 8080

Network:
FQDN: "packagelock.company.com"
ForceHTTP: False
SSL:
CertificatePath: "/etc/packagelock/ssl/cert.pem"
PrivateKeyPath: "/etc/packagelock/ssl/privkey.pem"
AllowSelfSigned: False
debug: true
production: false
network:
fqdn: 0.0.0.0
port: 8080
ssl: true
ssl-config:
redirecthttp: true
allowselfsigned: true
certificatepath: ./certs/testing.crt
privatekeypath: ./certs/testing.key
`)

err := config.ReadConfig(bytes.NewBuffer(yamlExample))
Expand Down
107 changes: 105 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"packagelock/certs"
"packagelock/config"
"packagelock/server"
"strconv"
"syscall"
"time"

Expand Down Expand Up @@ -38,9 +39,64 @@ var startCmd = &cobra.Command{
},
}

var restartCmd = &cobra.Command{
Use: "restart",
Short: "Restart the running server",
Run: func(cmd *cobra.Command, args []string) {
restartServer()
},
}

var stopCmd = &cobra.Command{
Use: "stop",
Short: "Stop the running server",
Run: func(cmd *cobra.Command, args []string) {
stopServer()
},
}

// Generate command
var generateCmd = &cobra.Command{
Use: "generate [certs|config]",
Short: "Generate certs or config files",
Long: "Generate certificates or configuration files required by the application.",
Args: cobra.MatchAll(cobra.ExactArgs(1), validGenerateArgs()), // Expect exactly one argument: either "certs" or "config"
ValidArgs: []string{"certs", "config"}, // Restrict arguments to these options
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "certs":
err := certs.CreateSelfSignedCert(
config.Config.GetString("network.ssl-config.certificatepath"),
config.Config.GetString("network.ssl-config.privatekeypath"))
if err != nil {
fmt.Println("There was an error generating the self signed certs: %w", err)
}
case "config":
config.CreateDefaultConfig(config.Config)
default:
fmt.Println("Invalid argument. Use 'certs' or 'config'.")
}
},
}

func validGenerateArgs() cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
validArgs := []string{"certs", "config"}
for _, valid := range validArgs {
if args[0] == valid {
return nil
}
}
return fmt.Errorf("invalid argument: '%s'. Must be one of 'certs' or 'config'", args[0])
}
}

func init() {
// Add commands to rootCmd
rootCmd.AddCommand(startCmd)
rootCmd.AddCommand(generateCmd)
rootCmd.AddCommand(restartCmd)
rootCmd.AddCommand(stopCmd)

// Initialize Viper config
cobra.OnInitialize(initConfig)
Expand Down Expand Up @@ -70,6 +126,13 @@ func initConfig() {

// startServer starts the Fiber server with appropriate configuration
func startServer() {
pid := os.Getpid()
err := os.WriteFile("packagelock.pid", []byte(strconv.Itoa(pid)), 0644)
if err != nil {
fmt.Printf("Failed to write PID file: %v\n", err)
return
}

fmt.Println(config.Config.AllSettings())

signal.Notify(quitChan, os.Interrupt, syscall.SIGTERM)
Expand All @@ -86,11 +149,12 @@ func startServer() {
go func() {
if config.Config.GetBool("network.ssl") {
fmt.Printf("Starting Fiber HTTPS server at https://%s...\n", serverAddr)
if err := server.ListenAndServeTLS(
err := server.ListenAndServeTLS(
router.Router,
config.Config.GetString("network.ssl-config.certificatepath"),
config.Config.GetString("network.ssl-config.privatekeypath"),
serverAddr); err != nil {
serverAddr)
if err != nil {
fmt.Printf("Server error: %s\n", err)
}
} else {
Expand All @@ -112,6 +176,7 @@ func startServer() {
} else {
fmt.Println("Server stopped.")
}
startServer()

case <-quitChan:
fmt.Println("Shutting down Fiber server...")
Expand Down Expand Up @@ -140,6 +205,44 @@ func startServer() {
fmt.Println("Main process exiting.")
}

func restartServer() {
stopServer()
fmt.Println("Restarting the Server...")
time.Sleep(5 * time.Second)
startServer()
}

func stopServer() {
// Read the PID from the file using os.ReadFile
data, err := os.ReadFile("packagelock.pid")
if err != nil {
fmt.Printf("Could not read PID file: %v\n", err)
return
}

pid, err := strconv.Atoi(string(data))
if err != nil {
fmt.Printf("Invalid PID found in file: %v\n", err)
return
}

// Send SIGTERM to the process
fmt.Printf("Stopping the server with PID: %d\n", pid)
err = syscall.Kill(pid, syscall.SIGTERM)
if err != nil {
fmt.Printf("Failed to stop the server: %v\n", err)
} else {
fmt.Println("Server stopped.")
// After successful stop, remove the PID file
err = os.Remove("packagelock.pid")
if err != nil {
fmt.Printf("Failed to remove PID file: %v\n", err)
} else {
fmt.Println("PID file removed successfully.")
}
}
}

func main() {
// Execute the Cobra root command
if err := rootCmd.Execute(); err != nil {
Expand Down
Loading