2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
|
|
|
|
2016-04-14 20:29:27 -04:00
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
2017-04-26 15:07:45 -04:00
|
|
|
"errors"
|
2016-04-14 20:29:27 -04:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
"github.com/hashicorp/errwrap"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2017-10-31 11:02:15 -04:00
|
|
|
tgo "github.com/joyent/triton-go"
|
2017-04-26 15:07:45 -04:00
|
|
|
"github.com/joyent/triton-go/authentication"
|
2017-10-31 11:02:15 -04:00
|
|
|
"github.com/joyent/triton-go/compute"
|
|
|
|
"github.com/joyent/triton-go/network"
|
2016-04-14 20:29:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// AccessConfig is for common configuration related to Triton access
|
|
|
|
type AccessConfig struct {
|
2019-05-28 11:50:58 -04:00
|
|
|
// The URL of the Triton cloud API to use. If omitted
|
|
|
|
// it will default to the us-sw-1 region of the Joyent Public cloud. If you
|
|
|
|
// are using your own private Triton installation you will have to supply the
|
|
|
|
// URL of the cloud API of your own Triton installation.
|
|
|
|
Endpoint string `mapstructure:"triton_url" required:"false"`
|
|
|
|
// The username of the Triton account to use when
|
|
|
|
// using the Triton Cloud API.
|
|
|
|
Account string `mapstructure:"triton_account" required:"true"`
|
|
|
|
// The username of a user who has access to your
|
|
|
|
// Triton account.
|
|
|
|
Username string `mapstructure:"triton_user" required:"false"`
|
|
|
|
// The fingerprint of the public key of the SSH key
|
|
|
|
// pair to use for authentication with the Triton Cloud API. If
|
|
|
|
// triton_key_material is not set, it is assumed that the SSH agent has the
|
|
|
|
// private key corresponding to this key ID loaded.
|
|
|
|
KeyID string `mapstructure:"triton_key_id" required:"true"`
|
|
|
|
// Path to the file in which the private key
|
|
|
|
// of triton_key_id is stored. For example /home/soandso/.ssh/id_rsa. If
|
|
|
|
// this is not specified, the SSH agent is used to sign requests with the
|
|
|
|
// triton_key_id specified.
|
|
|
|
KeyMaterial string `mapstructure:"triton_key_material" required:"false"`
|
|
|
|
//secure_skip_tls_verify - (bool) This allows skipping TLS verification
|
|
|
|
// of the Triton endpoint. It is useful when connecting to a temporary Triton
|
|
|
|
// installation such as Cloud-On-A-Laptop which does not generally use a
|
|
|
|
// certificate signed by a trusted root CA. The default is false.
|
|
|
|
InsecureSkipTLSVerify bool `mapstructure:"insecure_skip_tls_verify" required:"false"`
|
2017-04-26 15:07:45 -04:00
|
|
|
|
|
|
|
signer authentication.Signer
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|
|
|
|
|
2017-04-26 15:07:45 -04:00
|
|
|
// Prepare performs basic validation on the AccessConfig and ensures we can sign
|
|
|
|
// a request.
|
2016-04-14 20:29:27 -04:00
|
|
|
func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
if c.Endpoint == "" {
|
2017-04-26 15:07:45 -04:00
|
|
|
// Use Joyent public cloud as the default endpoint if none is specified
|
2017-04-26 18:20:44 -04:00
|
|
|
c.Endpoint = "https://us-sw-1.api.joyent.com"
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Account == "" {
|
2017-04-26 15:07:45 -04:00
|
|
|
errs = append(errs, errors.New("triton_account is required to use the triton builder"))
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.KeyID == "" {
|
2017-04-26 15:07:45 -04:00
|
|
|
errs = append(errs, errors.New("triton_key_id is required to use the triton builder"))
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|
|
|
|
|
2017-04-26 15:07:45 -04:00
|
|
|
if c.KeyMaterial == "" {
|
|
|
|
signer, err := c.createSSHAgentSigner()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
c.signer = signer
|
|
|
|
} else {
|
|
|
|
signer, err := c.createPrivateKeySigner()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
c.signer = signer
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-26 15:07:45 -04:00
|
|
|
func (c *AccessConfig) createSSHAgentSigner() (authentication.Signer, error) {
|
2017-12-29 08:00:47 -05:00
|
|
|
input := authentication.SSHAgentSignerInput{
|
|
|
|
KeyID: c.KeyID,
|
|
|
|
AccountName: c.Account,
|
|
|
|
Username: c.Username,
|
|
|
|
}
|
|
|
|
signer, err := authentication.NewSSHAgentSigner(input)
|
2016-04-14 20:29:27 -04:00
|
|
|
if err != nil {
|
2017-04-26 15:07:45 -04:00
|
|
|
return nil, fmt.Errorf("Error creating Triton request signer: %s", err)
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|
|
|
|
|
2017-04-26 15:07:45 -04:00
|
|
|
// Ensure we can sign a request
|
|
|
|
_, err = signer.Sign("Wed, 26 Apr 2017 16:01:11 UTC")
|
2016-04-14 20:29:27 -04:00
|
|
|
if err != nil {
|
2017-04-26 15:07:45 -04:00
|
|
|
return nil, fmt.Errorf("Error signing test request: %s", err)
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|
|
|
|
|
2017-04-26 15:07:45 -04:00
|
|
|
return signer, nil
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|
|
|
|
|
2017-04-26 15:07:45 -04:00
|
|
|
func (c *AccessConfig) createPrivateKeySigner() (authentication.Signer, error) {
|
|
|
|
var privateKeyMaterial []byte
|
|
|
|
var err error
|
2016-04-14 20:29:27 -04:00
|
|
|
|
|
|
|
// Check for keyMaterial being a file path
|
2017-04-26 15:07:45 -04:00
|
|
|
if _, err = os.Stat(c.KeyMaterial); err != nil {
|
|
|
|
privateKeyMaterial = []byte(c.KeyMaterial)
|
|
|
|
} else {
|
|
|
|
privateKeyMaterial, err = ioutil.ReadFile(c.KeyMaterial)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error reading key material from path '%s': %s",
|
|
|
|
c.KeyMaterial, err)
|
|
|
|
}
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|
|
|
|
|
2017-12-29 08:00:47 -05:00
|
|
|
input := authentication.PrivateKeySignerInput{
|
|
|
|
KeyID: c.KeyID,
|
|
|
|
AccountName: c.Account,
|
|
|
|
Username: c.Username,
|
|
|
|
PrivateKeyMaterial: privateKeyMaterial,
|
|
|
|
}
|
|
|
|
|
|
|
|
signer, err := authentication.NewPrivateKeySigner(input)
|
2016-04-14 20:29:27 -04:00
|
|
|
if err != nil {
|
2017-04-26 15:07:45 -04:00
|
|
|
return nil, fmt.Errorf("Error creating Triton request signer: %s", err)
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|
|
|
|
|
2017-04-26 15:07:45 -04:00
|
|
|
// Ensure we can sign a request
|
|
|
|
_, err = signer.Sign("Wed, 26 Apr 2017 16:01:11 UTC")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error signing test request: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return signer, nil
|
|
|
|
}
|
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
func (c *AccessConfig) CreateTritonClient() (*Client, error) {
|
|
|
|
|
|
|
|
config := &tgo.ClientConfig{
|
|
|
|
AccountName: c.Account,
|
|
|
|
TritonURL: c.Endpoint,
|
2017-12-29 08:00:47 -05:00
|
|
|
Username: c.Username,
|
2017-10-31 11:02:15 -04:00
|
|
|
Signers: []authentication.Signer{c.signer},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
2018-03-20 16:10:12 -04:00
|
|
|
config: config,
|
|
|
|
insecureSkipTLSVerify: c.InsecureSkipTLSVerify,
|
2017-10-31 11:02:15 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
2018-03-20 16:10:12 -04:00
|
|
|
config *tgo.ClientConfig
|
|
|
|
insecureSkipTLSVerify bool
|
2017-10-31 11:02:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Compute() (*compute.ComputeClient, error) {
|
|
|
|
computeClient, err := compute.NewClient(c.config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error Creating Triton Compute Client: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
2018-03-20 16:10:12 -04:00
|
|
|
if c.insecureSkipTLSVerify {
|
|
|
|
computeClient.Client.InsecureSkipTLSVerify()
|
|
|
|
}
|
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
return computeClient, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Network() (*network.NetworkClient, error) {
|
|
|
|
networkClient, err := network.NewClient(c.config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errwrap.Wrapf("Error Creating Triton Network Client: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
2018-03-20 16:10:12 -04:00
|
|
|
if c.insecureSkipTLSVerify {
|
|
|
|
networkClient.Client.InsecureSkipTLSVerify()
|
|
|
|
}
|
|
|
|
|
2017-10-31 11:02:15 -04:00
|
|
|
return networkClient, nil
|
2017-04-26 15:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AccessConfig) Comm() communicator.Config {
|
|
|
|
return communicator.Config{}
|
2016-04-14 20:29:27 -04:00
|
|
|
}
|