Hi! For some reason the `user_data` that I'm suppl...
# general
b
Hi! For some reason the
user_data
that I'm supplying to my ec2 instances isn't getting run. When I log in with ssh and run the same command (python -m SimpleHTTPServer), everything works as expected. Is there any way to debug this situation?
this is the code (copied mostly from the python ec2 webserver example)
Copy code
group = ec2.SecurityGroup('web-secgrp',
    description='Enable HTTP access',
    ingress=[
       { 'protocol': 'tcp', 'from_port': 22, 'to_port': 22, 'cidr_blocks': ['0.0.0.0/0'] },
       { 'protocol': 'tcp', 'from_port': 8888, 'to_port': 8888, 'cidr_blocks': ['0.0.0.0/0'] }
    ])

user_data = """
#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 8888 &
"""

server = ec2.Instance('web-server-www',
    instance_type=instance_type,
    key_name=key_pair.key_name,
    security_groups=[group.name],
    user_data=user_data,
    ami=ami)
the
key_pair
is a local .pem key that I use to be able to log in
m
@incalculable-sundown-82514 any ideas?
i
can you look at
/var/log/cloud-init-output.log
on your VM? If the user_data script ran, there should be some logs there.
If not, it’s possible we dropped it when creating the resource
b
thanks @incalculable-sundown-82514 I'll take a look. In what situations would we 'drop it' ?
i
If there’s a bug in Pulumi, I mean.
b
aaah
haha, okay. Let me check the log and see
Yep, something went wrong there:
Copy code
Cloud-init v. 18.3-9-g2e62cb8a-0ubuntu1~16.04.2 running 'modules:final' at Fri, 21 Sep 2018 18:01:08 +0000. Up 14.37 seconds.
2018-09-21 18:01:09,080 - util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [-]
2018-09-21 18:01:09,088 - cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
2018-09-21 18:01:09,088 - util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python3/dist-packages/cloudinit/config/cc_scripts_user.py'>) failed
Cloud-init v. 18.3-9-g2e62cb8a-0ubuntu1~16.04.2 finished at Fri, 21 Sep 2018 18:01:09 +0000. Datasource DataSourceEc2Local.  Up 14.49 seconds
@incalculable-sundown-82514 what would the above suggest? Is there something that's missing on the target machine? Wrong version of cloudinit in my local virtualenv?
i
hmm, I’m not totally sure.
There’s not a lot of detail in this log about why these things failed 😞
b
Yes indeed
at least it could have told us the line or something
i
can you see the contents of
/var/lib/cloud/instance/scripts/scripts-user
?
maybe you could run that and see what happens?
b
Copy code
2018-09-21 18:01:09,088 - util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python3/dist-packages/cloudinit/config/cc_scripts_user.py'>) failed
2018-09-21 18:01:09,088 - util.py[DEBUG]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python3/dist-packages/cloudinit/config/cc_scripts_user.py'>) failed
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/cloudinit/stages.py", line 806, in _run_modules
    freq=freq)
  File "/usr/lib/python3/dist-packages/cloudinit/cloud.py", line 54, in run
    return self._runners.run(name, functor, args, freq, clear_on_fail)
  File "/usr/lib/python3/dist-packages/cloudinit/helpers.py", line 188, in run
    return (True, results)
  File "/usr/lib/python3.5/contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/lib/python3/dist-packages/cloudinit/helpers.py", line 76, in lock
    yield self._acquire(name, freq)
  File "/usr/lib/python3/dist-packages/cloudinit/helpers.py", line 187, in run
    results = functor(*args)
  File "/usr/lib/python3/dist-packages/cloudinit/config/cc_scripts_user.py", line 45, in handle
    util.runparts(runparts_path)
  File "/usr/lib/python3/dist-packages/cloudinit/util.py", line 850, in runparts
    % (len(failed), len(attempted)))
that's from
cloud-init.log
Copy code
root@ip-172-31-41-50:~# cat /var/lib/cloud/instance/scripts/scripts-user
cat: /var/lib/cloud/instance/scripts/scripts-user: No such file or directory
i
hmm
can you run
pulumi stack export
on your stack and look for a json field named “user_data”?
just to confirm that Pulumi correctly passed user_data to AWS
b
what json key am I looking for?
user_data
?
i
yep, that’s the one!
Pulumi should have recorded it
pulumi stack export
basically dumps the entire state of your stack
b
Copy code
"userData": "\n#!/bin/bash\necho \"Hello, World!\" \u003e index.html\nnohup python -m SimpleHTTPServer 8888 \u003e /tmp/server 2\u003e\u00261 \u0026\nsleep 60\n"
it's there, with some minor changes that I just made (sleep 60)
i
okay, thanks! So it would seem that we’re sending it to Amazon OK, but it’s failing to run for some reason
though the unicode escapes in there look a /little/ suspect
hmm
b
yes they do
oh oh
let me specify the user_data with u"blah"
maybe this is a python2/python3 thing here
i
very possible
though if this is a unicode problem, it’s still a bug in Pulumi - we shouldn’t be messing up strings this badly
Python strings… 😢
b
Yeah, let's see if this is it. Would be good.
Still the same, even with user_data = u"thing"
same escapes
i
hmm
and you’re using python 2?
b
I'm using python2, yes
i
OK, what OS are you using?
b
Linux
Void linux to be precise -- locally, of course. The AMI is ubuntu
i
okay, thanks. I’ll try to repro this
b
Thank you @incalculable-sundown-82514
i
OK, so on my machine, this is what gets sent to AWS for user_data:
Copy code
"userData": "\n#!/bin/bash\necho \"Hello, World!\" > index.html\nnohup python -m SimpleHTTPServer 80 &\n"
this is on a mac using python 2.7.15
so the mystery is how these unicode escapes are appearing in the string
are those bytes in the source file by any chance?
b
Shouldn't be. Let me open it with another editor
no, but the string is a triple-quoted string. Maybe single-quotes might change this?
i
It’s triple-quoted in my example, too
b
okay, well at least we have something to go on. I'll try to see why this keeps getting escaped
i
ok, thanks! let me know if I can help at all
b
Will do Sean. I'm going to see whether this is a json encoder issue, perhaps. Try to wipe my virtualenv and only use pulumi
Well, this is strange -- the escapes are there is the export, but the userdata was run successfully. Well, at least I can see that it's some kind of weird interaction on my machine.
i
that is very strange!
b
You know what, I think I can trace this back to a failed pulumi stack new, when it tried to install something outside a virtualenv
so I nuked the virtualenv and reinstalled things in order and now it all works
Thank you for your help Sean!
i
yay! no problem, glad it worked out!
b
the unicode stuff is a red herring, probably an artifact of json encoding
So it worked once, and how this:
Copy code
2018-09-21 19:20:37,586 - __init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: 'b'echo ok > ok'...'
i
Now that is truly strange
b
this is with my old code, not the python webserver code, so the scenario is more complex. I set up userdata with the output of previous instances, so that workers can connect to a cluster leader.
"old code" == code that triggered the bug initially before I tried to repro on reduced example (python -m webserver)
i
I am very surprised that the userdata is not
text/plain