Skip to content

Commit 312f454

Browse files
authored
Merge pull request #59 from IBM/classic-userdata
Add support for userdata on classic infrastructure
2 parents 70a674c + 718fbf6 commit 312f454

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

.github/workflows/greetings.yml

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
name: Greetings
22

3-
on: [pull_request, issues]
4-
3+
on:
4+
issues:
5+
types: [opened]
6+
pull_request_target:
7+
types: [opened, reopened]
8+
59
jobs:
610
greeting:
711
runs-on: ubuntu-latest
812
permissions:
913
issues: write
1014
pull-requests: write
1115
steps:
12-
- uses: actions/first-interaction@v1
16+
- name: 'Greet the contributor'
17+
uses: garg3133/welcome-new-contributors@v1.2
1318
with:
1419
repo-token: ${{ secrets.GITHUB_TOKEN }}
15-
issue-message: 'Thank you so much for reporting an issue'
16-
pr-message: 'Thank you so much for contributing to this project with your first pull request'
20+
issue-message: 'Hi, @${{ github.actor }}. Thank you so much for reporting an issue.'
21+
pr-message: 'Hi, @${{ github.actor }}. Thank you so much for contributing to this project. Keep making such awesome contributions!'

builder/ibmcloud/classic/client.go

+10
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ type InstanceType struct {
4949
BaseImageId string
5050
BaseOsCode string
5151
PublicSecurityGroupIds []int64
52+
UserData []Attribute
53+
UserDataCount uint64
5254
}
5355

5456
type InstanceReq struct {
@@ -68,6 +70,8 @@ type InstanceReq struct {
6870
BlockDeviceTemplateGroup *BlockDeviceTemplateGroup `json:"blockDeviceTemplateGroup,omitempty"`
6971
OsReferenceCode string `json:"operatingSystemReferenceCode,omitempty"`
7072
SshKeys []*SshKey `json:"sshKeys,omitempty"`
73+
UserData []Attribute `json:"userData,omitempty"`
74+
UserDataCount uint64 `json:"userDataCount,omitempty"`
7175
}
7276

7377
type InstanceImage struct {
@@ -128,6 +132,10 @@ type SecurityGroup struct {
128132
Id int64 `json:"id,omitempty"`
129133
}
130134

135+
type Attribute struct {
136+
Value string `json:"value,omitempty"`
137+
}
138+
131139
func (s SoftlayerClient) New(user string, key string) *SoftlayerClient {
132140
return &SoftlayerClient{
133141
http: &http.Client{
@@ -302,6 +310,8 @@ func (s SoftlayerClient) CreateInstance(instance InstanceType) (map[string]inter
302310
MaxSpeed: instance.NetworkSpeed,
303311
},
304312
},
313+
UserData: instance.UserData,
314+
UserDataCount: instance.UserDataCount,
305315
}
306316

307317
if instance.ProvisioningSshKeyId != 0 {

builder/ibmcloud/classic/config.go

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package classic
44
import (
55
"errors"
66
"fmt"
7+
"os"
78
"time"
89

910
"github.com/hashicorp/packer-plugin-sdk/common"
@@ -38,6 +39,7 @@ type Config struct {
3839
InstanceNetworkSpeed int `mapstructure:"instance_network_speed"`
3940
ProvisioningSshKeyId int64 `mapstructure:"provisioning_ssh_key_id"`
4041
InstancePublicSecurityGroupIds []int64 `mapstructure:"public_security_groups"`
42+
UserDataFilePath string `mapstructure:"user_data_file_path"`
4143

4244
RawStateTimeout string `mapstructure:"instance_state_timeout"`
4345
StateTimeout time.Duration `mapstructure-to-hcl2:",skip"`
@@ -157,6 +159,13 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
157159
}
158160
c.StateTimeout = stateTimeout
159161

162+
if c.UserDataFilePath != "" {
163+
if _, err := os.ReadFile(c.UserDataFilePath); err != nil {
164+
errs = packer.MultiErrorAppend(
165+
errs, fmt.Errorf("[ERROR] Error reading user data file. Error: %s", err))
166+
}
167+
}
168+
160169
//log.Println(common.ScrubConfig(self.config, c.APIKey, c.Username))
161170

162171
if errs != nil && len(errs.Errors) > 0 {

builder/ibmcloud/classic/config.hcl2spec.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/ibmcloud/classic/step_create_instance.go

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"os"
78

89
"github.com/hashicorp/packer-plugin-sdk/multistep"
910
"github.com/hashicorp/packer-plugin-sdk/packer"
@@ -46,6 +47,17 @@ func (s *stepCreateInstance) Run(_ context.Context, state multistep.StateBag) mu
4647
BaseOsCode: config.BaseOsCode,
4748
}
4849

50+
if config.UserDataFilePath != "" {
51+
if userDataBytes, err := os.ReadFile(config.UserDataFilePath); err == nil {
52+
instanceDefinition.UserData = []Attribute{{Value: string(userDataBytes)}}
53+
instanceDefinition.UserDataCount += 1
54+
} else {
55+
ui.Error(err.Error())
56+
state.Put("error", err)
57+
return multistep.ActionHalt
58+
}
59+
}
60+
4961
ui.Say("Creating an instance...")
5062
instanceData, err := client.CreateInstance(*instanceDefinition)
5163
if err != nil {

0 commit comments

Comments
 (0)