Skip to content

Commit

Permalink
fix issue of sudo as parameter and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenix.lv committed Dec 4, 2018
1 parent f779dab commit d2bde6f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
21 changes: 17 additions & 4 deletions jobmgr/job.go
Expand Up @@ -9,24 +9,31 @@ import (
"github.com/gorilla/websocket"
)


/*
Define interface I_Job with only method Execute
*/
type I_Job interface {
Execute(*websocket.Conn)
// Execute()
}


/*
Implement I_Job
*/
type Job struct {
Machine I_Machine
Tasks []I_Task
}

/*
Formalize cmd of tasks for ssh
*/
func (job *Job) GetTaskCMD() string {
ts := []string{}
for index, _ := range job.Tasks {
task := job.Tasks[index].Serialize()
if strings.Contains(task, "sudo") {
task = strings.Replace(task, "sudo", "MYPASS="+ job.Machine.GetSudoPassword() + " SUDO_ASKPASS=`echo $HOME`/echopass" +" sudo -A", -1)
if strings.Contains(task, "sudo") && strings.Index(task, "sudo")==0 {
task = strings.Replace(task, "sudo", "MYPASS="+ job.Machine.GetSudoPassword() + " SUDO_ASKPASS=`echo $HOME`/echopass" +" sudo -A", 1)
task = "echo -e \"#! /bin/bash\necho \\$MYPASS\">echopass && chmod +x echopass && " + task + " && rm `echo $HOME`/echopass"
}
ts = append(ts, task)
Expand All @@ -37,6 +44,9 @@ func (job *Job) GetTaskCMD() string {
}


/*
Get ssh session and ssh client
*/
func (job *Job) GetSSH() (*ssh.Session, *ssh.Client) {

// Get client conf according to machine conf
Expand Down Expand Up @@ -70,6 +80,9 @@ func (job *Job) GetSSH() (*ssh.Session, *ssh.Client) {
return session, client
}

/*
Execute ssh job and communicate through websocket
*/
func (job *Job) Execute(conn *websocket.Conn) {

session, client := job.GetSSH()
Expand Down
4 changes: 4 additions & 0 deletions jobmgr/machine.go
Expand Up @@ -127,6 +127,10 @@ func (m *Machine) GetSigner() (ssh.Signer, error) {
}
}

/*
Implement I_Machine.GetAutoConf() Get auth conf for ssh
*/
func (m *Machine) GetAuthConf() (*ssh.ClientConfig, string, error) {
var auth []ssh.AuthMethod
var e error
Expand Down
20 changes: 18 additions & 2 deletions jobmgr/mgr.go
Expand Up @@ -6,21 +6,32 @@ import (
)


/*
Define interface: I_Mgr for handling ssh job and websocket communication
*/
type I_Mgr interface {
SetConn(*websocket.Conn)
ExecuteJob()
}


/*
Implement I_Mgr
*/
type Mgr struct {
Conn *websocket.Conn
Job I_Job
}

/*
Set websocket connection
*/
func (mgr *Mgr) SetConn(conn *websocket.Conn) {
mgr.Conn = conn
}

/*
Create job from job description, which is defined as jobmgr.Job
*/
func (mgr *Mgr) CreateJob(jobData []byte) error {
tmpJob := struct {
Machine Machine
Expand All @@ -36,6 +47,9 @@ func (mgr *Mgr) CreateJob(jobData []byte) error {
return err
}

/*
Get job description from websocket
*/
func (mgr *Mgr) getConf() []byte {
_, jobData, err := mgr.Conn.ReadMessage()
if err != nil {
Expand All @@ -44,8 +58,10 @@ func (mgr *Mgr) getConf() []byte {
return jobData
}

/*
Execute ssh job
*/
func (mgr *Mgr) ExecuteJob() {
mgr.CreateJob(mgr.getConf())
mgr.Job.Execute(mgr.Conn)
//mgr.Job.Execute()
}
5 changes: 4 additions & 1 deletion jobmgr/syncio.go
Expand Up @@ -6,7 +6,10 @@ import (
"encoding/json"
)


/*
Bind output of ssh session with websocket input
Bind websocket output with input of ssh session
*/
func syncIO(session *ssh.Session, client *ssh.Client, conn *websocket.Conn) {
go func(*ssh.Session, *ssh.Client, *websocket.Conn) {
sessionReader, err := session.StdoutPipe()
Expand Down

0 comments on commit d2bde6f

Please sign in to comment.