diff --git a/commands/upload_file.go b/commands/upload_file.go new file mode 100644 index 0000000..9980a78 --- /dev/null +++ b/commands/upload_file.go @@ -0,0 +1,90 @@ +package commands + +import ( + "fmt" + "io" + "os" + + "path/filepath" +) + +type UploadFileArgs struct { + Command string `json:"command"` + LocalFilepath string `json:"local_filepath"` + DestinationFilepath string `json:"destination_filepath"` + PrivateKeyFile string `json:"private_key_file"` + Username string `json:"username"` + Host string `json:"host"` +} + +func (ufa *UploadFileArgs) ReadArgs() { + + if len(os.Args) != 7 { + fmt.Fprintf(os.Stderr, "Usage: %s upload_file \n", os.Args[0]) + os.Exit(1) + } + + ufa.Command = os.Args[1] + ufa.LocalFilepath = os.Args[2] + ufa.DestinationFilepath = os.Args[3] + ufa.PrivateKeyFile = os.Args[4] + ufa.Username = os.Args[5] + ufa.Host = os.Args[6] + +} + +func UploadFile() { + + args := UploadFileArgs{} + args.ReadArgs() + + client := GetClient(args.PrivateKeyFile, args.Username, args.Host) + defer client.Close() + + session, err := client.NewSession() + if err != nil { + fmt.Fprintf(os.Stderr, "Error creating SSH session: %v\n", err) + os.Exit(1) + } + defer session.Close() + + source_file, err := os.Open(args.LocalFilepath) + if err != nil { + fmt.Fprintf(os.Stderr, "Error opening local file: %v\n", err) + os.Exit(1) + } + defer source_file.Close() + + info, err := source_file.Stat() + if err != nil { + fmt.Fprintf(os.Stderr, "Error stating local file: %v\n", err) + os.Exit(1) + } + + w, err := session.StdinPipe() + if err != nil { + fmt.Fprintf(os.Stderr, "Error creating stdin pipe: %v\n", err) + os.Exit(1) + } + + if err := session.Start("scp -t " + args.DestinationFilepath); err != nil { + fmt.Fprintf(os.Stderr, "Error starting scp: %v\n", err) + os.Exit(1) + } + + fmt.Fprintf(w, "C0644 %d %s\n", info.Size(), filepath.Base(args.LocalFilepath)) + if _, err := io.Copy(w, source_file); err != nil { + fmt.Fprintf(os.Stderr, "Error sending file: %v\n", err) + os.Exit(1) + } + fmt.Fprint(w, "\x00") + w.Close() + + if err := session.Wait(); err != nil { + fmt.Fprintf(os.Stderr, "Error during scp transfer: %v\n", err) + os.Exit(1) + } + + fmt.Println("Success") +} + diff --git a/main.go b/main.go index c285b00..9f739c5 100644 --- a/main.go +++ b/main.go @@ -2,164 +2,11 @@ package main import ( "fmt" - "io" "os" - "path/filepath" - "golang.org/x/crypto/ssh" "periodic/prddeploy/commands" ) -type UploadArgs struct { - Command string `json:"command"` - LocalFilepath string `json:"local_filepath"` - DestinationFilepath string `json:"destination_filepath"` - PrivateKeyFile string `json:"private_key_file"` - Username string `json:"username"` - Host string `json:"host"` -} - -type RunCommandArgs struct { - Command string `json:"command"` - RemoteCommand string `json:"remote_command"` - PrivateKeyFile string `json:"private_key_file"` - Username string `json:"username"` - Host string `json:"host"` -} - -func upload_read_args() UploadArgs { - - if len(os.Args) != 7 { - fmt.Fprintf(os.Stderr, "Usage: %s \n", os.Args[0]) - os.Exit(1) - } - - return UploadArgs{ - os.Args[1], - os.Args[2], - os.Args[3], - os.Args[4], - os.Args[5], - os.Args[6], - } -} - -func get_signer(private_key_file string) ssh.Signer { - - key_bytes, err := os.ReadFile(private_key_file) - if err != nil { - fmt.Fprintf(os.Stderr, "Error reading private key: %v\n", err) - os.Exit(1) - } - - signer, err := ssh.ParsePrivateKey(key_bytes) - if err != nil { - fmt.Fprintf(os.Stderr, "Error parsing private key: %v\n", err) - os.Exit(1) - } - - return signer - -} - -func resolve_port(host string) string { - - if len(host) > 0 { - hasPort := false - for i := len(host) - 1; i >= 0; i-- { - if host[i] == ':' { - hasPort = true - break - } - if host[i] == ']' { - break - } - } - if !hasPort { - host = host + ":22" - } - } - - return host - -} - -func get_client(private_key_file, username, host string) *ssh.Client { - - signer := get_signer(private_key_file) - - config := &ssh.ClientConfig{ - User: username, - Auth: []ssh.AuthMethod{ - ssh.PublicKeys(signer), - }, - HostKeyCallback: ssh.InsecureIgnoreHostKey(), - } - - addr := resolve_port(host) - - client, err := ssh.Dial("tcp", addr, config) - if err != nil { - fmt.Fprintf(os.Stderr, "Error connecting to server: %v\n", err) - os.Exit(1) - } - - return client -} - -func upload_file() { - args := upload_read_args() - - client := get_client(args.PrivateKeyFile, args.Username, args.Host) - defer client.Close() - - session, err := client.NewSession() - if err != nil { - fmt.Fprintf(os.Stderr, "Error creating SSH session: %v\n", err) - os.Exit(1) - } - defer session.Close() - - source_file, err := os.Open(args.LocalFilepath) - if err != nil { - fmt.Fprintf(os.Stderr, "Error opening local file: %v\n", err) - os.Exit(1) - } - defer source_file.Close() - - info, err := source_file.Stat() - if err != nil { - fmt.Fprintf(os.Stderr, "Error stating local file: %v\n", err) - os.Exit(1) - } - - w, err := session.StdinPipe() - if err != nil { - fmt.Fprintf(os.Stderr, "Error creating stdin pipe: %v\n", err) - os.Exit(1) - } - - if err := session.Start("scp -t " + args.DestinationFilepath); err != nil { - fmt.Fprintf(os.Stderr, "Error starting scp: %v\n", err) - os.Exit(1) - } - - fmt.Fprintf(w, "C0644 %d %s\n", info.Size(), filepath.Base(args.LocalFilepath)) - if _, err := io.Copy(w, source_file); err != nil { - fmt.Fprintf(os.Stderr, "Error sending file: %v\n", err) - os.Exit(1) - } - fmt.Fprint(w, "\x00") - w.Close() - - if err := session.Wait(); err != nil { - fmt.Fprintf(os.Stderr, "Error during scp transfer: %v\n", err) - os.Exit(1) - } - - fmt.Println("Success") -} - func main() { if len(os.Args) < 2 { @@ -169,7 +16,8 @@ func main() { switch os.Args[1] { case "upload_file": - upload_file() + commands.UploadFile() + //upload_file() case "run_command": commands.RunCommand() default: