Skip to content

Commit

Permalink
Merge pull request #829 from aziontech/dev
Browse files Browse the repository at this point in the history
Deploy to Production - 2024/05/31
  • Loading branch information
PatrickMenoti committed May 31, 2024
2 parents 1e38da0 + 97a2c36 commit 57fd274
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 43 deletions.
11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,14 @@ Enhancement suggestions are tracked as [GitHub issues](https://github.com/aziont
2. Clone your forked repo
3. Commit the changes you’ve developed on your fork
4. If everything looks good, create a pull request on the base repo with your changes.

#### We use semantic commit

To ensure that all commits follow the semantic commit pattern, we need to configure Git to use our custom hooks.
Run the following command to configure the hooks directory:
```bash
git config core.hooksPath hooks

# To check that the hooks configuration is correct, you can do an empty commit
# git commit --allow-empty -m "Hook commit-msg"
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ require (
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/segmentio/backo-go v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDjyw0ULyrTYWeN0UNCCkmCWfjPnIA2W6oviI=
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
Expand Down
26 changes: 26 additions & 0 deletions hooks/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

# Path to commit message file
COMMIT_MSG_FILE="$1"

# Check if the commit message file exists
if [ -f "$COMMIT_MSG_FILE" ]; then
commit_msg=$(cat "$COMMIT_MSG_FILE")
# echo "Commit message received: $commit_msg" # Debug

# check the commit message is in the standard conventional commits https://www.conventionalcommits.org/en/v1.0.0/
pattern="^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .+$"
if [[ ! $commit_msg =~ $pattern ]]; then
echo "Error: The commit message does not follow the semantic commit pattern."
echo "The pattern should be: <type>(optional scope): description"
echo "Valid example: 'feat: Add new functionality'"
echo "Invalid example: 'Add new feature'"
exit 1
else
exit 0
fi
else
echo "Commit message file not found: $COMMIT_MSG_FILE"
exit 1
fi

12 changes: 6 additions & 6 deletions messages/edge_storage/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ const (
EXAMPLE_CREATE = "$ azion create edge-storage\n$ azion create edge-storage --help"
EXAMPLE_CREATE_BUCKET = "$ azion create edge-storage bucket --name 'zorosola' --edge-access 'read_only'\n$ azion create edge-storage bucket --help"
EXAMPLE_UPDATE_BUCKET = "$ azion update edge-storage bucket --name 'zorosola' --edge-access 'read_only'\n$ azion update edge-storage bucket --help"
EXAMPLE_LIST = "$ azion list edge-storage\n$ azion list edge-storage --help"
EXAMPLE_LIST_BUCKET = "$ azion list edge-storage bucket\n$ azion list edge-storage bucket --page 1 --page-size 3\n$ azion list edge-storage bucket --help"
EXAMPLE_LIST_OBJECT = "$ azion list edge-storage object --bucket-name 'balde'\n$ azion list edge-storage object --page 1 --page-size 3 --details\n$ azion list edge-storage object --help"
EXAMPLE_LIST = "$ azion list edge-storage bucket\n$ azion list edge-storage object --bucket-name nomedobalde\n$ azion list edge-storage --help"
EXAMPLE_LIST_BUCKET = "$ azion list edge-storage bucket\n$ azion list edge-storage bucket --page 1 --page-size 3"
EXAMPLE_LIST_OBJECT = "$ azion list edge-storage object --bucket-name 'balde'\n$ azion list edge-storage object --page-size 3 --details\n$ azion list edge-storage object --help"
EXAMPLE_DELETE = "$ azion delete edge-storage\n$ azion delete edge-storage --help"
EXAMPLE_DELETE_BUCKET = "$ azion delete edge-storage bucket --name 'bucket-name'\n$ azion delete edge-storage bucket --help"
EXAMPLE_UPDATE = "$ azion update edge-storage\n$ azion update edge-storage --help"
EXAMPLE_CREATE_OBJECTS = "$ azion create edge-storage object --bucket-name 'mynewbucket' --object-key 'path/to/my/remote/file.txt' --source './local/file.txt'"
EXAMPLE_UPDATE_OBJECT = "$ azion update edge-storage object --bucket-name 'mybalde' --object-key 'path/index.html' --source './index.html'\n$ azion update edge-storage object --help"
EXAMPLE_DESCRIBE = "$ azion describe edge-storage\n$ azion describe edge-storage --help"
EXAMPLE_DESCRIBE_OBJECT = "$ azion describe edge-storage objects --help\n$ azion describe edge-storage objects --bucket-name 'asdf' --object-key 'test.json'\n$ azion describe edge-storage objects --bucket-name 'asdf' --object-key 'test.json' --format json\n$ azion describe edge-storage objects --bucket-name 'asdf' --object-key 'test.json' --out './tmp/test.json'"
EXAMPLE_DESCRIBE_OBJECT = "$ azion describe edge-storage object --help\n$ azion describe edge-storage object --bucket-name 'asdf' --object-key 'test.json'\n$ azion describe edge-storage objects --bucket-name 'asdf' --object-key 'test.json' --format json\n$ azion describe edge-storage objects --bucket-name 'asdf' --object-key 'test.json' --out './tmp/test.json'"
EXAMPLE_DELETE_OBJECTS = "$ azion delete edge-storage object --bucket-name 'bucket-name'\n$ azion delete edge-storage object --help"

FLAG_HELP = "Displays more information about the edge-storage command"
Expand All @@ -42,7 +42,7 @@ const (
FLAG_HELP_LIST_OBJECT = "Displays more information about the list edge-storege object command"
FLAG_HELP_DELETE_BUCKET = "Displays more information about the delete edge-storege bucket command"
FLAG_NAME_BUCKET = "The name of the Edge Storage bucket"
FLAG_FORCE = "Argument to force bucket delete"
FLAG_FORCE = "Indicates that the bucket should be emptied and deleted"
FLAG_EDGE_ACCESS_CREATE_BUCKET = "Indicates the type of permission for actions within the bucket. Possible values: read_only, read_write or restricted"
FLAG_SOURCE_UPDATE_OBJECT = "file path field to update the edge storage bucket object"
FLAG_FILE_JSON_CREATE_BUCKET = "Path to a JSON file containing the attributes of the bucket that will be created; you can use - for reading from stdin"
Expand All @@ -63,7 +63,7 @@ const (
ASK_OBJECT_KEY_CREATE_OBJECT = "Enter your object name: "
ASK_SOURCE_CREATE_OBJECT = "Enter your path source: "
ASK_OBJECT_DELETE_OBJECT = "Enter the name of the Object you wish to delete: "
ASK_NOT_EMPTY_BUCKET = "Your bucket can't be deleted because it's not empty. Do you want to empty it? (y/N)"
ASK_NOT_EMPTY_BUCKET = "Your bucket can't be deleted because it's not empty. Do you want to empty it? (y/N)"

OUTPUT_CREATE_BUCKET = "Bucket created successfully"
OUTPUT_DELETE_BUCKET = "Bucket %s was deleted successfully"
Expand Down
2 changes: 2 additions & 0 deletions messages/init/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ var (
ErrorDeps = errors.New("Failed to install project dependencies")
ErrorWorkingDir = errors.New("Failed to change current working directory")
ErrorModeNotFound = errors.New("No mode was found for the selected template. For more information, run the command again using the '--debug' flag. If the problem persists, contact Azion’s support")
ErrorReadingGitignore = errors.New("Failed to read your .gitignore file")
ErrorWritingGitignore = errors.New("Failed to write to your .gitignore file")
)
6 changes: 6 additions & 0 deletions messages/init/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ var (
InitDevCommand = "If you want to start a local development server later, run 'azion dev'\n"
InitDeployCommand = "If you want to deploy your application later, run 'azion deploy'\n"
InitInstallDeps = "Installing application dependencies"
AskDeploy = "Do you want to deploy your project? (y/N)"
AskInstallDepsDev = "Do you want to install project dependencies? This may be required to start local development server (y/N)"
AskInstallDepsDeploy = "Do you want to install project dependencies? This may be required to deploy your project (y/N)"
AskLocalDev = "Do you want to start a local development server? (y/N)"
AskGitignore = "Azion CLI creates some files during the build process for internal use. Would you like to add these to your .gitignore file? (Y/n)"
WrittenGitignore = "Sucessfully written to your .gitignore file\n"
)
2 changes: 2 additions & 0 deletions messages/link/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ var (
ErrorFailedCreatingWorkerDirectory = errors.New("Failed to create the worker directory. The worker's parent directory is read-only and/or isn't accessible. Change the permissions of the parent directory to read and write and/or give access to it")
ErrorFailedCreatingAzionDirectory = errors.New("Failed to create the azion directory. The public's parent directory is read-only and/or isn't accessible. Change the permissions of the parent directory to read and write and/or give access to it")
ErrorDeps = errors.New("Failed to install project dependencies")
ErrorReadingGitignore = errors.New("Failed to read your .gitignore file")
ErrorWritingGitignore = errors.New("Failed to write to your .gitignore file")
)
6 changes: 6 additions & 0 deletions messages/link/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ var (
LinkDevCommand = "If you want to start a local development server later, run 'azion dev'\n"
LinkDeployCommand = "If you want to deploy your application later, run 'azion deploy'\n"
LinkFlagAuto = "If sent, the entire flow of the command will be run without interruptions"
AskDeploy = "Do you want to deploy your project? (y/N)"
AskInstallDepsDev = "Do you want to install project dependencies? This may be required to start local development server (y/N)"
AskInstallDepsDeploy = "Do you want to install project dependencies? This may be required to deploy your project (y/N)"
AskLocalDev = "Do you want to start a local development server? (y/N)"
AskGitignore = "Azion CLI creates some files during the build process for internal use. Would you like to add these to your .gitignore file? (Y/n)"
WrittenGitignore = "Sucessfully written to your .gitignore file\n"
)

const (
Expand Down
27 changes: 15 additions & 12 deletions pkg/cmd/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/aziontech/azion-cli/pkg/cmd/deploy"
"github.com/aziontech/azion-cli/pkg/cmd/dev"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/github"
"github.com/aziontech/azion-cli/pkg/iostreams"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/node"
Expand Down Expand Up @@ -153,12 +154,20 @@ func (cmd *initCmd) Run(_ *cobra.Command, _ []string) error {
return msg.ErrorWorkingDir
}

shouldDev := cmd.shouldDevDeploy("Do you want to start a local development server? (y/N)", cmd.globalFlagAll, false)
gitignore, err := github.CheckGitignore(cmd.pathWorkingDir)
if err != nil {
return msg.ErrorReadingGitignore
}

if shouldDev {
shouldDeps := cmd.shouldDevDeploy("Do you want to install project dependencies? This may be required to start local development server (y/N)", cmd.globalFlagAll, false)
if !gitignore && (cmd.f.GlobalFlagAll || utils.Confirm(cmd.f.GlobalFlagAll, msg.AskGitignore, true)) {
if err := github.WriteGitignore(cmd.pathWorkingDir); err != nil {
return msg.ErrorWritingGitignore
}
logger.FInfo(cmd.f.IOStreams.Out, msg.WrittenGitignore)
}

if shouldDeps {
if cmd.shouldDevDeploy(msg.AskLocalDev, cmd.globalFlagAll, false) {
if cmd.shouldDevDeploy(msg.AskInstallDepsDev, cmd.globalFlagAll, false) {
answer, err := utils.GetPackageManager()
if err != nil {
return err
Expand All @@ -181,14 +190,8 @@ func (cmd *initCmd) Run(_ *cobra.Command, _ []string) error {
logger.FInfo(cmd.io.Out, msg.InitDevCommand)
}

shouldDeploy := cmd.shouldDevDeploy("Do you want to deploy your project? (y/N)", cmd.globalFlagAll, false)
if shouldDeploy {
shouldDeps := cmd.shouldDevDeploy("Do you want to install project dependencies? This may be required to deploy your project (y/N)", cmd.globalFlagAll, false)
if err != err {
return err
}

if shouldDeps {
if cmd.shouldDevDeploy(msg.AskDeploy, cmd.globalFlagAll, false) {
if cmd.shouldDevDeploy(msg.AskInstallDepsDeploy, cmd.globalFlagAll, false) {
answer, err := utils.GetPackageManager()
if err != nil {
return err
Expand Down
27 changes: 16 additions & 11 deletions pkg/cmd/link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,22 @@ func (cmd *LinkCmd) run(info *LinkInfo) error {

logger.FInfo(cmd.Io.Out, msg.WebAppLinkCmdSuccess)

if !info.Auto {
shouldDev := cmd.ShouldDevDeploy(info, "Do you want to start a local development server? (y/N)", false)
//asks if user wants to add files to .gitignore
gitignore, err := github.CheckGitignore(info.PathWorkingDir)
if err != nil {
return msg.ErrorReadingGitignore
}

if shouldDev {
shouldDeps := cmd.ShouldDevDeploy(info, "Do you want to install project dependencies? This may be required to start local development server (y/N)", false)
if !gitignore && (info.Auto || info.GlobalFlagAll || utils.Confirm(info.GlobalFlagAll, msg.AskGitignore, true)) {
if err := github.WriteGitignore(info.PathWorkingDir); err != nil {
return msg.ErrorWritingGitignore
}
logger.FInfo(cmd.Io.Out, msg.WrittenGitignore)
}

if shouldDeps {
if !info.Auto {
if cmd.ShouldDevDeploy(info, msg.AskLocalDev, false) {
if cmd.ShouldDevDeploy(info, msg.AskInstallDepsDev, false) {
answer, err := utils.GetPackageManager()
if err != nil {
return err
Expand All @@ -222,12 +231,8 @@ func (cmd *LinkCmd) run(info *LinkInfo) error {
logger.FInfo(cmd.Io.Out, msg.LinkDevCommand)
}

shouldDeploy := cmd.ShouldDevDeploy(info, "Do you want to deploy your project? (y/N)", false)

if shouldDeploy {
shouldYarn := cmd.ShouldDevDeploy(info, "Do you want to install project dependencies? This may be required to deploy the project (y/N)", false)

if shouldYarn {
if cmd.ShouldDevDeploy(info, msg.AskDeploy, false) {
if cmd.ShouldDevDeploy(info, msg.AskInstallDepsDeploy, false) {
answer, err := utils.GetPackageManager()
if err != nil {
return err
Expand Down
10 changes: 6 additions & 4 deletions pkg/contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,12 @@ type CacheSetting struct {
}

type Origin struct {
Name string `json:"name"`
OriginType string `json:"origin_type,omitempty"`
Bucket string `json:"bucket,omitempty"`
Prefix string `json:"prefix,omitempty"`
Name string `json:"name"`
OriginType string `json:"origin_type,omitempty"`
Bucket string `json:"bucket,omitempty"`
Prefix string `json:"prefix,omitempty"`
Addresses []sdk.CreateOriginsRequestAddresses `json:"addresses,omitempty"`
HostHeader string `json:"host_header,omitempty"`
}

type RuleEngine struct {
Expand Down
60 changes: 60 additions & 0 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package github

import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/utils"
"github.com/go-git/go-git/v5"
"go.uber.org/zap"

gitignore "github.com/sabhiram/go-gitignore"
)

type Release struct {
Expand Down Expand Up @@ -71,3 +76,58 @@ func GetNameRepo(url string) string {
repoPart = strings.TrimSuffix(repoPart, ".git")
return repoPart
}

func CheckGitignore(path string) (bool, error) {
logger.Debug("Checking .gitignore file for existence of Vulcan files")
path = filepath.Join(path, ".gitignore")

object, err := gitignore.CompileIgnoreFile(path)
if err != nil {
// if the error is "no such file or directory" we can return false and nil for error, because the code that called this func will create
// the .gitignore file
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, err
}

if !object.MatchesPath(".edge/") || !object.MatchesPath(".vulcan") {
return false, nil
}

return true, nil
}

func WriteGitignore(path string) error {
logger.Debug("Writing .gitignore file")
path = filepath.Join(path, ".gitignore")

// Lines to add to .gitignore
linesToAdd := []string{"#Paths added by Azion CLI", ".edge/", ".vulcan"}

// Open the file in append mode, create if not exists
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
logger.Error("Error opening file", zap.Error(err))
return err
}
defer file.Close()

writer := bufio.NewWriter(file)

for _, line := range linesToAdd {
_, err := writer.WriteString(line + "\n")
if err != nil {
logger.Error("Error writing to .gitignore file", zap.Error(err))
return err
}
}

// Ensure all data is written to the file
if err := writer.Flush(); err != nil {
logger.Error("Error flushing writer", zap.Error(err))
return err
}

return nil
}
1 change: 1 addition & 0 deletions pkg/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func (man *ManifestInterpreter) CreateResources(conf *contracts.AzionApplication
return nil
}

// this is called to delete resources no longer present in manifest.json
func deleteResources(ctx context.Context, f *cmdutil.Factory, conf *contracts.AzionApplicationOptions) error {
client := apiEdgeApplications.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))
clientCache := apiCache.NewClient(f.HttpClient, f.Config.GetString("api_url"), f.Config.GetString("token"))
Expand Down
44 changes: 34 additions & 10 deletions pkg/manifest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,25 @@ func makeRuleRequestCreate(rule contracts.RuleEngine, conf *contracts.AzionAppli
func makeOriginCreateRequest(origin contracts.Origin, conf *contracts.AzionApplicationOptions) *apiOrigin.CreateRequest {
request := &apiOrigin.CreateRequest{}

request.SetBucket(conf.Bucket)
switch origin.OriginType {
case "single_origin":
if origin.HostHeader != "" {
request.SetHostHeader(origin.HostHeader)
}
if len(origin.Addresses) > 0 {
request.SetAddresses(origin.Addresses)
}

case "object_storage":
request.SetBucket(conf.Bucket)

if origin.Prefix != "" {
request.SetPrefix(origin.Prefix)
} else {
request.SetPrefix(conf.Prefix)
if origin.Prefix != "" {
request.SetPrefix(origin.Prefix)
} else {
request.SetPrefix(conf.Prefix)
}
}

if origin.OriginType != "" {
request.SetOriginType(origin.OriginType)
}
Expand All @@ -340,13 +352,25 @@ func makeOriginCreateRequest(origin contracts.Origin, conf *contracts.AzionAppli
func makeOriginUpdateRequest(origin contracts.Origin, conf *contracts.AzionApplicationOptions) *apiOrigin.UpdateRequest {
request := &apiOrigin.UpdateRequest{}

request.SetBucket(conf.Bucket)
switch origin.OriginType {
case "single_origin":
if origin.HostHeader != "" {
request.SetHostHeader(origin.HostHeader)
}
if len(origin.Addresses) > 0 {
request.SetAddresses(origin.Addresses)
}

case "object_storage":
request.SetBucket(conf.Bucket)

if origin.Prefix != "" {
request.SetPrefix(origin.Prefix)
} else {
request.SetPrefix(conf.Prefix)
if origin.Prefix != "" {
request.SetPrefix(origin.Prefix)
} else {
request.SetPrefix(conf.Prefix)
}
}

if origin.OriginType != "" {
request.SetOriginType(origin.OriginType)
}
Expand Down

0 comments on commit 57fd274

Please sign in to comment.