m110 / packer-builder-hcloud

Packer builder plugin for Hetzner Cloud - https://hetzner.cloud
MIT License
25 stars 4 forks source link

Shell provider not working? #3

Open krepflap opened 6 years ago

krepflap commented 6 years ago

Thanks for this project.

Is it possible the shell provider isn't working?

hcloud output will be in this color.                                                   

==> hcloud: Creating temporary ssh key                                                   
==> hcloud: Creating new server: packer-hcloud-1525943403                                                                                                      
==> hcloud: Created server 692159                                                                                                             
==> hcloud: Waiting for the server to be running...                               
==> hcloud: Waiting for SSH to become available...                                          
==> hcloud: Connected to SSH!                     
==> hcloud: Provisioning with shell script: /tmp/packer-shell525686023                      
==> hcloud: Waiting for server 692159 to be destroyed...                                                                                                       
==> hcloud: Deleting temporary ssh key...
==> hcloud: Got state error: Retryable error: Error uploading script: connection is shut down
Build 'hcloud' errored: Retryable error: Error uploading script: connection is shut down

==> Some builds didn't complete successfully and had errors:
--> hcloud: Retryable error: Error uploading script: connection is shut down

==> Builds finished but no artifacts were created.

The step ==> hcloud: Provisioning with shell script: ... takes a long time.

Packer definition:

{
  "variables": {
    "hcloud_token": "{{ env `HCLOUD_TOKEN` }}"
  },
  "builders": [
    {
      "type": "hcloud",
      "token": "{{ user `hcloud_token` }}",
      "server_type": "cx11",
      "location": "nbg1",
      "source_image": "ubuntu-18.04",
      "image_name": "k8s",
      "ssh_username": "root"
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "inline": [
        "apt-get update",
        "apt-get -y dist-upgrade",
        "apt-get install -y sudo python",
        "apt-get autoremove -y",
        "apt-get clean"
      ]
    }
  ]
}

I've just tried with ansible provisioner and that works (using paramiko).

Thanks!

m110 commented 6 years ago

Hi @krepflap. I had similar issues and I think it may be due to Hetzner's ubuntu image doing apt upgrade on boot.

I tried to work around it like this:

https://github.com/m110/infrastructure-as-code/blob/master/packer/hcloud/base.json https://github.com/m110/infrastructure-as-code/blob/master/packer/scripts/python.sh

It did help with this issue, but it's far from elegant solution. :(

If all you need is to install python, you could consider setting cloudinit data in user_data field. I tested in only with Terraform, but it should work as well with the packer build.

You could try:

#cloud-config
packages:
  - python
arosenhagen commented 6 years ago

are there any news on this issue? neither the shell solution nor the proposed cloudinit solution ("user_data": "#cloud-config\npackages:\n - python\n") work for me. So atm there is no way to start ubuntu servers with ansible provisioning...

krepflap commented 6 years ago

@arosenhagen : I ended up doing it like this since I wanted to provision with ansible as well. In the playbook you call from packer:

[...]
  tasks:
    - name: Kill auto apt update
      raw: pkill -9 apt

    - name: Update apt
      raw: apt-get -qq update

    - name: Install python and sudo
      raw: apt-get install -y -qq python sudo

Next task you can use normal ansible modules... Of course this is a nasty workaround, but waiting for the locks to clear while the Hetzner images were auto updating gave me way too much issues.

krepflap commented 6 years ago

Don't forget in a next task to actually update the packages of course, since we killed that process, something like:

    - name: Update all packages
      apt:
        name: "*"
        state: latest
        update_cache: no
        autoremove: yes
arosenhagen commented 6 years ago

I don't think this will work. In order to use ansible you already have to have python installed:

TASK [Gathering Facts] *********************************************************
    fatal: [default]: FAILED! => {"changed": false, "module_stderr": "/bin/sh: 1: /usr/bin/python: not found\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 127}

So the only way is to actually use the shell provisioner (which actually fails with a timeout).

krepflap commented 6 years ago

It works, you have to disable gather facts on the first play though. Here is the full code of the bootstrap play. After that you can do whatever, including gathering facts etc. Ansible can work with raw module without python.

---
- name: Bootstrap server
  hosts: all
  gather_facts: no
  tasks:
    - name: Kill auto apt update
      raw: pkill -9 apt

    - name: Update apt
      raw: apt-get -qq update

    - name: Install python and sudo
      raw: apt-get install -y -qq python sudo

    - name: Update all packages
      apt:
        name: "*"
        state: latest
        update_cache: no
        autoremove: yes

    - name: Cleanup
      command: apt-get clean
arosenhagen commented 6 years ago

thx! that works out in the end ;-)