2018-09-06 01:44:31 -04:00
package classic
import (
"context"
2018-09-07 19:18:47 -04:00
"fmt"
2018-10-18 00:23:22 -04:00
"log"
2018-09-07 19:18:47 -04:00
"strings"
2018-09-06 01:44:31 -04:00
2018-10-18 00:23:22 -04:00
"github.com/hashicorp/go-oracle-terraform/compute"
2018-09-06 01:44:31 -04:00
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
2018-09-12 19:16:08 -04:00
"github.com/hashicorp/packer/template/interpolate"
2018-09-06 01:44:31 -04:00
)
2018-09-12 19:16:08 -04:00
type stepCreateImage struct {
2018-10-18 00:23:22 -04:00
uploadImageCommand string
destinationContainer string
imageName string
2018-09-12 19:16:08 -04:00
}
type uploadCmdData struct {
2018-10-18 00:23:22 -04:00
Username string
Password string
AccountID string
Container string
ImageName string
2018-09-12 19:16:08 -04:00
}
2018-09-06 01:44:31 -04:00
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 )
2018-10-18 00:23:22 -04:00
client := state . Get ( "client" ) . ( * compute . ComputeClient )
2018-09-12 19:16:08 -04:00
config := state . Get ( "config" ) . ( * Config )
config . ctx . Data = uploadCmdData {
2018-10-18 00:23:22 -04:00
Username : config . Username ,
Password : config . Password ,
AccountID : config . IdentityDomain ,
Container : s . destinationContainer ,
ImageName : s . imageName ,
2018-09-12 19:16:08 -04:00
}
uploadImageCmd , err := interpolate . Render ( s . uploadImageCommand , & config . ctx )
if err != nil {
err := fmt . Errorf ( "Error processing image upload command: %s" , err )
state . Put ( "error" , err )
ui . Error ( err . Error ( ) )
return multistep . ActionHalt
}
2018-09-08 02:34:37 -04:00
2018-09-12 19:16:08 -04:00
command := fmt . Sprintf ( ` # ! / bin / sh
2018-09-07 19:18:47 -04:00
set - e
mkdir / builder
mkfs - t ext3 / dev / xvdb
mount / dev / xvdb / builder
chown opc : opc / builder
cd / builder
dd if = / dev / xvdc bs = 8 M status = progress | cp -- sparse = always / dev / stdin diskimage . raw
2018-09-12 19:16:08 -04:00
tar czSf . / diskimage . tar . gz . / diskimage . raw
2018-10-18 00:30:22 -04:00
rm diskimage . raw
2018-09-12 20:12:35 -04:00
% s ` , uploadImageCmd )
2018-09-07 19:18:47 -04:00
dest := "/tmp/create-packer-diskimage.sh"
comm . Upload ( dest , strings . NewReader ( command ) , nil )
cmd := & packer . RemoteCmd {
Command : fmt . Sprintf ( "sudo /bin/sh %s" , dest ) ,
2018-09-06 01:44:31 -04:00
}
2018-09-07 19:18:47 -04:00
if err := cmd . StartWithUi ( comm , ui ) ; err != nil {
2018-10-17 23:16:23 -04:00
err = fmt . Errorf ( "Problem creating image`: %s" , err )
ui . Error ( err . Error ( ) )
state . Put ( "error" , err )
return multistep . ActionHalt
}
if cmd . ExitStatus != 0 {
err = fmt . Errorf ( "Create Disk Image command failed with exit code %d" , cmd . ExitStatus )
2018-09-07 19:18:47 -04:00
ui . Error ( err . Error ( ) )
state . Put ( "error" , err )
return multistep . ActionHalt
2018-09-06 01:44:31 -04:00
}
2018-10-18 00:23:22 -04:00
// 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()
2018-09-06 01:44:31 -04:00
return multistep . ActionContinue
}
func ( s * stepCreateImage ) Cleanup ( state multistep . StateBag ) { }