1local cmd = import '../cmd/cmd.libsonnet';
2
3{
4  // A mixin on top of a Debian instance that manages an Nginx installation.  This supports:
5  // - Monitoring is enabled by default.
6  // - /var/www is initialized correctly
7  // - Two hooks are provided for running handler daemons.
8  DebianNginxMixin:: {
9
10    local version = self,
11
12    enableMonitoring: version.supportsMonitoring,
13    enableLogging: version.supportsLogging,
14
15    StandardRootImage+: {
16      local image = self,
17
18      aptPackages+: ['nginx'],
19
20      // Configure nginx to expose stub_status to the local agent.
21      nginxMonitoringConf:: |||
22        server {
23            listen 80;
24            server_name local-stackdriver-agent.stackdriver.com;
25            location /nginx_status {
26              stub_status on;
27              access_log   off;
28              allow 127.0.0.1;
29              deny all;
30            }
31            location / {
32              root /dev/null;
33            }
34        }
35      |||,
36
37      // Configure Collectd to find the stub_status at the above URL.
38      nginxCollectdConf:: |||
39        LoadPlugin "nginx"
40        <Plugin "nginx">
41          URL "http://local-stackdriver-agent.stackdriver.com/nginx_status"
42        </Plugin>
43      |||,
44
45
46      cmds+: (if version.enableMonitoring then [
47                cmd.LiteralFile {
48                  to: '/etc/nginx/conf.d/monitoring.conf',
49                  content: image.nginxMonitoringConf,
50                },
51                cmd.LiteralFile {
52                  to: '/opt/stackdriver/collectd/etc/collectd.d/nginx.conf',
53                  content: image.nginxCollectdConf,
54                },
55              ] else []) + [
56
57        // Disable the default site.
58        'rm /etc/nginx/sites-enabled/default',
59      ],
60    },
61
62
63    // Files that must exist on top before any handling daemons are started.
64    // Extend this to deploy more content for the web server.
65    httpContentCmds:: [
66      cmd.EnsureDir { dir: '/var/www', owner: 'www-data' },
67    ],
68
69    // Running handling daemons.
70    httpHandlerCmds:: [
71    ],
72
73    // Additional Nginx config commands.
74    nginxAdditionalCmds:: [
75    ],
76
77    cmds+: self.httpContentCmds + self.httpHandlerCmds + self.nginxAdditionalCmds + [
78      'nginx -s reload',
79    ],
80  },
81}
82