Skip to content

Commit

Permalink
Fixing provisioners
Browse files Browse the repository at this point in the history
  • Loading branch information
knabben committed Apr 7, 2024
1 parent 93063b9 commit 46257fd
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 167 deletions.
30 changes: 30 additions & 0 deletions pkg/pwsh/iface/runners.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package iface

import (
"swdt/apis/config/v1alpha1"
"swdt/pkg/executors/exec"
"swdt/pkg/executors/iface"
"swdt/pkg/pwsh/kubernetes"
"swdt/pkg/pwsh/setup"
)

type Runner[R RunnerInterface] struct {
Inner R
}

type RunnerInterface interface {
*setup.Runner | *kubernetes.Runner
SetLocal(executor iface.Executor)
SetRemote(executor iface.SSHExecutor)
}

// NewRunner returns the encapsulated picked runner and sets its executors
func NewRunner[R RunnerInterface](ssh *v1alpha1.SSHSpec, run R) (*Runner[R], error) {
var sshExec = exec.NewSSHExecutor(ssh) // Start SSH Connection
run.SetRemote(sshExec)
run.SetLocal(exec.NewLocalExecutor()) // Start Local executor
if err := sshExec.Connect(); err != nil {
return nil, err
}
return &Runner[R]{Inner: run}, nil
}
49 changes: 35 additions & 14 deletions pkg/pwsh/kubernetes/provisioners.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,64 @@ import (
"github.com/fatih/color"
klog "k8s.io/klog/v2"
"swdt/apis/config/v1alpha1"
"swdt/pkg/connections"
"swdt/pkg/executors/iface"
)

var (
resc = color.New(color.FgHiGreen).Add(color.Bold)
permission = "0755"
)

type KubernetesRunner struct {
conn connections.Connection
run func(args string) (string, error)
copy func(local, remote, perm string) error
type Runner struct {
remote iface.SSHExecutor
local iface.Executor
}

func (r *KubernetesRunner) SetConnection(conn *connections.Connection) {
r.conn = *conn
r.run = r.conn.Run
r.copy = r.conn.Copy
func (r *Runner) SetLocal(executor iface.Executor) {
r.local = executor
}

func (r *KubernetesRunner) InstallProvisioners(provisioners []v1alpha1.ProvisionerSpec) error {
func (r *Runner) SetRemote(executor iface.SSHExecutor) {
r.remote = executor
}

// runL runs a local command using the local executor
func (r *Runner) runL(args string) error {
return r.local.Run(args, nil)
}

// runLstd runs a local command using the local executor
func (r *Runner) runLstd(args string, stdout *chan string) error {
return r.local.Run(args, stdout)
}

// runR runs a local command using the local executor
func (r *Runner) runR(args string) error {
return r.remote.Run(args, nil)
}

// runRstdout runs a local command using the local executor
func (r *Runner) runRstd(args string, stdout *chan string) error {
return r.remote.Run(args, stdout)
}

func (r *Runner) InstallProvisioners(provisioners []v1alpha1.ProvisionerSpec) error {
for _, provisioner := range provisioners {
source, destination := provisioner.SourceURL, provisioner.Destination
name := provisioner.Name
klog.Info(resc.Sprintf("Service %s binary replacement, trying to stop service...", name))
_, err := r.run(fmt.Sprintf("Stop-Service -name %s -Force", name))
err := r.runR(fmt.Sprintf("Stop-Service -name %s -Force", name))
if err != nil {
klog.Error(err)
continue
}
klog.Infof("Service stopped. Copying file %s to remote %s...", source, destination)
if err = r.copy(source, destination, permission); err != nil {
/*if err = (*r.remote).Copy(source, destination, permission); err != nil {
klog.Error(err)
continue
}
}*/
klog.Infof("starting service %s again...", name)
_, err = r.run(fmt.Sprintf("Start-Service -name %s", name))
err = r.runR(fmt.Sprintf("Start-Service -name %s", name))
if err != nil {
klog.Error(err)
continue
Expand Down
4 changes: 2 additions & 2 deletions pkg/pwsh/kubernetes/provisioners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ var (
calls = []string{}
)

func validateRun(args string) (string, error) {
func validateRun(args string) error {
calls = append(calls, args)
return "cmd stdout", nil
return nil
}

func validateCopy(l, r, p string) error {
Expand Down
Loading

0 comments on commit 46257fd

Please sign in to comment.