|
| 1 | +# Cloud Carbon Footprint |
| 2 | + |
| 3 | +## Prepare Azure |
| 4 | + |
| 5 | +Create an App registration, add a new client secret and save the following information: |
| 6 | + |
| 7 | +- Application (client) ID => AZURE_CLIENT_ID |
| 8 | +- Directory (tenant) ID => AZURE_TENANT_ID |
| 9 | +- client secret value (not Secret ID) => AZURE_CLIENT_SECRET |
| 10 | + |
| 11 | +Give this app registration rights in the subscriptions you want to track and add the following roles: |
| 12 | + |
| 13 | +- Reader |
| 14 | +- Billing Reader |
| 15 | +- Carbon Optimization Reader |
| 16 | + |
| 17 | +Install a VM in Azure and SSH to the machine e.g. using a private key: |
| 18 | + |
| 19 | +``` sh |
| 20 | +chmod 400 ~/.ssh/ccfkey.pem |
| 21 | +ssh -i ~/.ssh/ccfkey.pem carbonuser@1.1.1.1 |
| 22 | +``` |
| 23 | + |
| 24 | +## Install dependencies |
| 25 | + |
| 26 | +``` sh |
| 27 | +sudo apt install iptables-persistent nodejs |
| 28 | +nvm install --lts |
| 29 | +curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash |
| 30 | +export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" |
| 31 | +[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" |
| 32 | +nvm install --lts |
| 33 | +corepack enable yarn |
| 34 | +``` |
| 35 | + |
| 36 | +Check installed versions |
| 37 | + |
| 38 | +``` sh |
| 39 | +nvm --version |
| 40 | +yarn --version |
| 41 | +node --version |
| 42 | +npm --version |
| 43 | +``` |
| 44 | + |
| 45 | +## Install CCF |
| 46 | + |
| 47 | +### Download |
| 48 | + |
| 49 | +Download and unzip the latest release |
| 50 | + |
| 51 | +``` sh |
| 52 | +wget https://github.com/cloud-carbon-footprint/cloud-carbon-footprint/archive/refs/tags/latest.tar.gz |
| 53 | +tar -xzvf latest.tar.gz |
| 54 | +cd cloud-carbon-footprint-latest |
| 55 | +``` |
| 56 | + |
| 57 | +### Config .env |
| 58 | + |
| 59 | +Go to cloud-carbon-footprint folder and update the .env file |
| 60 | + |
| 61 | +``` sh |
| 62 | +sudo nano ~/cloud-carbon-footprint-latest/packages/api/.env |
| 63 | +``` |
| 64 | + |
| 65 | +with the following content (e.g. for azure) |
| 66 | + |
| 67 | +``` txt |
| 68 | +# Azure |
| 69 | +AZURE_USE_BILLING_DATA=true |
| 70 | +AZURE_INCLUDE_ESTIMATES=true |
| 71 | +AZURE_CLIENT_ID=<add here> |
| 72 | +AZURE_CLIENT_SECRET=<add here> |
| 73 | +AZURE_TENANT_ID=<add here> |
| 74 | +AZURE_AUTH_MODE=default |
| 75 | +AZURE_CONSUMPTION_CHUNKS_DAYS=0 |
| 76 | +AZURE_SUBSCRIPTION_CHUNKS=0 |
| 77 | +``` |
| 78 | + |
| 79 | +do the same for: |
| 80 | + |
| 81 | +``` sh |
| 82 | +sudo nano ~/cloud-carbon-footprint-latest/packages/cli/.env |
| 83 | +``` |
| 84 | + |
| 85 | +Use the `.env.template` as orientation for required variables. |
| 86 | + |
| 87 | +### Install and run |
| 88 | + |
| 89 | +Install and run cloud carbon footprint |
| 90 | + |
| 91 | +``` sh |
| 92 | +cd ~/cloud-carbon-footprint-latest |
| 93 | +yarn install |
| 94 | +``` |
| 95 | + |
| 96 | +You need to terminals. |
| 97 | + |
| 98 | +Run this in the first terminal: |
| 99 | + |
| 100 | +``` sh |
| 101 | +cd ~/cloud-carbon-footprint-latest |
| 102 | +npm run start-api |
| 103 | +``` |
| 104 | + |
| 105 | +and this in the second terminal: |
| 106 | + |
| 107 | +``` sh |
| 108 | +cd ~/cloud-carbon-footprint-latest |
| 109 | +yarn start |
| 110 | +``` |
| 111 | + |
| 112 | +The application should now run and be available on Port 3000. Open the URL (e.g. localhost:3000 or your-ip:3000) and check both terminals to see if there are any errors. The first run should take a while and required some refreshes on the web interface for me to start triggering the API to call and deliver data. |
| 113 | + |
| 114 | +## Optimize |
| 115 | + |
| 116 | +### Redirect to Port 80 |
| 117 | + |
| 118 | +Redirect port 3000 to standard http port 80. |
| 119 | + |
| 120 | +``` sh |
| 121 | +sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000 |
| 122 | +sudo netfilter-persistent save |
| 123 | +``` |
| 124 | + |
| 125 | +### Configure nginx |
| 126 | + |
| 127 | +TODO |
| 128 | + |
| 129 | +### Enable HTTPS |
| 130 | + |
| 131 | +TODO |
| 132 | + |
| 133 | +``` sh |
| 134 | +sudo apt install certbot python3-certbot-nginx |
| 135 | +sudo certbot --nginx |
| 136 | +sudo certbot renew --dry-run |
| 137 | +``` |
| 138 | + |
| 139 | +### Autostart |
| 140 | + |
| 141 | +TODO |
| 142 | + |
| 143 | +To automatically start cloud-carbon-footprint do the following: |
| 144 | + |
| 145 | +``` sh |
| 146 | +sudo nano /etc/systemd/system/cloud-carbon-footprint-api.service |
| 147 | +``` |
| 148 | + |
| 149 | +And add this content |
| 150 | + |
| 151 | +``` sh |
| 152 | +#!/bin/bash |
| 153 | +# Load nvm so that the proper Node version is available |
| 154 | +export NVM_DIR="/home/youruser/.nvm" |
| 155 | +[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" |
| 156 | +cd cloud-carbon-footprint-latest |
| 157 | +npm run start-api |
| 158 | +``` |
0 commit comments