Skip to content

Commit

Permalink
Feature/haris #86c1hjnjz license server support (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
charkops authored Jan 23, 2025
1 parent d6ca859 commit 815d877
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
1 change: 1 addition & 0 deletions commands/image_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ func mergeImageProperties(layers []layerProps) (*schema.ImageProperties, error)
func resolveDeploymentTemplateProperties(properties *schema.ImageProperties, deploymentTemplateJSONRaw []byte) []byte {
return schema.ReplacePlaceholders(deploymentTemplateJSONRaw, map[string]string{
schema.BuiltInSessionHostProxyWhitelistPlaceholder: properties.WhitelistedHosts.KeyString(),
schema.BuiltInInternalServicesPlaceholder: properties.InternalServices.String(),
}, schema.BuiltInPlaceholder)
}

Expand Down
53 changes: 49 additions & 4 deletions commands/package_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"io/fs"

"io"
"net/url"
"os"
"path"
Expand Down Expand Up @@ -90,6 +91,13 @@ var PackageDeployCommand = &cli.Command{
Aliases: []string{"i"},
Value: true,
},
&cli.StringFlag{
Name: "deployment-template-output",
Usage: "Path to save the resolved deployment template",
Required: true,
Aliases: []string{"dto"},
TakesFile: true,
},
},
Action: func(c *cli.Context) error {
imageTemplateName := c.Path("name")
Expand All @@ -103,6 +111,7 @@ var PackageDeployCommand = &cli.Command{
timeoutFlag := c.Duration("timeout")
envFilePaths := c.StringSlice("env")
resolveInteractively := c.Bool("resolve-interactively")
deploymentTemplateOutput := c.Path("deployment-template-output")

ctx := context.Background()
if timeoutFlag > 0 {
Expand All @@ -118,7 +127,7 @@ var PackageDeployCommand = &cli.Command{
fullPackagePath := filepath.Join(cwd, packagePath)

packageFs := os.DirFS(packagePath)
imageProperties, resourcesArchiveChecksum, err := scanPackagePath(packageFs, envFilePaths, resolveInteractively)
imageProperties, resourcesArchiveChecksum, err := scanPackagePath(packageFs, deploymentTemplateOutput, envFilePaths, resolveInteractively)
if err != nil {
return errors.Wrapf(err, "failed to scan image package directory %s", fullPackagePath)
}
Expand Down Expand Up @@ -227,7 +236,8 @@ func parseResourcesURI(resourcesURI string) (*storageAccountBlob, error) {
}, nil
}

func scanPackagePath(packageFs fs.FS, envFiles []string, resolveInteractively bool) (imageProperties *schema.ImageProperties, archiveSha256 []byte, err error) {
func scanPackagePath(packageFs fs.FS, deploymentTemplateOutputPath string, envFiles []string, resolveInteractively bool) (imageProperties *schema.ImageProperties, archiveSha256 []byte, err error) {
// resolve parameters in the properties file
propertiesFile, err := packageFs.Open(imagePropertiesFileWithExtension)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to open properties file")
Expand All @@ -239,10 +249,12 @@ func scanPackagePath(packageFs fs.FS, envFiles []string, resolveInteractively bo
return nil, nil, errors.Wrap(err, "failed to read properties file")
}

var resolvedParams map[string]string
paramsToResolve := schema.FindPlaceholdersInJSON(propertiesFileContent, schema.ParameterPlaceholder)
if len(paramsToResolve) > 0 {
fmt.Printf("Resolving %d package parameters\n", len(paramsToResolve))
resolvedParams, err := resolveParameters(envFiles, paramsToResolve, resolveInteractively)
var err error
resolvedParams, err = resolveParameters(envFiles, paramsToResolve, resolveInteractively)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to resolve parameters")
}
Expand All @@ -259,6 +271,39 @@ func scanPackagePath(packageFs fs.FS, envFiles []string, resolveInteractively bo
return nil, nil, errors.Wrapf(err, "failed to parse image properties json:\n%s", resolvedPropertiesFileContent)
}

// resolve parameters in deployment template
deploymentTemplateFile, err := packageFs.Open(deploymentTemplateFileWithExtension)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to open deployment template file")
}
defer deploymentTemplateFile.Close()

originalDeploymentTemplateFileContents, err := io.ReadAll(deploymentTemplateFile)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to read deployment template file contents")
}

// first do a pass with previously resolved parameters from the properties file content
// so we don't resolve them again
resolvedDeploymentTemplateFileContents := schema.ReplacePlaceholders(originalDeploymentTemplateFileContents, resolvedParams, schema.ParameterPlaceholder)

// search for unresolved parameters
paramsToResolve = schema.FindPlaceholdersInJSON(resolvedDeploymentTemplateFileContents, schema.ParameterPlaceholder)
if len(paramsToResolve) > 0 {
fmt.Printf("Resolving %d deployment template parameters\n", len(paramsToResolve))
resolvedParams, err := resolveParameters(envFiles, paramsToResolve, resolveInteractively)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to resolve parameters")
}

resolvedDeploymentTemplateFileContents = schema.ReplacePlaceholders(resolvedDeploymentTemplateFileContents, resolvedParams, schema.ParameterPlaceholder)
}

// Write the deployment template output file
if err := os.WriteFile(deploymentTemplateOutputPath, resolvedDeploymentTemplateFileContents, 0644); err != nil {
return nil, nil, errors.Wrap(err, "failed to write deployment template file to disk")
}

hash := sha256.New()
resourcesFile, err := packageFs.Open(resourcesArchiveName)
if err != nil {
Expand Down
36 changes: 33 additions & 3 deletions schema/image_props.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ package schema

import (
"encoding/json"
"fmt"
"regexp"
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/virtualmachineimagebuilder/armvirtualmachineimagebuilder"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/schoolyear/avd-cli/lib"
"regexp"
"strings"
)

type ImageProperties struct {
PlaceholderProperties PlaceholderProperties `json:"placeholderProperties,omitempty"`
WhitelistedHosts WhitelistedHosts `json:"whitelistedHosts"`
InternalServices InternalServices `json:"internalServices,omitempty"`
ImageTemplate lib.JSON5Unsupported[armvirtualmachineimagebuilder.ImageTemplate] `json:"imageTemplate"`
}

func (i ImageProperties) Validate() error {
return validation.ValidateStruct(&i,
validation.Field(&i.PlaceholderProperties, validation.Length(0, 50)),
validation.Field(&i.WhitelistedHosts, validation.Length(0, 75)),
validation.Field(&i.InternalServices, validation.Length(0, 500)),
validation.Field(&i.ImageTemplate, validation.Required),
)
}
Expand All @@ -27,6 +31,23 @@ type PlaceholderProperties map[string]json.RawMessage

type WhitelistedHosts map[string]struct{}

type InternalServices map[string]string

func (i InternalServices) String() string {
// we don't want empty services to be marshalled as null
// but as empty JSON instead
if i == nil {
i = map[string]string{}
}

data, err := json.Marshal(i)
if err != nil {
fmt.Println("[Error]: failed to marshal internal services: " + err.Error())
}

return string(data)
}

func (w WhitelistedHosts) KeyString() string {
builder := strings.Builder{}
first := true
Expand All @@ -51,6 +72,7 @@ const (

const (
BuiltInSessionHostProxyWhitelistPlaceholder string = "sessionHostProxyWhitelist"
BuiltInInternalServicesPlaceholder string = "internalServiceLinkIdsJSON"
)

var placeholderRegex = regexp.MustCompile(`\[{3}([a-zA-Z0-9_]+):([a-zA-Z0-9_]+)]{3}`)
Expand All @@ -75,7 +97,15 @@ func ReplacePlaceholders(bytes []byte, mapping map[string]string, placeholderTyp
if string(match[1]) != string(placeholderType) {
return bytes
}
return []byte(mapping[string(match[2])])
// Do not replace values that don't exist in the mapping
mappedValue, ok := mapping[string(match[2])]
if !ok {
return bytes
}

// escape double quotes " since this will be embedded
// inside JSON
return []byte(strings.ReplaceAll(mappedValue, `"`, `\"`))
})
}

Expand Down

0 comments on commit 815d877

Please sign in to comment.