|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "html/template" |
| 7 | + "log" |
| 8 | + "runtime" |
| 9 | + |
| 10 | + "github.com/fynelabs/selfupdate/cmd/selfupdatectl/internal/cloud" |
| 11 | + "github.com/urfave/cli/v2" |
| 12 | +) |
| 13 | + |
| 14 | +func upload() *cli.Command { |
| 15 | + var endpoint string |
| 16 | + var region string |
| 17 | + var bucket string |
| 18 | + var akid string |
| 19 | + var secret string |
| 20 | + var baseS3Path string |
| 21 | + var binaryPattern string |
| 22 | + |
| 23 | + a := &application{} |
| 24 | + |
| 25 | + return &cli.Command{ |
| 26 | + Name: "upload", |
| 27 | + Usage: "Upload an executable which has been properly signed to S3", |
| 28 | + Description: "The executable specified will get its signature generated and checked before being uploaded to a S3 bucket than can be optionally specified.", |
| 29 | + Flags: []cli.Flag{ |
| 30 | + &cli.StringFlag{ |
| 31 | + Name: "private-key", |
| 32 | + Aliases: []string{"priv"}, |
| 33 | + Usage: "The private key file to store the new key in.", |
| 34 | + Destination: &a.privateKey, |
| 35 | + Value: "ed25519.key", |
| 36 | + }, |
| 37 | + &cli.StringFlag{ |
| 38 | + Name: "public-key", |
| 39 | + Aliases: []string{"pub"}, |
| 40 | + Usage: "The public key file to store the new key in.", |
| 41 | + Destination: &a.publicKey, |
| 42 | + Value: "ed25519.pem", |
| 43 | + }, |
| 44 | + &cli.StringFlag{ |
| 45 | + Name: "aws-endpoint", |
| 46 | + Aliases: []string{"e"}, |
| 47 | + Usage: "AWS endpoint to connect to (can be used to connect to non AWS S3 services)", |
| 48 | + EnvVars: []string{"AWS_S3_ENDPOINT"}, |
| 49 | + Destination: &endpoint, |
| 50 | + }, |
| 51 | + &cli.StringFlag{ |
| 52 | + Name: "aws-region", |
| 53 | + Aliases: []string{"r"}, |
| 54 | + Usage: "AWS region to connect to", |
| 55 | + EnvVars: []string{"AWS_S3_REGION"}, |
| 56 | + Destination: ®ion, |
| 57 | + }, |
| 58 | + &cli.StringFlag{ |
| 59 | + Name: "aws-bucket", |
| 60 | + Aliases: []string{"b"}, |
| 61 | + Usage: "AWS bucket to store data into", |
| 62 | + EnvVars: []string{"AWS_S3_BUCKET"}, |
| 63 | + Destination: &bucket, |
| 64 | + }, |
| 65 | + &cli.StringFlag{ |
| 66 | + Name: "aws-secret", |
| 67 | + Aliases: []string{"s"}, |
| 68 | + Usage: "AWS secret to use to establish S3 connection", |
| 69 | + Destination: &secret, |
| 70 | + }, |
| 71 | + &cli.StringFlag{ |
| 72 | + Name: "aws-AKID", |
| 73 | + Aliases: []string{"a"}, |
| 74 | + Usage: "AWS Access Key ID to use to establish S3 connection", |
| 75 | + Destination: &akid, |
| 76 | + }, |
| 77 | + &cli.StringFlag{ |
| 78 | + Name: "aws-base-s3-path", |
| 79 | + Aliases: []string{"path", "p"}, |
| 80 | + Usage: "Specify the sub path in which the binary will be uploaded", |
| 81 | + Destination: &baseS3Path, |
| 82 | + }, |
| 83 | + &cli.StringFlag{ |
| 84 | + Name: "binary-template", |
| 85 | + Aliases: []string{"template", "t"}, |
| 86 | + Usage: "Specify the pattern to use for the binary once uploaded", |
| 87 | + Destination: &binaryPattern, |
| 88 | + Value: "{{.Executable}}-{{.OS}}-{{.Arch}}{{.Ext}}", |
| 89 | + }, |
| 90 | + }, |
| 91 | + Action: func(ctx *cli.Context) error { |
| 92 | + if ctx.Args().Len() == 0 { |
| 93 | + return fmt.Errorf("at least one executable to upload") |
| 94 | + } |
| 95 | + |
| 96 | + log.Println("Connecting to AWS") |
| 97 | + aws, err := cloud.NewAWSSession(akid, secret, endpoint, region, bucket) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + ext := "" |
| 103 | + if runtime.GOOS == "windows" { |
| 104 | + ext = ".exe" |
| 105 | + } |
| 106 | + |
| 107 | + p := platform{ |
| 108 | + OS: runtime.GOOS, |
| 109 | + Arch: runtime.GOARCH, |
| 110 | + Ext: ext, |
| 111 | + } |
| 112 | + |
| 113 | + t, err := template.New("platform").Parse(binaryPattern) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + for _, exe := range ctx.Args().Slice() { |
| 119 | + if runtime.GOOS == "windows" { |
| 120 | + p.Executable = exe[:len(exe)-len(".exe")] |
| 121 | + } else { |
| 122 | + p.Executable = exe |
| 123 | + } |
| 124 | + |
| 125 | + buf := &bytes.Buffer{} |
| 126 | + err = t.Execute(buf, p) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + err = a.upload(aws, exe, baseS3Path+"/"+buf.String()) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + } |
| 136 | + return nil |
| 137 | + }, |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +type platform struct { |
| 142 | + OS string |
| 143 | + Arch string |
| 144 | + Ext string |
| 145 | + Executable string |
| 146 | +} |
| 147 | + |
| 148 | +func (a *application) upload(aws *cloud.AWSSession, executable string, destination string) error { |
| 149 | + if a.check(executable) != nil { |
| 150 | + if err := a.sign(executable); err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + if err := a.check(executable); err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + err := aws.UploadFile(executable, destination) |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + fmt.Println() |
| 163 | + |
| 164 | + defer fmt.Println() |
| 165 | + return aws.UploadFile(executable+".ed25519", destination+".ed25519") |
| 166 | +} |
0 commit comments