-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Installing node from images.json #203
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5c4c5a4
POC of fetching the node versions from images.json
pacostas 082b4c6
finalizing the implementation
pacostas af7a1b8
adding error checking on imagesJson decoding
pacostas fdde2c3
using imagesManager to fetch imagejson file path
pacostas fe4b218
refactor: adding more stuff inside createTomlFileFunction
pacostas 78ac7c9
proper name for the extension unit tests
pacostas 53e055f
creating utils with tests and testdata
pacostas 02938ef
removing imagesManager and adding a path instead
pacostas 9071d79
saving config.toml file
pacostas a8bceb7
refactor: generate tests
pacostas 406cafa
using structs
pacostas 4b1645c
refactor: moving rest of the utils under utils package
pacostas d98d467
fixing integration tests to work with copied images.json
pacostas 1031862
fix: adding validation in case there are no nodejs stacks
pacostas 64250c5
moving generateImagesJson testhelper function in a common place
pacostas 276a7bc
fix: fetch run image from env integration tests
pacostas 7a42902
reafactor:adding a wrapper on create config content functions
pacostas 4ccc558
fix: importing default values from extension
pacostas 15c9400
fix: remove tmp directory
pacostas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package constants | ||
|
||
const DEFAULT_USER_ID = 1002 | ||
const DEFAULT_GROUP_ID = 1000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
package ubinodejsextension | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/paketo-community/ubi-nodejs-extension/internal/utils" | ||
"github.com/paketo-community/ubi-nodejs-extension/structs" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
"github.com/paketo-buildpacks/libnodejs" | ||
|
@@ -18,30 +15,8 @@ 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" | ||
|
||
var DEFAULT_USER_ID = 1002 | ||
var DEFAULT_GROUP_ID = 1000 | ||
|
||
type DuringBuildPermissions struct { | ||
CNB_USER_ID, CNB_GROUP_ID int | ||
} | ||
|
||
//go:embed templates/build.Dockerfile | ||
var buildDockerfileTemplate string | ||
|
||
type BuildDockerfileProps struct { | ||
NODEJS_VERSION uint64 | ||
CNB_USER_ID, CNB_GROUP_ID int | ||
CNB_STACK_ID, PACKAGES string | ||
} | ||
|
||
//go:embed templates/run.Dockerfile | ||
var runDockerfileTemplate string | ||
|
||
type RunDockerfileProps struct { | ||
Source string | ||
} | ||
const PACKAGES = "make gcc gcc-c++ libatomic_ops git openssl-devel nodejs npm nodejs-nodemon nss_wrapper which python3" | ||
const CONFIG_TOML_PATH = "/tmp/config.toml" | ||
|
||
//go:generate faux --interface DependencyManager --output fakes/dependency_manager.go | ||
type DependencyManager interface { | ||
|
@@ -50,7 +25,7 @@ type DependencyManager interface { | |
GenerateBillOfMaterials(dependencies ...postal.Dependency) []packit.BOMEntry | ||
} | ||
|
||
func Generate(dependencyManager DependencyManager, logger scribe.Emitter, duringBuildPermissions DuringBuildPermissions) packit.GenerateFunc { | ||
func Generate(dependencyManager DependencyManager, logger scribe.Emitter, duringBuildPermissions structs.DuringBuildPermissions, imagesJsonPath string) packit.GenerateFunc { | ||
return func(context packit.GenerateContext) (packit.GenerateResult, error) { | ||
|
||
logger.Title("%s %s", context.Info.Name, context.Info.Version) | ||
|
@@ -65,10 +40,19 @@ func Generate(dependencyManager DependencyManager, logger scribe.Emitter, during | |
|
||
logger.Candidates(allNodeVersionsInPriorityOrder) | ||
|
||
// Search and fetch the version from the extension.toml | ||
configTomlFileContent, err := utils.GenerateConfigTomlContentFromImagesJson(imagesJsonPath, context.Stack) | ||
if err != nil { | ||
return packit.GenerateResult{}, err | ||
} | ||
|
||
//save config.toml file | ||
err = os.WriteFile(CONFIG_TOML_PATH, configTomlFileContent, 0644) | ||
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) | ||
dependency, err := dependencyManager.Resolve(CONFIG_TOML_PATH, highestPriorityNodeVersion.Name, nodeVersion, context.Stack) | ||
if err != nil { | ||
return packit.GenerateResult{}, err | ||
} | ||
|
@@ -91,26 +75,23 @@ func Generate(dependencyManager DependencyManager, logger scribe.Emitter, during | |
|
||
logger.Process("Selected Node Engine Major version %d", selectedNodeMajorVersion) | ||
|
||
// These variables have to be fetched from the env | ||
CNB_STACK_ID := os.Getenv("CNB_STACK_ID") | ||
|
||
// Generating build.Dockerfile | ||
buildDockerfileContent, err := FillPropsToTemplate(BuildDockerfileProps{ | ||
buildDockerfileContent, err := utils.GenerateBuildDockerfile(structs.BuildDockerfileProps{ | ||
NODEJS_VERSION: selectedNodeMajorVersion, | ||
CNB_USER_ID: duringBuildPermissions.CNB_USER_ID, | ||
CNB_GROUP_ID: duringBuildPermissions.CNB_GROUP_ID, | ||
CNB_STACK_ID: CNB_STACK_ID, | ||
CNB_STACK_ID: context.Stack, | ||
PACKAGES: PACKAGES, | ||
}, buildDockerfileTemplate) | ||
}) | ||
|
||
if err != nil { | ||
return packit.GenerateResult{}, err | ||
} | ||
|
||
// Generating run.Dockerfile | ||
runDockerfileContent, err := FillPropsToTemplate(RunDockerfileProps{ | ||
runDockerfileContent, err := utils.GenerateRunDockerfile(structs.RunDockerfileProps{ | ||
Source: selectedNodeRunImage, | ||
}, runDockerfileTemplate) | ||
}) | ||
|
||
if err != nil { | ||
return packit.GenerateResult{}, err | ||
|
@@ -123,58 +104,3 @@ func Generate(dependencyManager DependencyManager, logger scribe.Emitter, during | |
}, nil | ||
} | ||
} | ||
|
||
func FillPropsToTemplate(properties interface{}, templateString string) (result string, Error error) { | ||
|
||
templ, err := template.New("template").Parse(templateString) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var buf bytes.Buffer | ||
err = templ.Execute(&buf, properties) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return buf.String(), nil | ||
} | ||
|
||
func GetDuringBuildPermissions(filepath string) DuringBuildPermissions { | ||
|
||
defaultPermissions := DuringBuildPermissions{ | ||
CNB_USER_ID: DEFAULT_USER_ID, | ||
CNB_GROUP_ID: DEFAULT_GROUP_ID, | ||
} | ||
re := regexp.MustCompile(`cnb:x:(\d+):(\d+)::`) | ||
|
||
etcPasswdFile, err := os.ReadFile(filepath) | ||
|
||
if err != nil { | ||
return defaultPermissions | ||
} | ||
etcPasswdContent := string(etcPasswdFile) | ||
|
||
matches := re.FindStringSubmatch(etcPasswdContent) | ||
|
||
if len(matches) != 3 { | ||
return defaultPermissions | ||
} | ||
|
||
CNB_USER_ID, err := strconv.Atoi(matches[1]) | ||
|
||
if err != nil { | ||
return defaultPermissions | ||
} | ||
|
||
CNB_GROUP_ID, err := strconv.Atoi(matches[2]) | ||
|
||
if err != nil { | ||
return defaultPermissions | ||
} | ||
|
||
return DuringBuildPermissions{ | ||
CNB_USER_ID: CNB_USER_ID, | ||
CNB_GROUP_ID: CNB_GROUP_ID, | ||
} | ||
} | ||
Comment on lines
-127
to
-180
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It has been moved untouched to utils |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has been moved to utils