Executing bash script from Go using goa-webFramework / Command not executing

154 views Asked by At

Tech-stack: HyperledgerFabric 1.4, Project-repository- FirstNetwork under FabricSamples Go – v1.15 , Goa- v3.2.5

Requirement: Using Goa framework, I need to trigger a Post request(http://localhost:8000/createChannelProfile) and it has to return the content of the file as a response. The user will input the channelName and channelProfile from frontend. Basically the routing happens this way ,triggers rest-api request-- this method has to call the script file---- the command in the script file should generate a file---that file has to be sent as a response. The point where I am stuck right now is the command in the script file is not getting executed. It is exiting with status code 127 and returning “failed” response. As part of first trial i'm just returning the string as a response instead of a generated output file. I am kinda new to go and Goa, still figuring out how to send a file as a response.If anyone knew please try to give me a hint. These are the commands i have executed as per GOA documentation: goa gen GoApp2/design, goa example GoApp2/design, go build ./cmd/fabric, ./fabric

I have tried with the similar solutions from stack overflow. But not able to resolve the error.

 design.go 
    
    package design
    
    import (
        "fmt"
        . "goa.design/goa/v3/dsl"
    )
    
    var _ = API("fabric", func() {
        Title("An api for fabric")
        Description("A simple goa service")
        Server("fabric", func() {
            Host("localhost", func() {
                URI("http://localhost:8000")
            })
        })
    })
    
    var _ = Service("fabric", func() {
        Description("The service to create a channel.tx file under channel-artifacts folder")
        //Method to add a new Channel Profile
        Method("create", func() {
            Payload(func() {
                Field(1, "channelName", String, "Name of the channel")
                Field(2, "channelProfile", String, "Name of the channel profile")
                Required("channelName", "channelProfile")
            })
            Result(String)
            Error("not_found", ErrorResult, "channelProfile not found")
            HTTP(func() {
                POST("/createChannelProfile")
                Response(StatusCreated)
            })
        })
        Files("/openapi.json", "./gen/http/openapi.json")
    })

fabric.go

//This is the file which calls the generateChannel.sh file

package fabricapi

import (
    fabric "GoApp2/gen/fabric"
    "context"
    "fmt"
    "log"
    "os/exec"
)

// fabric service example implementation.
// The example methods log the requests and return zero values.
type fabricsrvc struct {
    logger *log.Logger
}

// NewFabric returns the fabric service implementation.
func NewFabric(logger *log.Logger) fabric.Service {
    return &fabricsrvc{logger}
}

// Create implements create.
func (s *fabricsrvc) Create(ctx context.Context, p *fabric.CreatePayload) (res string, err error) {
    s.logger.Print("fabric.create")
    cmd := exec.Command("/bin/bash", "../generateChannel.sh", p.ChannelName, p.ChannelProfile)
    stdout, err := cmd.Output()
    fmt.Errorf("error %s", err)
    s.logger.Print(p.ChannelName)
    s.logger.Print(p.ChannelProfile)
    output := string(stdout)
    s.logger.Print(output)
    res = output

    return res, nil
}

generateChannel.sh file

#!/bin/bash
export CHANNELNAME="$1"
export CHANNELPROFILE="$2"

../bin/configtxgen -profile "${CHANNELPROFILE}" -outputCreateChannelTx "./channel-artifacts/${CHANNELNAME}.tx" -channelID "${CHANNELNAME}"
res=$?
echo ${res}
if [ $res -eq 0 ]; then
  echo "success"
  else
  echo "failed"
fi

Note: I have tested alone this file, it is executing perfectly. The command above in the script file looks for channelProfile and channelName which are defined in the configtx.yaml file and accordingly generates the channel.tx file.I have defined the environment variables and added the paths to the .bashrc and .profile files. Please help me where i am going wrong.

screenshot of go env screenshot of go env

Output from Postman:

Output from Postman:

Output from Terminal:

:~/workspace/fabric-samples/first-network/GoApp2$ ./fabric
[fabricapi] 12:47:59 HTTP "Create" mounted on POST /createChannelProfile
[fabricapi] 12:47:59 HTTP "./gen/http/openapi.json" mounted on GET /openapi.json
[fabricapi] 12:47:59 HTTP server listening on "localhost:8000"
[fabricapi] 12:48:05 id=95ISzo9L req=POST /createChannelProfile from=127.0.0.1
[fabricapi] 12:48:05 fabric.create
[fabricapi] 12:48:06 mychannel
[fabricapi] 12:48:06 TwoOrgsChannel
[fabricapi] 12:48:06 127
failed
0

There are 0 answers