Skip to content

Commit

Permalink
Add Vultr terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
muratugureminoglu committed Dec 25, 2024
1 parent 1cd1856 commit c8aaa28
Show file tree
Hide file tree
Showing 9 changed files with 1,805 additions and 0 deletions.

Large diffs are not rendered by default.

Binary file not shown.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
Terraform Provider for Vultr
==================

- Quickstart Guide: [How to Provision a Vultr Cloud Server with Terraform and Cloud-Init](https://www.vultr.com/docs/provision-a-vultr-cloud-server-with-terraform-and-cloud-init/)
- Vultr Website: https://www.vultr.com
- Terraform Website: https://www.terraform.io

<img src="https://raw.githubusercontent.com/hashicorp/terraform-website/master/public/img/logo-hashicorp.svg" width="600px">

Requirements
------------

- [Terraform](https://www.terraform.io/downloads.html) 0.12.x+
- [Go](https://golang.org/doc/install) 1.22.x+ (to build the provider plugin)

Building The Provider
---------------------

Clone repository to: `$GOPATH/src/github.com/vultr/terraform-provider-vultr`

``` sh
$ mkdir -p $GOPATH/src/github.com/vultr; cd $GOPATH/src/github.com/vultr
$ git clone [email protected]:vultr/terraform-provider-vultr.git
```

Enter the provider directory and build the provider

``` sh
$ cd $GOPATH/src/github.com/vultr/terraform-provider-vultr
$ make build
```

Using the provider
----------------------

See the [Vultr Provider documentation](website/docs/index.html.markdown) to get started using the Vultr provider.

Please read about [V2 changes from V1](example/V2Changes.md) for a list of new changes made to the Vultr Terraform Provider

### Installation
In your terraform config, define `vultr/vultr` in your `required_providers` and set your API key:
``` hcl
terraform {
required_providers {
vultr = {
source = "vultr/vultr"
version = "2.22.1"
}
}
}
provider "vultr" {
api_key = "..."
}
```

After, run `terraform init` to install the provider.

#### Manual Installation
If you don't have internet access on the target machine or prefer to install
the provider locally follow the steps for the relevant operating system and architecture.

1) [Download](https://github.com/vultr/terraform-provider-vultr/releases) and
extract the binary for your system and architecture

2) Make the local plugins directory:

On linux
``` sh
export VERSION=2.22.1 OS=linux ARCH=amd64
mkdir -p ~/.terraform.d/plugins/local/vultr/vultr/${VERSION}/${OS}_${ARCH}/'
```
On mac
``` sh
export VERSION=2.22.1 OS=darwin ARCH=arm64
mkdir -p ~/.terraform.d/plugins/local/vultr/vultr/${VERSION}/${OS}_${ARCH}/'
```

3) Rename and copy the binary into the local plugins directory:
``` sh
mv terraform-provider-vultr_v${VERSION} ~/.terraform.d/plugins/local/vultr/vultr/${VERSION}/${OS}_${ARCH}/terraform-provider-vultr_v${VERSION}
```

4) Add the local provider to your terraform `main.tf` config:
``` hcl
terraform {
required_providers {
vultr = {
source = "local/vultr/vultr"
version = "2.22.1"
}
}
}
provider "vultr" {
api_key = "..."
}
```

5) Test everything by running `terraform init` in that same directory.

Developing the Provider
---------------------------

If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (version 1.11+ is *required*). You'll also need to correctly setup a [GOPATH](http://golang.org/doc/code.html#GOPATH), as well as adding `$GOPATH/bin` to your `$PATH`.

To compile the provider, run `make build`. This will build the provider and put the provider binary in the `$GOPATH/bin` directory.

``` sh
$ make build
...
$ $GOPATH/bin/terraform-provider-vultr
...
```

In order to test the provider, you can simply run `make test`.

``` sh
$ make test
```

In order to run the full suite of acceptance tests, run `make testacc`.

*Note:* Acceptance tests create real resources, and often cost money to run.

``` sh
$ make testacc
```

In order to run a specific acceptance test, use the `TESTARGS` environment variable. For example, the following command will run `TestAccVultrUser_base` acceptance test only:

``` sh
$ make testacc TESTARGS='-run=TestAccVultrUser_base'
```
Binary file not shown.
101 changes: 101 additions & 0 deletions terraform/vultr/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#resource "vultr_instance" "enterprise" {
# plan = var.vultr_instance
# region = var.vultr_region
# os_id = 2284
# label = var.resource_group_name_community
#}

resource "vultr_ssh_key" "key" {
name = "vultr-ssh-key"
ssh_key = file(var.publickeypath)
}

resource "vultr_instance" "community" {
plan = var.vultr_instance
region = var.vultr_region
os_id = 2284
label = var.resource_group_name_community
ssh_key_ids = [vultr_ssh_key.key.id]
}


resource "null_resource" "ams-marketplace-setup_community" {
provisioner "remote-exec" {
connection {
type = "ssh"
user = var.user
host = vultr_instance.community.main_ip
private_key = file(var.privatekeypath)
}
inline = [
"sudo sed -i 's/#\\$nrconf{kernelhints} = -1;/\\$nrconf{kernelhints} = -1;/g' /etc/needrestart/needrestart.conf",
"echo 'NEEDRESTART_SUSPEND=1' >> /etc/environment",
"sudo source /etc/environment",
"while sudo fuser /var/lib/dpkg/lock >/dev/null 2>&1; do sleep 1; done",
"while sudo fuser /var/lib/apt/lists/lock >/dev/null 2>&1; do sleep 1; done",
"sudo apt-get update -y",
"sudo apt-get dist-upgrade -y",
"wget https://raw.githubusercontent.com/ant-media/Scripts/master/install_ant-media-server.sh",
"curl -L -o ant-media-server-community.zip https://github.com/ant-media/Ant-Media-Server/releases/download/ams-v${var.ams_version}/ant-media-server-community-${var.ams_version}.zip",
"sudo bash ./install_ant-media-server.sh -i ant-media-server-community.zip",
"sudo sed -i 's/server.marketplace=.*/server.marketplace=azure/g' /usr/local/antmedia/conf/red5.properties",
"sudo systemctl stop antmedia",
"sudo rm -rf /usr/local/antmedia/conf/instanceId",
"sudo rm -rf /usr/local/antmedia/*.db.* && sudo rm -rf /usr/local/antmedia/*.db",
"sudo rm -rf /root/*.zip && sudo rm -rf /root/install*",
"sudo rm -rf /root/.ssh",
"history -c",
"sudo shutdown -h now",

]
}
depends_on = [
vultr_instance.community
]

}

resource "vultr_firewall_group" "my_firewall_grp" {
description = "Firewall"
}
resource "vultr_firewall_rule" "allow_http" {
firewall_group_id = vultr_firewall_group.my_firewall_grp.id
protocol = "tcp"
ip_type = "v4"
subnet = "0.0.0.0"
subnet_size = 0
port = "5080"
}
resource "vultr_firewall_rule" "allow_https" {
firewall_group_id = vultr_firewall_group.my_firewall_grp.id
protocol = "tcp"
ip_type = "v4"
subnet = "0.0.0.0"
subnet_size = 0
port = "5443"
}
resource "vultr_firewall_rule" "allow_ssh" {
firewall_group_id = vultr_firewall_group.my_firewall_grp.id
protocol = "tcp"
ip_type = "v4"
subnet = "0.0.0.0"
subnet_size = 0
port = "22"
}

resource "vultr_snapshot" "snapshot" {
instance_id = resource.vultr_instance.community.id


timeouts {
create = "60m"
}
depends_on = [
null_resource.ams-marketplace-setup_community
]

}

output "ipv4_address" {
value = vultr_instance.community.main_ip
}
15 changes: 15 additions & 0 deletions terraform/vultr/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
terraform {
required_providers {
vultr = {
source = "vultr/vultr"
version = "2.23.1"
}
}
}

# Configure the Vultr Provider
provider "vultr" {
api_key = var.vultr_token
rate_limit = 100
retry_limit = 3
}
50 changes: 50 additions & 0 deletions terraform/vultr/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
variable "vultr_token" {
type = string
}

variable "vultr_region" {
type = string
default = "fra"
}

variable "vultr_instance" {
type = string
default = "vc2-2c-4gb"
}

variable "resource_group_name_enterprise" {
default = "enterprise"
description = "Name"
}


variable "resource_group_name_community" {
default = "community"
description = "Name"
}

variable "publickeypath" {
type = string
default = "./ssh/id_rsa.pub"
}

variable "privatekeypath" {
type = string
default = "./ssh/id_rsa"
}

variable "user" {
type = string
default = "root"

}

variable "ams_version" {
type = string
description = "AMS Version"
}

variable "zip_file_id" {
description = "Google drive ID"
type = string
}

0 comments on commit c8aaa28

Please sign in to comment.