Skip to content

Commit

Permalink
POC of fetching the node versions from images.json
Browse files Browse the repository at this point in the history
  • Loading branch information
pacostas committed Aug 16, 2024
1 parent 1db86a6 commit cecf236
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 29 deletions.
23 changes: 0 additions & 23 deletions extension.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,3 @@ description = "This extension installs the appropriate Node.js runtime via dnf"
[metadata]
pre-package = "./scripts/build.sh"
include-files = ["bin/generate", "bin/detect", "bin/run", "extension.toml"]
[metadata.default-versions]
node = "20.*.*"

[[metadata.dependencies]]
id = "node"
name = "Ubi Node Extension"
stacks = ["io.buildpacks.stacks.ubi8"]
source = "paketocommunity/run-nodejs-20-ubi-base"
version = "20.1000"

[[metadata.dependencies]]
id = "node"
name = "Ubi Node Extension"
stacks = ["io.buildpacks.stacks.ubi8"]
source = "paketocommunity/run-nodejs-18-ubi-base"
version = "18.1000"

[[metadata.dependencies]]
id = "node"
name = "Ubi Node Extension"
stacks = ["io.buildpacks.stacks.ubi8"]
source = "paketocommunity/run-nodejs-16-ubi-base"
version = "16.1000"
94 changes: 88 additions & 6 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package ubinodejsextension
import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"text/template"

"github.com/BurntSushi/toml"
"github.com/Masterminds/semver/v3"
"github.com/paketo-buildpacks/libnodejs"
"github.com/paketo-buildpacks/packit/v2"
Expand All @@ -18,10 +22,34 @@ import (
"github.com/paketo-buildpacks/packit/v2/scribe"
)

var PACKAGES = "make gcc gcc-c++ libatomic_ops git openssl-devel nodejs npm nodejs-nodemon nss_wrapper which python3"
const PACKAGES = "make gcc gcc-c++ libatomic_ops git openssl-devel nodejs npm nodejs-nodemon nss_wrapper which python3"

const DEFAULT_USER_ID = 1002
const DEFAULT_GROUP_ID = 1000

// TODO
// Same struct as in images.json is on the stacks
// Also we dont need all these fields, remove the unused ones.
type StackImages struct {
Name string `json:"name"`
ConfigDir string `json:"config_dir"`
OutputDir string `json:"output_dir"`
BuildImage string `json:"build_image"`
RunImage string `json:"run_image"`
BuildReceiptFilename string `json:"build_receipt_filename"`
RunReceiptFilename string `json:"run_receipt_filename"`
CreateBuildImage bool `json:"create_build_image,omitempty"`
BaseBuildContainerImage string `json:"base_build_container_image,omitempty"`
BaseRunContainerImage string `json:"base_run_container_image"`
Type string `json:"type,omitempty"`
}

var DEFAULT_USER_ID = 1002
var DEFAULT_GROUP_ID = 1000
type ImagesJson struct {
SupportUsns bool `json:"support_usns"`
UpdateOnNewImage bool `json:"update_on_new_image"`
ReceiptsShowLimit int `json:"receipts_show_limit"`
StackImages []StackImages `json:"images"`
}

type DuringBuildPermissions struct {
CNB_USER_ID, CNB_GROUP_ID int
Expand Down Expand Up @@ -65,10 +93,64 @@ func Generate(dependencyManager DependencyManager, logger scribe.Emitter, during

logger.Candidates(allNodeVersionsInPriorityOrder)

// Search and fetch the version from the extension.toml
imagesJsonPath, err := os.Open("/etc/buildpacks/images.json")
if err != nil {
return packit.GenerateResult{}, err
}

var imagesJson ImagesJson
json.NewDecoder(imagesJsonPath).Decode(&imagesJson)

Check failure on line 102 in generate.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `(*encoding/json.Decoder).Decode` is not checked (errcheck)
err = imagesJsonPath.Close()
if err != nil {
return packit.GenerateResult{}, err
}

nodeVersion, _ := highestPriorityNodeVersion.Metadata["version"].(string)
extensionFilePath := filepath.Join(context.CNBPath, "extension.toml")
dependency, err := dependencyManager.Resolve(extensionFilePath, highestPriorityNodeVersion.Name, nodeVersion, context.Stack)

nodejsRegex, _ := regexp.Compile("^nodejs")

var dependencies []map[string]interface{}

for _, stack := range imagesJson.StackImages {
if !nodejsRegex.MatchString(stack.Name) {
continue
}

//TODO fetch the stacks from the images.json
nodeVersion := strings.Split(stack.Name, "-")[1]
dependency := map[string]interface{}{
"id": "node",
"stacks": []string{"io.buildpacks.stacks.ubi8"},
"version": fmt.Sprintf("%s.1000", nodeVersion),
"source": fmt.Sprintf("paketocommunity/run-nodejs-%s-ubi-base", nodeVersion),
}
dependencies = append(dependencies, dependency)

}

config := map[string]interface{}{
"metadata": map[string]interface{}{
"default-versions": map[string]string{
"node": "20.*.*",
},
"dependencies": dependencies,
},
}

buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(config); err != nil {
log.Fatal(err)
}
fmt.Println(buf.String())

err = os.WriteFile("./config.toml", buf.Bytes(), 0744)
if err != nil {
return packit.GenerateResult{}, err
}

// Search and fetch the version from the config.toml
configFilePath := filepath.Join("./config.toml")
dependency, err := dependencyManager.Resolve(configFilePath, highestPriorityNodeVersion.Name, nodeVersion, context.Stack)
if err != nil {
return packit.GenerateResult{}, err
}
Expand Down

0 comments on commit cecf236

Please sign in to comment.