• Trellis

Using DigitalOcean’s Block Storage with Trellis

DigitalOcean’s Block Storage feature allows you to attach scalable storage volumes to your droplets.

If you’re using DigitalOcean as your VPS provider with Trellis and not offloading your media library, then you might want to consider configuring your sites to use block storage volumes for your WordPress uploads directory.

# roles/wordpress-setup/tasks/main.yml

...
- block:
  - name: Check if block storage volume is mounted
    shell: "mountpoint -q {{ www_root }}/{{ item.key }}/shared/storage"
    with_dict: "{{ wordpress_sites }}"
    register: block_storage_volume_mounted
    changed_when: block_storage_volume_mounted.rc != 0
    failed_when: false

  - name: Check if block storage volume is present
    shell: "lsblk | grep -q {{ www_root }}/{{ item.key }}/shared/storage"
    with_dict: "{{ wordpress_sites }}"
    register: block_storage_volume_exists
    changed_when: block_storage_volume_exists.rc != 0
    failed_when: false

  - block:
    - name: Create block storage volume
      digital_ocean_block_storage:
        state: present
        command: create
        api_token: "{{ digitalocean_api_key }}"
        region: nyc3
        block_size: 30
        volume_name: "{{ item.key | replace('.', '-') }}-{{ env }}"
      with_dict: "{{ wordpress_sites }}"

    - name: Detach block storage volume
      digital_ocean_block_storage:
        state: absent
        command: attach
        api_token: "{{ digitalocean_api_key }}"
        region: nyc3
        droplet_id: "{{ digitalocean_droplet }}"
        volume_name: "{{ item.key | replace('.', '-') }}-{{ env }}"
      with_dict: "{{ wordpress_sites }}"

    - name: Attach block storage volume
      digital_ocean_block_storage:
        state: present
        command: attach
        api_token: "{{ digitalocean_api_key }}"
        region: nyc3
        droplet_id: "{{ digitalocean_droplet }}"
        volume_name: "{{ item.key | replace('.', '-') }}-{{ env }}"
      with_dict: "{{ wordpress_sites }}"

    - name: Format block storage volume
      filesystem:
        fstype: ext4
        force: no
        dev: "/dev/disk/by-id/scsi-0DO_Volume_{{ item.key | replace('.', '-') }}-{{ env }}"
        resizefs: yes
      with_dict: "{{ wordpress_sites }}"
    when: block_storage_volume_exists is failed

  - name: Unmount block storage volume
    mount:
      name: "{{ www_root }}/{{ item.key }}/shared/storage"
      state: unmounted
    with_dict: "{{ wordpress_sites }}"
    when: block_storage_volume_mounted is success and block_storage_volume_exists is failed

  - name: Mount block storage volume
    mount:
      name: "{{ www_root }}/{{ item.key }}/shared/storage"
      src: "/dev/disk/by-id/scsi-0DO_Volume_{{ item.key | replace('.', '-') }}-{{ env }}"
      fstype: ext4
      state: mounted
      boot: yes
      opts: defaults,nofail,discard
    with_dict: "{{ wordpress_sites }}"

  - name: "Set POSIX permissions for {{ web_user }}:{{ web_group }} in block storage"
    file:
      path: "{{ www_root }}/{{ item.key }}/shared/storage"
      owner: "{{ web_user }}"
      group: "{{ web_group }}"
      mode: "u=rwX,g=rX,o=rX"
      recurse: yes
      state: directory
    with_dict: "{{ wordpress_sites }}"

  - name: Create uploads folder
    file:
      state: directory
      path: "{{ www_root }}/{{ item.key }}/shared/storage/uploads"
    with_dict: "{{ wordpress_sites }}"
  when: env != 'development'
# group_vars/all/main.yml
...

digitalocean_api_key: "{{ vault_digitalocean_api_key }}" # Defined in group_vars/all/vault.yml
# roles/deploy/defaults/main.yml

project_shared_children:
  - path: web/app/uploads
    src: storage/uploads

Join the discussion on Roots Discourse

Join over 5,800 subscribers on our newsletter to get the latest Roots updates, along with occasional tips on building better WordPress sites.

Looking for WordPress plugin recommendations, the newest modern WordPress projects, and general web development tips and articles?

“Easily the best WordPress email I get.” Colin OBrien

Follow us on Twitter @rootswp

Ready to checkout?