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

Script Runner addon #229

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 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
4 changes: 2 additions & 2 deletions cmd/cluster_addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func validateAddonSubCommand(cmd *cobra.Command, args []string) error {
if idx == -1 {
return fmt.Errorf("cluster '%s' not found", name)
}
if len(args) != 1 {
return errors.New("exactly one argument expected")
if len(args) < 1 {
mavimo marked this conversation as resolved.
Show resolved Hide resolved
mavimo marked this conversation as resolved.
Show resolved Hide resolved
return errors.New("please add the addon name argument")
}
addonName := args[0]
provider := hetzner.NewHetznerProvider(AppConf.Context, AppConf.Client, *cluster, AppConf.CurrentContext.Token)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cluster_addon_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var clusterAddonInstallCmd = &cobra.Command{
FatalOnError(err)

addon := addonService.GetAddon(addonName)
addon.Install()
addon.Install(args...)

log.Printf("addon %s successfully installed", addonName)
},
Expand Down
83 changes: 83 additions & 0 deletions pkg/addons/addon_script_runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package addons

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"strings"
"time"

"github.com/xetys/hetzner-kube/pkg/clustermanager"
)

// ScriptRunnerAddon installs script runner
type ScriptRunnerAddon struct {
communicator clustermanager.NodeCommunicator
nodes []clustermanager.Node
cluster clustermanager.Cluster
}

// NewScriptRunnerAddon installs script runner to the cluster
func NewScriptRunnerAddon(provider clustermanager.ClusterProvider, communicator clustermanager.NodeCommunicator) ClusterAddon {
return ScriptRunnerAddon{communicator: communicator, nodes: provider.GetAllNodes(), cluster: provider.GetCluster()}
}

func init() {
addAddon(NewScriptRunnerAddon)
}

// Name returns the addons name
func (addon ScriptRunnerAddon) Name() string {
return "script-runner"
}

// Requires returns a slice with the name of required addons
func (addon ScriptRunnerAddon) Requires() []string {
return []string{}
}

// Description returns the addons description
func (addon ScriptRunnerAddon) Description() string {
return "Bash remote script runner"
}

// URL returns the URL of the addons underlying project
func (addon ScriptRunnerAddon) URL() string {
return "https://www.gnu.org/software/bash/"
}

// Install performs all steps to install the addon
func (addon ScriptRunnerAddon) Install(args ...string) {

if len(args) < 2 {
log.Fatal("path argument is missing")
}
scriptPath := args[1]
scriptContents, err := ioutil.ReadFile(scriptPath)
FatalOnError(err)

clusterInfoBin, err := json.Marshal(addon.cluster)
FatalOnError(err)

replacer := strings.NewReplacer("\n", "", "'", "\\'")
clusterInfo := replacer.Replace(string(clusterInfoBin))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand why we are going to use replacer here, this is needed since there are some info in JSON encoded that can generate invalid string argument? Is possibile to use some different approach in order to do not manipulate result value, like:

echo $(cat <<EOF
<JSON_MARSHALLED_VALUE>
EOF)

I'm not sure is doable, but will allow us to do not manipulate the value and be "safe" on changes we do in config..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the run command will allow multi line. If it works for you, please let me know.


for _, node := range addon.nodes {
scriptRemotePath := fmt.Sprintf("/tmp/script-%s-%d.sh", time.Now().Format("20060102150405"), rand.Int31())
err = addon.communicator.WriteFile(node, scriptRemotePath, string(scriptContents), true)
FatalOnError(err)

output, err := addon.communicator.RunCmd(
node,
fmt.Sprintf("bash %s %s '%s'", scriptRemotePath, node.Group, clusterInfo))
FatalOnError(err)
fmt.Printf("%s %s: script ran successfully..\n%s\n", node.Name, node.IPAddress, output)
}
}

// Uninstall performs all steps to remove the addon
func (addon ScriptRunnerAddon) Uninstall() {
fmt.Println("no uninstall for this addon")
}
1 change: 1 addition & 0 deletions pkg/clustermanager/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package clustermanager
type Node struct {
Name string `json:"name"`
Type string `json:"type"`
Group string `json:"group"`
IsMaster bool `json:"is_master"`
IsEtcd bool `json:"is_etcd"`
IPAddress string `json:"ip_address"`
Expand Down
16 changes: 8 additions & 8 deletions pkg/hetzner/hetzner_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewHetznerProvider(context context.Context, client *hcloud.Client, cluster
}

// CreateNodes creates hetzner nodes
func (provider *Provider) CreateNodes(suffix string, template clustermanager.Node, datacenters []string, count int, offset int) ([]clustermanager.Node, error) {
func (provider *Provider) CreateNodes(template clustermanager.Node, datacenters []string, count int, offset int) ([]clustermanager.Node, error) {
sshKey, _, err := provider.client.SSHKey.Get(provider.context, template.SSHKeyName)

if err != nil {
Expand All @@ -52,7 +52,7 @@ func (provider *Provider) CreateNodes(suffix string, template clustermanager.Nod
return nil, fmt.Errorf("we got some problem with the SSH-Key '%s', chances are you are in the wrong context", template.SSHKeyName)
}

serverNameTemplate := fmt.Sprintf("%s-%s-@idx", provider.clusterName, suffix)
serverNameTemplate := fmt.Sprintf("%s-%s-@idx", provider.clusterName, template.Group)
serverOptsTemplate := hcloud.ServerCreateOpts{
Name: serverNameTemplate,
ServerType: &hcloud.ServerType{
Expand Down Expand Up @@ -127,20 +127,20 @@ func (provider *Provider) CreateNodes(suffix string, template clustermanager.Nod

// CreateEtcdNodes creates nodes with type 'etcd'
func (provider *Provider) CreateEtcdNodes(sshKeyName string, masterServerType string, datacenters []string, count int) ([]clustermanager.Node, error) {
template := clustermanager.Node{SSHKeyName: sshKeyName, IsEtcd: true, Type: masterServerType}
return provider.CreateNodes("etcd", template, datacenters, count, 0)
template := clustermanager.Node{SSHKeyName: sshKeyName, IsEtcd: true, Type: masterServerType, Group: "etcd"}
return provider.CreateNodes(template, datacenters, count, 0)
}

// CreateMasterNodes creates nodes with type 'master'
func (provider *Provider) CreateMasterNodes(sshKeyName string, masterServerType string, datacenters []string, count int, isEtcd bool) ([]clustermanager.Node, error) {
template := clustermanager.Node{SSHKeyName: sshKeyName, IsMaster: true, Type: masterServerType, IsEtcd: isEtcd}
return provider.CreateNodes("master", template, datacenters, count, 0)
template := clustermanager.Node{SSHKeyName: sshKeyName, IsMaster: true, Type: masterServerType, IsEtcd: isEtcd, Group: "master"}
return provider.CreateNodes(template, datacenters, count, 0)
}

// CreateWorkerNodes create new worker node on provider
func (provider *Provider) CreateWorkerNodes(sshKeyName string, workerServerType string, datacenters []string, count int, offset int) ([]clustermanager.Node, error) {
template := clustermanager.Node{SSHKeyName: sshKeyName, IsMaster: false, Type: workerServerType}
return provider.CreateNodes("worker", template, datacenters, count, offset)
template := clustermanager.Node{SSHKeyName: sshKeyName, IsMaster: false, Type: workerServerType, Group: "worker"}
return provider.CreateNodes(template, datacenters, count, offset)
}

// GetAllNodes retrieves all nodes
Expand Down