-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.go
75 lines (57 loc) · 1.6 KB
/
publish.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package goku
import (
"fmt"
"os"
"os/exec"
docker "github.com/fsouza/go-dockerclient"
)
const nginxTemplate = `
server {
listen 80;
server_name %s;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:%s/;
}
}
`
// publish publishes a container via nginx
func publish(proj Project, container *docker.Container) error {
ports := container.NetworkSettings.Ports
var port docker.PortBinding
for p, binding := range ports {
if p.Port() == "80" {
port = binding[0]
break
}
}
return saveNginxProfile(proj.Domain, proj.Name, port.HostPort)
}
func saveNginxProfile(domain, name, port string) error {
l := NewLog("[publish processor]", true)
siteAvailablePath := fmt.Sprintf("/etc/nginx/sites-available/%s", name)
fout, err := os.Create(siteAvailablePath)
if err != nil {
l.Tracef("could not create nginx configuration file for %s", name)
return err
}
defer fout.Close()
nginxConf := fmt.Sprintf(nginxTemplate, domain, port)
l.Trace(nginxConf)
if _, err = fout.WriteString(nginxConf); err != nil {
l.Tracef("could not write nginx configuration for %s", name)
return err
}
siteEnabledPath := fmt.Sprintf("/etc/nginx/sites-enabled/%s", name)
if _, err := os.Stat(siteEnabledPath); os.IsNotExist(err) && os.Symlink(siteAvailablePath, siteEnabledPath) != nil {
l.Trace("sym link failed")
return err
}
reloadCmd := exec.Command("service", "nginx", "reload")
if err := reloadCmd.Run(); err != nil {
l.Tracef("could not reload nginx profile\n%s", err.Error())
return err
}
return nil
}