Skip to content

Commit

Permalink
Update rad bicep download to use official bicep compiler (#7663)
Browse files Browse the repository at this point in the history
# Description

Update rad bicep download to download the official bicep compiler

<img width="679" alt="Screenshot 2024-06-03 at 10 45 00 PM"
src="https://github.com/radius-project/radius/assets/42750942/70b36aef-701b-484e-aa25-d690146126ce">
[downloading the official release 0.27.1 instead of our release version
0.34]

## Type of change

<!--

Please select **one** of the following options that describes your
change and delete the others. Clearly identifying the type of change you
are making will help us review your PR faster, and is used in authoring
release notes.

If you are making a bug fix or functionality change to Radius and do not
have an associated issue link please create one now.

-->

- This pull request fixes a bug in Radius and has an approved issue
(issue link required).
- This pull request adds or changes features of Radius and has an
approved issue (issue link required).
- This pull request is a minor refactor, code cleanup, test improvement,
or other maintenance task and doesn't change the functionality of Radius
(issue link optional).

<!--

Please update the following to link the associated issue. This is
required for some kinds of changes (see above).

-->

Fixes: #issue_number

---------

Signed-off-by: sk593 <shruthikumar@microsoft.com>
  • Loading branch information
sk593 committed Jun 17, 2024
1 parent c8d3618 commit f65a916
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 51 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/bicep/bicep.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"os"
"time"

"github.com/radius-project/radius/pkg/cli/tools"
"github.com/radius-project/radius/pkg/cli/bicep/tools"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/bicep/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"regexp"
"strings"

"github.com/radius-project/radius/pkg/cli/tools"
"github.com/radius-project/radius/pkg/cli/bicep/tools"
)

// Official regex for semver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,29 @@ limitations under the License.
package tools

import (
"context"
"fmt"
"io"
"net/http"
"os"
"path"
"runtime"

"github.com/radius-project/radius/pkg/version"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content/file"
"oras.land/oras-go/v2/registry/remote"
"github.com/radius-project/radius/pkg/cli/clients"
)

const (
// binaryRepo is the name of the remote bicep binary repository
binaryRepo = "ghcr.io/radius-project/radius/bicep/rad-bicep/"
binaryRepo = "https://github.com/Azure/bicep/releases/latest/download/"
)

// validPlatforms is a map of valid platforms to download for. The key is the combination of GOOS and GOARCH.
var validPlatforms = map[string]string{
"windows-amd64": "windows-x64",
"linux-amd64": "linux-x64",
"linux-arm": "linux-arm",
"linux-arm64": "linux-arm64",
"darwin-amd64": "macos-x64",
"darwin-arm64": "macos-arm64",
"windows-amd64": "bicep-win-x64",
"windows-arm64": "bicep-win-arm64",
"linux-amd64": "bicep-linux-x64",
"linux-arm64": "bicep-linux-arm64",
"darwin-amd64": "bicep-osx-x64",
"darwin-arm64": "bicep-osx-arm64",
}

// GetLocalFilepath returns the local binary file path. It does not verify that the file
Expand Down Expand Up @@ -123,58 +121,49 @@ func GetValidPlatform(currentOS, currentArch string) (string, error) {
return platform, nil
}

// DownloadToFolder creates a folder and a file, uses the ORAS client to copy from the remote repository to the file,
// and makes the file executable by everyone. An error is returned if any of these steps fail.
// DownloadToFolder creates a folder and a file, downloads the bicep binary to the file,
func DownloadToFolder(filepath string) error {
// create folders
err := os.MkdirAll(path.Dir(filepath), os.ModePerm)
// Create the file
bicepBinary, err := os.Create(filepath)
if err != nil {
return fmt.Errorf("failed to create folder %s: %v", path.Dir(filepath), err)
}

// Create a file store
fs, err := file.New(path.Dir(filepath))
if err != nil {
return fmt.Errorf("failed to create file store %s: %v", filepath, err)
return err
}
defer fs.Close()
defer bicepBinary.Close()

ctx := context.Background()
platform, err := GetValidPlatform(runtime.GOOS, runtime.GOARCH)
// Get file binary
binary, err := GetValidPlatform(runtime.GOOS, runtime.GOARCH)
if err != nil {
return err
}

// Define remote repository
repo, err := remote.NewRepository(binaryRepo + platform)
// Get binaryName extension
binaryName, err := getFilename(binary)
if err != nil {
return err
}

// Copy the artifact from the registry into the file store
tag := version.Channel()
if version.IsEdgeChannel() {
tag = "latest"
}
_, err = oras.Copy(ctx, repo, tag, fs, tag, oras.DefaultCopyOptions)
if err != nil {
// Get the data
resp, err := http.Get(binaryRepo + binaryName)
if clients.Is404Error(err) {
return fmt.Errorf("unable to locate bicep binary resource %s: %v", binaryRepo+binaryName, err)
} else if err != nil {
return err
}
defer resp.Body.Close()

// Open the folder so we can mark it as executable
bicepBinary, err := os.Open(filepath)
// Write the body to file
_, err = io.Copy(bicepBinary, resp.Body)
if err != nil {
return fmt.Errorf("failed to open file %s: %v", filepath, err)
return err
}
defer bicepBinary.Close()

// get the filemode so we can mark it as executable
// Get the filemode so we can mark it as executable
file, err := bicepBinary.Stat()
if err != nil {
return fmt.Errorf("failed to read file attributes %s: %v", filepath, err)
}

// make file executable by everyone
// Make file executable by everyone
err = bicepBinary.Chmod(file.Mode() | 0111)
if err != nil {
return fmt.Errorf("failed to change permissions for %s: %v", filepath, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,38 @@ func TestGetValidPlatform(t *testing.T) {
{
currentOS: "darwin",
currentArch: "amd64",
out: "macos-x64",
out: "bicep-osx-x64",
},
{
currentOS: "darwin",
currentArch: "arm64",
out: "macos-arm64",
out: "bicep-osx-arm64",
},
{
currentOS: "windows",
currentArch: "amd64",
out: "windows-x64",
out: "bicep-win-x64",
},
{
currentOS: "windows",
currentArch: "arm64",
out: "",
err: errors.New("unsupported platform windows/arm64"),
out: "bicep-win-arm64",
},
{
currentOS: "linux",
currentArch: "amd64",
out: "linux-x64",
out: "bicep-linux-x64",
},
{
currentOS: "linux",
currentArch: "arm",
out: "linux-arm",
out: "",
err: errors.New("unsupported platform linux/arm"),
},
{
currentOS: "linux",
currentArch: "arm64",
out: "linux-arm64",
out: "bicep-linux-arm64",
},
}

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit f65a916

Please sign in to comment.