Class: Hashicorptools::Packer

Inherits:
Thor
  • Object
show all
Includes:
Ec2Utilities, Variables
Defined in:
lib/hashicorptools/packer.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Variables

#variables

Methods included from Ec2Utilities

#amis, #current_ami, #ec2, #internet_gateway_for_vpc, #sort_by_created_at, #vpc_with_name

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/hashicorptools/packer.rb', line 8

def self.exit_on_failure?
  true
end

Instance Method Details

#bootObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hashicorptools/packer.rb', line 65

def boot
  run_instances_resp = ec2.run_instances({
    image_id: current_ami('base-image').image_id,
    min_count: 1,
    max_count: 1,
    instance_type: "t2.micro"
  })

  ec2.create_tags({
    resources: run_instances_resp.instances.collect(&:instance_id),
    tags: [ {key: 'Name', value: "packer test boot #{tag_name}"},
            {key: 'environment', value: 'packer-development'},
            {key: 'temporary', value: 'kill me'}]
  })

  require 'byebug'
  byebug
end

#buildObject



14
15
16
# File 'lib/hashicorptools/packer.rb', line 14

def build
  _build
end

#cleanObject



42
43
44
# File 'lib/hashicorptools/packer.rb', line 42

def clean
  clean_amis
end

#clean_snapshotsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hashicorptools/packer.rb', line 47

def clean_snapshots
  snapshots = ec2.describe_snapshots({owner_ids: ['self']}).snapshots
  snapshots.each do |snapshot|
    match = snapshot.description.match(/Created by CreateImage\(.+\) for (ami-[0-9a-f]+) from vol-.+/)
    if match.nil?
      puts "Skipping #{snapshot.id} - #{snapshot.description}"
      next
    end

    ami_id = match[1]
    unless Aws::EC2::Image.new(ami_id, region: region).exists?
      puts "Removing obsolete snapshot #{snapshot.snapshot_id} - #{snapshot.description}"
      ec2.delete_snapshot({snapshot_id: snapshot.snapshot_id})
    end
  end
end

#consoleObject



29
30
31
32
# File 'lib/hashicorptools/packer.rb', line 29

def console
  require 'byebug'
  byebug
end

#fixObject



24
25
26
# File 'lib/hashicorptools/packer.rb', line 24

def fix
  system "packer fix #{ami_config_path}"
end

#listObject



35
36
37
38
39
# File 'lib/hashicorptools/packer.rb', line 35

def list
  amis_in_region(region).each do |ami|
    puts ami.image_id
  end
end

#validateObject



19
20
21
# File 'lib/hashicorptools/packer.rb', line 19

def validate
  system "packer validate #{ami_config_path}"
end