Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ssh key configuration option, fixed example config in readme #1

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ HOSTNAME=coolpeoplenetworks
NAMESPACE=com
NAME=smartos
BINARY=terraform-provider-${NAME}
VERSION=0.1.0
VERSION=0.1.1
OS_ARCH=darwin_amd64

default: install
Expand Down Expand Up @@ -34,4 +34,4 @@ test:
echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4

testacc:
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
157 changes: 41 additions & 116 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ NOTE: Currently, this provider only supports a subset of properties for SmartOS

```hcl
provider "smartos" {
"host" = "10.99.50.60:22"
"user" = "root"
hosts = {
"node01" = "10.99.50.60"
}
user = "root"
sshkey = "~/.ssh/id_ed25519"
}
```

The following arguments are supported.

- `host` - (Required) This is the address of the global zone on the SmartOS host.
- `hosts` - (Required) These are the addresses of the global zone on the SmartOS hosts.
- `user` - (Required) This is the authenticated SSH user which will run provisioning commands. Normally this is 'root'.
- `sshkey` - (Required) This is the private SSH key of the provisioning user. Only passwordless keys work at the moment.

### Resources and Data Providers ###

Expand All @@ -48,91 +52,53 @@ Many of the properties defined in the man page are not yet supported by the prov

### Example ###

The following example shows you how to configure two simple zones - one running Illumos (base-64-lts from Joyent) and the other running Ubuntu 16.04.

(See the included sample.tf)
The following example shows you how to configure a bhyve VM running provided Debian 11 image.

```hcl
provider "smartos" {
"host" = "10.99.50.60:22"
"user" = "root"
}

data "smartos_image" "illumos" {
"name" = "base-64-lts"
"version" = "18.4.0"
}

data "smartos_image" "linux" {
"name" = "ubuntu-16.04"
"version" = "20170403"
hosts = {
"node01" = "10.99.50.60"
}
user = "root"
sshkey = "~/.ssh/id_ed25519"
}

data "smartos_image" "linux_kvm" {
"name" = "ubuntu-certified-16.04"
"version" = "20190212"
data "smartos_image" "bhyve_debian11" {
node_name = "node01"
name = "debian-11"
version = "20220228"
}

resource "smartos_machine" "illumos" {
"alias" = "illumos"
"brand" = "joyent"
"cpu_cap" = 100
resource "smartos_machine" "linux-byve" {
node_name = "node01"
alias = "provider-test-linux-bhyve"
brand = "bhyve"
vcpus = 2

# These fields are required in order for provisioning (below) to function.
"customer_metadata" = {
"root_authorized_keys" = "... copy this from your ~/.ssh/id_rsa.pub ..."
"user-script" = "/usr/sbin/mdata-get root_authorized_keys > ~root/.ssh/authorized_keys"
customer_metadata = {
"root_authorized_keys" = "ssh-ed25519 AAAA......."
}

"image_uuid" = "${data.smartos_image.illumos.id}"
"maintain_resolvers" = true
"max_physical_memory" = 512
"nics" = [
{
"nic_tag" = "external"
"ips" = ["10.0.222.222/16"]
"gateways" = ["10.0.0.1"]
"interface" = "net4"
maintain_resolvers = true
ram = 512
nics {
nic_tag = "admin"
ips = ["192.168.0.10/24"]
gateways = ["192.168.0.1"]
vlan_id = "10"
interface = "net0"
model = "virtio"
}
]
"quota" = 25
quota = 25

"resolvers" = ["1.1.1.1", "1.0.0.1"]
resolvers = ["1.0.0.1", "1.1.1.1"]

provisioner "remote-exec" {
inline = [
"pkgin -y update",
"pkgin -y in htop",
]
}
}

resource "smartos_machine" "linux" {
"alias" = "provider-test-linux"
"brand" = "lx"
"kernel_version" = "3.16.0"
"cpu_cap" = 100

"customer_metadata" = {
# Note: this is my public SSH key...use your own. :-)
"root_authorized_keys" = "... copy this from your ~/.ssh/id_rsa.pub ..."
"user-script" = "/usr/sbin/mdata-get root_authorized_keys > ~root/.ssh/authorized_keys"
}

"image_uuid" = "${data.smartos_image.linux.id}"
"maintain_resolvers" = true
"max_physical_memory" = 512
"nics" = [
{
"nic_tag" = "external"
"ips" = ["10.0.222.223/16"]
"gateways" = ["10.0.0.1"]
"interface" = "net5"
disks {
boot = true
image_uuid = "${data.smartos_image.bhyve_debian11.id}"
compression = "lz4"
model = "virtio"
}
]
"quota" = 25

"resolvers" = ["1.1.1.1", "1.0.0.1"]

provisioner "remote-exec" {
inline = [
Expand All @@ -142,50 +108,9 @@ resource "smartos_machine" "linux" {
}
}

resource "smartos_machine" "linux-kvm" {
"alias" = "provider-test-linux-kvm"
"brand" = "kvm"
"kernel_version" = "3.16.0"
"vcpus" = 2

"customer_metadata" = {
# Note: this is my public SSH key...use your own. :-)
"root_authorized_keys" = "... copy this from your ~/.ssh/id_rsa.pub ..."
}

"maintain_resolvers" = true
"ram" = 512
"nics" = [
{
"nic_tag" = "external"
"ips" = ["10.0.222.224/16"]
"gateways" = ["10.0.0.1"]
"interface" = "net0"
"model" = "virtio"
}
]
"quota" = 25

"resolvers" = ["1.1.1.1", "1.0.0.1"]

"disks" = [
{
"boot" = true
"image_uuid" = "${data.smartos_image.linux_kvm.id}"
"compression" = "lz4"
"model" = "virtio"
}
]

provisioner "remote-exec" {
inline = [
"apt-get update",
"apt-get -y install htop",
]
}
}

```

Links:
https://learn.hashicorp.com/tutorials/terraform/provider-release-publish?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS
https://learn.hashicorp.com/tutorials/terraform/provider-release-publish?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS
6 changes: 6 additions & 0 deletions smartos/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func providerSchema() map[string]*schema.Schema {
Required: true,
Description: "User to authenticate with.",
},
"sshkey": {
Type: schema.TypeString,
Required: true,
Description: "User's private SSH key.",
},
}
}

Expand Down Expand Up @@ -63,6 +68,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
agentConnection: agentConnection,
authMethods: authMethods,
clients: make(map[string]*ssh.Client),
sshkey: d.Get("sshkey").(string),
}

return &client, nil
Expand Down
15 changes: 14 additions & 1 deletion smartos/smartos_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net"
"regexp"
"io/ioutil"

"github.com/google/uuid"
"golang.org/x/crypto/ssh"
Expand All @@ -18,6 +19,7 @@ type SmartOSClient struct {
clients map[string]*ssh.Client
agentConnection net.Conn
authMethods []ssh.AuthMethod
sshkey string
}

func (c *SmartOSClient) Connect(nodeName string) error {
Expand All @@ -27,9 +29,20 @@ func (c *SmartOSClient) Connect(nodeName string) error {
return nil
}

keyfile, err := ioutil.ReadFile(c.sshkey)
if err != nil {
log.Println("SSH: Can't read key: ", err.Error())
}

keyparser, err := ssh.ParsePrivateKey(keyfile)
if err != nil {
log.Println("SSH: Can't parse key: ", err.Error())
}


config := &ssh.ClientConfig{
User: c.user,
Auth: c.authMethods,
Auth: []ssh.AuthMethod{ssh.PublicKeys(keyparser),},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}

Expand Down