methods/pk_json.rb
# Create Packer JSON file for AWS
Create Packer JSON file for AWS
Create Packer JSON file for AWS
def create_packer_aws_json(options)
  options['service'] = options['q_struct']['type'].value
  options['access']  = options['q_struct']['access_key'].value
  options['secret']  = options['q_struct']['secret_key'].value
  options['ami']     = options['q_struct']['source_ami'].value
  options['region']  = options['q_struct']['region'].value
  options['size']    = options['q_struct']['instance_type'].value
  options['keyfile'] = File.basename(options['q_struct']['keyfile'].value,".pem")+".key.pub"
  options['name']    = options['q_struct']['ami_name'].value
  options['adminuser'] = options['q_struct']['ssh_username'].value
  options['clientdir'] = packer_dir+"/aws/"+options['name']
  tmp_keyfile    = "/tmp/"+options['keyfile']
  user_data_file = options['q_struct']['user_data_file'].value
  packer_dir     = options['clientdir']+"/packer"
  json_file      = options['clientdir']+"/"+options['name']+".json"
  check_dir_exists(options,options['clientdir'])
  json_data = {
    :builders => [
      {
        :name             => "aws",
        :type             => options['service'],
        :access_key       => options['access'],
        :secret_key       => options['secret'],
        :source_ami       => options['ami'],
        :region           => options['region'],
        :instance_type    => options['size'],
        :ssh_username     => options['adminuser'],
        :ami_name         => options['name'],
        :user_data_file   => user_data_file
      }
    ],
    :provisioners => [
      {
        :type             => "file",
        :source           => options['keyfile'],
        :destination      => tmp_keyfile
      },
      {
        :type             => "shell",
        :execute_command  => "{{ .Vars }} sudo -E -S sh '{{ .Path }}'",
        :scripts          => [
          "scripts/vagrant.sh"
        ]
      }
    ],
    :"post-processors"    => [
      {
        :output           => "builds/packer_{{.BuildName}}_{{.Provider}}.box",
        :type             => "vagrant"
      }
    ]
  }
  json_output = JSON.pretty_generate(json_data)
  delete_file(json_file)
  File.write(json_file,json_output)
  set_file_perms(json_file,"600")
  print_contents_of_file(options,"",json_file)
  return options
end
def create_packer_aws_json ( options ) options [ 'service' ] = options [ 'q_struct' ] [ 'type' ] . value options [ 'access' ] = options [ 'q_struct' ] [ 'access_key' ] . value options [ 'secret' ] = options [ 'q_struct' ] [ 'secret_key' ] . value options [ 'ami' ] = options [ 'q_struct' ] [ 'source_ami' ] . value options [ 'region' ] = options [ 'q_struct' ] [ 'region' ] . value options [ 'size' ] = options [ 'q_struct' ] [ 'instance_type' ] . value options [ 'keyfile' ] = File . basename ( options [ 'q_struct' ] [ 'keyfile' ] . value , ".pem" ) + ".key.pub" options [ 'name' ] = options [ 'q_struct' ] [ 'ami_name' ] . value options [ 'adminuser' ] = options [ 'q_struct' ] [ 'ssh_username' ] . value options [ 'clientdir' ] = packer_dir + "/aws/" + options [ 'name' ] tmp_keyfile = "/tmp/" + options [ 'keyfile' ] user_data_file = options [ 'q_struct' ] [ 'user_data_file' ] . value packer_dir = options [ 'clientdir' ] + "/packer" json_file = options [ 'clientdir' ] + "/" + options [ 'name' ] + ".json" check_dir_exists ( options , options [ 'clientdir' ] ) json_data = { :builders => [ { :name => "aws" , :type => options [ 'service' ] , :access_key => options [ 'access' ] , :secret_key => options [ 'secret' ] , :source_ami => options [ 'ami' ] , :region => options [ 'region' ] , :instance_type => options [ 'size' ] , :ssh_username => options [ 'adminuser' ] , :ami_name => options [ 'name' ] , :user_data_file => user_data_file } ] , :provisioners => [ { :type => "file" , :source => options [ 'keyfile' ] , :destination => tmp_keyfile } , { :type => "shell" , :execute_command => "{{ .Vars }} sudo -E -S sh '{{ .Path }}'" , :scripts => [ "scripts/vagrant.sh" ] } ] , :" post-processors " => [ { :output => "builds/packer_{{.BuildName}}_{{.Provider}}.box" , :type => "vagrant" } ] } json_output = JSON . pretty_generate ( json_data ) delete_file ( json_file ) File . write ( json_file , json_output ) set_file_perms ( json_file , "600" ) print_contents_of_file ( options , "" , json_file ) return options end
Create Packer JSON file for AWS
Create Packer JSON file for AWS
options
<not_specific>