Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) Added Validation to Packer Binary Wrapper #27

Closed
wants to merge 6 commits into from

Conversation

Hrily
Copy link

@Hrily Hrily commented Oct 8, 2017

PR for #21

I was unsure if I'm going in right direction.

I have just implemented validation part, please have a look and tell if this is good.

PS: Are we taking care of -var in Template itself???

@coveralls
Copy link

Coverage Status

Coverage decreased (-5.3%) to 75.888% when pulling fb671ed on Hrily:master into 2e61b71 on mayn:master.

@mayn
Copy link
Owner

mayn commented Oct 9, 2017

@Hrily this is a great start!
yes variables are in the template (https://github.com/mayn/packerlicious/blob/master/src/packerlicious/template.py#L42) . here is them in action from the example https://github.com/mayn/packerlicious/blob/master/examples/getting_started_example.py#L33

I'll leave you some comments for this PR.
I'll also make some additional tickets for initial non-required changes. Your choice if you want to do them all in 1 PR or make some follow-up PRs.

thanks for your effort!
p.s. will need need to add unit tests in the end😉

@Hrily
Copy link
Author

Hrily commented Oct 9, 2017

@mayn
That's good to hear... :)

yes variables are in the template

Okay, then I'll remove them from packer.py

p.s. will need need to add unit tests in the end:wink:

Sure 👍

Copy link
Owner

@mayn mayn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hrily here is some feedback.
let me know your thoughts!

packer_path += "/"
self.packer_path = packer_path
self.template = template
self._vars = _vars
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vars are already an attribute of template and should be accesible via template. variables

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do this 👍

if len(packer_path) > 0 and packer_path[-1] != '/':
packer_path += "/"
self.packer_path = packer_path
self.template = template
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see template as just a method parameter vs an instance variable. and Packer class as stateless
so it is easy to use:

packer.validate(my_template)
packer.validate(my_template2)

or whatever without having to re-initial a packer object.

what do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay 👍

So each method will require template as parameter.
Also Should the packer binary path be the parameter for each method?
I did it because, I did not make assumption whether user has updated path variable w.r.t. packer.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So each method will require template as parameter.

correct

Also Should the packer binary path be the parameter for each method?

I understand. I think it is fine the way it is as a class variable. A future enhancement could be to look at os.env variable or something if it is useful

For now I think this is a fair expectation that the user know where packer is on their system and provide the location if necessary.

The check_available method should catch if packer isn't found in the location

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

So each method will require template as parameter.

I think we won't require Packer class. All methods will be part of packer.

But in this case, packer_path should be a parameter for each method, since we don't have class.

What do you think?
Correct me if I'm wrong.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try it out, can always change it back. so long as you aren't making packer_path required for the user to provide i think it should be fine.

for key in self._vars:
validate_command += "-var '" + key + "=" + self._vars[key] + "' "
template_file, template_filename = tempfile.mkstemp()
os.write(template_file, self.template.to_json())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally this would pipe directly to packer without creating a tmp file.
if you keep the tmp file writing, please add error handling

if you don't change it on this pr, please create an issue about this so the idea doesn't get lost.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not find a way to pipe it directly without creating file.
If you have a way, plz tell me so I can update accordingly.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be able to do it via stdin of the popen.communicate. see hashicorp/packer#2914 for an example. looks like there is a trailing - they pass to the cmd.

Also more info here hashicorp/packer#4132

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, did it 👍
Thanks :)

proc = subprocess.Popen(shlex.split(validate_command), stdout=subprocess.PIPE)
out = proc.communicate()[0]
os.remove(template_filename)
return proc.returncode, out
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to handle return by converting to a python object we can interact with.

please create an issue so we don't loose this thought ( don't expect as part of initial submission)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do this 👍

os.write(template_file, self.template.to_json())
os.close(template_file)
validate_command += template_filename
proc = subprocess.Popen(shlex.split(validate_command), stdout=subprocess.PIPE)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should capture and return stderr also

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do this 👍

@coveralls
Copy link

Coverage Status

Coverage decreased (-9.4%) to 71.795% when pulling dabe2df on Hrily:master into 2e61b71 on mayn:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-9.7%) to 71.479% when pulling a9d66c0 on Hrily:master into 2e61b71 on mayn:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-7.8%) to 73.415% when pulling 9b14f14 on Hrily:master into 2e61b71 on mayn:master.

@mayn mayn changed the title Added Validation to Packer Binary Wrapper (WIP) Added Validation to Packer Binary Wrapper Oct 10, 2017
@coveralls
Copy link

Coverage Status

Coverage decreased (-9.6%) to 71.604% when pulling d2a5b23 on Hrily:master into 2e61b71 on mayn:master.

@Hrily
Copy link
Author

Hrily commented Oct 10, 2017

I did the mentioned changes, please review.

Sorry for late push, was travelling yesterday.

Also, Can you tell me how would I pass this coverage test?

@mayn
Copy link
Owner

mayn commented Oct 10, 2017

thanks @Hrily!
re: test coverage. you click over into the coveralls coverage report for your pr.
Basically you need to add the appropriate pytests for the code being added. Currently there isn't any so this is causing the project coverage to decrease and build to fail. see packer.py coverage for your last commit (https://coveralls.io/builds/13644387/source?filename=src%2Fpackerlicious%2Fpacker.py)

Will review over next day...

@Hrily
Copy link
Author

Hrily commented Oct 13, 2017

@mayn

Could you give me some test cases for packer.py

I think I have written some wrong test case... 🙈

@mayn
Copy link
Owner

mayn commented Oct 14, 2017

@Hrily the tests are failing because packer binary is not currently present on the ci systems (#63) for integration testing.

From a unit test perspective you would have to mock/patch the calls to the OS to emulate the interaction to packer binary.

@mayn mayn closed this Oct 20, 2017
@mayn mayn reopened this Oct 20, 2017
@Hrily
Copy link
Author

Hrily commented Oct 22, 2017

Hie @mayn

Sorry for a real late reply.

The build test on travis is failing with this output

amazon-ebs output will be in this color.

==> amazon-ebs: Prevalidating AMI Name...
==> amazon-ebs: Error querying AMI: NoCredentialProviders: no valid providers in chain
==> amazon-ebs: caused by: EnvAccessKeyNotFound: failed to find credentials in the environment.
==> amazon-ebs: SharedCredsLoad: failed to load profile, .
==> amazon-ebs: EC2RoleRequestError: no EC2 instance role found
==> amazon-ebs: caused by: RequestError: send request failed
==> amazon-ebs: caused by: Get http://169.254.169.254/latest/meta-data/iam/security-credentials: dial tcp 169.254.169.254:80: getsockopt: no route to host
Build 'amazon-ebs' errored: Error querying AMI: NoCredentialProviders: no valid providers in chain
caused by: EnvAccessKeyNotFound: failed to find credentials in the environment.
SharedCredsLoad: failed to load profile, .
EC2RoleRequestError: no EC2 instance role found
caused by: RequestError: send request failed
caused by: Get http://169.254.169.254/latest/meta-data/iam/security-credentials: dial tcp 169.254.169.254:80: getsockopt: no route to host

==> Some builds didn't complete successfully and had errors:
--> amazon-ebs: Error querying AMI: NoCredentialProviders: no valid providers in chain
caused by: EnvAccessKeyNotFound: failed to find credentials in the environment.
SharedCredsLoad: failed to load profile, .
EC2RoleRequestError: no EC2 instance role found
caused by: RequestError: send request failed
caused by: Get http://169.254.169.254/latest/meta-data/iam/security-credentials: dial tcp 169.254.169.254:80: getsockopt: no route to host

==> Builds finished but no artifacts were created.

Return code: 1
Error: None

Can I get help regarding this???

I'm using the same Templates used by example.

@Hrily
Copy link
Author

Hrily commented Nov 11, 2017

@mayn
Any updates for my last comment?

@mayn
Copy link
Owner

mayn commented Dec 1, 2017

@Hrily apologies for the delay, I think you are getting the error because the example template doesn't actually have ec2 credentials in it.
You'd have to provide valid credentials or create a simple packerlicious example to use that doesn't add aws as a layer

@mayn mayn added the wontfix label Mar 25, 2018
@mayn
Copy link
Owner

mayn commented Mar 25, 2018

Build an alternate implementation as standalone repo https://github.com/mayn/packer.py

@mayn mayn closed this Mar 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants