|
| 1 | +If you want to use a persistent GCE instance to manage your bot, use this config. A persistent GCE instance offers more flexibility in line with more overhead, say if you want to run sidecar services and so on. Use the cloudrun config if you're just looking for the quick and dirty. |
| 2 | + |
| 3 | +Firstly, set up your environment and create the VM |
| 4 | + |
| 5 | +```bash |
| 6 | +export TF_VAR_zone="us-central1-c" |
| 7 | +export TF_VAR_instance_name="skip-mev-juno-arb" |
| 8 | +export PROJECT=$(gcloud config get-value project) |
| 9 | + |
| 10 | +# Copy the .template files |
| 11 | +cp variables.template variables.tf |
| 12 | +cp provider.template provider.tf |
| 13 | + |
| 14 | +# Dynamic substitution for the terraform config |
| 15 | +sed -i "" s/PROJECT_ID/$PROJECT/g variables.tf |
| 16 | +sed -i "" s/PROJECT_ID/$PROJECT/g provider.tf |
| 17 | +sed -i "" s/ZONE/$TF_VAR_zone/g variables.tf |
| 18 | +sed -i "" s/ZONE/$TF_VAR_zone/g provider.tf |
| 19 | +sed -i "" s/INSTANCE_NAME/$TF_VAR_instance_name/g variables.tf |
| 20 | + |
| 21 | +# You can always reset to the template with the following |
| 22 | +# sed -i "" s/$PROJECT/PROJECT_ID/g variables.template |
| 23 | +# sed -i "" s/$PROJECT/PROJECT_ID/g provider.template |
| 24 | +# sed -i "" s/$TF_VAR_zone/ZONE/g variables.template |
| 25 | +# sed -i "" s/$TF_VAR_zone/ZONE/g provider.template |
| 26 | +# sed -i "" s/$TF_VAR_instance_name/INSTANCE_NAME/g variables.template |
| 27 | + |
| 28 | +# Initialize terraform state in GCS |
| 29 | +terraform init \ |
| 30 | + -backend-config="bucket=$PROJECT-tfstate" \ |
| 31 | + -backend-config="prefix=$TF_VAR_instance_name" |
| 32 | +terraform apply |
| 33 | +``` |
| 34 | + |
| 35 | +Now, we can copy the bot to the instance. Make sure the mnemonic in your `.env` is popualted correctly. |
| 36 | + |
| 37 | +```bash |
| 38 | +gcloud compute scp --recurse ../../../*.py ../../../*.json ../../../*.txt ../../../.env ubuntu@$TF_VAR_instance_name:/home/ubuntu --zone $TF_VAR_zone |
| 39 | + |
| 40 | +# SSH onto the machine and follow setup instructions for the bot, namely |
| 41 | +gcloud compute ssh --zone $TF_VAR_zone ubuntu@$TF_VAR_instance_name --tunnel-through-iap --project $PROJECT |
| 42 | + |
| 43 | +# Set up dependencies to run from the root, so that the startup script can function |
| 44 | +sudo apt update |
| 45 | +sudo apt-get install python3-pip -y |
| 46 | +sudo mkdir -p /app && sudo cp ~/.env ~/* /app |
| 47 | +cd /app |
| 48 | +sudo pip install -r requirements.txt |
| 49 | +exit |
| 50 | +``` |
| 51 | + |
| 52 | +[Optionally, but recommended] install the cloudwatch ops agent |
| 53 | +*You can also install via the console, by visiting https://console.cloud.google.com/monitoring/dashboards/resourceList/gce_instance* |
| 54 | + |
| 55 | +```bash |
| 56 | +cd ~/ && curl -sSO https://dl.google.com/cloudagents/add-google-cloud-ops-agent-repo.sh |
| 57 | +sudo bash add-google-cloud-ops-agent-repo.sh --also-install |
| 58 | +``` |
| 59 | + |
| 60 | +Finally, restart the instance. The bot is initialized via the startup script specified in the terraform config |
| 61 | + |
| 62 | +```bash |
| 63 | +gcloud compute instances reset $TF_VAR_instance_name --zone $TF_VAR_zone |
| 64 | +``` |
| 65 | + |
| 66 | +To verify the startup script worked, you can run `sudo journalctl -u google-startup-scripts.service` and can view logs by querying the following in the logs explorer |
| 67 | + |
| 68 | +```bash |
| 69 | +resource.type="gce_instance" |
| 70 | +sourceLocation.function="main.setupAndRunScript" |
| 71 | +resource.labels.instance_id="THE_INSTANCE_ID_RETURNED_FROM_TERRAFORM" |
| 72 | +``` |
0 commit comments