Skip to content

Commit

Permalink
Merge pull request #37 from mvle/ictf
Browse files Browse the repository at this point in the history
regexp, ssh password, image name
  • Loading branch information
juanpinzon authored Aug 13, 2021
2 parents 061fc3a + 39f1b84 commit 2a67637
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
29 changes: 27 additions & 2 deletions builder/ibmcloud/vpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type InstanceType struct {
ResourceGroupID string
VPCSSHKeyID string `json:",omitempty"`
VSIName string
VSIBaseImageID string `json:",omitempty"`
VSIBaseImageID string
VSIBaseImageName string
VSIProfile string `json:",omitempty"`
VSIInterface string `json:",omitempty"`
VSIUserDataFile string `json:",omitempty"`
Expand Down Expand Up @@ -184,7 +185,7 @@ func (client IBMCloudClient) getIAMToken(state multistep.StateBag) (map[string]i
func (client IBMCloudClient) VPCCreateInstance(instance InstanceType, state multistep.StateBag) (map[string]interface{}, error) {
ui := state.Get("ui").(packer.Ui)

validName, err := regexp.Compile(`[^A-Za-z0-9\-\.]+`)
validName, err := regexp.Compile(`[^a-z0-9\-]+`)
if err != nil {
err := fmt.Errorf("[ERROR] Error validating the Instance's name. Error: %s", err)
ui.Error(err.Error())
Expand Down Expand Up @@ -797,6 +798,30 @@ func (client IBMCloudClient) addNetworkInterfaceToSecurityGroup(SecurityGroupID
return response, nil
}

func (client IBMCloudClient) getImageIDByName(name string, state multistep.StateBag) (string, error) {
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(Config)

validName, err := regexp.Compile(`[^a-z0-9\-]+`)
if err != nil {
err := fmt.Errorf("[ERROR] Error validating the image's name. Error: %s", err)
ui.Error(err.Error())
log.Println(err.Error())
return "", err
}
name = validName.ReplaceAllString(name, "")

url := config.EndPoint + "/" + "images" + "?" + "name=" + name + "&" + config.Version + "&" + config.Generation
response, err := client.newHttpRequest(url, nil, "GET", state)
if err != nil {
err := fmt.Errorf("[ERROR] Error sending the HTTP request that get the Images. Error: %s", err)
ui.Error(err.Error())
log.Println(err.Error())
return "", err
}
return response["images"].([]interface{})[0].(map[string]interface{})["id"].(string), nil
}

// func (client IBMCloudClient) createSnapshot(state multistep.StateBag, snapshotData SnapshotReq) (map[string]interface{}, error) {
// ui := state.Get("ui").(packer.Ui)
// payload, err := json.Marshal(snapshotData)
Expand Down
5 changes: 3 additions & 2 deletions builder/ibmcloud/vpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
ResourceGroupID string `mapstructure:"resource_group_id"`
SecurityGroupID string `mapstructure:"security_group_id"`
VSIBaseImageID string `mapstructure:"vsi_base_image_id"`
VSIBaseImageName string `mapstructure:"vsi_base_image_name"`
VSIProfile string `mapstructure:"vsi_profile"`
VSIInterface string `mapstructure:"vsi_interface"`
VSIUserDataFile string `mapstructure:"vsi_user_data_file"`
Expand Down Expand Up @@ -81,8 +82,8 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
errs = packer.MultiErrorAppend(errs, errors.New("a subnet_id must be specified"))
}

if c.VSIBaseImageID == "" {
errs = packer.MultiErrorAppend(errs, errors.New("a vsi_base_image_id must be specified"))
if c.VSIBaseImageID == "" && c.VSIBaseImageName == "" {
errs = packer.MultiErrorAppend(errs, errors.New("a vsi_base_image_id or vsi_base_image_name must be specified"))
}

if c.VSIProfile == "" {
Expand Down
1 change: 1 addition & 0 deletions builder/ibmcloud/vpc/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func sshConfig(state multistep.StateBag) (*ssh.ClientConfig, error) {
User: config.Comm.SSHUsername,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
ssh.Password(config.Comm.SSHPassword),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}, nil
Expand Down
4 changes: 4 additions & 0 deletions builder/ibmcloud/vpc/step_capture_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vpc
import (
"context"
"fmt"
"regexp"

"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -46,6 +47,9 @@ func (s *stepCaptureImage) Run(_ context.Context, state multistep.StateBag) mult
bootVolumeId := bootVolume["id"].(string)
// ui.Say(fmt.Sprintf("Instance's Boot-Volume-ID: %s", bootVolumeId))

validName := regexp.MustCompile(`[^a-z0-9\-]+`)
config.ImageName = validName.ReplaceAllString(config.ImageName, "")

imageRequest := &ImageReq{
Name: config.ImageName,
SourceVolume: &ResourceByID{
Expand Down
16 changes: 16 additions & 0 deletions builder/ibmcloud/vpc/step_create_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,29 @@ func (step *stepCreateInstance) Run(_ context.Context, state multistep.StateBag)
VPCSSHKeyID: state.Get("vpc_ssh_key_id").(string),
VSIName: config.VSIName,
VSIBaseImageID: config.VSIBaseImageID,
VSIBaseImageName: config.VSIBaseImageName,
VSIProfile: config.VSIProfile,
VSIInterface: config.VSIInterface,
VSIUserDataFile: config.VSIUserDataFile,
}
state.Put("instance_definition", *instanceDefinition)

ui.Say("Creating Instance...")

// Get Image ID
if instanceDefinition.VSIBaseImageName != "" {
ui.Say("Fetching ImageID...")
baseImageID,err := client.getImageIDByName(instanceDefinition.VSIBaseImageName, state)
if err != nil {
err := fmt.Errorf("[ERROR] Error getting image ID: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
instanceDefinition.VSIBaseImageID = baseImageID
ui.Say(fmt.Sprintf("ImageID fetched: %s", string(instanceDefinition.VSIBaseImageID)))
}

instanceData, err := client.VPCCreateInstance(*instanceDefinition, state)
if err != nil {
err := fmt.Errorf("[ERROR] Error creating the instance: %s", err)
Expand Down

0 comments on commit 2a67637

Please sign in to comment.