caktus / margarita

A collection of delicious Salt states for Django project deployments.
BSD 3-Clause "New" or "Revised" License
34 stars 8 forks source link

Set hostname #122

Open vkurup opened 8 years ago

vkurup commented 8 years ago

rsyslog uses hostname --fqdn to determine its hostname. Papertrail uses that hostname to help determine which 'system' to send the logs to. If hostname --fqdn of a newly launched system is set to 'localhost' (as is the default), and if an existing system already has that same hostname (which is likely since our current deploy process doesn't change the hostname), then Papertrail will send the new system's logs to the existing system.

Example: https://s3.amazonaws.com/uploads.hipchat.com/62946/455126/kS5Bl9FQrxnCX4o/upload_thumb.png

How do you set hostname --fqdn?

It is not enough to use the hostname command or /etc/hostname. That only sets hostname --short. You need to update /etc/hosts so that the FIRST entry after the IP address is your desired hostname.

This is not good:

127.0.0.1   localhost myproject-staging

This is good:

127.0.0.1   myproject-staging localhost

This also works:

#1.2.3.4 is the internal IP of the server
1.2.3.4  myproject-staging

So I was able to fix it in my project by doing that, but I'm not sure how to generalize this for margarita. Would this work for a multiserver setup?

My "fix":

# project/hostname.sls
{% import 'project/_vars.sls' as vars with context %}
{%- set hostname = pillar['project_name'] + "-" + pillar['environment'] %}

# Map internal IP to our desired hostname.
host-entry:
  host.present:
    - ip: {{ vars.current_ip }}
    - names:
      - {{ hostname }}

/etc/hostname:
  file.managed:
    - contents: {{ hostname }}
    - backup: false

set-hostname:
  cmd.wait:
    - name: hostname {{ hostname }}
    - watch:
      - file: /etc/hostname
dpoirier commented 8 years ago

After thinking about this off and on, I think the hard problem is to come up with unique hostnames for all servers, given the way we currently deploy. If we have more than one server, then pillar['project_name'] + "-" + pillar['environment'] won't be unique, but nothing in our fabfile would know it.

If we appended something else to project_name-environment that made it unique, say the IP address or mac address, would papertrail still be able to identify the "system"? Or we could use "{ip_address}.{environment}.{project}" if that would work better for papertrail.