Skip to content

Commit

Permalink
Add exec script function to sesha3
Browse files Browse the repository at this point in the history
* add exec get request

* request json: add script_name

* exec: change responce

* change func name
  • Loading branch information
biosugar0 authored and flowerinthenight committed Oct 23, 2017
1 parent c4de8ec commit c0f70e3
Showing 1 changed file with 143 additions and 0 deletions.
143 changes: 143 additions & 0 deletions mobingi/sesha3/sesha3.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,149 @@ type GetSessionUrlInput struct {
Timeout int64
}

type ExecScriptInput struct {
StackId string
Target string
Script string
ScriptName string
InstUser string
Flag string
}

type ScriptRes struct {
Out string `json:"stdout"`
Err string `json:"stderr"`
}

func (s *sesha3) ExecScript(in *ExecScriptInput) (*client.Response, []byte, ScriptRes, error) {
var sresp ScriptRes

if in == nil {
return nil, nil, sresp, errors.New("input cannot be nil")
}
if in.Target == "" {
return nil, nil, sresp, errors.New("target cannot be empty")
}
if in.Script == "" {
return nil, nil, sresp, errors.New("script cannot be empty")
}
if in.ScriptName == "" {
return nil, nil, sresp, errors.New("script cannot be empty")
}

if s.session.Config.ApiVersion >= 3 {
if in.Flag == "" {
return nil, nil, sresp, errors.New("flag cannot be empty")
}
}

if in.InstUser == "" {
return nil, nil, sresp, errors.New("instance username cannot be empty")
}

// get pem url from stack id
almsvc := alm.New(s.session)
inpem := alm.GetPemInput{
StackId: in.StackId,
}

if s.session.Config.ApiVersion >= 3 {
inpem.Flag = in.Flag
}

resp, body, _, err := almsvc.GetPem(&inpem)
if err != nil {
return resp, body, sresp, errors.Wrap(err, "get pem failed")
}

type rsaurl struct {
Status string `json:"status"`
Data string `json:"data"`
}

var ru rsaurl
err = json.Unmarshal(body, &ru)
if err != nil {
return resp, body, sresp, errors.Wrap(err, "url body unmarshal failed")
}

pemurl := strings.Replace(ru.Data, "\\", "", -1)

//get sesha3 token
type payload_token_t struct {
Username string `json:"username"`
Passwd string `json:"passwd"`
}
payloadToken := payload_token_t{
Username: s.session.Config.Username,
Passwd: s.session.Config.Password,
}

b, err := json.Marshal(payloadToken)
if err != nil {
return resp, body, sresp, errors.Wrap(err, "payload token marshal failed")
}

ep := s.session.Sesha3Endpoint() + "/token"
req, err := http.NewRequest(http.MethodGet, ep, bytes.NewBuffer(b))
req.Header.Add("Content-Type", "application/json")
resp, body, err = s.client.Do(req)
if err != nil {
return resp, body, sresp, errors.Wrap(err, "client do failed")
}

var m map[string]string
err = json.Unmarshal(body, &m)

if err != nil {
return resp, body, sresp, errors.Wrap(err, "token reply unmarshal failed")
}

token, ok := m["key"]
if !ok {
return resp, body, sresp, errors.Wrap(err, "can't find token")
}

type payload_t struct {
Pem string `json:"pem"`
StackId string `json:"stackid"`
Target string `json:"target"`
Script string `json:"script"`
ScriptName string `json:"script_name"`
User string `json:"user"`
}

payload := payload_t{
Pem: pemurl,
StackId: in.StackId,
Target: in.Target,
Script: in.Script,
ScriptName: in.ScriptName,
User: in.InstUser,
}

b, err = json.Marshal(payload)
if err != nil {
return resp, body, sresp, errors.Wrap(err, "payload marshal failed")
}

ep = s.session.Sesha3Endpoint() + "/exec"
req, err = http.NewRequest(http.MethodGet, ep, bytes.NewBuffer(b))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+token)
resp, body, err = s.client.Do(req)
if err != nil {
return resp, body, sresp, errors.Wrap(err, "client do failed")
}

err = json.Unmarshal(body, &sresp)
if err != nil {
return resp, body, sresp, errors.Wrap(err, "reply unmarshal failed")
}

return resp, body, sresp, nil
}

func (s *sesha3) GetSessionUrl(in *GetSessionUrlInput) (*client.Response, []byte, string, error) {
var u string

Expand Down

0 comments on commit c0f70e3

Please sign in to comment.