| 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 terraform = self, |
| 19 |
|
| 20 |
// Basic template for a GCP instance, adds default service account scopes, and factors out a few |
| 21 |
// things into top-level fields. |
| 22 |
GcpInstance:: { |
| 23 |
local instance = self, |
| 24 |
name: error "Instance must have field: name", |
| 25 |
image:: error "Instance must have field: image", |
| 26 |
address:: null, |
| 27 |
|
| 28 |
scopes:: ["devstorage.read_only", "logging.write"], |
| 29 |
|
| 30 |
service_account: [ |
| 31 |
{ |
| 32 |
scopes: ["https://www.googleapis.com/auth/" + s for s in instance.scopes], |
| 33 |
}, |
| 34 |
], |
| 35 |
|
| 36 |
machine_type: "f1-micro", |
| 37 |
zone: "us-central1-f", |
| 38 |
tags: ["terraform"], |
| 39 |
|
| 40 |
disk: { |
| 41 |
image: instance.image, |
| 42 |
}, |
| 43 |
|
| 44 |
startup_script:: [], |
| 45 |
|
| 46 |
addFile(v, dest):: |
| 47 |
"echo %s > %s" % [std.escapeStringBash(v), std.escapeStringBash(dest)], |
| 48 |
|
| 49 |
metadata: { |
| 50 |
"startup-script": std.lines(instance.startup_script), |
| 51 |
}, |
| 52 |
|
| 53 |
network_interface: [{ |
| 54 |
network: "default", |
| 55 |
access_config: if instance.address != null then [ |
| 56 |
{ nat_ip: instance.address }, |
| 57 |
] else [ |
| 58 |
{}, |
| 59 |
], |
| 60 |
}], |
| 61 |
|
| 62 |
}, |
| 63 |
|
| 64 |
// Allow http for servers tagged with "http-server" on the given network. |
| 65 |
GcpFirewallHttp:: { |
| 66 |
local fw = self, |
| 67 |
source_ranges: ["0.0.0.0/0"], |
| 68 |
port:: 80, |
| 69 |
network: error "GcpFirewallHttp must have field: network", |
| 70 |
allow: [{ protocol: "tcp", ports: [std.toString(fw.port)] }], |
| 71 |
target_tags: ["http-server"], |
| 72 |
}, |
| 73 |
|
| 74 |
// Allow ssh for all servers on the given network. |
| 75 |
GcpFirewallSsh:: { |
| 76 |
source_ranges: ["0.0.0.0/0"], |
| 77 |
network: error "GcpFirewallSsh must have field: network", |
| 78 |
allow: [{ protocol: "tcp", ports: ["22"] }], |
| 79 |
}, |
| 80 |
} |