Skip to content

Commit

Permalink
Merge pull request #2814 from ActiveState/mitchell/dx-1960
Browse files Browse the repository at this point in the history
Support a master State Tool versions list
  • Loading branch information
mitchell-as authored Oct 12, 2023
2 parents d8f71ad + 1c465a6 commit ebb45f5
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ jobs:
with:
go-version: ${{ matrix.go-version }}

- # === Install State Tool ===
name: Install State Tool
uses: ActiveState/setup-state-tool@v1

- # === Setup ===
name: Setup
shell: bash
Expand Down Expand Up @@ -465,6 +469,11 @@ jobs:
aws-region: ${{ env.AWS_REGION }}
mask-aws-account-id: true

- # === Generate updated master versions.json if necessary ===
name: Generate version list
shell: bash
run: state run generate-versions-list

- # === Deploy ===
name: Deploy
shell: bash
Expand Down
11 changes: 10 additions & 1 deletion activestate.generators.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ scripts:
value: |
set -e
$constants.SET_ENV
echo "# Generate payload"
go run ./scripts/ci/payload-generator/main.go "$@"
- name: generate-update
Expand Down Expand Up @@ -120,3 +120,12 @@ scripts:
go run scripts/ci/payload-generator/main.go -b ${TEST_CHANNEL} -v ${TEST_VERSION}
copy_test_payload
go run scripts/ci/update-generator/main.go -b ${TEST_CHANNEL} -v ${TEST_VERSION} -o ${TEST_ARCHIVE_DIR}
- name: generate-versions-list
language: bash
standalone: true
description: Generates master versions.json from S3 and info.json's from generate-update
value: |
set -e
$constants.SET_ENV
go run scripts/ci/update-version-list/main.go ./build/update
99 changes: 99 additions & 0 deletions scripts/ci/update-version-list/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/thoas/go-funk"

"github.com/ActiveState/cli/internal/condition"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/httputil"
"github.com/ActiveState/cli/internal/updater"
)

// Where the master version file lives on S3.
const S3PrefixURL = "https://state-tool.s3.amazonaws.com/"
const S3Bucket = "update/state/"
const VersionsJson = "versions.json"

// Valid channels to update the master version file with.
var ValidChannels = []string{constants.BetaBranch, constants.ReleaseBranch}

func init() {
if !condition.OnCI() {
// Allow testing with artifacts produced by `state run generate-test-update`
ValidChannels = append(ValidChannels, "test-channel")
}
}

func main() {
if len(os.Args) != 2 {
log.Fatalf("Usage: %s <build-dir>", os.Args[0])
}

// Fetch the current master list from S3.
versions := []updater.AvailableUpdate{}
fmt.Printf("Fetching master %s file from S3\n", VersionsJson)
bytes, err := httputil.Get(S3PrefixURL + S3Bucket + VersionsJson)
if err != nil {
log.Fatalf("Failed to fetch file: %s", err.Error())
}
err = json.Unmarshal(bytes, &versions)
if err != nil {
log.Fatalf("Failed to decode JSON: %s", err.Error())
}

// Find info.json files to add to the master list and add them.
updated := false
buildDir := os.Args[1]
fmt.Printf("Searching for info.json files in %s\n", buildDir)
files, err := fileutils.ListDir(buildDir, false)
if err != nil {
log.Fatalf("Failed to search %s: %s", buildDir, err.Error())
}
for _, file := range files {
if file.Name() != "info.json" {
continue
}
channel := strings.Split(file.RelativePath(), string(filepath.Separator))[0]
if !funk.Contains(ValidChannels, channel) {
continue
}
fmt.Printf("Found %s\n", file.RelativePath())
bytes, err := fileutils.ReadFile(file.Path())
if err != nil {
log.Fatalf("Unable to read file: %s", err.Error())
}
info := updater.AvailableUpdate{}
err = json.Unmarshal(bytes, &info)
if err != nil {
log.Fatalf("Unable to decode JSON: %s", err.Error())
}
info.Path = S3PrefixURL + S3Bucket + info.Path // convert relative path to full URL
versions = append(versions, info)
updated = true
}

if !updated {
fmt.Println("No updates found.")
return
}

// Write the updated list to disk. The s3-deployer script should pick it up and upload it.
localVersionsJson := filepath.Join(buildDir, VersionsJson)
fmt.Printf("Writing updated %s locally to %s\n", VersionsJson, localVersionsJson)
bytes, err = json.Marshal(versions)
if err != nil {
log.Fatalf("Failed to encode JSON: %s", err.Error())
}
err = fileutils.WriteFile(localVersionsJson, bytes)
if err != nil {
log.Fatalf("Failed to write file: %s", err.Error())
}
}

0 comments on commit ebb45f5

Please sign in to comment.