| 1 |
/* |
| 2 |
Copyright 2015 Google Inc. All rights reserved. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
{ |
| 18 |
local packer = self, |
| 19 |
|
| 20 |
// A Packer shell provisioner to run something as root. |
| 21 |
RootShell:: { |
| 22 |
type: "shell", |
| 23 |
execute_command: "{{ .Vars }} sudo -E /bin/bash '{{ .Path }}'", |
| 24 |
}, |
| 25 |
|
| 26 |
// A Packer shell provisioner to run something as the current user. |
| 27 |
Shell:: { |
| 28 |
type: "shell", |
| 29 |
}, |
| 30 |
|
| 31 |
// A Packer file provisioner. |
| 32 |
File:: { |
| 33 |
type: "file", |
| 34 |
destination: error "File requires field: destination", |
| 35 |
source: error "File requires field: source", |
| 36 |
}, |
| 37 |
|
| 38 |
// A Packer provisioner that creates a file from inline content, with the given permissions. |
| 39 |
EnsureFile:: packer.RootShell { |
| 40 |
content:: error "EnsureFile provisioner must have field content", |
| 41 |
destination:: error "EnsureFile provisioner must have field destination", |
| 42 |
permissions:: "644", |
| 43 |
user:: "root", |
| 44 |
group:: self.user, |
| 45 |
environment_vars: [ |
| 46 |
// TODO(dcunnin): escaping bash is a workaround for Packer #1733 |
| 47 |
"PACKER_EXPLICIT_FILE_CONTENT='%s'" % std.escapeStringBash(self.content), |
| 48 |
"PACKER_EXPLICIT_FILE=" + self.destination, |
| 49 |
], |
| 50 |
inline: [ |
| 51 |
"mkdir -v -p \"$(dirname \"$PACKER_EXPLICIT_FILE\")\"", |
| 52 |
"echo \"Creating file: $PACKER_EXPLICIT_FILE\"", |
| 53 |
"echo -n \"$PACKER_EXPLICIT_FILE_CONTENT\" > \"$PACKER_EXPLICIT_FILE\"", |
| 54 |
"chmod -v %s \"$PACKER_EXPLICIT_FILE\"" % [self.permissions], |
| 55 |
"chown -v %s.%s \"$PACKER_EXPLICIT_FILE\"" % [self.user, self.group], |
| 56 |
], |
| 57 |
}, |
| 58 |
|
| 59 |
// A Packer provisioner that creates a directory with given permissions. |
| 60 |
EnsureDir:: packer.RootShell { |
| 61 |
dir:: error "EnsureDir provisioner must have field dir", |
| 62 |
permissions:: "775", |
| 63 |
user:: "root", |
| 64 |
group:: self.user, |
| 65 |
environment_vars: [ |
| 66 |
"PACKER_DIR=" + self.dir, |
| 67 |
], |
| 68 |
inline: [ |
| 69 |
"mkdir -pv \"$PACKER_DIR\"", |
| 70 |
"chmod -v %s \"$PACKER_DIR\"" % [self.permissions], |
| 71 |
"chown -v %s.%s \"$PACKER_DIR\"" % [self.user, self.group], |
| 72 |
], |
| 73 |
}, |
| 74 |
|
| 75 |
// A Packer provisioner that creates a symlink. |
| 76 |
EnsureSymLink:: packer.RootShell { |
| 77 |
from:: error "EnsureSymLink provisioner must have field: from", |
| 78 |
to:: error "EnsureSymLink provisioner must have field: to", |
| 79 |
environment_vars: [ |
| 80 |
"PACKER_SYM_FROM=" + self.from, |
| 81 |
"PACKER_SYM_TO=" + self.to, |
| 82 |
], |
| 83 |
inline: [ |
| 84 |
"ln -sfv \"$PACKER_SYM_FROM\" \"$PACKER_SYM_TO\"", |
| 85 |
], |
| 86 |
}, |
| 87 |
|
| 88 |
// A Packer provisioner to install Python packages via Pip. Pip must already be installed. The |
| 89 |
// packages are given as an array of strings. |
| 90 |
Pip:: packer.RootShell { |
| 91 |
packages:: error "Pip provisioner must have field: packages", |
| 92 |
inline: ["pip install " + std.join(" ", self.packages)], |
| 93 |
}, |
| 94 |
|
| 95 |
// A Packer provisioner to install Apt packages. This provisioner can be configured with |
| 96 |
// additional keys and repositories. The packages are given as an array of strings. |
| 97 |
Apt:: packer.RootShell { |
| 98 |
packages:: error "Apt provisioner must have field: packages", |
| 99 |
keyUrls:: [], |
| 100 |
// { foo: "..." } will add a foo.list containing the given content. |
| 101 |
repoLines:: {}, |
| 102 |
// { foo: "..." } will add a foo.list fetched from the given URL. |
| 103 |
repoUrls:: {}, |
| 104 |
|
| 105 |
keyCommands:: ["curl --silent %s | apt-key add -" % [url] for url in self.keyUrls], |
| 106 |
|
| 107 |
local dir = "/etc/apt/sources.list.d", |
| 108 |
local repoLineCommands = ["echo \"%s\" > %s/%s.list" % [self.repoLines[k], dir, k] |
| 109 |
for k in std.objectFields(self.repoLines)], |
| 110 |
local repoUrlCommands = ["curl -o %s/%s.list %s" % [dir, k, self.repoUrls[k]] |
| 111 |
for k in std.objectFields(self.repoUrls)], |
| 112 |
repoCommands:: repoLineCommands + repoUrlCommands, |
| 113 |
|
| 114 |
local opts = "-o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold", |
| 115 |
installCommands:: ["apt-get -qq -y %s install %s" % [opts, std.join(" ", self.packages)]], |
| 116 |
|
| 117 |
environment_vars: ["DEBIAN_FRONTEND=noninteractive"], |
| 118 |
inline: self.repoCommands + self.keyCommands |
| 119 |
+ ["apt-get -qq -y update"] + self.installCommands, |
| 120 |
}, |
| 121 |
// TODO(dcunnin): yum |
| 122 |
/* |
| 123 |
# RHEL and CentOS. |
| 124 |
rpm -Uvh --quiet "RPM_URL" |
| 125 |
yum makecache |
| 126 |
yum -q -y install package1 package2 ... |
| 127 |
*/ |
| 128 |
|
| 129 |
// TODO(dcunnin): zypper |
| 130 |
/* |
| 131 |
zypper refresh |
| 132 |
zypper --non-interactive --quiet install package1 package2 ... |
| 133 |
*/ |
| 134 |
|
| 135 |
// A template for building an image on Google Cloud Platform. This brings out build attributes |
| 136 |
// to the top level and provides defaults for others. |
| 137 |
GcpImage:: { |
| 138 |
|
| 139 |
// Override these |
| 140 |
name:: error "GcpImage must have field: name", |
| 141 |
description: "Packer GcpImage: " + self.name, |
| 142 |
source_image:: error "GcpImage must have field: name", |
| 143 |
project_id:: error "GcpImage must have field: project_id", |
| 144 |
account_file:: error "GcpImage must have field: account_file", |
| 145 |
sshUsername:: error "GcpImage must have field: sshUsername", |
| 146 |
|
| 147 |
local img = self, |
| 148 |
builder:: { |
| 149 |
type: "googlecompute", |
| 150 |
|
| 151 |
name: img.name, |
| 152 |
image_name: "%s" % [self.name], |
| 153 |
image_description: "GCP builder for " + self.name, |
| 154 |
|
| 155 |
// Project & authentication |
| 156 |
project_id: img.project_id, |
| 157 |
account_file: img.account_file, |
| 158 |
|
| 159 |
// Instance mechanics |
| 160 |
machine_type: "n1-standard-1", // Multicore probably doesn't provide any benefit |
| 161 |
source_image: img.source_image, |
| 162 |
instance_name: "packer-" + self.name, |
| 163 |
zone: "us-central1-a", |
| 164 |
ssh_username: img.sshUsername, |
| 165 |
[if std.objectHas(img, "disk_size") then "disk_size"]: img.disk_size, |
| 166 |
}, |
| 167 |
|
| 168 |
builders: [self.builder], |
| 169 |
|
| 170 |
provisioners: [], |
| 171 |
}, |
| 172 |
|
| 173 |
// A template for building a Debian image on Google Cloud Platform. This allows specifying apt |
| 174 |
// and pip attributes at the top-level and automatically provisions the desired packages. |
| 175 |
GcpDebianImage:: packer.GcpImage { |
| 176 |
local image = self, |
| 177 |
|
| 178 |
source_image: "backports-debian-7-wheezy-v20141017", |
| 179 |
|
| 180 |
local pip_pkgs = self.aptPackages |
| 181 |
+ if std.length(self.pipPackages) == 0 then [] else ["python-pip"], |
| 182 |
aptPackages:: [], |
| 183 |
aptKeyUrls:: [], |
| 184 |
aptRepoLines:: {}, |
| 185 |
aptRepoUrls:: {}, |
| 186 |
|
| 187 |
pipPackages:: [], |
| 188 |
|
| 189 |
local apt_provisioners = |
| 190 |
if std.length(pip_pkgs) == 0 |
| 191 |
&& std.length(self.aptKeyUrls) == 0 |
| 192 |
&& std.length(self.aptRepos) == 0 then [ |
| 193 |
] else [ |
| 194 |
packer.Apt { |
| 195 |
packages: pip_pkgs, |
| 196 |
keyUrls: image.aptKeyUrls, |
| 197 |
repoLines: image.aptRepoLines, |
| 198 |
repoUrls: image.aptRepoUrls, |
| 199 |
}, |
| 200 |
], |
| 201 |
|
| 202 |
local pip_provisioners = |
| 203 |
if std.length(self.pipPackages) == 0 then [ |
| 204 |
] else [ |
| 205 |
packer.Pip { packages: image.pipPackages }, |
| 206 |
], |
| 207 |
|
| 208 |
provisioners+: apt_provisioners + pip_provisioners, |
| 209 |
|
| 210 |
}, |
| 211 |
|
| 212 |
// A template for building Nginx/uwsgi/flask based application servers. The uwsgi configuration |
| 213 |
// is provided at the top level and is automatically compiler to INI format. A simple cron line |
| 214 |
// is used to start the uwsgi emperor at boot. The given Flask module must exist in /var/www, |
| 215 |
// so extend this template with additional provisioners to create that content. |
| 216 |
GcpDebianNginxUwsgiFlaskImage:: packer.GcpDebianImage { |
| 217 |
local image = self, |
| 218 |
|
| 219 |
module:: error "NginxUwsgiFlaskImage must have field: module", |
| 220 |
application:: "app", |
| 221 |
port:: 80, |
| 222 |
uwsgiSocket:: "/var/www/uwsgi.sock", |
| 223 |
|
| 224 |
aptPackages+: ["nginx", "python-dev"], |
| 225 |
pipPackages+: ["flask", "uwsgi"], |
| 226 |
|
| 227 |
uwsgiConf:: { |
| 228 |
chdir: "/var/www", |
| 229 |
base: "/var/www", |
| 230 |
module: image.module, |
| 231 |
pythonpath: "/var/www", |
| 232 |
socket: image.uwsgiSocket, |
| 233 |
"chmod-socket": "644", |
| 234 |
callable: image.application, |
| 235 |
logto: "/var/log/uwsgi/uwsgi.log", |
| 236 |
}, |
| 237 |
|
| 238 |
nginxConf:: [ |
| 239 |
"server {", |
| 240 |
" listen %d;" % image.port, |
| 241 |
" server_name localhost;", |
| 242 |
" charset utf-8;", |
| 243 |
" client_max_body_size 75M;", |
| 244 |
" location / { try_files $uri @yourapplication; }", |
| 245 |
" location @yourapplication {", |
| 246 |
" include uwsgi_params;", |
| 247 |
" uwsgi_pass unix:%s;" % image.uwsgiSocket, |
| 248 |
" }", |
| 249 |
"}", |
| 250 |
], |
| 251 |
|
| 252 |
provisioners+: [ |
| 253 |
packer.RootShell { inline: ["rm /etc/nginx/sites-enabled/default"] }, |
| 254 |
packer.EnsureFile { |
| 255 |
destination: "/etc/nginx/conf.d/frontend_nginx.conf", |
| 256 |
content: std.lines(image.nginxConf), |
| 257 |
}, |
| 258 |
packer.EnsureFile { |
| 259 |
destination: "/etc/uwsgi/vassals/uwsgi.ini", |
| 260 |
content: std.manifestIni({ |
| 261 |
sections: { |
| 262 |
uwsgi: image.uwsgiConf, |
| 263 |
}, |
| 264 |
}), |
| 265 |
}, |
| 266 |
packer.EnsureFile { |
| 267 |
destination: "/etc/cron.d/emperor", |
| 268 |
content: "@reboot root /usr/local/bin/uwsgi --master --emperor /etc/uwsgi/vassals " |
| 269 |
+ "--daemonize /var/log/uwsgi/emperor.log --pidfile /var/run/uwsgi.pid " |
| 270 |
+ "--die-on-term --uid www-data --gid www-data\n", |
| 271 |
}, |
| 272 |
packer.EnsureDir { dir: "/var/log/uwsgi", user: "www-data" }, |
| 273 |
packer.EnsureDir { dir: "/var/www", user: "www-data" }, |
| 274 |
], |
| 275 |
}, |
| 276 |
|
| 277 |
|
| 278 |
// A template to help build PostgreSQL images (experimental). |
| 279 |
GcpDebianPostgresqlImage: packer.GcpDebianImage { |
| 280 |
local image = self, |
| 281 |
|
| 282 |
rootPassword:: error "GcpDebianPostgresqlImage: must have field: rootPassword", |
| 283 |
|
| 284 |
aptPackages+: ["postgresql", "postgresql-contrib"], |
| 285 |
|
| 286 |
initSql:: [ |
| 287 |
"ALTER USER POSTGRES WITH PASSWORD '%s';" % image.rootPassword, |
| 288 |
], |
| 289 |
|
| 290 |
provisioners+: [ |
| 291 |
packer.RootShell { inline: [ |
| 292 |
"echo %s | sudo -u postgres psql" % std.escapeStringBash(std.lines(image.initSql)), |
| 293 |
] }, |
| 294 |
packer.EnsureFile { |
| 295 |
destination: "/etc/postgresql/9.1/main/postgresql.conf", |
| 296 |
content: std.lines([ |
| 297 |
"data_directory = '/var/lib/postgresql/9.1/main'", |
| 298 |
"hba_file = '/etc/postgresql/9.1/main/pg_hba.conf'", |
| 299 |
"ident_file = '/etc/postgresql/9.1/main/pg_ident.conf'", |
| 300 |
"external_pid_file = '/var/run/postgresql/9.1-main.pid'", |
| 301 |
"listen_addresses = '*'", |
| 302 |
"port = 5432", |
| 303 |
"max_connections = 100", |
| 304 |
"unix_socket_directory = '/var/run/postgresql'", |
| 305 |
"ssl = true", |
| 306 |
"shared_buffers = 32MB", |
| 307 |
"log_line_prefix = '%t '", |
| 308 |
"datestyle = 'iso, mdy'", |
| 309 |
"lc_messages = 'en_US.UTF-8'", |
| 310 |
"lc_monetary = 'en_US.UTF-8'", |
| 311 |
"lc_numeric = 'en_US.UTF-8'", |
| 312 |
"lc_time = 'en_US.UTF-8'", |
| 313 |
"default_text_search_config = 'pg_catalog.english'", |
| 314 |
]), |
| 315 |
}, |
| 316 |
packer.EnsureFile { |
| 317 |
destination: "/etc/postgresql/9.1/main/pg_hba.conf", |
| 318 |
content: std.lines([ |
| 319 |
"local all all md5", |
| 320 |
"host all all 255.255.255.255/0 md5", |
| 321 |
"host all all ::1/128 md5", |
| 322 |
]), |
| 323 |
}, |
| 324 |
], |
| 325 |
}, |
| 326 |
|
| 327 |
// A template to help build MySQL images (experimental). |
| 328 |
GcpDebianMysqlImage: packer.GcpDebianImage { |
| 329 |
local image = self, |
| 330 |
|
| 331 |
rootPassword:: error "GcpDebianMysqlImage: must have field: rootPassword", |
| 332 |
|
| 333 |
aptPackages+: ["mysql-server"], |
| 334 |
|
| 335 |
initSql:: [], |
| 336 |
|
| 337 |
provisioners+: [ |
| 338 |
packer.RootShell { inline: [ |
| 339 |
"mysqladmin -u root password '%s'" % std.escapeStringBash(image.rootPassword), |
| 340 |
"echo %s | mysql -u root --password=%s" |
| 341 |
% [std.escapeStringBash(std.lines(image.initSql)), |
| 342 |
std.escapeStringBash(image.rootPassword)], |
| 343 |
] }, |
| 344 |
packer.EnsureFile { |
| 345 |
destination: "/etc/mysql/conf.d/local.cnf", |
| 346 |
content: std.manifestIni({ |
| 347 |
sections: { |
| 348 |
mysqld: { |
| 349 |
"bind-address": "0.0.0.0", |
| 350 |
}, |
| 351 |
}, |
| 352 |
}), |
| 353 |
}, |
| 354 |
], |
| 355 |
}, |
| 356 |
} |