mercredi 28 mai 2014

Ruby sur rails - traitant les octets null lors de la création ec2 avec user_data à l'aide de brouillard - Stack Overflow


I am trying to provision an ec2 instance using fog, here is the code that I am using:


compute = Fog::Compute.new provider: 'AWS',
region: 'us-east-1', aws_access_key_id: ACCESS_KEY,
aws_secret_access_key: SECRET_ACCESS_KEY
options = {
image_id: 'ami-xxxxxx',
flavor_id: 'm1.small',
#custom security group created in AWS Account with open ports
groups: ['myGroup'],
private_key_path: '~/.ssh/id_rsa',
public_key_path: '~/.ssh/id_rsa.pub',
username: 'ec2-user',
user_data: File.read(Rails.root.join('public', 'somefile.zip'))
}
compute.servers.bootstrap options

When, I run this. I get following error:


Fog::JSON::EncodeError: string contains null byte
from /home/gaurish/.rvm/gems/ruby-2.0.0-p247/gems/multi_json-1.8.2/lib/multi_json/adapters/oj.rb:20:in `dump'

As you may notice, above. I am supplying a ZIP file for user_data option. And this is what I think the problem occurs. My guess is that the zip file or encoding it to base64 somehow adds a null byte("\0") due to which Oj can't encode it to JSON format.


Now,



  • Can anyone verify if its a bug in fog or am I doing anything wrong?

  • Any workarounds to avoid null bytes?


Versions used:



  • Fog 1.19

  • multi_json-1.8.2

  • oj-2.2.3




I have solved this issue. here is how:


file = File.open(path, 'rb') #path => path to zip file
contents = file.read
file.close
user_data = Base64.encode64 contents

now, this user_data can be safely passed into options[:user_data] hash without null byte errors. this issue is being tracked here:


https://github.com/fog/fog/issues/2506


Upvote if you found this helpful.



I am trying to provision an ec2 instance using fog, here is the code that I am using:


compute = Fog::Compute.new provider: 'AWS',
region: 'us-east-1', aws_access_key_id: ACCESS_KEY,
aws_secret_access_key: SECRET_ACCESS_KEY
options = {
image_id: 'ami-xxxxxx',
flavor_id: 'm1.small',
#custom security group created in AWS Account with open ports
groups: ['myGroup'],
private_key_path: '~/.ssh/id_rsa',
public_key_path: '~/.ssh/id_rsa.pub',
username: 'ec2-user',
user_data: File.read(Rails.root.join('public', 'somefile.zip'))
}
compute.servers.bootstrap options

When, I run this. I get following error:


Fog::JSON::EncodeError: string contains null byte
from /home/gaurish/.rvm/gems/ruby-2.0.0-p247/gems/multi_json-1.8.2/lib/multi_json/adapters/oj.rb:20:in `dump'

As you may notice, above. I am supplying a ZIP file for user_data option. And this is what I think the problem occurs. My guess is that the zip file or encoding it to base64 somehow adds a null byte("\0") due to which Oj can't encode it to JSON format.


Now,



  • Can anyone verify if its a bug in fog or am I doing anything wrong?

  • Any workarounds to avoid null bytes?


Versions used:



  • Fog 1.19

  • multi_json-1.8.2

  • oj-2.2.3



I have solved this issue. here is how:


file = File.open(path, 'rb') #path => path to zip file
contents = file.read
file.close
user_data = Base64.encode64 contents

now, this user_data can be safely passed into options[:user_data] hash without null byte errors. this issue is being tracked here:


https://github.com/fog/fog/issues/2506


Upvote if you found this helpful.


0 commentaires:

Enregistrer un commentaire