Using DigitalOcean Volumes Block Storage with Trellis

I need to add a block storage in a droplet provisioned with Trellis. As far as I know, this block is mounted as a HD. So, is it possible to tell Trellis that must use it as normal storage for bedrock projects inside?

Thank you!

Are you wanting to use block storage for the entire Bedrock project or just the uploads?

This is what I’m using for DO’s block storage to handle the uploads folder (props to @QWp6t):

# roles/wordpress-setuptasks/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
7 Likes

That sounds good. Thank you. I will try it.

Where is the digitalocean_droplet value coming from?

BTW, I’m fairly new to Trellis in general (LOVE IT!), and completely new to anything deeper than the basic docs (this is my first time digging deeper than wordpress_sites.yml, vault.yml, hosts, etc). So, if you have any additional advice for this particular of deep dig (storing images in DO Spaces), please do share.

Late to the party, but digitalocean_droplet is the ID of the droplet you created for Trellis. I define it in vault.yml.

In which vault.yml? It seems like it ought to be in the environment’s vault.yml, yes?

That makes the most sense to me.

Just realized I tried this 2 years ago, wasn’t able to make it work then, still not able to make it work now. Doesn’t seem to be creating the volume, and unfortunately the logs just return plaintext vault information. If this still works for you, I’d love some hints

I recommend using GitHub - humanmade/S3-Uploads: The WordPress Plugin to Store Uploads on Amazon S3 now, which is compatible with DigitalOcean Spaces and some other providers as well, such as Cloudflare R2.

If you’re not interested in that solution and still looking for further support on getting this setup with Trellis, could you share your full roles/wordpress-setup/tasks/main.yml file, along with the entire output of those tasks when the provisioning is running?

1 Like