Class: Linecook::Packer

Inherits:
Object
  • Object
show all
Includes:
Downloader, Locking
Defined in:
lib/linecook-gem/packager/packer/base.rb

Direct Known Subclasses

AmiPacker, GCPPacker

Constant Summary collapse

SOURCE_URL =
'https://releases.hashicorp.com/packer/'
PACKER_VERSION =
'0.12.0'
PACKER_PATH =
File.join(Linecook::Config::LINECOOK_HOME, 'bin', 'packer')
PRE_MOUNT_COMMANDS =
[
  'parted -s {{.Device}} mklabel msdos',
  'parted -s {{.Device}} mkpart primary ext2 0% 100%',
  'mkfs.ext4 {{.Device}}1',
  'tune2fs -L cloudimg-rootfs {{.Device}}1',
]
POST_MOUNT_COMMANDS =
[
  'tar -C {{.MountPath}} -xpf {{ user `source_image_path` }}',
  'cp /etc/resolv.conf {{.MountPath}}/etc',
  'echo "LABEL=cloudimg-rootfs   /        ext4   defaults,discard        0 0" > {{.MountPath}}/etc/fstab',
  'grub-install --root-directory={{.MountPath}} {{.Device}}',
  'rm -rf {{.MountPath}}/etc/network',
  'cp -r /etc/network {{.MountPath}}/etc/',
]
CHROOT_COMMANDS =
[
  'apt-get update',
  'DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y --force-yes --no-upgrade install grub-pc grub-legacy-ec2', # FIXME - ec2 specific
  'update-grub',
  'rm -f /etc/init/fake-container-events.conf', # HACK
  'mkdir -p /run/resolvconf/interface',
  'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure resolvconf' # re-linkify resolvconf
]

Constants included from Downloader

Downloader::LOCK_WAIT_TIMEOUT

Instance Method Summary collapse

Methods included from Locking

#clear_lock, #lock, #lock_path, #lockfile, #unlock

Methods included from Downloader

#download, #unzip

Constructor Details

#initializePacker

Returns a new instance of Packer.


49
50
51
# File 'lib/linecook-gem/packager/packer/base.rb', line 49

def initialize
  raise 'Not implemented'
end

Instance Method Details

#package(image, directory) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/linecook-gem/packager/packer/base.rb', line 53

def package(image, directory)
  @image = image
  kitchen_config = load_config(directory).send(:data).instance_variable_get(:@data)
  image_config = kitchen_config[:suites].find{ |x| x[:name] == image.name }
  if image_config && image_config[:packager]
    packager = image_config[:packager] || {}
  end
  conf_file = Tempfile.new("#{@image.id}-packer.json")
  config = generate_config(packager)
  conf_file.write(config)
  conf_file.close
  @output = []
  PTY.spawn("sudo #{PACKER_PATH} build -machine-readable #{conf_file.path}") do |stdout, _, _|
    begin
      stdout.each do |line|
        @output << line if line =~ /artifact/
        tokens = line.split(',')
        if tokens.length > 4
          out = tokens[4].gsub('%!(PACKER_COMMA)', ',')
          time = DateTime.strptime(tokens[0], '%s').strftime('%c')
          puts "#{time} | #{out}"
        else
          puts "unexpected output format"
          puts tokens
        end
      end
    rescue Errno::EIO
      puts "Packer finshed executing"
    end
  end
ensure
  conf_file.close
  conf_file.unlink
end