Skip to content

Commit

Permalink
sst upgrade command
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Mar 11, 2024
1 parent 3bd32cb commit eaa76a4
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 24 deletions.
24 changes: 23 additions & 1 deletion cmd/sst/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,25 @@ This will create a ` + "`sst.config.ts`" + ` file and configure the types for yo

},
},
{
Name: "upgrade",
Description: Description{
Short: "Upgrade the CLI to the latest version",
},
Args: ArgumentList{
{
Name: "version",
Description: Description{
Short: "The version to upgrade to",
},
},
},
Run: func(cli *Cli) error {
return global.Upgrade(
cli.Positional(0),
)
},
},
{
Name: "state",
Hidden: true,
Expand Down Expand Up @@ -1042,6 +1061,9 @@ func (c *Cli) Arguments() []string {
}

func (c *Cli) Positional(index int) string {
if index >= len(c.arguments) {
return ""
}
return c.arguments[index]
}

Expand Down Expand Up @@ -1166,7 +1188,7 @@ func (c CommandPath) PrintHelp() error {
maxFlag := 0
for _, cmd := range c {
for _, f := range cmd.Flags {
l := len(f.Name) + 2
l := len(f.Name) + 3
if l > maxFlag {
maxFlag = l
}
Expand Down
132 changes: 109 additions & 23 deletions examples/nextjs/README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,123 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
#

## Getting Started

First, run the development server:
- **10x faster** deploys
- Native **multi-region** support
- No more cyclical dependencies
- No stacks or stack resource limits
- No CDK or npm package conflicts
- Native support for **non-AWS** providers

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
[Read the full announcement here](https://sst.dev/blog/moving-away-from-cdk.html).

_Note: Ion is currently in alpha and only supports deploying Next.js sites and L1 AWS resources. We'll be sharing docs and technical details soon._

## Installation

### curl
sst can be installed using a simple curl command
```
curl -fsSL https://ion.sst.dev/install | bash
```

#### macOS

`sst` is available via a Homebrew Tap, and as downloadable binary from the [releases](https://github.com/sst/ion/releases/latest) page:

```
brew install sst/tap/sst
```

To upgrade to the latest version:

```
brew upgrade sst
```

You might have to run `brew update` before upgrading.

#### Linux

`sst` is available as downloadable binaries from the [releases](https://github.com/sst/ion/releases/latest) page. Download the .deb or .rpm from the [releases](https://github.com/sst/ion/releases/latest) page and install with `sudo dpkg -i` and `sudo rpm -i` respectively.

For arch linux it's available in the [aur](https://aur.archlinux.org/packages/sst-bin)

#### Windows

`sst` is available via [scoop](https://scoop.sh/), and as a downloadable binary from the [releases](https://github.com/sst/ion/releases/latest) page:

```
scoop bucket add sst https://github.com/sst/scoop-bucket.git
scoop install sst
```

To upgrade to the latest version:

```
scoop update sst
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
#### Manually

Download the pre-compiled binaries from the [releases](https://github.com/sst/ion/releases/latest) page and copy to the desired location.

## Quick start

1. Create a new Next.js app.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
```bash
npx create-next-app@latest
```

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
2. Initialize SST in the root of your Next.js app.

## Learn More
```bash
cd my-app
sst create
```

To learn more about Next.js, take a look at the following resources:
- This creates an `sst.config.ts` in your project root.
- `sst.config.ts` is automatically added to the the `exclude` array in `tsconfig.json` to prevent TypeScript from trying to type-check it when building your app
- You should also add `/.sst` and `/.open-next` to your `.gitignore` file.

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
3. Deploy! Ensure you have AWS credentials setup and run:

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
```bash
sst deploy
```

## Deploy with SST
### Custom domains

You can configure the app with a custom domain hosted on [Route 53](https://aws.amazon.com/route53/).

```js {3}
new Nextjs("Web", {
domain: "my-app.com",
});
```

You can setup `www.my-app.com` redirect to `my-app.com`.

```js {3}
new Nextjs("Web", {
domain: {
domainName: "my-app.com",
redirects: ["www.my-app.com"],
},
});
```

Or you can have `www.my-app.com` serve out the same site without redirecting.

```js {3}
new Nextjs("Web", {
domain: {
domainName: "my-app.com",
aliases: ["www.my-app.com"],
},
});
```

Deploy NextJS on AWS with [SST](https://sst.dev) and [Open-Next](https://open-next.js.org/) (It's free NextJS!)
Open-Next is much easier to integrate with an existing AWS back-end, and is a lot cheaper than Vercel.
---

Check out our [SST with Next.js deployment documentation](https://docs.sst.dev/start/nextjs) for more details.
Join the `#ion` channel in our [Discord](https://sst.dev/discord) to learn more and contribute.
111 changes: 111 additions & 0 deletions pkg/global/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package global

import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
)

func Upgrade(version string) error {
var filename string
switch runtime.GOOS {
case "darwin":
filename = "mac-"
case "windows":
filename = "windows-"
default:
filename = "linux-"
}

switch runtime.GOARCH {
case "amd64":
filename += "x86_64.tar.gz"
case "arm64":
filename += "arm64.tar.gz"
case "386":
filename += "i386.tar.gz"
default:
return fmt.Errorf("unsupported architecture")
}
url := "https://github.com/sst/ion/releases/latest/download/sst-" + filename
if version != "" {
if !strings.HasPrefix(version, "v") {
version = "v" + version
}
url = "https://github.com/sst/ion/releases/download/" + version + "/sst-" + filename
}
slog.Info("downloading", "url", url)
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected HTTP status when downloading release: %s", resp.Status)
}

homeDir, err := os.UserHomeDir()
if err != nil {
return err
}

sstBinPath := filepath.Join(homeDir, ".sst", "bin")
if err := os.MkdirAll(sstBinPath, os.ModePerm); err != nil {
return err
}

// Assuming we have a variable `resp` which is the response from a *http.Request
body, err := gzip.NewReader(resp.Body)
if err != nil {
return err
}
defer body.Close()

if err := untar(body, sstBinPath); err != nil {
return err
}

if err := os.Chmod(filepath.Join(sstBinPath, "sst"), 0755); err != nil {
return err
}

return nil
}

func untar(reader io.Reader, target string) error {
tarReader := tar.NewReader(reader)
for {
header, err := tarReader.Next()
if err == io.EOF {
break // End of tarball
}
if err != nil {
return err
}
switch header.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(filepath.Join(target, header.Name), 0755); err != nil {
return err
}
case tar.TypeReg:
outFile, err := os.Create(filepath.Join(target, header.Name))
if err != nil {
return err
}
if _, err := io.Copy(outFile, tarReader); err != nil {
outFile.Close()
return err
}
outFile.Close()
}
}
return nil
}

0 comments on commit eaa76a4

Please sign in to comment.