I am using our go library (it could be changed if necessary) with a folder named "devops", which contains nested content.
in the library, I copy the devops folder with his content, to another folder.
this is the copy function:
func CopyDirectory(scrDir, dest string) error { // scrDir = devops
entries, err := ioutil.ReadDir(scrDir)
if err != nil {
return err
}
for _, entry := range entries {
sourcePath := filepath.Join(scrDir, entry.Name())
destPath := filepath.Join(dest, entry.Name())
fileInfo, err := os.Stat(sourcePath)
fmt.Println(fileInfo)
if err != nil {
return err
}
stat, ok := fileInfo.Sys().(*syscall.Stat_t)
if !ok {
return fmt.Errorf("failed to get raw syscall.Stat_t data for '%s'", sourcePath)
}
switch fileInfo.Mode() & os.ModeType {
case os.ModeDir:
if err := CreateIfNotExists(destPath, 0755); err != nil { // CreateIfNotExists is a internal function
return err
}
if err := CopyDirectory(sourcePath, destPath); err != nil { // CopyDirectory is a internal function
return err
}
case os.ModeSymlink:
if err := CopySymLink(sourcePath, destPath); err != nil { // CopySymLink is a internal function
return err
}
default:
if err := Copy(sourcePath, destPath); err != nil { // Copy is a internal function
return err
}
}
if err := os.Lchown(destPath, int(stat.Uid), int(stat.Gid)); err != nil {
return err
}
isSymlink := entry.Mode()&os.ModeSymlink != 0
if !isSymlink {
if err := os.Chmod(destPath, entry.Mode()); err != nil {
return err
}
}
}
return nil
}
the library runs well as self-running, but when I use it from another project, it tries to copy the devops folder from the project path, and of course, fails.
I tried doing it with embed pkg, but the code fails when calling
fileInfo.Sys().(*syscall.Stat_t)
with an error that doesn't give any details.
this is the my code:
//go:embed devops
var f embed.FS
...
dir, _ := f.ReadDir("devops")
for _, entry := range dir {
fileInfo, _ := entry.Info()
err, ok := fileInfo.Sys().(*syscall.Stat_t)
if !ok {
println(err) // prints 0x0
}
}
How to solve it?
P.S. the code runs in Linux OS.
Related
Able to fetch details about VM from azure using object mentioned here: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-03-01/compute#v55.0.0+incompatible#VirtualMachine
Not able to fetch the privateIPAddress for that VM or can't find way to do it. What would be the way to fetch private ip address for any given vm.
The IP address is associated with IP configurations of the network interface for the Azure VM, you could find the privateIPAddress for that VM from the network Package.
You may get reference from type InterfaceIPConfigurationsClientAPI or type IPConfigurationPropertiesFormat
Here is a good example that worked for me:
Ref: https://github.com/Azure/azure-sdk-for-go/issues/18705#issuecomment-1196930026
ctx := context.Background()
// assumes you authenicated on the command line with `az login`
cred, err := azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{})
if err != nil {
fmt.Println(err)
}
vmClient, err := armcompute.NewVirtualMachinesClient(subscriptionID, cred, nil)
if err != nil {
fmt.Println(err)
}
nicClient, err := armnetwork.NewInterfacesClient(subscriptionID, cred, nil)
if err != nil {
fmt.Println(err)
}
vm, err := vmClient.Get(ctx, "yourResourceGrp", "yourVmName", nil)
if err != nil {
fmt.Println(err)
}
for _, nicRef := range vm.Properties.NetworkProfile.NetworkInterfaces {
nicID, err := arm.ParseResourceID(*nicRef.ID)
if err != nil {
fmt.Println(err)
}
nic, err := nicClient.Get(ctx, nicID.ResourceGroupName, nicID.Name, nil)
if err != nil {
fmt.Println(err)
}
for _, ipCfg := range nic.Properties.IPConfigurations {
if ipCfg.Properties.PublicIPAddress != nil &&
ipCfg.Properties.PublicIPAddress.Properties != nil {
publicIP := *ipCfg.Properties.PublicIPAddress.Properties.IPAddress
fmt.Println("publicIP:", publicIP)
}
if ipCfg.Properties.PrivateIPAddress != nil {
privateIP := *ipCfg.Properties.PrivateIPAddress
log.Println("privateIP:", privateIP)
}
}
}
I am trying to upload a video to azure media services, but once i create the asset and then upload the video to blob with the SAS url, the asset does not contain the video.
main.go
func main() {
assetName := "sample1"
asset, err := azureMedia.CreateAsset(assetName)
if err != nil {
panic(err)
}
log.Println("Asset Name: ", *asset.Name)
sasURL, err := azureMedia.GetStorageURLForAsset(assetName)
if err != nil {
panic(err)
}
log.Println(sasURL)
err = storage.UploadToBlobFromSASUrl(sasURL)
if err != nil {
panic(err)
}
return
}
media_services.go
func CreateAsset(assetName string) (*media.Asset, error) {
asset, err := assetsClient.CreateOrUpdate(context.Background(), resourceGroupName, accountName, assetName, media.Asset{})
if err != nil {
panic(err)
}
return &asset, nil
}
func GetStorageURLForAsset(assetName string) (string, error) {
result, err := assetsClient.ListContainerSas(context.Background(), resourceGroupName, accountName, assetName, media.ListContainerSasInput{
ExpiryTime: &date.Time{Time: time.Now().Add(time.Hour * 4).UTC()},
Permissions: media.ReadWrite,
})
if err != nil {
panic(err)
}
return (*result.AssetContainerSasUrls)[0], nil
}
storage.go
func UploadToBlobFromSASUrl(sasUrl string) error {
// When someone receives the URL, they access the SAS-protected resource with code like this:
u, err := url.Parse(sasUrl)
if err != nil {
panic(err)
}
containerURL := azblob.NewContainerURL(*u, azblob.NewPipeline(azblob.NewAnonymousCredential(), azblob.PipelineOptions{}))
blockBlobUrl := containerURL.NewBlockBlobURL("sample.mp4")
video, err := os.Open("./sample_videos/sample1.mp4")
if err != nil {
panic(err)
}
resp, err := blockBlobUrl.Upload(context.Background(), video, azblob.BlobHTTPHeaders{}, azblob.Metadata{}, azblob.BlobAccessConditions{})
if err != nil {
panic(err)
}
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(resp.Response().Body)
if err != nil {
panic(err)
}
newStr := buf.String()
log.Println(resp.Status(), resp.StatusCode(), newStr)
return nil
}
From what i understood from the docs, the asset should be present and be associated with the video blob, but when i tried to encode it, it says there is no file associated with the asset.
Is there something I'm missing? Any help is appreciated
I am trying to use sidecar mode in kubernetes to create a logs sidecar to expose specific container logs. And I am using kubernetes client to fetch logs from kubernetes api and send it out by websocket. The code shows below:
func serveWs(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
log.Println(err)
}
return
}
defer conn.Close()
logsClient, err := InitKubeLogsClient(config.InCluster)
if err != nil {
log.Fatalln(err)
}
stream, err := logsClient.GetLogs(config.Namespace, config.PodName, config.ContainerName)
if err != nil {
log.Fatalln(err)
}
defer stream.Close()
reader := bufio.NewReader(stream)
for {
line, err := reader.ReadString('\n')
if err != nil {
log.Fatalln(err)
}
conn.WriteMessage(websocket.TextMessage, []byte(line))
}
}
I am using https://github.com/gorilla/websocket as the websocket lib. And on the browser
Is this the best way to do what I want? Is there some better way to just expose the logs api from k8s to websocket?
Put my final code here, thanks for the tips from #Peter:
func serveWs(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
log.Println(err)
}
return
}
log.Println("create new connection")
defer func() {
conn.Close()
log.Println("connection close")
}()
logsClient, err := InitKubeLogsClient(config.InCluster)
if err != nil {
log.Println(err)
return
}
stream, err := logsClient.GetLogs(config.Namespace, config.PodName, config.ContainerName)
if err != nil {
log.Println(err)
return
}
defer stream.Close()
reader := bufio.NewReaderSize(stream, 16)
lastLine := ""
for {
data, isPrefix, err := reader.ReadLine()
if err != nil {
log.Println(err)
return
}
lines := strings.Split(string(data), "\r")
length := len(lines)
if len(lastLine) > 0 {
lines[0] = lastLine + lines[0]
lastLine = ""
}
if isPrefix {
lastLine = lines[length-1]
lines = lines[:(length - 1)]
}
for _, line := range lines {
if err := conn.WriteMessage(websocket.TextMessage, []byte(line)); err != nil {
log.Println(err)
return
}
}
}
}
I am trying to implement custom VSCC (Validation System Chaincode) to add some extra logic on the basis of FunctionName and and the payload data(ChaincodeProposalPayload) . Currently i am able to fetch ChaincodeProposalPayload from ChaincodeActionPayload but the data seems to be encoded.
Following is the code i am using.
// args[0] - function name (not used now)
// args[1] - serialized Envelope
// args[2] - serialized policy
args := stub.GetArgs()
// get the envelope...
env, err := utils.GetEnvelopeFromBlock(args[1])
if err != nil {
logger.Errorf("VSCC error: GetEnvelope failed, err %s", err)
return shim.Error(err.Error())
}
// ...and the payload...
payl, err := utils.GetPayload(env)
if err != nil {
logger.Errorf("VSCC error: GetPayload failed, err %s", err)
return shim.Error(err.Error())
}
// ...and the transaction...
tx, err := utils.GetTransaction(payl.Data)
if err != nil {
logger.Errorf("VSCC error: GetTransaction failed, err %s", err)
return shim.Error(err.Error())
}
// loop through each of the actions within
fmt.Println(len(tx.Actions))
for _, act := range tx.Actions {
cap, err := utils.GetChaincodeActionPayload(act.Payload)
if err != nil {
logger.Errorf("VSCC error: GetChaincodeActionPayload failed, err %s", err)
return shim.Error(err.Error())
}
fmt.Println("payload " + string(cap.ChaincodeProposalPayload))
}
In the console Payload is printed as encoded string like
mycc
invoke
a
b
10
How can i properly decode the payload into json?
Something like this should work:
// ChaincodeProposalPayload
cpp, err := utils.GetChaincodeProposalPayload(cap.ChaincodeProposalPayload)
if err != nil {
logger.Errorf("GetChaincodeProposalPayload failed: %s", err)
return shim.Error(err.Error())
}
// ChaincodeInvocationSpec
cis := &peer.ChaincodeInvocationSpec{}
err = proto.Unmarshal(cpp.Input, cis)
if err != nil {
logger.Errorf("GetChaincodeInvokeSpec failed: %s", err)
return shim.Error(err.Error())
}
spec := &peer.ChaincodeSpec
err = proto.Unmarshal(cis.GetChaincodeSpec(),spec)
if err != nil {
logger.Errorf("Unmarshal ChaincodeSpec failed: %s", err)
return shim.Error(err.Error())
}
ccName := spec.GetChaincodeId().GetName()
ccArgs := spec.GetInput().GetArgs()
fnName := ccArgs[0]
for _, arg := range ccArgs[1:] {
// do what you want with your args
}
I'm trying to run a script via the SSH package in my Go program (so far I've had success).
My issue is, the script attempts to run a command with sudo if the user has sudo privileges, and this causes the bash script to pause until a password is entered by the user.
For example:
[ERROR ] Install cs-server: Checking dependencies: missing: lib32gcc1
# It attempts to install the missing dependencies with sudo but pauses here
[sudo] password for guest:
In my Go program, I have written something that looks similar to this:
// Connect to SSH and retreive session...
out, err := session.StdoutPipe()
if err != nil {
log.Fatal(err)
}
go func(out io.Reader) {
r := bufio.NewScanner(out)
for r.Scan() {
fmt.Println(r.Text())
}
}(out)
// Execute ssh command...
And I receive the exact same output as the example above, only in this case, I don't even see the line [sudo] password for guest:... it only prints up to [ERROR ] Install cs-server: Checking dependencies: missing: lib32gcc1 and pauses forever.
How can I bypass this pause? My options are to either enter the password from my Go program automatically, or end the ssh execution and just receive the output.
I managed to fix this issue by making use of the session.StdoutPipe() and session.StdinPipe(). I wrote a go routine which scans each byte and checks if the last written line starts with "[sudo] password for " and ends with ": ". It will write the password + "\n" to the session.StdinPipe() which continues execution of the script.
Here's all of the code I have for this.
package ssh
import (
"bufio"
"io"
"log"
"net"
"strings"
"golang.org/x/crypto/ssh"
)
type Connection struct {
*ssh.Client
password string
}
func Connect(addr, user, password string) (*Connection, error) {
sshConfig := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
}
conn, err := ssh.Dial("tcp", addr, sshConfig)
if err != nil {
return nil, err
}
return &Connection{conn, password}, nil
}
func (conn *Connection) SendCommands(cmds ...string) ([]byte, error) {
session, err := conn.NewSession()
if err != nil {
log.Fatal(err)
}
defer session.Close()
modes := ssh.TerminalModes{
ssh.ECHO: 0, // disable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
err = session.RequestPty("xterm", 80, 40, modes)
if err != nil {
return []byte{}, err
}
in, err := session.StdinPipe()
if err != nil {
log.Fatal(err)
}
out, err := session.StdoutPipe()
if err != nil {
log.Fatal(err)
}
var output []byte
go func(in io.WriteCloser, out io.Reader, output *[]byte) {
var (
line string
r = bufio.NewReader(out)
)
for {
b, err := r.ReadByte()
if err != nil {
break
}
*output = append(*output, b)
if b == byte('\n') {
line = ""
continue
}
line += string(b)
if strings.HasPrefix(line, "[sudo] password for ") && strings.HasSuffix(line, ": ") {
_, err = in.Write([]byte(conn.password + "\n"))
if err != nil {
break
}
}
}
}(in, out, &output)
cmd := strings.Join(cmds, "; ")
_, err = session.Output(cmd)
if err != nil {
return []byte{}, err
}
return output, nil
}
And an example of how you could use it.
// ssh refers to the custom package above
conn, err := ssh.Connect("0.0.0.0:22", "username", "password")
if err != nil {
log.Fatal(err)
}
output, err := conn.SendCommands("sleep 2", "echo Hello!")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(output))
This is an issue that output stream can't be fully captured for #acidic's code.
The updated code is as following
package main
import (
"bytes"
"fmt"
"io"
"log"
"net"
"strings"
"golang.org/x/crypto/ssh"
)
type Connection struct {
*ssh.Client
password string
}
func Connect(addr, user, password string) (*Connection, error) {
sshConfig := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
}
conn, err := ssh.Dial("tcp", addr, sshConfig)
if err != nil {
return nil, err
}
return &Connection{conn, password}, nil
}
func (conn *Connection) SendCommands(cmds string) ([]byte, error) {
session, err := conn.NewSession()
if err != nil {
log.Fatal(err)
}
defer session.Close()
modes := ssh.TerminalModes{
ssh.ECHO: 0, // disable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
err = session.RequestPty("xterm", 80, 40, modes)
if err != nil {
return []byte{}, err
}
stdoutB := new(bytes.Buffer)
session.Stdout = stdoutB
in, _ := session.StdinPipe()
go func(in io.Writer, output *bytes.Buffer) {
for {
if strings.Contains(string(output.Bytes()), "[sudo] password for ") {
_, err = in.Write([]byte(conn.password + "\n"))
if err != nil {
break
}
fmt.Println("put the password --- end .")
break
}
}
}(in, stdoutB)
err = session.Run(cmds)
if err != nil {
return []byte{}, err
}
return stdoutB.Bytes(), nil
}
func main() {
// ssh refers to the custom package above
conn, err := Connect("0.0.0.0:22", "username", "password")
if err != nil {
log.Fatal(err)
}
output, err := conn.SendCommands("sudo docker ps")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(output))
}
A work around is converting sudo [cmd] to echo [password] | sudo -S [cmd], it is not good, but working for me.
Another workaround if you dont want to use ssh library is to make a pseudo terminal using pty library. An extremely simple example as above
import (
"io"
"os"
"os/exec"
"time"
"github.com/creack/pty"
)
func main() {
c := exec.Command("ssh", "<user>#<IP>")
f, err := pty.Start(c)
if err != nil {
panic(err)
}
time.Sleep(2 * time.Second)
f.Write([]byte("1234\n"))
io.Copy(os.Stdout, f)
}