Skip to content

Commit

Permalink
Fix OAR punch submission
Browse files Browse the repository at this point in the history
  • Loading branch information
mickours committed Feb 21, 2024
1 parent 6261fe9 commit bfb5d35
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
41 changes: 23 additions & 18 deletions connectors/exec/ssh.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
package exec

import (
"bytes"
"encoding/base64"
"os"

"github.com/apex/log"
"golang.org/x/crypto/ssh"
)

func ExecuteCommand(cmd string) (string, error) {
type SSHConfig struct {
user string
hostname string
port string
keyBase64 string
}

// From file
//key, err := os.ReadFile("pkey.tmp")
//if err != nil {
// log.Fatalf("unable to read private key: %v", err)
//}
var setup = setupFromEnv

func setupFromEnv() SSHConfig {
user := os.Getenv("BEBIDA_SSH_USER")
hostname := os.Getenv("BEBIDA_SSH_HOSTNAME")
port := os.Getenv("BEBIDA_SSH_PORT")
connectionUrl := hostname + ":" + port
// from base64 encoded env var
keyBase64 := os.Getenv("BEBIDA_SSH_PKEY")
key, err := base64.StdEncoding.DecodeString(keyBase64)
return SSHConfig{user: user, hostname: hostname, port: port, keyBase64: keyBase64}
}

func ExecuteCommand(cmd string) (string, error) {
sshConfig := setup()
connectionUrl := sshConfig.hostname + ":" + sshConfig.port

key, err := base64.StdEncoding.DecodeString(sshConfig.keyBase64)
if err != nil {
log.Fatalf("unable to decode private key: %v", err)
}

// Create the Signer for this private key.
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
log.Fatalf("unable to parse private key: %v", err)
}

config := &ssh.ClientConfig{
User: user,
User: sshConfig.user,
Auth: []ssh.AuthMethod{
// Use the PublicKeys method for remote authentication.
ssh.PublicKeys(signer),
Expand All @@ -58,13 +64,12 @@ func ExecuteCommand(cmd string) (string, error) {

// Once a Session is created, you can execute a single command on
// the remote side using the Run method.
var b bytes.Buffer
session.Stdout = &b
log.Infof("Running SSH on host %s with command: %s", hostname, cmd)
if err := session.Run(cmd); err != nil {
log.Infof("Running SSH on host '%s' with command: %s", sshConfig.hostname, cmd)
if out, err := session.CombinedOutput(cmd); err != nil {
log.Error("Failed to run: " + err.Error())
return "", err
return string(out), err
} else {
log.Infof("Completed SSH on host %s with command: %s\nOUTPUTS: %s", sshConfig.hostname, cmd, out)
return string(out), nil
}
log.Infof("Completed SSH on host %s with command: %s\nOUTPUTS: %s", hostname, cmd, b.String())
return b.String(), nil
}
3 changes: 3 additions & 0 deletions connectors/exec/ssh_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package exec

import (
"os"
"testing"
)

func TestSSH(t *testing.T) {
// Assuming that you have ssh on localhost on port 22 working
setup = func() SSHConfig {return SSHConfig{user: os.Getenv("USER"), hostname: "localhost", port: "22", keyBase64: os.Getenv("BEBIDA_SSH_PKEY")}}
out, err := ExecuteCommand("echo toto")
if err != nil {
t.Error(err)
Expand Down
5 changes: 3 additions & 2 deletions connectors/oar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package connectors

import (
"fmt"
"regexp"
"strconv"
"strings"
"regexp"

"github.com/RyaxTech/bebida-shaker/connectors/exec"
"github.com/RyaxTech/bebida-shaker/connectors/utils"
Expand All @@ -18,7 +18,8 @@ var ExecuteCommand = exec.ExecuteCommand
func (OAR) Punch(nbCpuPerJob int, jobDurationInSeconds int) (string, error) {
// TODO put this in a config file (or env var)
randomSuffix := utils.RandomString(8)
cmd := fmt.Sprintf("oarsub --name BEBIDA_NOOP_%s -l cores=%d sleep %d", randomSuffix, nbCpuPerJob, jobDurationInSeconds)
// FIXME: user1 is hardcoded here, maybe we should use the right user for Bebida directly ass SSH level...
cmd := fmt.Sprintf("su user1 --command 'oarsub --name BEBIDA_NOOP_%s -l nodes=%d,walltime=%d \"sleep %d\"'", randomSuffix, nbCpuPerJob, jobDurationInSeconds/60, jobDurationInSeconds)
out, err := ExecuteCommand(cmd)
log.Infof("Punch command output: %s", string(out))

Expand Down

0 comments on commit bfb5d35

Please sign in to comment.