Skip to content
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

Golden Path Changes - Part 2 #481

Merged
merged 18 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions internal/acceptance/workflows/baking_a_tile.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@ Feature: As a developer, I want to bake a tile
And "bake_records/0.2.0-dev.json" contains substring: "source_revision": "bc3ac24e192ba06a2eca19381ad785ec7069e0d0"
And "bake_records/0.2.0-dev.json" contains substring: "tile_directory": "."
And "bake_records/0.2.0-dev.json" contains substring: "kiln_version": "0.0.0+acceptance-tests"
And "bake_records/0.2.0-dev.json" contains substring: "file_checksum": "3ac44ecc0215677ddde5e3d12df7c8ea7ac6e9ade0bf3c957bbed6645edf6811"
And "tile-0.2.0-dev.pivotal" has sha256 sum "3ac44ecc0215677ddde5e3d12df7c8ea7ac6e9ade0bf3c957bbed6645edf6811"
And "bake_records/0.2.0-dev.json" contains substring: "file_checksum": "25a0bac10db840b33f2e281e7bb82627ce6c8f8d7c157af7d41d1e6d45d0cbd0"
And "tile-0.2.0-dev.pivotal" has sha256 sum "25a0bac10db840b33f2e281e7bb82627ce6c8f8d7c157af7d41d1e6d45d0cbd0"

Scenario: it reads directory configuration from Kilnfile
Given I have a tile source directory "testdata/tiles/non-standard-paths"
When I invoke kiln
| bake |
| --stub-releases |
Then a Tile is created

Scenario: it bakes a tile from a bake record
Given I have a tile source directory "testdata/tiles/bake-record"
When I invoke kiln
| re-bake |
| --output-file=tile-0.1.0.pivotal |
| tile/bake_records/0.1.0.json |
Then a Tile is created
And the Tile contains
| metadata/metadata.yml |
| releases/bpm-1.1.21.tgz |
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
releases/*.tgz
*.pivotal
bake_records/*.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
slug: min
release_sources:
- type: bosh.io
releases:
- name: bpm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
releases:
- name: bpm
sha1: e8abe19ec186962828de843f8f281cddb6141904
version: 1.1.21
remote_source: bosh.io
remote_path: https://bosh.io/d/github.com/cloudfoundry/bpm-release?v=1.1.21
stemcell_criteria:
os: ubuntu-xenial
version: "621.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"source_revision": "4b872e02af39ac7a025af39e1da55b3e0a3cbfe0",
"version": "0.1.0",
"kiln_version": "0.0.0+acceptance-tests",
"tile_directory": "tile",
"file_checksum": "9ef9c2dfcc87688a35d880bfc9141b724a8d61e6de0bbf2b16d226ac57182a98"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
name: bpm
label: BPM
description: It just installs the BOSH Package Manager on an instance
icon_image: $( icon )

metadata_version: "2.7.0"
minimum_version_for_upgrade: 0.1.0
product_version: $( version )
provides_product_versions:
- name: bpm
version: $( version )

rank: 90
serial: false

releases:
- $( release "bpm" )

stemcell_criteria: $( stemcell )

job_types:
- name: hello-server
label: Server
resource_label: Server
description: HTTP Server

templates:
- name: bpm
release: bpm
manifest: {}

static_ip: 1
dynamic_ip: 0

max_in_flight: 1
single_az_only: true

instance_definition:
name: instances
type: integer
label: Instances
configurable: true
default: 1
constraints:
min: 0
max: 1

resource_definitions:
- name: ram
type: integer
label: RAM
configurable: true
default: 1024
constraints:
min: 1024

- name: ephemeral_disk
type: integer
label: Ephemeral Disk
configurable: true
default: 4000
constraints:
min: 2000

- name: persistent_disk
type: integer
label: Persistent Disk
configurable: false
default: 4000
constraints:
min: 2000

- name: cpu
type: integer
label: CPU
configurable: true
default: 1
constraints:
min: 1

runtime_configs: []
property_blueprints: []
form_types: []
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
1 change: 1 addition & 0 deletions internal/acceptance/workflows/using_kiln.feature
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Feature: As a developer, I want the Kiln CLI to be usable
Examples:
| command |
| bake |
| re-bake |
| fetch |
| find-release-version |
| find-stemcell-version |
Expand Down
87 changes: 87 additions & 0 deletions internal/commands/rebake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package commands

import (
"encoding/json"
"fmt"
"github.com/pivotal-cf/jhanda"
"github.com/pivotal-cf/kiln/internal/builder"
"github.com/pivotal-cf/kiln/pkg/bake"
"log"
"os"
"path/filepath"
"strings"
)

type ReBake struct {
bake jhanda.Command
Options struct {
OutputFile string `short:"o" long:"output-file" required:"true" description:"path to where the tile will be output"`
}
}

func NewReBake(bake jhanda.Command) ReBake {
return ReBake{bake: bake}
}

func (cmd ReBake) Execute(args []string) error {
records, err := jhanda.Parse(&cmd.Options, args)
if err != nil {
return err
}
if len(records) != 1 {
return fmt.Errorf("please add exactly one required bake record argument: %d bake arguments passed", len(records))
}
recordBuffer, err := os.ReadFile(records[0])
if err != nil {
return fmt.Errorf("failed to read bake record file: %w", err)
}

var record bake.Record
if err := json.Unmarshal(recordBuffer, &record); err != nil {
return fmt.Errorf("failed to parse bake record: %w", err)
}

workingDirectorySHA, err := builder.GitMetadataSHA(".", false)
if err != nil {
return err
}

if got, exp := workingDirectorySHA, record.SourceRevision; got != exp {
return fmt.Errorf("expected the current worktree to be checked out at the source revision from the record %s but the current head is %s", exp, got)
}

tileDir := filepath.FromSlash(record.TileDirectory)

bakeFlags := []string{
"--version", record.Version,
"--kilnfile", filepath.Join(tileDir, "Kilnfile"),
"--output-file", cmd.Options.OutputFile,
}

if record.TileName != "" {
bakeFlags = append(bakeFlags, strings.Join([]string{"--variable", builder.TileNameVariable, record.TileName}, "="))
}

if err := cmd.bake.Execute(bakeFlags); err != nil {
return err
}

newRecord, err := bake.NewRecordFromFile(cmd.Options.OutputFile)
if err != nil {
return err
}

if !record.IsEquivalent(newRecord, log.New(os.Stderr, "bake record diff: ", 0)) {
return fmt.Errorf("expected tile bake records to be equivilant")
}

return nil
}

func (cmd ReBake) Usage() jhanda.Usage {
return jhanda.Usage{
Description: "re-bake (aka record bake) builds a tile from a bake record if the current HEAD is does not match the record the command will fail",
crhntr marked this conversation as resolved.
Show resolved Hide resolved
ShortDescription: "re-bake constructs a tile from a bake record",
Flags: &cmd.Options,
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func main() {
bakeCommand := commands.NewBake(fs, releasesService, outLogger, errLogger, fetch)
bakeCommand.KilnVersion = version
commandSet["bake"] = bakeCommand
commandSet["re-bake"] = commands.NewReBake(bakeCommand)

commandSet["test"] = commands.NewTileTest()
commandSet["help"] = commands.NewHelp(os.Stdout, globalFlagsUsage, commandSet)
Expand Down
29 changes: 29 additions & 0 deletions pkg/bake/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"io/fs"
"log"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -145,6 +146,34 @@ func (record Record) IsDevBuild() bool {
return record.SourceRevision == builder.DirtyWorktreeSHAValue
}

func (record Record) IsEquivalent(other Record, logger *log.Logger) bool {
if logger == nil {
logger = log.New(io.Discard, "", 0)
}

// if tile records differ on the following fields the tiles are still close enough
record.KilnVersion = ""
other.KilnVersion = ""
record.TileDirectory = ""
other.TileDirectory = ""
record.TileName = ""
other.TileName = ""

if exp, got := record.Version, other.Version; exp != got {
logger.Printf("tile versions are not the same: expected %q but got %q", exp, got)
}

if exp, got := record.SourceRevision, other.SourceRevision; exp != got {
logger.Printf("tile source revisions are not the same: expected %q but got %q", exp, got)
}

if exp, got := record.FileChecksum, other.FileChecksum; exp != got {
logger.Printf("tile file checksums are not the same: expected %q but got %q", exp, got)
}

return record == other
}

func (record Record) WriteFile(tileSourceDirectory string) error {
if record.Version == "" {
return fmt.Errorf("missing required version field")
Expand Down