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

Commit 82cce26

Browse files
committed
feat: folder and Link resource handling
1 parent 5bc75c2 commit 82cce26

File tree

8 files changed

+228
-151
lines changed

8 files changed

+228
-151
lines changed

commands/project/deploy.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ var DeployApplication = func() *cobra.Command {
4343

4444
err = taskRunner.RunTask(routines.PrepareProjectTask(projectDefinition))
4545
if err != nil {
46+
if system.Context.CurrentDeployment.Version > 0 {
47+
_ = xfs.DeleteFolder("ssh://"+system.Context.CurrentDeployment.GetPath(), true)
48+
}
4649
return
4750
}
4851
err = taskRunner.RunTask(routines.CollectResourcesTask(projectDefinition))
4952
if err != nil {
53+
_ = xfs.DeleteFolder("ssh://"+system.Context.CurrentDeployment.GetPath(), true)
5054
return
5155
}
5256

commands/project/destroy.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package project
22

33
import (
4-
"fmt"
5-
64
xfs "github.com/saitho/golang-extended-fs/v2"
75
"github.com/spf13/cobra"
86

@@ -12,6 +10,12 @@ import (
1210
"github.com/getstackhead/stackhead/system"
1311
)
1412

13+
func reverse[S ~[]E, E any](s S) {
14+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
15+
s[i], s[j] = s[j], s[i]
16+
}
17+
}
18+
1519
// DestroyApplication is a command object for Cobra that provides the destroy command
1620
var DestroyApplication = &cobra.Command{
1721
Use: "destroy [path to project definition] [ipv4 address]",
@@ -26,22 +30,28 @@ var DestroyApplication = &cobra.Command{
2630
}
2731
commands.PrepareContext(args[1], system.ContextActionProjectDeploy, projectDefinition)
2832

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

34-
// Init modules
39+
modules := system.Context.GetModulesInOrder()
40+
reverse(modules)
41+
42+
// Run modules destroy steps
3543
for _, module := range modules {
3644
moduleSettings := system.GetModuleSettings(module.GetConfig().Name)
3745
module.Init(moduleSettings)
3846
}
3947
taskRunner := routines.TaskRunner{}
4048

41-
subTasks := []routines.Task{}
49+
subTasks := []routines.Task{
50+
// Remove resources from deployment
51+
routines.RemoveResources(latestDeployment),
52+
}
4253

4354
if hasProjectDir, _ := xfs.HasFolder("ssh://" + projectDefinition.GetDirectoryPath()); hasProjectDir {
44-
4555
// Run destroy scripts from plugins
4656
for _, module := range modules {
4757
moduleSettings := system.GetModuleSettings(module.GetConfig().Name)
@@ -65,13 +75,10 @@ var DestroyApplication = &cobra.Command{
6575
})
6676
}
6777

68-
_ = taskRunner.RunTask(routines.Task{
69-
Name: fmt.Sprintf("Destroying project \"%s\" on server with IP \"%s\"", args[0], args[1]),
70-
Run: func(r *routines.Task) error {
71-
return nil
72-
},
73-
SubTasks: subTasks,
74-
//RunAllSubTasksDespiteError: true,
75-
})
78+
for _, task := range subTasks {
79+
if err = taskRunner.RunTask(task); err != nil {
80+
panic(err)
81+
}
82+
}
7683
},
7784
}

modules/proxy/nginx/deploy.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,6 @@ func (Module) Deploy(_modulesSettings interface{}) error {
117117
fmt.Println("Deploy step")
118118
paths := getPaths()
119119

120-
if err := xfs.CreateFolder("ssh://" + paths.CertificatesProjectDirectory); err != nil {
121-
return err
122-
}
123-
124120
serverConfig := buildServerConfig(system.Context.Project, proxy.Context.AllPorts)
125121
nginxConfigResource := system.Resource{
126122
Type: system.TypeFile,
@@ -132,36 +128,42 @@ func (Module) Deploy(_modulesSettings interface{}) error {
132128
system.Context.CurrentDeployment.ResourceGroups = append(system.Context.CurrentDeployment.ResourceGroups, system.ResourceGroup{
133129
Name: "proxy-nginx-" + system.Context.Project.Name,
134130
Resources: []system.Resource{
135-
system.Resource{
131+
{
132+
Type: system.TypeFolder,
133+
Operation: system.OperationCreate,
134+
Name: paths.CertificatesProjectDirectory,
135+
ExternalResource: true,
136+
},
137+
{
136138
Type: system.TypeFolder,
137139
Operation: system.OperationCreate,
138140
Name: "certificates",
139141
},
140142
nginxConfigResource,
141143
// Symlink project certificate files to snakeoil files after initial creation
142-
system.Resource{
144+
{
143145
Type: system.TypeLink,
144146
Operation: system.OperationCreate,
145147
Name: paths.CertificatesProjectDirectory + "/fullchain.pem",
146148
ExternalResource: true,
147149
LinkSource: paths.SnakeoilFullchainPath,
148150
},
149-
system.Resource{
151+
{
150152
Type: system.TypeLink,
151153
Operation: system.OperationCreate,
152154
Name: paths.CertificatesProjectDirectory + "/privkey.pem",
153155
ExternalResource: true,
154156
LinkSource: paths.SnakeoilPrivkeyPath,
155157
},
156-
system.Resource{
158+
{
157159
Type: system.TypeLink,
158160
Operation: system.OperationCreate,
159161
Name: "/etc/nginx/sites-available/stackhead_" + system.Context.Project.Name + ".conf",
160162
ExternalResource: true,
161163
LinkSource: nginxConfigResourcePath,
162164
EnforceLink: true,
163165
},
164-
system.Resource{
166+
{
165167
Type: system.TypeLink,
166168
Operation: system.OperationCreate,
167169
Name: moduleSettings.Config.VhostPath + "/stackhead_" + system.Context.Project.Name + ".conf",

modules/proxy/nginx/destroy.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@ func (m Module) Destroy(_modulesSettings interface{}) error {
2828
return fmt.Errorf("Unable to remove ACME challenge directory: " + err.Error())
2929
}
3030

31-
if err := xfs.DeleteFile("ssh:///etc/nginx/sites-available/stackhead_" + system.Context.Project.Name + ".conf"); err != nil {
32-
return fmt.Errorf("Unable to remove Nginx symlink: " + err.Error())
33-
}
34-
if err := xfs.DeleteFile("ssh://" + moduleSettings.Config.VhostPath + "/stackhead_" + system.Context.Project.Name + ".conf"); err != nil {
35-
return fmt.Errorf("Unable to remove Nginx symlink: " + err.Error())
36-
}
37-
if err := xfs.DeleteFolder("ssh://"+CertificatesDirectory+"/"+system.Context.Project.Name, true); err != nil {
38-
return fmt.Errorf("Unable to remove certificates directory: " + err.Error())
39-
}
40-
4131
if _, err := system.SimpleRemoteRun("systemctl", system.RemoteRunOpts{Args: []string{"reload", "nginx"}, Sudo: true}); err != nil {
4232
return fmt.Errorf("Unable to reload Nginx service: " + err.Error())
4333
}

0 commit comments

Comments
 (0)