|
| 1 | +/* |
| 2 | + * Copyright 2017 The Nakama Authors |
| 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 | +terraform { |
| 18 | + required_version = ">= 0.8, < 0.9" |
| 19 | +} |
| 20 | + |
| 21 | +provider "google" { |
| 22 | + project = "${var.gce_project_name}" |
| 23 | + region = "${var.gce_region}" |
| 24 | + credentials = "${file("account.json")}" |
| 25 | +} |
| 26 | + |
| 27 | +resource "google_compute_address" "api" { |
| 28 | + name = "api-address" |
| 29 | +} |
| 30 | + |
| 31 | +resource "google_compute_target_pool" "api" { |
| 32 | + name = "api-target-pool" |
| 33 | + instances = ["${google_compute_instance.api.*.self_link}"] |
| 34 | + health_checks = ["${google_compute_http_health_check.healthcheck.name}"] |
| 35 | +} |
| 36 | + |
| 37 | +resource "google_compute_http_health_check" "healthcheck" { |
| 38 | + name = "api-healthcheck" |
| 39 | + port = 8081 |
| 40 | + request_path = "/v0/health" |
| 41 | + check_interval_sec = 5 |
| 42 | + healthy_threshold = 1 |
| 43 | + unhealthy_threshold = 3 |
| 44 | + timeout_sec = 2 |
| 45 | +} |
| 46 | + |
| 47 | +resource "google_compute_firewall" "api" { |
| 48 | + name = "api-firewall" |
| 49 | + network = "default" |
| 50 | + |
| 51 | + allow { |
| 52 | + protocol = "icmp" |
| 53 | + } |
| 54 | + |
| 55 | + allow { |
| 56 | + protocol = "tcp" |
| 57 | + ports = ["22", "80", "443"] |
| 58 | + } |
| 59 | + |
| 60 | + source_ranges = ["0.0.0.0/0"] |
| 61 | + target_tags = ["api-node"] |
| 62 | +} |
| 63 | + |
| 64 | +resource "google_compute_disk" "default" { |
| 65 | + name = "api-disk" |
| 66 | + type = "pd-ssd" |
| 67 | + zone = "${var.gce_region_zone}" |
| 68 | + size = 10 |
| 69 | +} |
| 70 | + |
| 71 | +resource "google_compute_instance" "api" { |
| 72 | + count = 1 |
| 73 | + name = "api-node-${count.index}" |
| 74 | + machine_type = "${var.app_machine_type}" |
| 75 | + zone = "${var.gce_region_zone}" |
| 76 | + tags = ["api-node"] |
| 77 | + |
| 78 | + disk { |
| 79 | + image = "ubuntu-os-cloud/ubuntu-1604-lts" |
| 80 | + } |
| 81 | + |
| 82 | + disk { |
| 83 | + disk = "${google_compute_disk.default.name}" |
| 84 | + } |
| 85 | + |
| 86 | + network_interface { |
| 87 | + network = "default" |
| 88 | + access_config {} # Ephemeral |
| 89 | + } |
| 90 | + |
| 91 | + service_account { |
| 92 | + scopes = ["userinfo-email", "compute-ro", "storage-ro"] |
| 93 | + } |
| 94 | + |
| 95 | + metadata { |
| 96 | + ssh-keys = "${var.gce_ssh_user}:${file(var.gce_ssh_public_key_file)}" |
| 97 | + } |
| 98 | + |
| 99 | + provisioner "file" { |
| 100 | + connection { |
| 101 | + user = "${var.gce_ssh_user}" |
| 102 | + private_key = "${file(var.gce_ssh_private_key_file)}" |
| 103 | + agent = false |
| 104 | + timeout = "30s" |
| 105 | + } |
| 106 | + source = "systemd/" |
| 107 | + destination = "/etc/systemd/system" |
| 108 | + } |
| 109 | + |
| 110 | + provisioner "remote-exec" { |
| 111 | + connection { |
| 112 | + user = "${var.gce_ssh_user}" |
| 113 | + private_key = "${file(var.gce_ssh_private_key_file)}" |
| 114 | + agent = false |
| 115 | + timeout = "30s" |
| 116 | + } |
| 117 | + inline = [ |
| 118 | + "cd /home/ubuntu", |
| 119 | + |
| 120 | + # Setup cockroachdb |
| 121 | + "wget --no-verbose https://binaries.cockroachdb.com/cockroach-${var.app_cockroachdb_version}.linux-amd64.tgz", |
| 122 | + "tar zxvf cockroach-${var.app_cockroachdb_version}.linux-amd64.tgz", |
| 123 | + "chmod +x ./cockroach-${var.app_cockroachdb_version}.linux-amd64/cockroach", |
| 124 | + "ln -s ./cockroach-${var.app_cockroachdb_version}.linux-amd64/cockroach /home/ubuntu/cockroach", |
| 125 | + "systemctl start cockroach", |
| 126 | + |
| 127 | + # Setup nakama |
| 128 | + "wget --no-verbose https://github.com/heroiclabs/nakama/releases/download/v${var.app_nakama_version}/nakama-${var.app_nakama_version}-linux-amd64.tar.gz", |
| 129 | + "mkdir -p nakama-${var.app_nakama_version}-linux-amd64", |
| 130 | + "tar zxvf nakama-${var.app_nakama_version}-linux-amd64.tar.gz -C nakama-${var.app_nakama_version}-linux-amd64", |
| 131 | + "chmod +x ./nakama-${var.app_nakama_version}-linux-amd64/nakama", |
| 132 | + "ln -s ./nakama-${var.app_nakama_version}-linux-amd64/nakama", |
| 133 | + "./nakama migrate up --db root@127.0.0.1:26257", |
| 134 | + "systemctl start nakama" |
| 135 | + ] |
| 136 | + } |
| 137 | +} |
0 commit comments