-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacker.pkr.hcl
163 lines (136 loc) · 3.83 KB
/
packer.pkr.hcl
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
packer {
required_version = ">= 1.7.0"
required_plugins {
googlecompute = {
version = ">= 1.0"
source = "github.com/hashicorp/googlecompute"
}
}
}
locals {
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
}
variable "gcp_project_id" {
type = string
default = "csye-6225-terraform-packer"
}
variable "source_image_family" {
type = string
default = "centos-stream-8"
}
variable "machine_type" {
type = string
default = "custom-1-2048"
}
variable "application_name" {
type = string
default = "webapp"
}
variable "service_name" {
type = string
default = "webapp.service"
}
variable "zone" {
type = string
default = "asia-east1-a"
}
variable "ssh_username" {
type = string
default = "centos"
}
variable golang_version {
type = string
default = ""
}
source "googlecompute" "webapp-source" {
image_name = "webapp-${local.timestamp}"
project_id = var.gcp_project_id
machine_type = var.machine_type
source_image_family = var.source_image_family
ssh_username = var.ssh_username
zone = var.zone
}
build {
sources = [
"source.googlecompute.webapp-source"
]
// PostgreSQL Installation
// provisioner "shell" {
// inline = [
// "sudo yum update -y",
// "sudo yum install -y postgresql-server postgresql-contrib",
// "sudo postgresql-setup --initdb --unit postgresql",
// "sudo systemctl enable postgresql",
// "sudo systemctl start postgresql",
// ]
// }
// PostgreSQL user and database creation and assign perms
// provisioner "shell" {
// script = "./db.sh"
// }
provisioner "file" {
source = "./webapp"
destination = "/tmp/webapp"
}
provisioner "file" {
source = "./webapp.zip"
destination = "/tmp/webapp.zip"
}
provisioner "file" {
source = "./webapp.service"
destination = "/tmp/webapp.service"
}
// provisioner "file" {
// source = "./.env"
// destination = "/tmp/.env"
// }
provisioner "file" {
source = "./config.yaml"
destination = "/tmp/config.yaml"
}
provisioner "shell" {
inline = [
// Create group and user
"getent group csye6225 || sudo groupadd csye6225",
// Check if user exists, create if it does not
"id -u csye6225 || sudo useradd -g csye6225 -m csye6225",
// Move webapp and enable service
"sudo mv /tmp/webapp /usr/local/bin",
"sudo mv /tmp/webapp.zip /usr/local/bin",
// "sudo mv /tmp/.env /usr/local/bin",
"sudo mv /tmp/webapp.service /etc/systemd/system",
"sudo sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config",
"sudo restorecon -rv /usr/local/bin/webapp",
// "sudo touch /home/csye6225/webapp/userdata.sh",
"sudo chown csye6225:csye6225 /usr/local/bin/webapp",
// "sudo chown csye6225:csye6225 /usr/local/bin/.env",
"sudo chmod 750 /usr/local/bin/webapp",
// "sudo chmod 755 /usr/local/bin/.env",
"sudo mkdir -p /var/log/myapp",
"sudo touch /var/log/myapp/app.log",
"sudo chown csye6225:csye6225 /var/log/myapp/app.log",
"sudo chmod 766 /var/log/myapp/app.log",
//set nologin to webapp user
"sudo usermod csye6225 --shell /usr/sbin/nologin",
"cd"
]
}
// Cloud Ops Agent installation
provisioner "shell" {
inline = [
"curl -sSO https://dl.google.com/cloudagents/add-google-cloud-ops-agent-repo.sh",
"sudo bash add-google-cloud-ops-agent-repo.sh --also-install"
]
}
provisioner "shell" {
inline = [
"sudo mv /tmp/config.yaml /etc/google-cloud-ops-agent/config.yaml",
"sudo systemctl enable google-cloud-ops-agent",
"sudo systemctl restart google-cloud-ops-agent.service"
]
}
// Enable and start webapp
provisioner "shell" {
script = "./webapp_start.sh"
}
}