|
| 1 | +--- |
| 2 | +- name: Install, configure Dokku and setup department-website application |
| 3 | + hosts: ec2 |
| 4 | + become: yes |
| 5 | + vars_prompt: |
| 6 | + - name: ssh_email |
| 7 | + prompt: "Enter email address for SSH key" |
| 8 | + private: no |
| 9 | + |
| 10 | + - name: SECRET_KEY |
| 11 | + prompt: "Enter SECRET_KEY" |
| 12 | + private: yes |
| 13 | + |
| 14 | + - name: DEBUG |
| 15 | + prompt: "Enter DEBUG (True/False)" |
| 16 | + private: no |
| 17 | + |
| 18 | + - name: LOCAL_DEVELOPMENT |
| 19 | + prompt: "Enter LOCAL_DEVELOPMENT (True/False)" |
| 20 | + private: no |
| 21 | + |
| 22 | + - name: DATABASE_URL |
| 23 | + prompt: "Enter DATABASE_URL" |
| 24 | + private: yes |
| 25 | + |
| 26 | + - name: DASHBOARD_URL |
| 27 | + prompt: "Enter DASHBOARD_URL" |
| 28 | + private: no |
| 29 | + |
| 30 | + - name: ENCRYPTION_SALT |
| 31 | + prompt: "Enter ENCRYPTION_SALT" |
| 32 | + private: yes |
| 33 | + |
| 34 | + - name: ENCRYPTION_ITERATION |
| 35 | + prompt: "Enter ENCRYPTION_ITERATION" |
| 36 | + private: no |
| 37 | + |
| 38 | + - name: PAYTM_MERCHANT_ID |
| 39 | + prompt: "Enter PAYTM_MERCHANT_ID" |
| 40 | + private: no |
| 41 | + |
| 42 | + - name: PAYTM_MERCHANT_KEY |
| 43 | + prompt: "Enter PAYTM_MERCHANT_KEY" |
| 44 | + private: yes |
| 45 | + |
| 46 | + - name: GOOGLE_CLIENT_ID |
| 47 | + prompt: "Enter GOOGLE_CLIENT_ID" |
| 48 | + private: no |
| 49 | + |
| 50 | + - name: GOOGLE_CLIENT_SECRET |
| 51 | + prompt: "Enter GOOGLE_CLIENT_SECRET" |
| 52 | + private: yes |
| 53 | + |
| 54 | + - name: EMAIL_HOST_USER |
| 55 | + prompt: "Enter EMAIL_HOST_USER" |
| 56 | + private: no |
| 57 | + |
| 58 | + - name: EMAIL_HOST_PASSWORD |
| 59 | + prompt: "Enter EMAIL_HOST_PASSWORD" |
| 60 | + private: yes |
| 61 | + |
| 62 | + tasks: |
| 63 | + - name: Update and upgrade apt packages |
| 64 | + apt: |
| 65 | + update_cache: yes |
| 66 | + upgrade: yes |
| 67 | + |
| 68 | + - name: Install required dependencies |
| 69 | + apt: |
| 70 | + name: |
| 71 | + - wget |
| 72 | + - curl |
| 73 | + - apt-transport-https |
| 74 | + - ca-certificates |
| 75 | + - gnupg |
| 76 | + state: present |
| 77 | + |
| 78 | + - name: Download Dokku bootstrap script |
| 79 | + get_url: |
| 80 | + url: https://dokku.com/install/v0.36.0/bootstrap.sh |
| 81 | + dest: /tmp/bootstrap.sh |
| 82 | + mode: "0755" |
| 83 | + |
| 84 | + - name: Install Dokku |
| 85 | + command: bash /tmp/bootstrap.sh |
| 86 | + environment: |
| 87 | + DOKKU_TAG: v0.36.0 |
| 88 | + args: |
| 89 | + creates: /usr/bin/dokku |
| 90 | + |
| 91 | + - name: Create Dokku app department-website |
| 92 | + command: dokku apps:create department-website |
| 93 | + args: |
| 94 | + creates: /home/dokku/department-website |
| 95 | + |
| 96 | + - name: Add python-poetry buildpack |
| 97 | + command: dokku buildpacks:add department-website https://github.com/moneymeets/python-poetry-buildpack.git |
| 98 | + |
| 99 | + - name: Add heroku-python buildpack |
| 100 | + command: dokku buildpacks:add department-website https://github.com/heroku/heroku-buildpack-python.git |
| 101 | + |
| 102 | + - name: Check if redis plugin exists |
| 103 | + command: dokku plugin:list |
| 104 | + register: plugin_list |
| 105 | + changed_when: false |
| 106 | + |
| 107 | + - name: Install Dokku redis plugin if missing |
| 108 | + command: dokku plugin:install https://github.com/dokku/dokku-redis.git redis |
| 109 | + when: "'redis' not in plugin_list.stdout" |
| 110 | + |
| 111 | + - name: Create redis service |
| 112 | + command: dokku redis:create dept-redis |
| 113 | + |
| 114 | + - name: Link redis with department-website |
| 115 | + command: dokku redis:link dept-redis department-website |
| 116 | + |
| 117 | + - name: Disable static file collection |
| 118 | + command: dokku config:set department-website DISABLE_COLLECTSTATIC=1 |
| 119 | + |
| 120 | + - name: Set environment variables in Dokku |
| 121 | + command: dokku config:set department-website |
| 122 | + SECRET_KEY="{{ SECRET_KEY }}" |
| 123 | + DEBUG="{{ DEBUG }}" |
| 124 | + LOCAL_DEVELOPMENT="{{ LOCAL_DEVELOPMENT }}" |
| 125 | + DATABASE_URL="{{ DATABASE_URL }}" |
| 126 | + DASHBOARD_URL="{{ DASHBOARD_URL }}" |
| 127 | + ENCRYPTION_SALT="{{ ENCRYPTION_SALT }}" |
| 128 | + ENCRYPTION_ITERATION="{{ ENCRYPTION_ITERATION }}" |
| 129 | + PAYTM_MERCHANT_ID="{{ PAYTM_MERCHANT_ID }}" |
| 130 | + PAYTM_MERCHANT_KEY="{{ PAYTM_MERCHANT_KEY }}" |
| 131 | + GOOGLE_CLIENT_ID="{{ GOOGLE_CLIENT_ID }}" |
| 132 | + GOOGLE_CLIENT_SECRET="{{ GOOGLE_CLIENT_SECRET }}" |
| 133 | + EMAIL_HOST_USER="{{ EMAIL_HOST_USER }}" |
| 134 | + EMAIL_HOST_PASSWORD="{{ EMAIL_HOST_PASSWORD }}" |
| 135 | + |
| 136 | + - name: Generate SSH key on the EC2 server |
| 137 | + command: ssh-keygen -t rsa -b 4096 -C "{{ ssh_email }}" -f /home/ubuntu/id_rsa -N "" |
| 138 | + args: |
| 139 | + creates: /home/ubuntu/id_rsa |
| 140 | + |
| 141 | + - name: Read private key from server |
| 142 | + slurp: |
| 143 | + src: /home/ubuntu/id_rsa |
| 144 | + register: server_private_key |
| 145 | + |
| 146 | + - name: Get app config |
| 147 | + command: dokku config:show department-website |
| 148 | + register: app_config |
| 149 | + changed_when: false |
| 150 | + |
| 151 | + - name: Print app config |
| 152 | + debug: |
| 153 | + msg: "{{ app_config.stdout_lines }}" |
| 154 | + |
| 155 | + - name: Show private key in output |
| 156 | + debug: |
| 157 | + msg: "{{ server_private_key.content | b64decode | split('\n') }}" |
| 158 | + |
| 159 | + - name: Clear configs |
| 160 | + set_fact: |
| 161 | + app_config: "" |
| 162 | + server_private_key: "" |
0 commit comments