Skip to content

Commit 17dbafb

Browse files
kangastaBrandonRomanonywilkenlbajolet-hashicorp
authored
chore: prepare for Packer integration (#51)
Co-authored-by: BrandonRomano <brandon@hashicorp.com> Co-authored-by: Wilken Rivera <dev@wilkenrivera.com> Co-authored-by: Lucas Bajolet <lucas.bajolet@hashicorp.com>
1 parent 4f2c259 commit 17dbafb

File tree

19 files changed

+480
-712
lines changed

19 files changed

+480
-712
lines changed

.github/workflows/docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
with:
2222
go-version-file: 'go.mod'
2323
- name: Generate documentation
24-
run: make generate
24+
run: make docs
2525
- name: Create PR for docs update
2626
uses: peter-evans/create-pull-request@38e0b6e68b4c852a5500a94740f0e535e0d7ba54 # v4.2.4
2727
with:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Manual release workflow is used for deploying documentation updates
2+
# on the specified branch without making an official plugin release.
3+
name: Notify Integration Release (Manual)
4+
on:
5+
workflow_dispatch:
6+
inputs:
7+
version:
8+
description: "The release version (semver)"
9+
default: 1.0.0
10+
required: false
11+
branch:
12+
description: "A branch or SHA"
13+
default: 'main'
14+
required: false
15+
jobs:
16+
notify-release:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout this repo
20+
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
21+
with:
22+
ref: ${{ github.event.inputs.branch }}
23+
# Ensure that Docs are Compiled
24+
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
25+
- shell: bash
26+
run: make docs
27+
- shell: bash
28+
run: |
29+
if [[ -z "$(git status -s)" ]]; then
30+
echo "OK"
31+
else
32+
echo "Docs have been updated, but the compiled docs have not been committed."
33+
echo "Run 'make docs', and commit the result to resolve this error."
34+
exit 1
35+
fi
36+
# Perform the Release
37+
- name: Checkout integration-release-action
38+
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
39+
with:
40+
repository: hashicorp/integration-release-action
41+
path: ./integration-release-action
42+
- name: Notify Release
43+
uses: ./integration-release-action
44+
with:
45+
# The integration identifier will be used by the Packer team to register the integration
46+
# the expected format is packer/<GitHub Org Name>/<plugin-name>
47+
integration_identifier: "packer/UpCloudLtd/upcloud"
48+
release_version: ${{ github.event.inputs.version }}
49+
release_sha: ${{ github.event.inputs.branch }}
50+
github_token: ${{ secrets.GITHUB_TOKEN }}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Notify Integration Release (Tag)
2+
on:
3+
push:
4+
tags:
5+
- '*.*.*' # Proper releases
6+
jobs:
7+
strip-version:
8+
runs-on: ubuntu-latest
9+
outputs:
10+
packer-version: ${{ steps.strip.outputs.packer-version }}
11+
steps:
12+
- name: Strip leading v from version tag
13+
id: strip
14+
env:
15+
REF: ${{ github.ref_name }}
16+
run: |
17+
echo "packer-version=$(echo "$REF" | sed -E 's/v?([0-9]+\.[0-9]+\.[0-9]+)/\1/')" >> "$GITHUB_OUTPUT"
18+
notify-release:
19+
needs:
20+
- strip-version
21+
runs-on: ubuntu-latest
22+
steps:
23+
- name: Checkout this repo
24+
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
25+
with:
26+
ref: ${{ github.ref }}
27+
# Ensure that Docs are Compiled
28+
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
29+
- shell: bash
30+
run: make docs
31+
- shell: bash
32+
run: |
33+
if [[ -z "$(git status -s)" ]]; then
34+
echo "OK"
35+
else
36+
echo "Docs have been updated, but the compiled docs have not been committed."
37+
echo "Run 'make docs', and commit the result to resolve this error."
38+
exit 1
39+
fi
40+
# Perform the Release
41+
- name: Checkout integration-release-action
42+
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
43+
with:
44+
repository: hashicorp/integration-release-action
45+
path: ./integration-release-action
46+
- name: Notify Release
47+
uses: ./integration-release-action
48+
with:
49+
# The integration identifier will be used by the Packer team to register the integration
50+
# the expected format is packer/<GitHub Org Name>/<plugin-name>
51+
integration_identifier: "packer/UpCloudLtd/upcloud"
52+
release_version: ${{ needs.strip-version.outputs.packer-version }}
53+
release_sha: ${{ github.ref }}
54+
github_token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test.yaml

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ jobs:
1010
test:
1111
strategy:
1212
matrix:
13-
go-version: [1.18.x]
1413
os: [ubuntu-latest, macos-latest, windows-latest]
1514

1615
runs-on: ${{ matrix.os }}
@@ -21,24 +20,17 @@ jobs:
2120
- name: Setup Go
2221
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1
2322
with:
24-
go-version: ${{ matrix.go-version }}
25-
26-
- name: Dependencies
27-
run: |
28-
go version
29-
go get -u golang.org/x/lint/golint
23+
go-version-file: "go.mod"
3024

3125
- name: Build
3226
run: make build
3327

34-
- name: Test PR
35-
if: github.ref != 'refs/heads/main'
28+
- name: Run unit-tests
3629
run: make test
3730

38-
- name: Test main
39-
if: github.ref == 'refs/heads/main'
31+
- name: Run acceptance tests
4032
env:
41-
UPCLOUD_API_USER: ${{ secrets.UPCLOUD_API_USER }}
42-
UPCLOUD_API_PASSWORD: ${{ secrets.UPCLOUD_API_PASSWORD }}
33+
UPCLOUD_USERNAME: ${{ secrets.UPCLOUD_API_USER }}
34+
UPCLOUD_PASSWORD: ${{ secrets.UPCLOUD_API_PASSWORD }}
4335
PACKER_ACC: 1
4436
run: make test_integration

.web-docs/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
This is a plugin for Packer which can be used to generate storage templates on UpCloud.
2+
It utilises the [UpCloud Go API](https://github.com/UpCloudLtd/upcloud-go-api) to interface with the UpCloud API.
3+
4+
### Installation
5+
6+
To install this plugin, copy and paste this code into your Packer configuration, then run [`packer init`](https://www.packer.io/docs/commands/init).
7+
8+
9+
```hcl
10+
packer {
11+
required_plugins {
12+
upcloud = {
13+
version = ">=v1.0.0"
14+
source = "github.com/UpCloudLtd/upcloud"
15+
}
16+
}
17+
}
18+
19+
```
20+
21+
Alternatively, you can use `packer plugins install` to manage installation of this plugin.
22+
23+
```sh
24+
$ packer plugins install github.com/UpCloudLtd/upcloud
25+
```
26+
27+
28+
### Components
29+
30+
#### Builders
31+
32+
33+
- [upcloud](/packer/integrations/upcloudltd/upcloud/latest/components/builder/upcloud) - The upcloud builder is used to generate storage templates on UpCloud.
34+
35+
#### Post-processors
36+
37+
- [upcloud-import](/packer/integrations/upcloudltd/upcloud/latest/components/post-processor/import) - The upcloud import post-processors is used to import disk images to UpCloud.
38+
39+
### JSON Templates
40+
From Packer version 1.7.0, template HCL2 becomes officially the preferred way to write Packer configuration. While the `json` format is still supported, but certain new features, such as `packer init` works only in newer HCL2 format.
41+
If you are using `json` config templates, please consider upgrading them using the packer built-in command:
42+
43+
```sh
44+
$ packer hcl2_upgrade example.json
45+
Successfully created example.json.pkr.hcl
46+
```

docs/builders/upcloud.mdx renamed to .web-docs/components/builder/upcloud/README.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
---
2-
description: >
3-
This is a builder plugin for Packer which can be used to generate storage templates on UpCloud.
4-
It utilises the UpCloud Go API to interface with the UpCloud API.
5-
page_title: UpCloud - Builders
6-
---
7-
8-
9-
# UpCloud
101

112
Type: `upcloud`
123

@@ -375,4 +366,4 @@ build {
375366
}
376367
}
377368
378-
```
369+
```

docs/post-processors/upcloud-import.mdx renamed to .web-docs/components/post-processor/upcloud-import/README.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
---
2-
description: >
3-
This is a post-processor for Packer which can be used to import raw disk images as private templates to UpCloud.
4-
It utilises the UpCloud Go API to interface with the UpCloud API.
5-
page_title: UpCloud - Post-processors
6-
---
7-
8-
9-
# UpCloud Import Post-Processor
101

112
Type: `upcloud-import`
123
Artifact BuilderId: `packer.post-processor.upcloud-import`

.web-docs/metadata.hcl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# For full specification on the configuration of this file visit:
2+
# https://github.com/hashicorp/integration-template#metadata-configuration
3+
integration {
4+
name = "UpCloud"
5+
description = "A builder plugin for Packer which can be used to generate storage templates on UpCloud."
6+
identifier = "packer/UpCloudLtd/upcloud"
7+
component {
8+
type = "builder"
9+
name = "UpCloud"
10+
slug = "upcloud"
11+
}
12+
component {
13+
type = "post-processor"
14+
name = "UpCloud"
15+
slug = "import"
16+
}
17+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/env bash
2+
3+
# Converts the folder name that the component documentation file
4+
# is stored in into the integration slug of the component.
5+
componentTypeFromFolderName() {
6+
if [[ "$1" = "builders" ]]; then
7+
echo "builder"
8+
elif [[ "$1" = "provisioners" ]]; then
9+
echo "provisioner"
10+
elif [[ "$1" = "post-processors" ]]; then
11+
echo "post-processor"
12+
elif [[ "$1" = "datasources" ]]; then
13+
echo "data-source"
14+
else
15+
echo ""
16+
fi
17+
}
18+
19+
# $1: The content to adjust links
20+
# $2: The organization of the integration
21+
rewriteLinks() {
22+
local result="$1"
23+
local organization="$2"
24+
25+
urlSegment="([^/]+)"
26+
urlAnchor="(#[^/]+)"
27+
28+
# Rewrite Component Index Page links to the Integration root page.
29+
#
30+
# (\1) (\2) (\3)
31+
# /packer/plugins/datasources/amazon#anchor-tag-->
32+
# /packer/integrations/hashicorp/amazon#anchor-tag
33+
local find="\(\/packer\/plugins\/$urlSegment\/$urlSegment$urlAnchor?\)"
34+
local replace="\(\/packer\/integrations\/$organization\/\2\3\)"
35+
result="$(echo "$result" | sed -E "s/$find/$replace/g")"
36+
37+
38+
# Rewrite Component links to the Integration component page
39+
#
40+
# (\1) (\2) (\3) (\4)
41+
# /packer/plugins/datasources/amazon/parameterstore#anchor-tag -->
42+
# /packer/integrations/{organization}/amazon/latest/components/datasources/parameterstore
43+
local find="\(\/packer\/plugins\/$urlSegment\/$urlSegment\/$urlSegment$urlAnchor?\)"
44+
local replace="\(\/packer\/integrations\/$organization\/\2\/latest\/components\/\1\/\3\4\)"
45+
result="$(echo "$result" | sed -E "s/$find/$replace/g")"
46+
47+
# Rewrite the Component URL segment from the Packer Plugin format
48+
# to the Integrations format
49+
result="$(echo "$result" \
50+
| sed "s/\/datasources\//\/data-source\//g" \
51+
| sed "s/\/builders\//\/builder\//g" \
52+
| sed "s/\/post-processors\//\/post-processor\//g" \
53+
| sed "s/\/provisioners\//\/provisioner\//g" \
54+
)"
55+
56+
echo "$result"
57+
}
58+
59+
# $1: Docs Dir
60+
# $2: Web Docs Dir
61+
# $3: Component File
62+
# $4: The org of the integration
63+
processComponentFile() {
64+
local docsDir="$1"
65+
local webDocsDir="$2"
66+
local componentFile="$3"
67+
68+
local escapedDocsDir="$(echo "$docsDir" | sed 's/\//\\\//g' | sed 's/\./\\\./g')"
69+
local componentTypeAndSlug="$(echo "$componentFile" | sed "s/$escapedDocsDir\///g" | sed 's/\.mdx//g')"
70+
71+
# Parse out the Component Slug & Component Type
72+
local componentSlug="$(echo "$componentTypeAndSlug" | cut -d'/' -f 2)"
73+
local componentType="$(componentTypeFromFolderName "$(echo "$componentTypeAndSlug" | cut -d'/' -f 1)")"
74+
if [[ "$componentType" = "" ]]; then
75+
echo "Failed to process '$componentFile', unexpected folder name."
76+
echo "Documentation for components must be stored in one of:"
77+
echo "builders, provisioners, post-processors, datasources"
78+
exit 1
79+
fi
80+
81+
82+
# Calculate the location of where this file will ultimately go
83+
local webDocsFolder="$webDocsDir/components/$componentType/$componentSlug"
84+
mkdir -p "$webDocsFolder"
85+
local webDocsFile="$webDocsFolder/README.md"
86+
local webDocsFileTmp="$webDocsFolder/README.md.tmp"
87+
88+
# Copy over the file to its webDocsFile location
89+
cp "$componentFile" "$webDocsFile"
90+
91+
# Remove the Header
92+
local lastMetadataLine="$(grep -n -m 2 '^\-\-\-' "$componentFile" | tail -n1 | cut -d':' -f1)"
93+
cat "$webDocsFile" | tail -n +"$(($lastMetadataLine+2))" > "$webDocsFileTmp"
94+
mv "$webDocsFileTmp" "$webDocsFile"
95+
96+
# Remove the top H1, as this will be added automatically on the web
97+
cat "$webDocsFile" | tail -n +3 > "$webDocsFileTmp"
98+
mv "$webDocsFileTmp" "$webDocsFile"
99+
100+
# Rewrite Links
101+
rewriteLinks "$(cat "$webDocsFile")" "$4" > "$webDocsFileTmp"
102+
mv "$webDocsFileTmp" "$webDocsFile"
103+
}
104+
105+
# Compiles the Packer SDC compiled docs folder down
106+
# to a integrations-compliant folder (web docs)
107+
#
108+
# $1: The directory of the plugin
109+
# $2: The directory of the SDC compiled docs files
110+
# $3: The output directory to place the web-docs files
111+
# $4: The org of the integration
112+
compileWebDocs() {
113+
local docsDir="$1/$2"
114+
local webDocsDir="$1/$3"
115+
116+
echo "Compiling MDX docs in '$2' to Markdown in '$3'..."
117+
# Create the web-docs directory if it hasn't already been created
118+
mkdir -p "$webDocsDir"
119+
120+
# Copy the README over
121+
cp "$docsDir/README.md" "$webDocsDir/README.md"
122+
123+
# Process all MDX component files (exclude index files, which are unsupported)
124+
for file in $(find "$docsDir" | grep "$docsDir/.*/.*\.mdx" | grep --invert-match "index.mdx"); do
125+
processComponentFile "$docsDir" "$webDocsDir" "$file" "$4"
126+
done
127+
}
128+
129+
compileWebDocs "$1" "$2" "$3" "$4"

0 commit comments

Comments
 (0)