Skip to content
This repository has been archived by the owner on Feb 15, 2025. It is now read-only.

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
hantdev committed Sep 30, 2024
1 parent f99d553 commit 97ea85f
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 57 deletions.
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

22 changes: 21 additions & 1 deletion install
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,24 @@ models:
secret_access_key: ${MINIO_SECRET_ACCESS_KEY}
EOF
echo "${bin} ${version} has been installed."
# Generate SSH keypair
ssh_dir="$HOME/.ssh"
key_name="vtsbackup_id_rsa"
key_path="$ssh_dir/$key_name"
if [ ! -f "$key_path" ]; then
echo "Generating SSH keypair for vtsbackup..."
mkdir -p "$ssh_dir"
ssh-keygen -t rsa -b 4096 -f "$key_path" -N "" -C "vtsbackup@$(hostname)"
chmod 600 "$key_path"
chmod 644 "$key_path.pub"
fi
# Read public key
public_key=$(cat "$key_path.pub")
# Register with control plane
echo "Registering with control plane..."
"${bin_path}" register --url https://192.168.1.7:8080 --public-key "$public_key"
echo "${bin} ${version} has been installed and registered with a new SSH keypair."
123 changes: 88 additions & 35 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package main

import (
"bytes"
"embed"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -84,6 +87,25 @@ func main() {
daemon.AddCommand(daemon.StringFlag(signal, "reload"), syscall.SIGHUP, reloadHandler)

app.Commands = []*cli.Command{
{
Name: "register",
Usage: "Register this agent with the control plane",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Usage: "Control plane URL",
Required: true,
},
&cli.StringFlag{
Name: "public-key",
Usage: "SSH public key",
Required: true,
},
},
Action: func(c *cli.Context) error {
return registerAgent(c.String("url"), c.String("public-key"))
},
},
{
Name: "perform",
Usage: "Perform backup pipeline using config file. If no model is specified, all models will be performed.",
Expand Down Expand Up @@ -749,39 +771,70 @@ func uninstallBackupAgent() error {
}

func runBashCommand(command string, args ...string) error {
// Create a temporary file to store the vts script
tmpFile, err := os.CreateTemp("", "vts-script")
if err != nil {
return fmt.Errorf("failed to create temporary file: %v", err)
}
defer os.Remove(tmpFile.Name())

// Read the embedded vts script
scriptContent, err := vtsScript.ReadFile("vts")
if err != nil {
return fmt.Errorf("failed to read embedded vts script: %v", err)
}

// Write the script content to the temporary file
if _, err := tmpFile.Write(scriptContent); err != nil {
return fmt.Errorf("failed to write vts script to temporary file: %v", err)
}
tmpFile.Close()

// Make the temporary file executable
if err := os.Chmod(tmpFile.Name(), 0755); err != nil {
return fmt.Errorf("failed to make temporary file executable: %v", err)
}

// Execute the script
cmdArgs := append([]string{tmpFile.Name(), command}, args...)
cmd := exec.Command("bash", cmdArgs...)

// Set up pipes for stdin, stdout, and stderr
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

// Run the command and wait for it to finish
return cmd.Run()
// Create a temporary file to store the vts script
tmpFile, err := os.CreateTemp("", "vts-script")
if err != nil {
return fmt.Errorf("failed to create temporary file: %v", err)
}
defer os.Remove(tmpFile.Name())

// Read the embedded vts script
scriptContent, err := vtsScript.ReadFile("vts")
if err != nil {
return fmt.Errorf("failed to read embedded vts script: %v", err)
}

// Write the script content to the temporary file
if _, err := tmpFile.Write(scriptContent); err != nil {
return fmt.Errorf("failed to write vts script to temporary file: %v", err)
}
tmpFile.Close()

// Make the temporary file executable
if err := os.Chmod(tmpFile.Name(), 0755); err != nil {
return fmt.Errorf("failed to make temporary file executable: %v", err)
}

// Execute the script
cmdArgs := append([]string{tmpFile.Name(), command}, args...)
cmd := exec.Command("bash", cmdArgs...)

// Set up pipes for stdin, stdout, and stderr
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

// Run the command and wait for it to finish
return cmd.Run()
}

func registerAgent(controlPlaneURL, publicKey string) error {
hostname, err := os.Hostname()
if err != nil {
return fmt.Errorf("failed to get hostname: %v", err)
}

data := map[string]string{
"hostname": hostname,
"public_key": publicKey,
}

jsonData, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("failed to marshal JSON: %v", err)
}

resp, err := http.Post(controlPlaneURL+"/register", "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to send registration request: %v", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("registration failed with status %d: %s", resp.StatusCode, string(body))
}

fmt.Println("Successfully registered with control plane")
return nil
}

0 comments on commit 97ea85f

Please sign in to comment.