## Description
<!-- **Please link some issue here describing what you are trying to achieve.**
In case there is no issue present for your PR, please consider creating one.
At least please give us some description what you are trying to achieve and why your change is needed. -->
relates to #1234
## Checklist
- [ ] Issue was linked above
- [ ] Code format was applied: `make fmt`
- [ ] Examples were added / adjusted (see `examples/` directory)
- [x] Docs are up-to-date: `make generate-docs` (will be checked by CI)
- [ ] Unit tests got implemented or updated
- [ ] Acceptance tests got implemented or updated (see e.g. [here](f5f99d1709/stackit/internal/services/dns/dns_acc_test.go))
- [x] Unit tests are passing: `make test` (will be checked by CI)
- [x] No linter issues: `make lint` (will be checked by CI)
Reviewed-on: #58
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Co-committed-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
169 lines
4.8 KiB
Go
169 lines
4.8 KiB
Go
package publish
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/url"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
type Architecture struct {
|
|
Protocols []string `json:"protocols"`
|
|
OS string `json:"os"`
|
|
Arch string `json:"arch"`
|
|
FileName string `json:"filename"`
|
|
DownloadUrl string `json:"download_url"`
|
|
ShaSumsUrl string `json:"shasums_url"`
|
|
ShaSumsSignatureUrl string `json:"shasums_signature_url"`
|
|
ShaSum string `json:"shasum"`
|
|
SigningKeys SigningKey `json:"signing_keys"`
|
|
}
|
|
|
|
type SigningKey struct {
|
|
GpgPublicKeys []GpgPublicKey `json:"gpg_public_keys"`
|
|
}
|
|
|
|
type GpgPublicKey struct {
|
|
KeyId string `json:"key_id"`
|
|
AsciiArmor string `json:"ascii_armor"`
|
|
TrustSignature string `json:"trust_signature"`
|
|
Source string `json:"source"`
|
|
SourceUrl string `json:"source_url"`
|
|
}
|
|
|
|
func (p *Provider) CreateArchitectureFiles() error {
|
|
// var namespace, provider, distPath, repoName, version, gpgFingerprint, gpgPubKeyFile, domain string
|
|
|
|
log.Println("* Creating architecture files in target directories")
|
|
|
|
// filename = terraform-provider-[provider]_0.0.1_darwin_amd64.zip - provider_name + version + target + architecture + .zip
|
|
// prefix := fmt.Sprintf("v1/providers/%s/%s/%s/", namespace, provider, version)
|
|
prefix := path.Join("v1", "providers", p.Namespace, p.Provider, p.Version)
|
|
|
|
// pathPrefix := fmt.Sprintf("release/%s", prefix)
|
|
pathPrefix := path.Join("release", prefix)
|
|
|
|
// urlPrefix := fmt.Sprintf("https://%s/%s", domain, prefix)
|
|
urlPrefix, err := url.JoinPath("https://", p.Domain, prefix)
|
|
if err != nil {
|
|
return fmt.Errorf("error creating base url: %w", err)
|
|
}
|
|
|
|
// download url = https://example.com/v1/providers/namespace/provider/0.0.1/download/terraform-provider_0.0.1_darwin_amd64.zip
|
|
downloadUrlPrefix, err := url.JoinPath(urlPrefix, "download")
|
|
if err != nil {
|
|
return fmt.Errorf("error crearting download url: %w", err)
|
|
}
|
|
downloadPathPrefix := path.Join(pathPrefix, "download")
|
|
|
|
// shasums url = https://example.com/v1/providers/namespace/provider/0.0.1/terraform-provider_0.0.1_SHA256SUMS
|
|
shasumsUrl, err := url.JoinPath(urlPrefix, fmt.Sprintf("%s_%s_SHA256SUMS", p.RepoName, p.Version))
|
|
if err != nil {
|
|
return fmt.Errorf("error creating shasums url: %w", err)
|
|
}
|
|
// shasums_signature_url = https://example.com/v1/providers/namespace/provider/0.0.1/terraform-provider_0.0.1_SHA256SUMS.sig
|
|
shasumsSigUrl := shasumsUrl + ".sig"
|
|
|
|
gpgAsciiPub, err := p.ReadGpgFile()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
shaSums, err := p.GetShaSums()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, sum := range shaSums {
|
|
downloadUrl, err := url.JoinPath(downloadUrlPrefix, sum.Path)
|
|
if err != nil {
|
|
return fmt.Errorf("error creating url: %w", err)
|
|
}
|
|
|
|
// get os and arch from filename
|
|
removeFileExtension := strings.Split(sum.Path, ".zip")
|
|
fileNameSplit := strings.Split(removeFileExtension[0], "_")
|
|
|
|
// Get build target and architecture from the zip file name
|
|
target := fileNameSplit[2]
|
|
arch := fileNameSplit[3]
|
|
|
|
// build filepath
|
|
archFileName := path.Join(downloadPathPrefix, target, arch)
|
|
|
|
a := Architecture{
|
|
Protocols: []string{"5.1", "6.0"},
|
|
OS: target,
|
|
Arch: arch,
|
|
FileName: sum.Path,
|
|
DownloadUrl: downloadUrl,
|
|
ShaSumsUrl: shasumsUrl,
|
|
ShaSumsSignatureUrl: shasumsSigUrl,
|
|
ShaSum: sum.Sum,
|
|
SigningKeys: SigningKey{},
|
|
}
|
|
|
|
a.SigningKeys = SigningKey{
|
|
GpgPublicKeys: []GpgPublicKey{
|
|
{
|
|
KeyId: p.GpgFingerprint,
|
|
AsciiArmor: gpgAsciiPub,
|
|
TrustSignature: "",
|
|
Source: "",
|
|
SourceUrl: "",
|
|
},
|
|
},
|
|
}
|
|
// var architectureTemplate = []byte(fmt.Sprintf(`
|
|
//{
|
|
// "protocols": [
|
|
// "4.0",
|
|
// "5.1",
|
|
// "6.0"
|
|
// ],
|
|
// "os": "%s",
|
|
// "arch": "%s",
|
|
// "filename": "%s",
|
|
// "download_url": "%s",
|
|
// "shasums_url": "%s",
|
|
// "shasums_signature_url": "%s",
|
|
// "shasum": "%s",
|
|
// "signing_keys": {
|
|
// "gpg_public_keys": [
|
|
// {
|
|
// "key_id": "%s",
|
|
// "ascii_armor": "%s",
|
|
// "trust_signature": "",
|
|
// "source": "",
|
|
// "source_url": ""
|
|
// }
|
|
// ]
|
|
// }
|
|
//}
|
|
// `, target, arch, fileName, downloadUrl, shasumsUrl, shasumsSigUrl, shasum, gpgFingerprint, gpgAsciiPub))
|
|
|
|
log.Printf(" - Arch file: %s", archFileName)
|
|
|
|
err = WriteArchitectureFile(archFileName, a)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func WriteArchitectureFile(filePath string, arch Architecture) error {
|
|
jsonString, err := json.Marshal(arch)
|
|
if err != nil {
|
|
return fmt.Errorf("error encoding data: %w", err)
|
|
}
|
|
|
|
err = os.WriteFile(filePath, jsonString, os.ModePerm)
|
|
if err != nil {
|
|
return fmt.Errorf("error writing data: %w", err)
|
|
}
|
|
return nil
|
|
}
|