Merge pull request #6079 from hashicorp/do_5866

add user data and user data file to oracle oci builder
This commit is contained in:
Megan Marsh 2018-04-02 16:48:48 -07:00 committed by GitHub
commit 67d2de8de1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 0 deletions

View File

@ -1,8 +1,11 @@
package oci
import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
@ -41,6 +44,9 @@ type Config struct {
BaseImageID string `mapstructure:"base_image_ocid"`
Shape string `mapstructure:"shape"`
ImageName string `mapstructure:"image_name"`
// UserData and UserDataFile file are both optional and mutually exclusive.
UserData string `mapstructure:"user_data"`
UserDataFile string `mapstructure:"user_data_file"`
// Networking
SubnetID string `mapstructure:"subnet_ocid"`
@ -195,6 +201,30 @@ func NewConfig(raws ...interface{}) (*Config, error) {
}
}
// Optional UserData config
if c.UserData != "" && c.UserDataFile != "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Only one of user_data or user_data_file can be specified."))
} else if c.UserDataFile != "" {
if _, err := os.Stat(c.UserDataFile); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("user_data_file not found: %s", c.UserDataFile))
}
}
// read UserDataFile into string.
if c.UserDataFile != "" {
fiData, err := ioutil.ReadFile(c.UserDataFile)
if err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("Problem reading user_data_file: %s", err))
}
c.UserData = string(fiData)
}
// Test if UserData is encoded already, and if not, encode it
if c.UserData != "" {
if _, err := base64.StdEncoding.DecodeString(c.UserData); err != nil {
log.Printf("[DEBUG] base64 encoding user data...")
c.UserData = base64.StdEncoding.EncodeToString([]byte(c.UserData))
}
}
if errs != nil && len(errs.Errors) > 0 {
return nil, errs
}

View File

@ -35,6 +35,9 @@ func (d *driverOCI) CreateInstance(publicKey string) (string, error) {
"ssh_authorized_keys": publicKey,
},
}
if d.cfg.UserData != "" {
params.Metadata["user_data"] = d.cfg.UserData
}
instance, err := d.client.Compute.Instances.Launch(params)
if err != nil {
return "", err

View File

@ -125,6 +125,15 @@ builder.
- `use_private_ip` (boolean) - Use private ip addresses to connect to the instance via ssh.
- `user_data` (string) - user_data to be used by cloud
init. See [the Oracle docs](https://docs.us-phoenix-1.oraclecloud.com/api/#/en/iaas/20160918/LaunchInstanceDetails) for more details. Generally speaking, it is easier to use the `user_data_file`,
but you can use this option to put either the platintext data or the base64
encoded data directly into your Packer config.
- `user_data_file` (string) - Path to a file to be used as user_data by cloud
init. See [the Oracle docs](https://docs.us-phoenix-1.oraclecloud.com/api/#/en/iaas/20160918/LaunchInstanceDetails) for more details. Example:
`"user_data_file": "./boot_config/myscript.sh"`
## Basic Example