Skip to content

Commit

Permalink
Updates tests, adds test helper scripts (#39)
Browse files Browse the repository at this point in the history
* Updates tests, adds test helper scripts

* removes extra step not required for k3s

* bump drone's tf version

* Bumps TF URL -- 0.12.18

* Adds test status to README

* updates scp path

* Adds default plans to baremetal_0 to drone
  • Loading branch information
jmarhee authored Mar 18, 2020
1 parent 3b5bcf1 commit fed9b6b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
30 changes: 22 additions & 8 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,40 @@ name: default

steps:

- name: Deploy
- name: Deploy K3s
image: alpine
environment:
PACKET_TOKEN:
from_secret: TF_VAR_auth_token
TF_VAR_auth_token:
from_secret: TF_VAR_auth_token
TF_VAR_count:
from_secret: TF_VAR_count
TF_VAR_project_id:
from_secret: TF_VAR_project_id
TF_VAR_plan_primary:
from_secret: TF_VAR_plan_primary
TF_VAR_plan_node:
from_secret: TF_VAR_plan_node
TF_VAR_facility:
from_secret: TF_VAR_facility
commands:
- apk --update add curl
- curl --silent --output terraform.zip "https://releases.hashicorp.com/terraform/0.12.6/terraform_0.12.6_linux_amd64.zip"
- apk --update add curl openssh-client jq
- ssh-keygen -t rsa -b 4096 -C "jmarhee+drone@packet.com" -f drone-key-$DRONE_COMMIT -P ""
- PACKET_TOKEN=$PACKET_TOKEN KEY_NAME="$DRONE_COMMIT" PUB_KEY="$(cat drone-key-$DRONE_COMMIT.pub)" sh scripts/test_functions-add.sh
- export TF_VAR_ssh_private_key_path=drone-key-$DRONE_COMMIT
- export TF_VAR_cluster_name=$DRONE_COMMIT
- curl --silent --output terraform.zip "https://releases.hashicorp.com/terraform/0.12.18/terraform_0.12.18_linux_amd64.zip"
- unzip terraform.zip ; rm -f terraform.zip; chmod +x terraform
- mkdir -p ${HOME}/bin ; export PATH=${PATH}:${HOME}/bin; mv terraform ${HOME}/bin/
- terraform -v
- terraform init
- terraform validate
- terraform apply -auto-approve
- if terraform apply -auto-approve; then echo "Terraform Applied"; else echo "Failed." ; terraform destroy -auto-approve; fi
- terraform destroy -auto-approve

- name: Clean Up
image: python
environment:
PACKET_TOKEN:
from_secret: TF_VAR_auth_token
commands:
- pip install requests
- export KEY_TAG=$DRONE_COMMIT
- PACKET_TOKEN=$PACKET_TOKEN KEY_TAG=$KEY_TAG python scripts/test_functions-delete.py
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
K3s on Packet
==

[![Build Status](https://cloud.drone.io/api/badges/packet-labs/packet-k3s/status.svg)](https://cloud.drone.io/packet-labs/packet-k3s)

This is a [Terraform](https://www.terraform.io/docs/providers/packet/index.html) project for deploying [K3s](https://k3s.io) on [Packet](https://packet.com).

This project configures your cluster with:
Expand Down
4 changes: 2 additions & 2 deletions modules/cluster_pool/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ resource "packet_device" "k3s_primary" {
user_data = data.template_file.controller[each.key].rendered

provisioner "local-exec" {
command = "scp -i ${var.ssh_private_key_path} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null scripts/create_bird_conf.sh root@${self.access_public_ipv4}:/root/create_bird_conf.sh"
command = "/usr/bin/scp -i ${var.ssh_private_key_path} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null scripts/create_bird_conf.sh root@${self.access_public_ipv4}:/root/create_bird_conf.sh"
}

billing_cycle = "hourly"
Expand Down Expand Up @@ -72,7 +72,7 @@ resource "packet_device" "worker_node" {
user_data = data.template_file.node[each.key].rendered

provisioner "local-exec" {
command = "scp -3 -i ${var.ssh_private_key_path} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -q root@${packet_device.k3s_primary[each.key].network.0.address}:/var/lib/rancher/k3s/server/node-token root@${self.access_public_ipv4}:node-token"
command = "/usr/bin/scp -3 -i ${var.ssh_private_key_path} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -q root@${packet_device.k3s_primary[each.key].network.0.address}:/var/lib/rancher/k3s/server/node-token root@${self.access_public_ipv4}:node-token"
}

billing_cycle = "hourly"
Expand Down
3 changes: 3 additions & 0 deletions scripts/test_functions-add.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/ash
# This script is not used by Terraform; this is soley to assist in the testing pipeline.
/usr/bin/curl -s -H 'Content-Type: application/json' -H "X-Auth-Token: $PACKET_TOKEN" https://api.packet.net/ssh-keys -d "{\"label\":\"$KEY_NAME\", \"key\":\"$(cat drone-key-$KEY_NAME.pub)\"}" -X POST | jq .id
17 changes: 17 additions & 0 deletions scripts/test_functions-delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import json
import requests
import os

# This is only used for the "Clean Up" pipeline stage in Drone-- If your name is not Drone, please stop.

PACKET_TOKEN = os.environ['PACKET_TOKEN']
KEY_TAG = os.environ['KEY_TAG']

response_keys = requests.get("https://api.packet.net/ssh-keys", headers={"X-Auth-Token":"%s" % (PACKET_TOKEN)}).text
keys = json.loads(response_keys)['ssh_keys']

for key in keys:
if key['label'].startswith(KEY_TAG):
print("Deleting %s" % key['id'])
d = requests.delete("https://api.packet.net/ssh-keys/%s" % (key['id']), headers={"X-Auth-Token":"%s" % (PACKET_TOKEN)})
print(d.text)

0 comments on commit fed9b6b

Please sign in to comment.