Skip to content

Commit

Permalink
add chmod
Browse files Browse the repository at this point in the history
  • Loading branch information
dpogorzelski committed Jul 26, 2023
1 parent c441281 commit 496faa3
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 116 deletions.
85 changes: 79 additions & 6 deletions cmd/speedrun/cli/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package cli
import (
"context"
"crypto/tls"
"errors"
"net"
"os"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -34,15 +36,24 @@ var readCmd = &cobra.Command{
var cpCmd = &cobra.Command{
Use: "cp <src> <dst>",
Short: "Copy a file",
Example: " speedrun file cp myfile /tmp/myfile",
Args: cobra.MinimumNArgs(1),
Example: " speedrun file cp myfile :/tmp/myfile\n speedrun file cp :/tmp/myfile myfile\n speedrun file cp :/tmp/myfile :/tmp/mynewfile",
Args: cobra.MinimumNArgs(2),
RunE: cp,
}

var chmodCmd = &cobra.Command{
Use: "chmod <path> <filemode>",
Short: "Chmod a file",
Example: " speedrun file chmod /tmp/myfile 0644",
Args: cobra.MinimumNArgs(2),
RunE: chmod,
}

func init() {
fileCmd.SetUsageTemplate(usage)
fileCmd.AddCommand(readCmd)
fileCmd.AddCommand(cpCmd)
fileCmd.AddCommand(chmodCmd)
}

func read(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -118,6 +129,10 @@ func cp(cmd *cobra.Command, args []string) error {
remoteSrc := strings.HasPrefix(args[0], ":")
remoteDst := strings.HasPrefix(args[1], ":")

if !remoteDst && !remoteSrc {
return errors.New("src and dst cannot be both local to your machine")
}

var content []byte
if !remoteSrc {
content, err = os.ReadFile(args[0])
Expand Down Expand Up @@ -155,14 +170,12 @@ func cp(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

var r *portalpb.FileCpResponse

r, err = c.FileCp(ctx, &portalpb.FileCpRequest{Src: strings.TrimPrefix(args[0], ":"), Dst: strings.TrimPrefix(args[1], ":"), Content: content, RemoteSrc: remoteSrc, RemoteDst: remoteDst})
r, err := c.FileCp(ctx, &portalpb.FileCpRequest{Src: strings.TrimPrefix(args[0], ":"), Dst: strings.TrimPrefix(args[1], ":"), Content: content, RemoteSrc: remoteSrc, RemoteDst: remoteDst})
if err != nil {
log.Error(err.Error())
return
}
if len(r.GetContent()) > 0 {
if !remoteDst {
err := os.WriteFile(args[1], r.GetContent(), 0644)
if err != nil {
log.Error(err.Error())
Expand All @@ -176,3 +189,63 @@ func cp(cmd *cobra.Command, args []string) error {
pool.StopAndWait()
return nil
}

func chmod(cmd *cobra.Command, args []string) error {
usePrivateIP := viper.GetBool("portal.use-private-ip")

tlsConfig, err := cloud.SetupTLS()
if err != nil {
return err
}

target, err := cmd.Flags().GetString("target")
if err != nil {
return err
}

filemode, err := strconv.Atoi(args[1])
if err != nil {
return err
}

portals, err := cloud.GetInstances(target)
if err != nil {
return err
}

pool := pond.New(1000, 10000)
for _, p := range portals {
portal := p
pool.Submit(func() {
fields := log.Fields{
"host": portal.Name,
"address": portal.GetAddress(usePrivateIP),
}
log := log.WithFields(fields)

addr := net.JoinHostPort(portal.GetAddress(usePrivateIP), "1337")
rawconn, err := tls.Dial("tcp", addr, tlsConfig)
if err != nil {
log.Error(err.Error())
return
}

conn := drpcconn.New(rawconn)
defer conn.Close()

c := portalpb.NewDRPCPortalClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

r, err := c.FileChmod(ctx, &portalpb.FileChmodRequest{Path: args[0], Filemode: uint32(filemode)})
if err != nil {
log.Error(err.Error())
return
}

log.WithField("state", r.GetState()).Infof("Done")
})
}
pool.StopAndWait()
return nil
}
19 changes: 19 additions & 0 deletions pkg/portal/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package portal

import (
"context"
"io/fs"
"os"

"github.com/apex/log"
Expand Down Expand Up @@ -64,3 +65,21 @@ func (s *Server) FileCp(ctx context.Context, file *portal.FileCpRequest) (*porta
}
return &portal.FileCpResponse{State: portal.State_UNKNOWN}, nil
}

func (s *Server) FileChmod(ctx context.Context, file *portal.FileChmodRequest) (*portal.FileChmodResponse, error) {
fields := log.Fields{
"context": "file",
"command": "read",
"name": file.GetPath(),
}
log := log.WithFields(fields)
log.Debug("Received file chmod request")

err := os.Chmod(file.GetPath(), fs.FileMode(file.GetFilemode()))
if err != nil {
log.Error(err.Error())
return nil, err
}

return &portal.FileChmodResponse{State: portal.State_UNKNOWN}, nil
}
Loading

0 comments on commit 496faa3

Please sign in to comment.