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

Programatically changing Base URL #971

Closed
nicholasamorim opened this issue Jun 20, 2019 · 6 comments · Fixed by #1587
Closed

Programatically changing Base URL #971

nicholasamorim opened this issue Jun 20, 2019 · 6 comments · Fixed by #1587

Comments

@nicholasamorim
Copy link

nicholasamorim commented Jun 20, 2019

Hi, I've automated installation of Kiwi in an Ansible playbook.

However, the fact that I need to login and change the base url manually is a let down. Is there any way to change the Base URL programatically?

It's been a long time I don't use Django so should I just update django_site using a SQL command or should I write a migration for it?

@nicholasamorim
Copy link
Author

nicholasamorim commented Jun 20, 2019

Well, I have solved this by creating a template on ansible called 0002_update_sites.py.j2.

from __future__ import unicode_literals

from django.db import migrations


def update_sites(apps, schema_editor):
    """Populate the sites model"""
    Site = apps.get_model('sites', 'Site')

    # Update Kiwi base url
    site = Site.objects.get(id=1)
    site.domain = '{{ kiwi_base_url }}'
    site.name = '{{ kiwi_name }}'
    site.save()

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0001_squashed'),
    ]

    operations = [
        migrations.RunPython(update_sites)
    ]

So I template that to the server and mount it on the docker-compose on the tcms/migrations folder and only run the initial migration after that.

At first I went for an sql approach but the hassle on running the client, passing password, etc, made the migration approach a better way.

@atodorov
Copy link
Member

@nicholasamorim this is still a valid request. We should think a bit about it but it can be done.

Do you mind sending your ansible playbook as a pull request and we can take things from there?

@nicholasamorim
Copy link
Author

Okay, will do that later as the playbook is idempotent - so it automates from zero to the point that Kiwi is running. So I'm going to merge all tasks into one playbook as I use separate playbooks to ensure python, docker and docker-compose are always present. I'll open a PR soon.

@nicholasamorim
Copy link
Author

nicholasamorim commented Jun 25, 2019

I've simplified several playbooks into one to paste it in here. It depends on the roles ndench.python and geerlinguy.pip (can probably be replaced with ansible's pip module) and geerlinguy.docker.

Playbook steps:

  • Sets up Python
  • Sets up Docker and Docker-compose (and its dependencies).
  • Creates a Kiwi folder in the server (/opt/kiwi)
  • Copies docker-compose.yaml and local_settings.py to /opt/kiwi. We use our own docker-compose.yaml to add some env vars (smtp, for example) and change the passwords. You can also see the docker-compose mounts 0002_update_sites.py
  • Templates the 0002_update_sites.py file with the correct base url.
  • Brings stack up.
  • Waits for port 80 to be open.
  • Execute migrations.
#### Python
- name: Setting up Python
  hosts: kiwi
  become: yes
  roles:
    - role: ndench.python

  post_tasks:
    - name: Checking if /usr/bin/python symlink exists
      stat:
        path: /usr/bin/python
      register: python_symlink

    # This is linking the distro default Python3 - not 3.7 (this might change)
    - name: Pointing /usr/bin/python to python3
      file:
        src: /usr/bin/python3
        dest: /usr/bin/python
        state: link
      when: python_symlink.stat.exists == False

    - name: Installing pip
      apt:
        name: python3-pip
        state: present

#### Docker 
- name: Setting up Docker
  hosts: kiwi
  become: yes
  vars:
    docker_install_compose: true
    pip_install_packages:
      - name: docker
      - name: docker-compose

  pre_tasks:
    - name: Ensuring dependencies for docker-compose are present
      apt:
        name:
          - libffi-dev
          - libssl-dev
        state: present

  roles:
    - geerlingguy.pip
    - geerlingguy.docker

### Kiwi

- name: Setting Kiwi
  hosts: kiwi
  become: yes
  vars:
    kiwi_folder: /opt/kiwi
    kiwi_base_url: "some-kiwi-url"

  tasks:
    - name: Ensuring kiwi folder exists
      file:
        path: "{{ kiwi_folder }}"
        state: directory

    - name: Copying configuration files
      copy:
        src: "files/kiwi/{{ item }}"
        dest: "{{ kiwi_folder }}"
      loop:
        - docker-compose.yaml
        - local_settings.py

    - name: Copying base_url migration
      template:
        src: templates/kiwi/0002_update_sites.py.j2
        dest: "{{ kiwi_folder }}/0002_update_sites.py"

    - name: Bringing stack up!
      docker_compose:
        project_src: "{{ kiwi_folder }}"
        state: present
        restarted: yes

    - name: Wait for port 80 to become open on the host
      wait_for:
        port: 80

    - name: Executing migration
      shell: "docker exec -it kiwi_web /Kiwi/manage.py migrate"

Custom migration 0002_update_sites.py.j2 that Ansible templates and copies to server.

from __future__ import unicode_literals

from django.db import migrations

def update_sites(apps, schema_editor):
    """Populate the sites model"""
    Site = apps.get_model('sites', 'Site')

    # Update Kiwi base url
    site = Site.objects.get(id=1)
    site.domain = '{{ kiwi_base_url }}'
    site.name = 'Some Name for Kiwi TCMS Site'
    site.save()

class Migration(migrations.Migration):

    dependencies = [
        ('core', '0001_squashed'),
    ]

    operations = [
        migrations.RunPython(update_sites)
    ]

@atodorov
Copy link
Member

@nicholasamorim thanks for sharing code. Can you make this into a pull request so we can start discussing changes and code review ?

@atodorov
Copy link
Member

atodorov commented Jul 7, 2019

@nicholasamorim ping. Could you send the ansible playbook as a pull request so we can attribute it properly ?

schwarzkrieger added a commit to schwarzkrieger/Kiwi that referenced this issue Apr 28, 2020
schwarzkrieger added a commit to schwarzkrieger/Kiwi that referenced this issue Apr 28, 2020
schwarzkrieger added a commit to schwarzkrieger/Kiwi that referenced this issue Apr 28, 2020
schwarzkrieger added a commit to schwarzkrieger/Kiwi that referenced this issue May 7, 2020
schwarzkrieger added a commit to schwarzkrieger/Kiwi that referenced this issue May 10, 2020
schwarzkrieger added a commit to schwarzkrieger/Kiwi that referenced this issue May 12, 2020
schwarzkrieger added a commit to schwarzkrieger/Kiwi that referenced this issue May 13, 2020
atodorov pushed a commit that referenced this issue May 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants