captainVersion: 4
services:
    # Mastodon
    $$cap_appname:
        environment:
            LOCAL_DOMAIN: $$cap_appname.$$cap_root_domain
            ALTERNATE_DOMAINS: $$cap_alt_domain
            REDIS_HOST: srv-captain--$$cap_appname-redis
            REDIS_PORT: 6379
            REDIS_PASSWORD: $$cap_redis_password
            DB_HOST: srv-captain--$$cap_appname-postgres
            DB_USER: mastodon
            DB_NAME: mastodon_production
            DB_PASS: $$cap_postgres_password
            DB_PORT: 5432
            ES_ENABLED: 'false'
            OTP_SECRET: $$cap_opt_secret_base
            SECRET_KEY_BASE: $$cap_secret_key_base
            VAPID_PRIVATE_KEY: $$cap_gen_random_hex(16)
            VAPID_PUBLIC_KEY: $$cap_gen_random_hex(16)
            SINGLE_USER_MODE: $$cap_su_mode
            SMTP_SERVER: $$cap_smtp_server
            SMTP_PORT: $$cap_smtp_port
            SMTP_LOGIN: $$cap_smtp_user
            SMTP_PASSWORD: $$cap_smtp_pass
            SMTP_FROM_ADDRESS: $$cap_smtp_from
            SMTP_TLS: $$cap_smtp_tls
            S3_ENABLED: 'false'
            RAILS_ENV: production
        volumes:
            - $$cap_appname-system:/mastodon/public/system
        caproverExtra:
            containerHttpPort: '3000'
            dockerfileLines:
                - FROM tootsuite/mastodon:$$cap_mastodon_version
                - CMD rm -f /mastodon/tmp/pids/server.pid; bundle exec rails db:setup; bundle exec rails s -b 0.0.0.0 -p 3000
    # Streaming
    $$cap_appname-streaming:
        environment:
            LOCAL_DOMAIN: $$cap_appname.$$cap_root_domain
            ALTERNATE_DOMAINS: $$cap_alt_domain
            REDIS_HOST: srv-captain--$$cap_appname-redis
            REDIS_PORT: 6379
            REDIS_PASSWORD: $$cap_redis_password
            DB_HOST: srv-captain--$$cap_appname-postgres
            DB_USER: mastodon
            DB_NAME: mastodon_production
            DB_PASS: $$cap_postgres_password
            DB_PORT: 5432
            ES_ENABLED: 'false'
            OTP_SECRET: $$cap_opt_secret_base
            SECRET_KEY_BASE: $$cap_secret_key_base
            VAPID_PRIVATE_KEY: $$cap_gen_random_hex(16)
            VAPID_PUBLIC_KEY: $$cap_gen_random_hex(16)
            SINGLE_USER_MODE: $$cap_su_mode
            SMTP_SERVER: $$cap_smtp_server
            SMTP_PORT: $$cap_smtp_port
            SMTP_LOGIN: $$cap_smtp_user
            SMTP_PASSWORD: $$cap_smtp_pass
            SMTP_FROM_ADDRESS: $$cap_smtp_from
            SMTP_TLS: $$cap_smtp_tls
            S3_ENABLED: 'false'
            RAILS_ENV: production
        volumes:
            - $$cap_appname-system:/mastodon/public/system
        caproverExtra:
            containerHttpPort: '4000'
            dockerfileLines:
                - FROM tootsuite/mastodon:$$cap_mastodon_version
                - CMD node ./streaming
    # Sidekiq
    $$cap_appname-sidekiq:
        environment:
            LOCAL_DOMAIN: $$cap_appname.$$cap_root_domain
            ALTERNATE_DOMAINS: $$cap_alt_domain
            REDIS_HOST: srv-captain--$$cap_appname-redis
            REDIS_PORT: 6379
            REDIS_PASSWORD: $$cap_redis_password
            DB_HOST: srv-captain--$$cap_appname-postgres
            DB_USER: mastodon
            DB_NAME: mastodon_production
            DB_PASS: $$cap_postgres_password
            DB_PORT: 5432
            ES_ENABLED: 'false'
            OTP_SECRET: $$cap_opt_secret_base
            SECRET_KEY_BASE: $$cap_secret_key_base
            VAPID_PRIVATE_KEY: $$cap_gen_random_hex(16)
            VAPID_PUBLIC_KEY: $$cap_gen_random_hex(16)
            SINGLE_USER_MODE: $$cap_su_mode
            SMTP_SERVER: $$cap_smtp_server
            SMTP_PORT: $$cap_smtp_port
            SMTP_LOGIN: $$cap_smtp_user
            SMTP_PASSWORD: $$cap_smtp_pass
            SMTP_FROM_ADDRESS: $$cap_smtp_from
            SMTP_TLS: $$cap_smtp_tls
            S3_ENABLED: 'false'
            RAILS_ENV: production
        volumes:
            - $$cap_appname-system:/mastodon/public/system
        caproverExtra:
            dockerfileLines:
                - FROM tootsuite/mastodon:$$cap_mastodon_version
                - CMD bundle exec sidekiq
            notExposeAsWebApp: 'true'
    # PostgreSQL
    $$cap_appname-postgres:
        image: postgres:12
        volumes:
            - $$cap_appname-postgres-data:/var/lib/postgresql/data
        environment:
            POSTGRES_USER: mastodon
            POSTGRES_PASSWORD: $$cap_postgres_password
            POSTGRES_DB: mastodon_production
        caproverExtra:
            notExposeAsWebApp: 'true'
    # Redis
    $$cap_appname-redis:
        volumes:
            - $$cap_appname-redis-data:/data
        environment:
            REDIS_PASSWORD: $$cap_redis_password
        caproverExtra:
            dockerfileLines:
                - FROM redis:alpine
                - CMD exec redis-server --requirepass "$$cap_redis_password"
            notExposeAsWebApp: 'true'
caproverOneClickApp:
    variables:
        - id: $$cap_mastodon_version
          label: Mastodon Version Tag
          description: You can use latest tag or check version builds on https://hub.docker.com/r/tootsuite/mastodon/tags
          defaultValue: v3.3.0

        - id: $$cap_secret_key_base
          defaultValue: $$cap_gen_random_hex(64)
          label: Secret Key Base
          description: The randomized string which is used to verify the integrity of signed cookies. Please use a string with more than 26 characters
          validRegex: /^[^\@]{26,}$/

        - id: $$cap_opt_secret_base
          defaultValue: $$cap_gen_random_hex(64)
          label: One-time password secret
          description: Two-Factor Authentication (2FA) Key
          validRegex: /^[^\@]{26,}$/

        - id: $$cap_postgres_password
          defaultValue: $$cap_gen_random_hex(12)
          label: Postgres Password
          description: Password must be at least 12 characters.  Please use a random string.
          validRegex: /^[^\@]{12,}$/

        - id: $$cap_redis_password
          defaultValue: $$cap_gen_random_hex(12)
          label: Redis Password
          description: Password must be at least 12 characters. Please use a random string.
          validRegex: /^[^\@]{12,}$/

        - id: $$cap_alt_domain
          label: Alternate domains (optional)
          description: If you have multiple domains pointed at your Mastodon server, this setting will allow Mastodon to recognize itself when users are addressed using those other domains. Separate the domains by commas, e.g. foo.com,bar.com

        - id: $$cap_su_mode
          label: Single user mode
          defaultValue: 'false'
          description: If set to true, the frontpage of your Mastodon server will always redirect to the first profile in the database and registrations will be disabled.

        - id: $$cap_smtp_server
          label: SMTP hostname
          validRegex: /^([^\s^\/])+$/

        - id: $$cap_smtp_user
          label: SMTP username
          validRegex: /^([^\s^\/])+$/

        - id: $$cap_smtp_from
          label: SMTP from
          validRegex: /^([^\s^\/])+$/

        - id: $$cap_smtp_pass
          label: SMTP password
          validRegex: /^([^\s^\/])+$/

        - id: $$cap_smtp_port
          label: SMTP port
          validRegex: /^([^\s^\/])+$/

        - id: $$cap_smtp_tls
          label: SMTP TLS
          validRegex: /^([^\s^\/])+$/
    instructions:
        start: >-
            Before you proceed keep a note of the following.

            --------------------------------------------------


            After you deploy mastodon and created your fist user in the browser, you will need to use the command line to give your newly created account admin privileges.


            Step 1: From your terminal run the following command.

            docker exec -it $(docker ps --filter name='srv-captain--{appname}.1' -q) /bin/bash


            Step 2: Now that you are inside your mastodon container: run the following command assuming your username is alice:

            "RAILS_ENV=production bin/tootctl accounts modify alice --role admin"


            Step 3: Now we have to give our persistent directory the correct user rights

            From your terminal run the following assuming your app name is mastodon

            chown -R 991:991 /var/lib/docker/volumes/captain--{appname}-system
        end: >-
            Aaaand you're done! 😄

            Your service is available at http://$$cap_appname.$$cap_root_domain


            You can add more environment variables to use with aws s3 for example

            Check out https://docs.joinmastodon.org/admin/config/ for more env vars
    displayName: Mastodon
    isOfficial: true
    description: Social networking, back in your hands.
    documentation: 'Read more at: https://docs.joinmastodon.org/'
