Skip to content

Commit

Permalink
Initial working example. (#1)
Browse files Browse the repository at this point in the history
* Initial working example.

* Add READMEs.

* Remove comments from example in main README.
  • Loading branch information
clstokes authored Feb 10, 2018
1 parent 664c84a commit 2d88b42
Show file tree
Hide file tree
Showing 11 changed files with 502 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# terraform #
#############
.terraform
*.tfstate*
terraform.tfvars

# packer #
##########
*-vars.json
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Triton Presto Terraform Module

A Terraform module to create a [Redash](https://prestodb.io/) server. Redash can then be used to
query a [Presto cluster](https://github.com/joyent/terraform-triton-presto) and Triton Object Storage data.

## Usage

```hcl
data "triton_image" "ubuntu" {
name = "ubuntu-16.04"
type = "lx-dataset"
most_recent = true
}
data "triton_network" "public" {
name = "Joyent-SDC-Public"
}
data "triton_network" "private" {
name = "My-Fabric-Network"
}
module "bastion" {
source = "github.com/joyent/terraform-triton-bastion"
name = "redash-basic-with-provisioning"
image = "${data.triton_image.ubuntu.id}"
package = "g4-general-4G"
networks = [
"${data.triton_network.public.id}",
"${data.triton_network.private.id}",
]
}
module "redash" {
source = "github.com/joyent/terraform-triton-redash"
name = "redash-basic-with-provisioning"
image = "${data.triton_image.ubuntu.id}"
package = "g4-general-4G"
networks = [
"${data.triton_network.public.id}",
"${data.triton_network.private.id}",
]
provision = "true"
private_key_path = "${var.private_key_path}"
client_access = ["any"]
bastion_host = "${element(module.bastion.bastion_ip,0)}"
bastion_user = "${module.bastion.bastion_user}"
bastion_role_tag = "${module.bastion.bastion_role_tag}"
}
```

## Examples
- [basic-with-provisioning](examples/basic-with-provisioning) - Deploys a Redash server. Redash server
will be _provisioned_ by Terraform.
- _Note: This method with Terraform provisioning is only recommended for prototyping and light testing._

## Resources created

- [`triton_machine.redash`](https://www.terraform.io/docs/providers/triton/r/triton_machine.html): The Redash machine.
- [`triton_firewall_rule.ssh`](https://www.terraform.io/docs/providers/triton/r/triton_firewall_rule.html): The firewall
rule(s) allowing SSH access FROM the bastion machine(s) TO the Redash machine.
- [`triton_firewall_rule.client_access`](https://www.terraform.io/docs/providers/triton/r/triton_firewall_rule.html): The
firewall rule(s) allowing access FROM client machines or addresses TO Redash web ports.
- [`triton_firewall_rule.redash_to_presto_coordinator`](https://www.terraform.io/docs/providers/triton/r/triton_firewall_rule.html): The
firewall rule(s) allowing access FROM the Redash machine TO Presto coordinator web ports.
23 changes: 23 additions & 0 deletions examples/basic-with-provisioning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Redash with Provisioning

Creates one Redash machine. Terraform to create and manage the infrastructure resources and provision the machines.

> :warning: _Note: This method with Terraform provisioning is only recommended for prototyping and light testing._
## Usage

Initialize and create the environment:

```
terraform init
terraform plan
terraform apply
```

## Cleanup

Remove all resources created by Terraform:

```
terraform destroy
```
56 changes: 56 additions & 0 deletions examples/basic-with-provisioning/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#
# Data Sources
#
data "triton_image" "ubuntu" {
name = "ubuntu-16.04"
type = "lx-dataset"
most_recent = true
}

data "triton_network" "public" {
name = "Joyent-SDC-Public"
}

data "triton_network" "private" {
name = "My-Fabric-Network"
}

#
# Modules
#
module "bastion" {
source = "github.com/joyent/terraform-triton-bastion"

name = "redash-basic-with-provisioning"
image = "${data.triton_image.ubuntu.id}"
package = "g4-general-4G"

# Public and Private
networks = [
"${data.triton_network.public.id}",
"${data.triton_network.private.id}",
]
}

module "redash" {
source = "../../"

name = "redash-basic-with-provisioning"
image = "${data.triton_image.ubuntu.id}" # note: using the UBUNTU image here
package = "g4-general-4G"

# Public and Private
networks = [
"${data.triton_network.public.id}",
"${data.triton_network.private.id}",
]

provision = "true" # note: we ARE provisioning as we are NOT using pre-built images.
private_key_path = "${var.private_key_path}"

client_access = ["any"]

bastion_host = "${element(module.bastion.bastion_ip,0)}"
bastion_user = "${module.bastion.bastion_user}"
bastion_role_tag = "${module.bastion.bastion_role_tag}"
}
14 changes: 14 additions & 0 deletions examples/basic-with-provisioning/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# Outputs
#
output "bastion_ip" {
value = ["${module.bastion.bastion_ip}"]
}

output "redash_ip" {
value = ["${module.redash.redash_ip}"]
}

output "redash_address" {
value = ["${module.redash.redash_address}"]
}
7 changes: 7 additions & 0 deletions examples/basic-with-provisioning/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#
# Variables
#
variable "private_key_path" {
description = "The path to the private key to use for provisioning machines."
type = "string"
}
75 changes: 75 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#
# Terraform/Providers
#
terraform {
required_version = ">= 0.11.0"
}

provider "triton" {
version = ">= 0.4.1"
}

#
# Data sources
#
data "triton_datacenter" "current" {}

data "triton_account" "current" {}

#
# Locals
#
locals {
redash_address = "${var.cns_service_name_redash}.svc.${data.triton_account.current.id}.${data.triton_datacenter.current.name}.${var.cns_fqdn_base}"
presto_coordinator_address = "${var.cns_service_name_presto_coordinator}.svc.${data.triton_account.current.id}.${data.triton_datacenter.current.name}.${var.cns_fqdn_base}"
}

#
# Machines
#
resource "triton_machine" "redash" {
name = "${var.name}-redash"
package = "${var.package}"
image = "${var.image}"

firewall_enabled = true

networks = ["${var.networks}"]

tags {
role = "${var.role_tag}"
}

cns {
services = ["${var.cns_service_name_redash}"]
}

metadata {
version_redash = "${var.version_redash}"
}
}

#
# Firewall Rules
#
resource "triton_firewall_rule" "ssh" {
rule = "FROM tag \"role\" = \"${var.bastion_role_tag}\" TO tag \"role\" = \"${var.role_tag}\" ALLOW tcp PORT 22"
enabled = true
description = "${var.name} - Allow access from bastion hosts to Redash servers."
}

resource "triton_firewall_rule" "client_access" {
count = "${length(var.client_access)}"

rule = "FROM ${var.client_access[count.index]} TO tag \"role\" = \"${var.role_tag}\" ALLOW tcp PORT 80"
enabled = true
description = "${var.name} - Allow access from clients to Redash servers."
}

resource "triton_firewall_rule" "redash_to_presto_coordinator" {
count = "${length(var.client_access)}"

rule = "FROM ${var.client_access[count.index]} TO tag \"triton.cns.services\" = \"${var.cns_service_name_presto_coordinator}\" ALLOW tcp PORT 8080"
enabled = true
description = "${var.name} - Allow access from Redash to Presto servers."
}
14 changes: 14 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# Outputs
#
output "redash_ip" {
value = ["${triton_machine.redash.*.primaryip}"]
}

output "redash_role_tag" {
value = "${var.role_tag}"
}

output "redash_address" {
value = "${local.redash_address}"
}
96 changes: 96 additions & 0 deletions packer/scripts/install_redash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/bash
#
# Installs Redash with some customizations specific to the overall project.
#
# Note: Generally follows guidelines at https://web.archive.org/web/20170701145736/https://google.github.io/styleguide/shell.xml.
#

set -e

# check_prerequisites - exits if distro is not supported.
#
# Parameters:
# None.
function check_prerequisites() {
local distro
if [[ -f "/etc/lsb-release" ]]; then
distro="Ubuntu"
fi

if [[ -z "${distro}" ]]; then
log "Unsupported platform. Exiting..."
exit 1
fi
}

# install_dependencies - installs dependencies
#
# Parameters:
# $1: the name of the distribution.
function install_dependencies() {
log "Updating package index..."
apt-get -qq -y update
log "Upgrading existing packages"
apt-get -qq -y upgrade
log "Installing prerequisites..."
apt-get -qq -y install --no-install-recommends \
wget
}

# check_arguments - exits if prerequisites are NOT satisfied
#
# Parameters:
# $1: the version of redash
function check_arguments() {
local -r version_redash=${1}

if [[ -z "${version_redash}" ]]; then
log "No Redash version provided. Exiting..."
exit 1
fi

}

# install - downloads and installs the specified tool and version
#
# Parameters:
# $1: the version of redash
function install_redash() {
local -r version_redash=${1}

local -r path_file="redash_bootstrap.sh"

log "Downloading Redash install script..."
wget -q -O ${path_file} "https://raw.githubusercontent.com/getredash/redash/master/setup/ubuntu/bootstrap.sh"

log "Installing Redash ${version_redash}..."
REDASH_VERSION=${version_redash} sh ${path_file}

}

# log - prints an informational message
#
# Parameters:
# $1: the message
function log() {
local -r message=${1}
local -r script_name=$(basename ${0})
echo -e "==> ${script_name}: ${message}"
}

# main
function main() {
check_prerequisites

local -r arg_version_redash=$(mdata-get 'version_redash')
check_arguments \
${arg_version_redash}

install_dependencies
install_redash \
${arg_version_redash}

log "Done."
}

main
Loading

0 comments on commit 2d88b42

Please sign in to comment.