Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
feat: folder and Link resource handling
Browse files Browse the repository at this point in the history
  • Loading branch information
saitho committed May 28, 2023
1 parent 5bc75c2 commit 82cce26
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 151 deletions.
4 changes: 4 additions & 0 deletions commands/project/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ var DeployApplication = func() *cobra.Command {

err = taskRunner.RunTask(routines.PrepareProjectTask(projectDefinition))
if err != nil {
if system.Context.CurrentDeployment.Version > 0 {
_ = xfs.DeleteFolder("ssh://"+system.Context.CurrentDeployment.GetPath(), true)
}
return
}
err = taskRunner.RunTask(routines.CollectResourcesTask(projectDefinition))
if err != nil {
_ = xfs.DeleteFolder("ssh://"+system.Context.CurrentDeployment.GetPath(), true)
return
}

Expand Down
39 changes: 23 additions & 16 deletions commands/project/destroy.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package project

import (
"fmt"

xfs "github.com/saitho/golang-extended-fs/v2"
"github.com/spf13/cobra"

Expand All @@ -12,6 +10,12 @@ import (
"github.com/getstackhead/stackhead/system"
)

func reverse[S ~[]E, E any](s S) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}

// DestroyApplication is a command object for Cobra that provides the destroy command
var DestroyApplication = &cobra.Command{
Use: "destroy [path to project definition] [ipv4 address]",
Expand All @@ -26,22 +30,28 @@ var DestroyApplication = &cobra.Command{
}
commands.PrepareContext(args[1], system.ContextActionProjectDeploy, projectDefinition)

modules := system.Context.GetModulesInOrder()
for i, j := 0, len(modules)-1; i < j; i, j = i+1, j-1 { // reverse module list
modules[i], modules[j] = modules[j], modules[i]
latestDeployment, err := system.GetLatestDeployment(projectDefinition)
if err != nil {
panic("unable to load latest deployment" + err.Error())
}
system.Context.CurrentDeployment = *latestDeployment

// Init modules
modules := system.Context.GetModulesInOrder()
reverse(modules)

// Run modules destroy steps
for _, module := range modules {
moduleSettings := system.GetModuleSettings(module.GetConfig().Name)
module.Init(moduleSettings)
}
taskRunner := routines.TaskRunner{}

subTasks := []routines.Task{}
subTasks := []routines.Task{
// Remove resources from deployment
routines.RemoveResources(latestDeployment),
}

if hasProjectDir, _ := xfs.HasFolder("ssh://" + projectDefinition.GetDirectoryPath()); hasProjectDir {

// Run destroy scripts from plugins
for _, module := range modules {
moduleSettings := system.GetModuleSettings(module.GetConfig().Name)
Expand All @@ -65,13 +75,10 @@ var DestroyApplication = &cobra.Command{
})
}

_ = taskRunner.RunTask(routines.Task{
Name: fmt.Sprintf("Destroying project \"%s\" on server with IP \"%s\"", args[0], args[1]),
Run: func(r *routines.Task) error {
return nil
},
SubTasks: subTasks,
//RunAllSubTasksDespiteError: true,
})
for _, task := range subTasks {
if err = taskRunner.RunTask(task); err != nil {
panic(err)
}
}
},
}
20 changes: 11 additions & 9 deletions modules/proxy/nginx/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ func (Module) Deploy(_modulesSettings interface{}) error {
fmt.Println("Deploy step")
paths := getPaths()

if err := xfs.CreateFolder("ssh://" + paths.CertificatesProjectDirectory); err != nil {
return err
}

serverConfig := buildServerConfig(system.Context.Project, proxy.Context.AllPorts)
nginxConfigResource := system.Resource{
Type: system.TypeFile,
Expand All @@ -132,36 +128,42 @@ func (Module) Deploy(_modulesSettings interface{}) error {
system.Context.CurrentDeployment.ResourceGroups = append(system.Context.CurrentDeployment.ResourceGroups, system.ResourceGroup{
Name: "proxy-nginx-" + system.Context.Project.Name,
Resources: []system.Resource{
system.Resource{
{
Type: system.TypeFolder,
Operation: system.OperationCreate,
Name: paths.CertificatesProjectDirectory,
ExternalResource: true,
},
{
Type: system.TypeFolder,
Operation: system.OperationCreate,
Name: "certificates",
},
nginxConfigResource,
// Symlink project certificate files to snakeoil files after initial creation
system.Resource{
{
Type: system.TypeLink,
Operation: system.OperationCreate,
Name: paths.CertificatesProjectDirectory + "/fullchain.pem",
ExternalResource: true,
LinkSource: paths.SnakeoilFullchainPath,
},
system.Resource{
{
Type: system.TypeLink,
Operation: system.OperationCreate,
Name: paths.CertificatesProjectDirectory + "/privkey.pem",
ExternalResource: true,
LinkSource: paths.SnakeoilPrivkeyPath,
},
system.Resource{
{
Type: system.TypeLink,
Operation: system.OperationCreate,
Name: "/etc/nginx/sites-available/stackhead_" + system.Context.Project.Name + ".conf",
ExternalResource: true,
LinkSource: nginxConfigResourcePath,
EnforceLink: true,
},
system.Resource{
{
Type: system.TypeLink,
Operation: system.OperationCreate,
Name: moduleSettings.Config.VhostPath + "/stackhead_" + system.Context.Project.Name + ".conf",
Expand Down
10 changes: 0 additions & 10 deletions modules/proxy/nginx/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ func (m Module) Destroy(_modulesSettings interface{}) error {
return fmt.Errorf("Unable to remove ACME challenge directory: " + err.Error())
}

if err := xfs.DeleteFile("ssh:///etc/nginx/sites-available/stackhead_" + system.Context.Project.Name + ".conf"); err != nil {
return fmt.Errorf("Unable to remove Nginx symlink: " + err.Error())
}
if err := xfs.DeleteFile("ssh://" + moduleSettings.Config.VhostPath + "/stackhead_" + system.Context.Project.Name + ".conf"); err != nil {
return fmt.Errorf("Unable to remove Nginx symlink: " + err.Error())
}
if err := xfs.DeleteFolder("ssh://"+CertificatesDirectory+"/"+system.Context.Project.Name, true); err != nil {
return fmt.Errorf("Unable to remove certificates directory: " + err.Error())
}

if _, err := system.SimpleRemoteRun("systemctl", system.RemoteRunOpts{Args: []string{"reload", "nginx"}, Sudo: true}); err != nil {
return fmt.Errorf("Unable to reload Nginx service: " + err.Error())
}
Expand Down
Loading

0 comments on commit 82cce26

Please sign in to comment.