@ -3,29 +3,42 @@ package classic
import (
"context"
"fmt"
"log"
"strings"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
)
type stepCreateImage struct {
uploadImageCommand string
uploadImageCommand string
destinationContainer string
imageName string
}
type uploadCmdData struct {
DiskImagePath string
Username string
Password string
AccountID string
Container string
ImageName string
}
func ( s * stepCreateImage ) Run ( _ context . Context , state multistep . StateBag ) multistep . StepAction {
//hook := state.Get("hook").(packer.Hook)
ui := state . Get ( "ui" ) . ( packer . Ui )
comm := state . Get ( "communicator" ) . ( packer . Communicator )
client := state . Get ( "client" ) . ( * compute . ComputeClient )
config := state . Get ( "config" ) . ( * Config )
config . ctx . Data = uploadCmdData {
DiskImagePath : "./diskimage.tar.gz" ,
Username : config . Username ,
Password : config . Password ,
AccountID : config . IdentityDomain ,
Container : s . destinationContainer ,
ImageName : s . imageName ,
}
uploadImageCmd , err := interpolate . Render ( s . uploadImageCommand , & config . ctx )
if err != nil {
@ -65,6 +78,35 @@ func (s *stepCreateImage) Run(_ context.Context, state multistep.StateBag) multi
return multistep . ActionHalt
}
// Image uploaded, let's register it
machineImageClient := client . MachineImages ( )
createMI := & compute . CreateMachineImageInput {
// Two-part name of the account
Account : "/Compute-identity_domain/cloud_storage" ,
Description : "Packer generated TODO" ,
// The three-part name of the object
Name : "/Compute-identity_domain/user/object" ,
// image_file.tar.gz, where image_file is the .tar.gz name of the machine image file that you have uploaded to Oracle Cloud Infrastructure Object Storage Classic.
File : fmt . Sprintf ( "%s.tar.gz" , s . imageName ) ,
}
mi , err := machineImageClient . CreateMachineImage ( createMI )
if err != nil {
err = fmt . Errorf ( "Error creating machine image: %s" , err )
ui . Error ( err . Error ( ) )
state . Put ( "error" , err )
return multistep . ActionHalt
}
log . Printf ( "Registered machine image: %+v" , mi )
/ * TODO :
1. POST / machineimage / , POST / imagelist / , and POST / imagelistentry / methods , in that order .
2. re - use step_list_images
3. Don ' t push commits with passwords in them
4. Documentation
5. Configuration ( master / builder images , entry , destination stuff , etc )
6. split master / builder image / connection config . i . e . build anything , master only linux
* /
//machineImageClient.CreateMachineImage()
return multistep . ActionContinue
}