2021-10-25 09:31:52 +00:00
package main
import (
2022-02-05 17:07:00 +00:00
"fmt"
"os"
"os/exec"
"strings"
2021-10-25 09:31:52 +00:00
2022-04-13 14:57:29 +00:00
"github.com/urfave/cli/v2"
2021-12-17 13:57:57 +00:00
sg msp: initial prototype (#55905)
The eventual goal is to be able to generate a spec somewhat close to the infrastructure definitions for Cody Gateway today based on the way we generate infrastructure for Cloud - see go/rfc-msp . This PR is a first-step prototype to demonstrate CDKTF integration, draft out the sketch as proposed in go/rfc-msp, and lay out some basic architecture for the stuff. A barebones CDKTF setup that imitates ~most of the Cody Gateway setup can be generated and interacted with (see test plan), but has not been applied live anywhere - a few components are still missing, and a migration plan needs to be determined.
The current goal is to land this PR as-is and iterate from here. The new command set `sg msp` is only included with the `msp` build tag, so it won't be distributed just yet. To install a version of `sg` that includes a functional `sg msp` command, run:
```
go build -tags=msp -o=./sg ./dev/sg && ./sg install -f -p=false
```
In a vanilla build of `sg`, you'll get an error when trying to use `sg msp`:

## Binary size
Thanks to Michael's improvements to generated CDKTF Go modules, we are not looking at a 500MB increase in `sg` binary sizes, but we are still faced with a ~20% increase at the moment:
```sh
# Fresh sg installation
$ curl --proto '=https' --tlsv1.2 -sSLf https://install.sg.dev | sh
$ du -h $(which sg)
132M /Users/robert@sourcegraph.com/.sg/sg
# MSP-enabled sg build
$ go build -tags=msp -o=./sg ./dev/sg && du -h ./sg
161M ./sg
```
However, the _default_ build of `sg` continues to not include `msp`, so the binary size remains the same:
```sh
$ go build -o=./sg ./dev/sg && du -h ./sg
132M ./sg
```
## Architecture
See `dev/managedservicesplatform/internal/stack/README.md` and `dev/managedservicesplatform/internal/resource/README.md` for some rudimentary docs on some CDKTF conventions.
## Specifications
Set up initial specifications here: https://github.com/sourcegraph/managed-services
## Test plan
Currently:
```
go build -tags=msp -o=./sg ./dev/sg && ./sg msp generate -o=./tmp ./dev/sg/msp/example/example-service.service.yaml dev
```
And check out the `./tmp` output. Each module is plan-able out of the box right now, as we default to a local Terraform backend:
```
cd ./tmp
asdf install # a .tool-versions is generated
cd stacks/project
terraform init
terraform plan
```
---------
Co-authored-by: Joe Chen <joe@sourcegraph.com>
2023-09-08 00:24:34 +00:00
"github.com/sourcegraph/sourcegraph/dev/sg/internal/category"
2022-02-05 17:07:00 +00:00
"github.com/sourcegraph/sourcegraph/dev/sg/internal/run"
2022-05-11 17:42:18 +00:00
"github.com/sourcegraph/sourcegraph/dev/sg/internal/std"
2022-04-13 17:23:17 +00:00
"github.com/sourcegraph/sourcegraph/lib/errors"
2022-02-05 17:07:00 +00:00
"github.com/sourcegraph/sourcegraph/lib/output"
2021-10-25 09:31:52 +00:00
)
var (
2022-04-13 14:57:29 +00:00
versionChangelogNext bool
versionChangelogEntries int
versionCommand = & cli . Command {
Name : "version" ,
Usage : "View details for this installation of sg" ,
2022-06-07 16:42:19 +00:00
Action : versionExec ,
sg msp: initial prototype (#55905)
The eventual goal is to be able to generate a spec somewhat close to the infrastructure definitions for Cody Gateway today based on the way we generate infrastructure for Cloud - see go/rfc-msp . This PR is a first-step prototype to demonstrate CDKTF integration, draft out the sketch as proposed in go/rfc-msp, and lay out some basic architecture for the stuff. A barebones CDKTF setup that imitates ~most of the Cody Gateway setup can be generated and interacted with (see test plan), but has not been applied live anywhere - a few components are still missing, and a migration plan needs to be determined.
The current goal is to land this PR as-is and iterate from here. The new command set `sg msp` is only included with the `msp` build tag, so it won't be distributed just yet. To install a version of `sg` that includes a functional `sg msp` command, run:
```
go build -tags=msp -o=./sg ./dev/sg && ./sg install -f -p=false
```
In a vanilla build of `sg`, you'll get an error when trying to use `sg msp`:

## Binary size
Thanks to Michael's improvements to generated CDKTF Go modules, we are not looking at a 500MB increase in `sg` binary sizes, but we are still faced with a ~20% increase at the moment:
```sh
# Fresh sg installation
$ curl --proto '=https' --tlsv1.2 -sSLf https://install.sg.dev | sh
$ du -h $(which sg)
132M /Users/robert@sourcegraph.com/.sg/sg
# MSP-enabled sg build
$ go build -tags=msp -o=./sg ./dev/sg && du -h ./sg
161M ./sg
```
However, the _default_ build of `sg` continues to not include `msp`, so the binary size remains the same:
```sh
$ go build -o=./sg ./dev/sg && du -h ./sg
132M ./sg
```
## Architecture
See `dev/managedservicesplatform/internal/stack/README.md` and `dev/managedservicesplatform/internal/resource/README.md` for some rudimentary docs on some CDKTF conventions.
## Specifications
Set up initial specifications here: https://github.com/sourcegraph/managed-services
## Test plan
Currently:
```
go build -tags=msp -o=./sg ./dev/sg && ./sg msp generate -o=./tmp ./dev/sg/msp/example/example-service.service.yaml dev
```
And check out the `./tmp` output. Each module is plan-able out of the box right now, as we default to a local Terraform backend:
```
cd ./tmp
asdf install # a .tool-versions is generated
cd stacks/project
terraform init
terraform plan
```
---------
Co-authored-by: Joe Chen <joe@sourcegraph.com>
2023-09-08 00:24:34 +00:00
Category : category . Util ,
2022-04-13 14:57:29 +00:00
Subcommands : [ ] * cli . Command {
2022-02-05 17:07:00 +00:00
{
2022-04-13 14:57:29 +00:00
Name : "changelog" ,
2022-04-13 17:23:17 +00:00
Aliases : [ ] string { "c" } ,
2022-04-13 14:57:29 +00:00
Usage : "See what's changed in or since this version of sg" ,
Flags : [ ] cli . Flag {
& cli . BoolFlag {
Name : "next" ,
Usage : "Show changelog for changes you would get if you upgrade." ,
Destination : & versionChangelogNext ,
} ,
& cli . IntFlag {
Name : "limit" ,
Usage : "Number of changelog entries to show." ,
2022-04-13 17:23:17 +00:00
Value : 5 ,
2022-04-13 14:57:29 +00:00
Destination : & versionChangelogEntries ,
} ,
} ,
2022-06-07 16:42:19 +00:00
Action : changelogExec ,
2022-02-05 17:07:00 +00:00
} ,
} ,
2021-10-25 09:31:52 +00:00
}
)
2024-04-25 15:10:55 +00:00
func versionExec ( c * cli . Context ) error {
2024-08-07 16:34:38 +00:00
// Write on stderr to ensure we can use the output in scripts without having to trim
// the output from the contextual infos.
outErr := std . NewOutput ( os . Stderr , verbose )
outErr . WriteNoticef ( "Showing the current version of the sg CLI, if you're looking for deployed Sourcegraph instances version, please use `sg live` instead." )
2024-04-25 15:10:55 +00:00
if verbose {
std . Out . Writef ( "Version: %s\nBuild commit: %s" ,
c . App . Version , BuildCommit )
} else {
std . Out . Write ( c . App . Version )
}
2021-10-25 09:31:52 +00:00
return nil
}
2022-02-05 17:07:00 +00:00
2022-06-07 16:42:19 +00:00
func changelogExec ( ctx * cli . Context ) error {
2022-04-13 17:55:29 +00:00
if _ , err := run . GitCmd ( "fetch" , "origin" , "main" ) ; err != nil {
return errors . Newf ( "failed to update main: %s" , err )
}
2022-02-05 17:07:00 +00:00
logArgs := [ ] string {
2022-02-07 09:04:34 +00:00
// Format nicely
2022-02-05 17:07:00 +00:00
"log" , "--pretty=%C(reset)%s %C(dim)%h by %an, %ar" ,
"--color=always" ,
2022-02-07 09:04:34 +00:00
// Filter out stuff we don't want
"--no-merges" ,
// Limit entries
2022-04-13 14:57:29 +00:00
fmt . Sprintf ( "--max-count=%d" , versionChangelogEntries ) ,
2022-02-05 17:07:00 +00:00
}
var title string
if BuildCommit != "dev" {
2022-03-02 16:46:29 +00:00
current := strings . TrimPrefix ( BuildCommit , "dev-" )
2022-04-13 14:57:29 +00:00
if versionChangelogNext {
2022-03-01 22:38:43 +00:00
logArgs = append ( logArgs , current + "..origin/main" )
2024-04-25 15:10:55 +00:00
title = fmt . Sprintf ( "Changes since sg release %s" , ReleaseName )
2022-02-05 17:07:00 +00:00
} else {
logArgs = append ( logArgs , current )
2024-04-25 15:10:55 +00:00
title = fmt . Sprintf ( "Changes in sg release %s" , ReleaseName )
2022-02-05 17:07:00 +00:00
}
} else {
2022-05-11 17:42:18 +00:00
std . Out . WriteWarningf ( "Dev version detected - just showing recent changes." )
2022-02-05 17:07:00 +00:00
title = "Recent sg changes"
}
gitLog := exec . Command ( "git" , append ( logArgs , "--" , "./dev/sg" ) ... )
gitLog . Env = os . Environ ( )
2024-08-13 11:22:21 +00:00
out , err := run . InRoot ( gitLog , run . InRootArgs { } )
2022-02-05 17:07:00 +00:00
if err != nil {
return err
}
2022-05-11 17:42:18 +00:00
block := std . Out . Block ( output . Styled ( output . StyleSearchQuery , title ) )
2022-02-05 17:07:00 +00:00
if len ( out ) == 0 {
block . Write ( "No changes found." )
} else {
block . Write ( out + "..." )
}
block . Close ( )
2022-05-11 17:42:18 +00:00
std . Out . WriteLine ( output . Styledf ( output . StyleSuggestion ,
2022-04-13 14:57:29 +00:00
"Only showing %d entries - configure with 'sg version changelog -limit=50'" , versionChangelogEntries ) )
2022-02-05 17:07:00 +00:00
return nil
}