-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate.yml
118 lines (113 loc) · 4.09 KB
/
create.yml
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
# Create VM
steps:
- name: cyclenerd/cloud-tools-container:latest
entrypoint: "bash"
args:
- "-c"
- |
echo "» JSON"
echo "Message Data:"
echo '${_JSON_DATA}'
echo '${_JSON_DATA}' > "/workspace/data.json" || exit 9
echo "File:"
jq < "/workspace/data.json" || exit 9
echo
echo "» VARIABLES"
echo -n "Image : "; jq -er '.image' < "/workspace/data.json" || exit 9
echo -n "Username : "; jq -er '.username' < "/workspace/data.json" || exit 9
echo -n "Password : "; jq -er '.password' < "/workspace/data.json" || exit 9
echo -n "DNS Name : "; jq -er '.dnsname' < "/workspace/data.json" || exit 9
echo 'Terraform State Bucket : ${_STATE_BUCKET}'
echo 'Cloud Build ID : $BUILD_ID'
echo 'Cloud Build Region : $LOCATION'
echo 'Project ID : $PROJECT_ID'
echo 'Project Nr : $PROJECT_NUMBER'
# Exit if SHA512 hash is less than 100 chars
if [ $(jq -er '.password' < "/workspace/data.json" | wc -c) -lt "100" ]; then echo "SHA512 password hash too short!"; exit 9; fi
- name: cyclenerd/cloud-tools-container:latest
entrypoint: "bash"
args:
- "-c"
- |
echo "» SSH"
echo
ssh-keygen -N '' -C 'ansible@cloudbuild' -t ed25519 -f '/workspace/ssh.key' || exit 9
echo
echo "Public Key :"
cat "/workspace/ssh.key.pub" || exit 9
- name: cyclenerd/cloud-tools-container:latest
entrypoint: "bash"
args:
- "-c"
- |
echo "» TERRAFORM"
echo
echo "Change dir"
cd "/workspace/cloudbuild/" || exit 9
echo
echo "Get variables (TFVARS)..."
gsutil cp "gs://${_STATE_BUCKET}/output.tfvars" "terraform.tfvars" || exit 9
echo
echo "Create state file with bucket and prefix..."
{
echo 'terraform {'
echo ' backend "gcs" {'
echo ' bucket = "${_STATE_BUCKET}"'
echo ' prefix = "terraform/state/gce-vm/${BUILD_ID}"'
echo ' }'
echo '}'
} > "terraform-state.tf" || exit 9
cat "terraform-state.tf" || exit 9
echo
echo "Create .terraform directory..."
mkdir -p "/workspace/test/integration/tmp/.terraform"
echo
echo "Initialize..."
terraform init || exit 9
echo
echo "» APPLY"
echo
terraform apply \
-var="build_id=$BUILD_ID" \
-var="dns-name=$(jq -er '.dnsname' < "/workspace/data.json")" \
-var="image=$(jq -er '.image' < "/workspace/data.json")" \
-auto-approve || exit 9
- name: cyclenerd/cloud-tools-container:latest
entrypoint: "bash"
args:
- "-c"
- |
echo "» WAIT for new GCE instance"
echo
echo -n "Public NAT IPv4 : '"
cat "/workspace/nat_ip.txt" || exit 9
echo "'"
bash "/workspace/cloudbuild/wait-for-ssh.sh" || exit 9
- name: cyclenerd/cloud-tools-container:latest
entrypoint: "bash"
args:
- "-c"
- |
echo "» ANSIBLE"
echo
echo "Change dir"
cd "/workspace/cloudbuild/" || exit 9
echo
echo "Create .netrc and set authorization..."
echo > "/builder/home/.netrc" || exit 9
chmod 600 "/builder/home/.netrc" || exit 9
echo
echo "Initialize..."
echo
echo "Try logging in again with SSH to get the Host Key..."
ssh -o 'StrictHostKeyChecking=accept-new' -i '/workspace/ssh.key' ansible@$(cat "/workspace/nat_ip.txt") uptime || exit 9
echo
echo "Run Ansible Playbook..."
ansible-playbook "setup.yml" \
-u "ansible" \
-e "ansible_ssh_private_key_file=/workspace/ssh.key" \
-e "username=$(jq -er '.username' < "/workspace/data.json")" \
-e "password=\"$(jq -er '.password' < "/workspace/data.json")\"" \
-i $(cat "/workspace/nat_ip.txt"), || exit 9
tags: ["create-gce-vm"]
timeout: 1200s