Skip to content

Commit

Permalink
Working out PSK and linux installer
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkmcc committed Nov 23, 2023
1 parent 890f68a commit 899db49
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 31 deletions.
32 changes: 8 additions & 24 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,12 @@ builds:
- -s -w -X github.com/clarkmcc/cloudcore/pkg/version.Version={{.Version}} -X github.com/clarkmcc/cloudcore/pkg/version.Hash={{.Commit}}
env:
- CGO_ENABLED=0
goos:
# - windows
- darwin
- linux
goarch:
# - amd64
- arm64
# - arm
goarm:
# - 6
- 7
ignore:
- goos: windows
goarch: arm64
- goos: windows
goarch: arm
- goos: darwin
goarch: arm
goarm: 6
- goos: darwin
goarch: arm
goarm: 7
targets:
- darwin_amd64
- darwin_arm64
- linux_amd64
- linux_arm64
- linux_arm_5
nfpms:
- id: cloudcored-linux
package_name: cloudcored
Expand All @@ -39,9 +23,9 @@ nfpms:
license: MIT
formats:
- deb
scripts:
preinstall: scripts/linux/preinstall.sh
# todo: Setup systemd service
# scripts:
# preinstall: scripts/linux/preinstall.sh
# postinstall: scripts/linux/postinstall.sh
# preremove: scripts/linux/preremove.sh
# postremove: scripts/linux/postremove.sh
Expand Down
83 changes: 77 additions & 6 deletions internal/agent/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package agent

import (
"errors"
"fmt"
"github.com/clarkmcc/cloudcore/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"path/filepath"
"runtime"
)

func init() {
Expand Down Expand Up @@ -43,17 +47,25 @@ type Logging struct {
func NewConfig(cmd *cobra.Command) (*Config, error) {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
if cwd, err := os.Getwd(); err == nil {
viper.AddConfigPath(cwd)
}
err := viper.ReadInConfig()

cwd, err := os.Getwd()
if err != nil {
return nil, err
}
viper.AddConfigPath(cwd)

psk := cmd.Flag("psk").Value.String()
err = viper.ReadInConfig()
var e viper.ConfigFileNotFoundError
if err != nil && !errors.As(err, &e) {
return nil, err
}

psk, err := getPsk(cmd, cwd)
if err != nil {
return nil, fmt.Errorf("getting psk: %w", err)
}
if len(psk) > 0 {
viper.Set("preSharedKey", psk)
viper.SetDefault("preSharedKey", psk)
}

var cfg Config
Expand All @@ -64,3 +76,62 @@ func NewConfig(cmd *cobra.Command) (*Config, error) {
utils.PrintStruct(cfg)
return &cfg, nil
}

// getPsk attempts to load the psk from any of the locations where it
// could be provided, specifically
// 1. The command line flags -- in which case we save it to a file so that
// it doesn't need to be provided every time.
// 2. The psk file (created previously).
//
// On linux we save this to the /etc/cloudcored/psk file.
// On darwin we save this to the ~/.cloudcored/psk file.
// Windows not supported for PSK files yet.
func getPsk(cmd *cobra.Command, cwd string) (string, error) {
psk := cmd.Flag("psk").Value.String()
if len(psk) > 0 {
return psk, writePskToFile(psk)
}
pskBytes, err := getPskFromFile(cwd)
if err != nil {
return "", err
}
return string(pskBytes), nil
}

func getPskFromFile(_ string) ([]byte, error) {
var filename string
switch runtime.GOOS {
case "linux":
filename = "/etc/cloudcored/psk"
case "darwin":
dir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("getting home dir: %w", err)
}
filename = filepath.Join(dir, ".cloudcored", "psk")
default:
return nil, fmt.Errorf("reading psk from file not supported on %s", runtime.GOOS)
}
return os.ReadFile(filename)
}

func writePskToFile(psk string) error {
var filename string
switch runtime.GOOS {
case "linux":
filename = "/etc/cloudcored/psk"
case "darwin":
dir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("getting home dir: %w", err)
}
filename = filepath.Join(dir, ".cloudcored", "psk")
default:
return fmt.Errorf("saving psk to file not supported on %s", runtime.GOOS)
}
err := os.MkdirAll(filepath.Dir(filename), 0600)
if err != nil {
return err
}
return os.WriteFile(filename, []byte(psk), 0600)
}
2 changes: 1 addition & 1 deletion internal/tasks/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (e *Executor) maybeSchedule(task *Task) {
e.logger.Debug("task not scheduled to run again", zap.String("task", task.Name))
return
}
e.logger.Debug("scheduling task", zap.String("task", task.Name), zap.Time("next", n), zap.String("in", n.Sub(time.Now()).String()))
e.logger.Info("scheduling task", zap.String("task", task.Name), zap.Time("next", n), zap.String("in", n.Sub(time.Now()).String()))
e.schedule(task, n)
}

Expand Down
88 changes: 88 additions & 0 deletions scripts/linux/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/bash

# Function to fetch the latest version number from GitHub
get_latest_version() {
curl -L -s \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/clarkmcc/cloudcore/releases/latest | jq -r .tag_name
}

# Function to determine OS
get_os() {
if [ -f /etc/debian_version ]; then
echo "debian"
elif [ -f /etc/alpine-release ]; then
echo "alpine"
elif [ -f /etc/arch-release ]; then
echo "arch"
elif [ -f /etc/redhat-release ]; then
echo "rhel"
else
echo "unsupported"
fi
}

# Function to determine architecture using lscpu
get_architecture() {
lscpu | grep Architecture | cut -d ':' -f 2 | sed 's/ //g'
}

# Function to download and install the package
install_package() {
local version=$1
local os=$2
local arch=$3
local base_url="https://github.com/clarkmcc/cloudcore/releases/download/${version}"
local url=""

echo "Installing version $version for $os on $arch architecture"

if [ "$os" = "debian" ] && [ "$arch" = "x86_64" ]; then
url="${base_url}/cloudcored_${version}_linux_amd64.deb"
elif [ "$os" = "debian" ] && [ "$arch" = "aarch64" ]; then
url="${base_url}/cloudcored_${version}_linux_arm64.deb"
elif [ "$os" = "debian" ] && [ "$arch" = "armv7l" ]; then
url="${base_url}/cloudcored_${version}_linux_arm5.deb"
# Add more cases for different OS and architecture combinations
else
echo "Unsupported OS or architecture: $os, $arch"
return
fi

echo -n "Fetching $url: "
curl -L -o package.deb "$url"
echo "done"

echo "Installing package"
sudo dpkg -i package.deb
rm package.deb
}

# Main installation process
version=$(get_latest_version)
os=$(get_os)
arch=$(get_architecture)

# Parse command-line arguments for the --psk parameter
while [ $# -gt 0 ]; do
case "$1" in
--psk)
CLOUDCORE_PSK="$2"
shift 2
;;
*)
break
;;
esac
done


if [ "$os" = "unsupported" ] || [ "$arch" = "unsupported" ]; then
echo "Error: Unsupported OS or architecture."
exit 1
fi

export CLOUDCORE_PSK
install_package "$version" "$os" "$arch"
unset CLOUDCORE_PSK
7 changes: 7 additions & 0 deletions scripts/linux/preinstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

# Access the environment variable
if [ ! -z "$CLOUDCORE_PSK" ]; then
echo "$CLOUDCORE_PSK" > /etc/cloudcored/psk
echo "Successfully loaded pre-shared key"
fi

0 comments on commit 899db49

Please sign in to comment.