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

EnvVar is creating user variables in > 1.0.2 #170

Closed
testworksau opened this issue Mar 17, 2019 · 5 comments
Closed

EnvVar is creating user variables in > 1.0.2 #170

testworksau opened this issue Mar 17, 2019 · 5 comments
Assignees
Labels
Milestone

Comments

@testworksau
Copy link

We are seeing an issue whereby using EnvVar (to read from the environment variable on our build agent) to populate UserVar results in the generated packer template containing only user variables.

packerlicious version

1.1.0 -> 1.3.1

Expected behavior

...
    aws_access_key = EnvVar("AWS_ACCESS_KEY_ID")
    aws_secret_access_key = EnvVar("AWS_SECRET_ACCESS_KEY")

    template = Template()
    var_aws_access_key = UserVar("AWS_ACCESS_KEY_ID", Ref(aws_access_key))
    var_aws_secret_key = UserVar("AWS_SECRET_ACCESS_KEY", Ref(aws_secret_key))
    user_variables = [
        var_aws_access_key,
        var_aws_secret_key
    ]
    
    template.add_variable(user_variables)
    amazon_ebs_builder = builder.AmazonEbs(
        access_key=Ref(var_aws_access_key),
        secret_key=Ref(var_aws_secret_key),
...

This should (or at least used to) yield environment variables in the .json:

...
  "builders": [
    {
      "access_key": "{{user `AWS_ACCESS_KEY_ID`}}",
      "secret_key": "{{user `AWS_SECRET_ACCESS_KEY`}}",
...

  "variables": {
    "AWS_ACCESS_KEY_ID": "{{env `AWS_ACCESS_KEY_ID`}}",
    "AWS_SECRET_ACCESS_KEY": "{{env `AWS_SECRET_ACCESS_KEY`}}"
  }

Actual behavior

User variables end up in the .json file:

  "builders": [
    {
      "access_key": "{{user `AWS_ACCESS_KEY_ID`}}",
      "secret_key": "{{user `AWS_SECRET_ACCESS_KEY`}}",
...

  "variables": {
    "AWS_ACCESS_KEY_ID": "{{user `aws_access_key_id`}}",
    "AWS_SECRET_ACCESS_KEY": "{{user `aws_secret_access_key`}}"
  }

Steps to reproduce

See expected / actual.

@mayn
Copy link
Owner

mayn commented Mar 18, 2019

hi @testworksau ,
please try removing the Ref() in the UserVar definition.
i.e UserVar("AWS_ACCESS_KEY_ID", aws_access_key)

from packerlicious import builder
aws_access_key = EnvVar("AWS_ACCESS_KEY_ID")
aws_secret_access_key = EnvVar("AWS_SECRET_ACCESS_KEY")
template = Template()
var_aws_access_key = UserVar("AWS_ACCESS_KEY_ID", aws_access_key)
var_aws_secret_key = UserVar("AWS_SECRET_ACCESS_KEY", aws_secret_access_key)
user_variables = [
    var_aws_access_key,
    var_aws_secret_key
]
template.add_variable(user_variables)
amazon_ebs_builder = builder.AmazonEbs(
    ami_name="my_ami",
    source_ami="source_ami",
    instance_type="t2.micro",
    region="us-east-1",
    access_key=Ref(var_aws_access_key),
    secret_key=Ref(var_aws_secret_key),
)
template.add_builder(amazon_ebs_builder)
print(template.to_json())

I believe this give you the output you are looking for.

{
  "builders": [
    {
      "access_key": "{{user `AWS_ACCESS_KEY_ID`}}",
      "ami_name": "my_ami",
      "instance_type": "t2.micro",
      "region": "us-east-1",
      "secret_key": "{{user `AWS_SECRET_ACCESS_KEY`}}",
      "source_ami": "source_ami",
      "type": "amazon-ebs"
    }
  ],
  "variables": {
    "AWS_ACCESS_KEY_ID": "{{env `AWS_ACCESS_KEY_ID`}}",
    "AWS_SECRET_ACCESS_KEY": "{{env `AWS_SECRET_ACCESS_KEY`}}"
  }
}

The behavior changed starting v1.1.0 via 54f7a97
I had the ticket [GH-153] marked as not backwards compatible, but seem i missed adding it to the changelog.

EnvVar were previously rending incorrectly, which is why you also had to specify UserVar().
With the above change, you can simplify your template by removing the use of UserVar:

from packerlicious import builder
aws_access_key = EnvVar("AWS_ACCESS_KEY_ID")
aws_secret_access_key = EnvVar("AWS_SECRET_ACCESS_KEY")
template = Template()
user_variables = [
    aws_access_key,
    aws_secret_access_key
]
template.add_variable(user_variables)
amazon_ebs_builder = builder.AmazonEbs(
    ami_name="my_ami",
    source_ami="source_ami",
    instance_type="t2.micro",
    region="us-east-1",
    access_key=Ref(aws_access_key),
    secret_key=Ref(aws_secret_access_key),
)
template.add_builder(amazon_ebs_builder)
print(template.to_json())

output:

{
  "builders": [
    {
      "access_key": "{{user `aws_access_key_id`}}",
      "ami_name": "my_ami",
      "instance_type": "t2.micro",
      "region": "us-east-1",
      "secret_key": "{{user `aws_secret_access_key`}}",
      "source_ami": "source_ami",
      "type": "amazon-ebs"
    }
  ],
  "variables": {
    "aws_access_key_id": "{{env `AWS_ACCESS_KEY_ID`}}",
    "aws_secret_access_key": "{{env `AWS_SECRET_ACCESS_KEY`}}"
  }
}

Let me know if this helps.

@mayn mayn self-assigned this Mar 18, 2019
@testworksau
Copy link
Author

I did notice a breaking change label on that particular issue, and went looking for mention of it in the docs.

In any case, I'm happy to report that the suggested change works 👍

@mayn
Copy link
Owner

mayn commented Mar 20, 2019

@testworksau , glad i was able to help.
Question would it help if I expanded out docs like I’ve done for
https://github.com/mayn/packer.py ?

@testworksau
Copy link
Author

Docs are always useful, but in my particular case, I'm not sure they would have helped. I didn't write the original python script for the packer build I was looking at; I was troubleshooting why a build that someone else in our company set up - which was working mid last year - had stopped working.

Of course, there was no commentary in the code as to why UserVar was being used in the first place 😄

@mayn mayn closed this as completed in 382f741 Mar 28, 2019
@mayn mayn added this to the 1.4.1 milestone Mar 28, 2019
@mayn mayn added the question label Mar 28, 2019
@mayn
Copy link
Owner

mayn commented Mar 28, 2019

thanks for the feedback @testworksau . i retroactively updated the changelog to mention this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants