github.com/kimor79/packer@v0.8.7-0.20151221212622-d507b18eb4cf/builder/amazon/chroot/builder.go (about)

     1  // The chroot package is able to create an Amazon AMI without requiring
     2  // the launch of a new instance for every build. It does this by attaching
     3  // and mounting the root volume of another AMI and chrooting into that
     4  // directory. It then creates an AMI from that attached drive.
     5  package chroot
     6  
     7  import (
     8  	"errors"
     9  	"log"
    10  	"runtime"
    11  
    12  	"github.com/aws/aws-sdk-go/aws/session"
    13  	"github.com/aws/aws-sdk-go/service/ec2"
    14  	"github.com/mitchellh/multistep"
    15  	awscommon "github.com/mitchellh/packer/builder/amazon/common"
    16  	"github.com/mitchellh/packer/common"
    17  	"github.com/mitchellh/packer/helper/config"
    18  	"github.com/mitchellh/packer/packer"
    19  	"github.com/mitchellh/packer/template/interpolate"
    20  )
    21  
    22  // The unique ID for this builder
    23  const BuilderId = "mitchellh.amazon.chroot"
    24  
    25  // Config is the configuration that is chained through the steps and
    26  // settable from the template.
    27  type Config struct {
    28  	common.PackerConfig    `mapstructure:",squash"`
    29  	awscommon.AccessConfig `mapstructure:",squash"`
    30  	awscommon.AMIConfig    `mapstructure:",squash"`
    31  
    32  	ChrootMounts   [][]string `mapstructure:"chroot_mounts"`
    33  	CommandWrapper string     `mapstructure:"command_wrapper"`
    34  	CopyFiles      []string   `mapstructure:"copy_files"`
    35  	DevicePath     string     `mapstructure:"device_path"`
    36  	MountPath      string     `mapstructure:"mount_path"`
    37  	SourceAmi      string     `mapstructure:"source_ami"`
    38  	RootVolumeSize int64      `mapstructure:"root_volume_size"`
    39  	MountOptions   []string   `mapstructure:"mount_options"`
    40  
    41  	ctx interpolate.Context
    42  }
    43  
    44  type wrappedCommandTemplate struct {
    45  	Command string
    46  }
    47  
    48  type Builder struct {
    49  	config Config
    50  	runner multistep.Runner
    51  }
    52  
    53  func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
    54  	b.config.ctx.Funcs = awscommon.TemplateFuncs
    55  	err := config.Decode(&b.config, &config.DecodeOpts{
    56  		Interpolate:        true,
    57  		InterpolateContext: &b.config.ctx,
    58  		InterpolateFilter: &interpolate.RenderFilter{
    59  			Exclude: []string{
    60  				"command_wrapper",
    61  				"mount_path",
    62  			},
    63  		},
    64  	}, raws...)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	// Defaults
    70  	if b.config.ChrootMounts == nil {
    71  		b.config.ChrootMounts = make([][]string, 0)
    72  	}
    73  
    74  	if b.config.CopyFiles == nil {
    75  		b.config.CopyFiles = make([]string, 0)
    76  	}
    77  
    78  	if len(b.config.ChrootMounts) == 0 {
    79  		b.config.ChrootMounts = [][]string{
    80  			[]string{"proc", "proc", "/proc"},
    81  			[]string{"sysfs", "sysfs", "/sys"},
    82  			[]string{"bind", "/dev", "/dev"},
    83  			[]string{"devpts", "devpts", "/dev/pts"},
    84  			[]string{"binfmt_misc", "binfmt_misc", "/proc/sys/fs/binfmt_misc"},
    85  		}
    86  	}
    87  
    88  	if len(b.config.CopyFiles) == 0 {
    89  		b.config.CopyFiles = []string{"/etc/resolv.conf"}
    90  	}
    91  
    92  	if b.config.CommandWrapper == "" {
    93  		b.config.CommandWrapper = "{{.Command}}"
    94  	}
    95  
    96  	if b.config.MountPath == "" {
    97  		b.config.MountPath = "/mnt/packer-amazon-chroot-volumes/{{.Device}}"
    98  	}
    99  
   100  	// Accumulate any errors
   101  	var errs *packer.MultiError
   102  	errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...)
   103  	errs = packer.MultiErrorAppend(errs, b.config.AMIConfig.Prepare(&b.config.ctx)...)
   104  
   105  	for _, mounts := range b.config.ChrootMounts {
   106  		if len(mounts) != 3 {
   107  			errs = packer.MultiErrorAppend(
   108  				errs, errors.New("Each chroot_mounts entry should be three elements."))
   109  			break
   110  		}
   111  	}
   112  
   113  	if b.config.SourceAmi == "" {
   114  		errs = packer.MultiErrorAppend(errs, errors.New("source_ami is required."))
   115  	}
   116  
   117  	if errs != nil && len(errs.Errors) > 0 {
   118  		return nil, errs
   119  	}
   120  
   121  	log.Println(common.ScrubConfig(b.config, b.config.AccessKey, b.config.SecretKey))
   122  	return nil, nil
   123  }
   124  
   125  func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
   126  	if runtime.GOOS != "linux" {
   127  		return nil, errors.New("The amazon-chroot builder only works on Linux environments.")
   128  	}
   129  
   130  	config, err := b.config.Config()
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  
   135  	session := session.New(config)
   136  	ec2conn := ec2.New(session)
   137  
   138  	wrappedCommand := func(command string) (string, error) {
   139  		ctx := b.config.ctx
   140  		ctx.Data = &wrappedCommandTemplate{Command: command}
   141  		return interpolate.Render(b.config.CommandWrapper, &ctx)
   142  	}
   143  
   144  	// Setup the state bag and initial state for the steps
   145  	state := new(multistep.BasicStateBag)
   146  	state.Put("config", &b.config)
   147  	state.Put("ec2", ec2conn)
   148  	state.Put("hook", hook)
   149  	state.Put("ui", ui)
   150  	state.Put("wrappedCommand", CommandWrapper(wrappedCommand))
   151  
   152  	// Build the steps
   153  	steps := []multistep.Step{
   154  		&awscommon.StepPreValidate{
   155  			DestAmiName:     b.config.AMIName,
   156  			ForceDeregister: b.config.AMIForceDeregister,
   157  		},
   158  		&StepInstanceInfo{},
   159  		&awscommon.StepSourceAMIInfo{
   160  			SourceAmi:          b.config.SourceAmi,
   161  			EnhancedNetworking: b.config.AMIEnhancedNetworking,
   162  		},
   163  		&StepCheckRootDevice{},
   164  		&StepFlock{},
   165  		&StepPrepareDevice{},
   166  		&StepCreateVolume{
   167  			RootVolumeSize: b.config.RootVolumeSize,
   168  		},
   169  		&StepAttachVolume{},
   170  		&StepEarlyUnflock{},
   171  		&StepMountDevice{
   172  			MountOptions: b.config.MountOptions,
   173  		},
   174  		&StepMountExtra{},
   175  		&StepCopyFiles{},
   176  		&StepChrootProvision{},
   177  		&StepEarlyCleanup{},
   178  		&StepSnapshot{},
   179  		&awscommon.StepDeregisterAMI{
   180  			ForceDeregister: b.config.AMIForceDeregister,
   181  			AMIName:         b.config.AMIName,
   182  		},
   183  		&StepRegisterAMI{
   184  			RootVolumeSize: b.config.RootVolumeSize,
   185  		},
   186  		&awscommon.StepAMIRegionCopy{
   187  			AccessConfig: &b.config.AccessConfig,
   188  			Regions:      b.config.AMIRegions,
   189  			Name:         b.config.AMIName,
   190  		},
   191  		&awscommon.StepModifyAMIAttributes{
   192  			Description: b.config.AMIDescription,
   193  			Users:       b.config.AMIUsers,
   194  			Groups:      b.config.AMIGroups,
   195  		},
   196  		&awscommon.StepCreateTags{
   197  			Tags: b.config.AMITags,
   198  		},
   199  	}
   200  
   201  	// Run!
   202  	if b.config.PackerDebug {
   203  		b.runner = &multistep.DebugRunner{
   204  			Steps:   steps,
   205  			PauseFn: common.MultistepDebugFn(ui),
   206  		}
   207  	} else {
   208  		b.runner = &multistep.BasicRunner{Steps: steps}
   209  	}
   210  
   211  	b.runner.Run(state)
   212  
   213  	// If there was an error, return that
   214  	if rawErr, ok := state.GetOk("error"); ok {
   215  		return nil, rawErr.(error)
   216  	}
   217  
   218  	// If there are no AMIs, then just return
   219  	if _, ok := state.GetOk("amis"); !ok {
   220  		return nil, nil
   221  	}
   222  
   223  	// Build the artifact and return it
   224  	artifact := &awscommon.Artifact{
   225  		Amis:           state.Get("amis").(map[string]string),
   226  		BuilderIdValue: BuilderId,
   227  		Conn:           ec2conn,
   228  	}
   229  
   230  	return artifact, nil
   231  }
   232  
   233  func (b *Builder) Cancel() {
   234  	if b.runner != nil {
   235  		log.Println("Cancelling the step runner...")
   236  		b.runner.Cancel()
   237  	}
   238  }