@@ -2,71 +2,35 @@ package proxy_caddy
22
33import (
44 "fmt"
5- "strings"
65
76 xfs "github.com/saitho/golang-extended-fs/v2"
87
98 "github.com/getstackhead/stackhead/modules/proxy"
10- "github.com/getstackhead/stackhead/project"
119 "github.com/getstackhead/stackhead/system"
1210)
1311
14- type Data struct {
15- Project * project.Project
16- AllPortsTfString string
17- DependentContainers string
18- }
19-
2012func (Module ) Deploy (modulesSettings interface {}) error {
21-
22- // - name: Include OS-specific variables.
23- // include_vars: "{{ ansible_os_family }}.yml"
24- // ignore_errors: yes
25- //
26- //- name: Check if authentications are defined
27- // set_fact:
28- // auths_basic: "{{ auths_basic|default([]) + item.security.authentication }}"
29- // when: item.security is defined and item.security.authentication is defined
30- // with_items: "{{ app_config.domains }}"
31- //
32- //- name: Generate Caddy Terraform file
33- // include_tasks: "{{ module_role_path|default(role_path) }}/tasks/steps/generate-serverconfig-tf.yml"
34-
35- var DependentContainers []string
36- for _ , domain := range system .Context .Project .Domains {
37- for i , expose := range domain .Expose {
38- ContainerResourceName := "docker_container.stackhead-" + system .Context .Project .Name + "-" + expose .Service
39- if expose .ExternalPort != 443 {
40- DependentContainers = append (DependentContainers , ContainerResourceName )
41- }
42- //expose.Service
43- proxy .Context .AllPorts = append (proxy .Context .AllPorts , proxy.PortService {
44- Expose : expose ,
45- ContainerResourceName : ContainerResourceName ,
46- Index : i ,
47- })
48- }
13+ caddyDirectives , err := system .RenderModuleTemplate (
14+ templates ,
15+ "Caddyfile_project.tmpl" ,
16+ nil ,
17+ proxy .FuncMap )
18+ if err != nil {
19+ return err
4920 }
5021
51- var AllPortsTfStrings [] string
52- for _ , port := range proxy . Context . AllPorts {
53- AllPortsTfStrings = append ( AllPortsTfStrings , port . GetTfString ())
22+ projectCaddyLocation := system . Context . Project . GetDirectoryPath () + "/Caddyfile"
23+ if err := xfs . WriteFile ( "ssh://" + projectCaddyLocation , caddyDirectives ); err != nil {
24+ return err
5425 }
5526
56- fmt .Println ("Deploy step" )
57-
58- data := map [string ]interface {}{
59- "AllPortsTfString" : strings .Join (AllPortsTfStrings , "," ),
60- "DependentContainers" : strings .Join (DependentContainers , "," ),
27+ if _ , err := system .SimpleRemoteRun ("ln" , system.RemoteRunOpts {Args : []string {"-sf " + projectCaddyLocation + " /etc/caddy/conf.d/stackhead_" + system .Context .Project .Name + ".conf" }}); err != nil {
28+ return fmt .Errorf ("Unable to symlink project Caddyfile: " + err .Error ())
6129 }
6230
63- caddyDirectives , err := system .RenderModuleTemplate (
64- templates ,
65- "caddy.tf.tmpl" ,
66- data ,
67- proxy .FuncMap )
68- if err != nil {
69- return err
31+ if _ , err := system .SimpleRemoteRun ("systemctl" , system.RemoteRunOpts {Args : []string {"reload" , "caddy" }, Sudo : true }); err != nil {
32+ return fmt .Errorf ("Unable to reload Caddy service: " + err .Error ())
7033 }
71- return xfs .WriteFile ("ssh://" + system .Context .Project .GetTerraformDirectoryPath ()+ "/caddy.tf" , caddyDirectives )
34+
35+ return nil
7236}
0 commit comments