|
| 1 | +########################################### |
| 2 | +# IMPORTANT |
| 3 | +# Comments matter! |
| 4 | +# The docs use the wrapping comments as |
| 5 | +# markers for including said instructions |
| 6 | +# as snippets in the docs. |
| 7 | +########################################### |
| 8 | +summary: How to create async Flask Charm |
| 9 | + |
| 10 | +kill-timeout: 90m |
| 11 | + |
| 12 | +environment: |
| 13 | + |
| 14 | +execute: | |
| 15 | + # Move everything to $HOME so that Juju deployment works |
| 16 | + mv *.yaml *.py *.txt $HOME |
| 17 | + cd $HOME |
| 18 | +
|
| 19 | + # Don't use the staging store for this test |
| 20 | + unset CHARMCRAFT_STORE_API_URL |
| 21 | + unset CHARMCRAFT_UPLOAD_URL |
| 22 | + unset CHARMCRAFT_REGISTRY_URL |
| 23 | +
|
| 24 | + # Add setup instructions |
| 25 | + snap install rockcraft --channel=latest/edge --classic |
| 26 | +
|
| 27 | + snap install microk8s --channel=1.31-strict/stable |
| 28 | + snap install juju --channel=3/stable |
| 29 | +
|
| 30 | + mkdir -p ~/.local/share |
| 31 | +
|
| 32 | + # MicroK8s config setup |
| 33 | + microk8s status --wait-ready |
| 34 | + microk8s enable hostpath-storage |
| 35 | + microk8s enable registry |
| 36 | + microk8s enable ingress |
| 37 | +
|
| 38 | + # Bootstrap controller |
| 39 | + juju bootstrap microk8s dev-controller |
| 40 | +
|
| 41 | + cd $HOME |
| 42 | + # [docs:create-venv] |
| 43 | + sudo apt-get update && sudo apt-get install python3-venv -y |
| 44 | + python3 -m venv .venv |
| 45 | + source .venv/bin/activate |
| 46 | + pip install -r requirements.txt |
| 47 | + # [docs:create-venv-end] |
| 48 | +
|
| 49 | + flask run -p 8000 & |
| 50 | + retry -n 5 --wait 2 curl --fail localhost:8000 |
| 51 | +
|
| 52 | + # [docs:curl-flask] |
| 53 | + curl localhost:8000 |
| 54 | + # [docs:curl-flask-end] |
| 55 | +
|
| 56 | + # [docs:curl-flask-async-app] |
| 57 | + curl localhost:8000/io |
| 58 | + # [docs:curl-flask-async-app-end] |
| 59 | +
|
| 60 | + kill $! |
| 61 | +
|
| 62 | + # [docs:create-rockcraft-yaml] |
| 63 | + rockcraft init --profile flask-framework |
| 64 | + # [docs:create-rockcraft-yaml-end] |
| 65 | +
|
| 66 | + sed -i "s/name: .*/name: flask-async-app/g" rockcraft.yaml |
| 67 | + sed -i "s/amd64/$(dpkg --print-architecture)/g" rockcraft.yaml |
| 68 | +
|
| 69 | + # [docs:pack] |
| 70 | + rockcraft pack |
| 71 | + # [docs:pack-end] |
| 72 | +
|
| 73 | + # [docs:ls-rock] |
| 74 | + ls *.rock -l |
| 75 | + # [docs:ls-rock-end] |
| 76 | +
|
| 77 | + # [docs:skopeo-copy] |
| 78 | + rockcraft.skopeo --insecure-policy copy --dest-tls-verify=false \ |
| 79 | + oci-archive:flask-async-app_0.1_$(dpkg --print-architecture).rock \ |
| 80 | + docker://localhost:32000/flask-async-app:0.1 |
| 81 | + # [docs:skopeo-copy-end] |
| 82 | +
|
| 83 | + # [docs:create-charm-dir] |
| 84 | + mkdir charm |
| 85 | + cd charm |
| 86 | + # [docs:create-charm-dir-end] |
| 87 | +
|
| 88 | + # [docs:charm-init] |
| 89 | + charmcraft init --profile flask-framework --name flask-async-app |
| 90 | + # [docs:charm-init-end] |
| 91 | +
|
| 92 | + sed -i "s/paas-charm.*/https:\/\/github.com\/canonical\/paas-charm\/archive\/async-workers.tar.gz/g" requirements.txt |
| 93 | +
|
| 94 | + # [docs:charm-pack] |
| 95 | + charmcraft pack |
| 96 | + # [docs:charm-pack-end] |
| 97 | +
|
| 98 | + # [docs:ls-charm] |
| 99 | + ls *.charm -l |
| 100 | + # [docs:ls-charm-end] |
| 101 | +
|
| 102 | + # [docs:add-juju-model] |
| 103 | + juju add-model flask-async-app |
| 104 | + # [docs:add-juju-model-end] |
| 105 | +
|
| 106 | + juju set-model-constraints -m flask-async-app arch=$(dpkg --print-architecture) |
| 107 | +
|
| 108 | + # [docs:deploy-juju-model] |
| 109 | + juju deploy ./flask-async-app_ubuntu-22.04-$(dpkg --print-architecture).charm \ |
| 110 | + flask-async-app --resource \ |
| 111 | + flask-app-image=localhost:32000/flask-async-app:0.1 |
| 112 | + # [docs:deploy-juju-model-end] |
| 113 | +
|
| 114 | + # [docs:deploy-nginx] |
| 115 | + juju deploy nginx-ingress-integrator --channel=latest/edge --base [email protected] |
| 116 | + juju integrate nginx-ingress-integrator flask-async-app |
| 117 | + # [docs:deploy-nginx-end] |
| 118 | +
|
| 119 | + # [docs:config-nginx] |
| 120 | + juju config nginx-ingress-integrator \ |
| 121 | + service-hostname=flask-async-app path-routes=/ |
| 122 | + # [docs:config-nginx-end] |
| 123 | +
|
| 124 | + # give Juju some time to deploy the apps |
| 125 | + juju wait-for application flask-async-app --query='status=="active"' --timeout 10m |
| 126 | + juju wait-for application nginx-ingress-integrator --query='status=="active"' --timeout 10m |
| 127 | +
|
| 128 | + # [docs:curl-init-deployment] |
| 129 | + curl http://flask-async-app --resolve flask-async-app:80:127.0.0.1 |
| 130 | + # [docs:curl-init-deployment-end] |
| 131 | +
|
| 132 | + # [docs:config-async] |
| 133 | + juju config flask-async-app webserver-worker-class=gevent |
| 134 | + # [docs:config-async-end] |
| 135 | +
|
| 136 | + juju wait-for application flask-async-app --query='status=="active"' --timeout 10m |
| 137 | +
|
| 138 | + # test the async flask service |
| 139 | + NUM_REQUESTS=15 |
| 140 | + ASYNC_RESULT='TRUE' |
| 141 | +
|
| 142 | + echo "Firing $NUM_REQUESTS requests to http://flask-async-app/io..." |
| 143 | +
|
| 144 | + overall_start_time=$(date +%s) |
| 145 | +
|
| 146 | + for i in $(seq 1 $NUM_REQUESTS); do |
| 147 | + ( |
| 148 | + start_time=$(date +%s) |
| 149 | + echo "Request $i start time: $start_time" |
| 150 | +
|
| 151 | + curl -s http://flask-async-app/io --resolve flask-async-app:80:127.0.0.1 |
| 152 | +
|
| 153 | + end_time=$(date +%s) |
| 154 | + pass_time=$((end_time - start_time)) |
| 155 | + echo "Request $i end time: $end_time == $pass_time" |
| 156 | + ) & |
| 157 | + done |
| 158 | +
|
| 159 | + wait |
| 160 | + end_time=$(date +%s) |
| 161 | + overall_passtime=$((end_time - overall_start_time)) |
| 162 | + echo "Total pass time: $overall_passtime" |
| 163 | + if [ $((3 < overall_passtime)) -eq 1 ]; then |
| 164 | + echo "Error!" |
| 165 | + ASYNC_RESULT='FALSE' |
| 166 | + exit 2 |
| 167 | + fi |
| 168 | + [ "$ASYNC_RESULT" == 'TRUE' ] |
| 169 | +
|
| 170 | + # Back out to main directory for clean-up |
| 171 | + cd .. |
| 172 | +
|
| 173 | + # [docs:clean-environment] |
| 174 | + # exit and delete the virtual environment |
| 175 | + deactivate |
| 176 | + rm -rf charm .venv __pycache__ |
| 177 | + # delete all the files created during the tutorial |
| 178 | + rm flask-async-app_0.1_$(dpkg --print-architecture).rock rockcraft.yaml app.py \ |
| 179 | + requirements.txt migrate.py |
| 180 | + # Remove the juju model |
| 181 | + juju destroy-model flask-async-app --destroy-storage --no-prompt --force |
| 182 | + # [docs:clean-environment-end] |
0 commit comments