Skip to content

Instantly share code, notes, and snippets.

@feczo
feczo / autohost.py
Created April 21, 2015 03:07
Automatically update your compute engine instance IP with its hostname in your public DNS zone
#!/usr/bin/python
import httplib2
import urllib
import json
import sys
from apiclient.discovery import build
from oauth2client import client as oauth2_client
# PREREQUISITE
@feczo
feczo / gist:ecf9b4b6c1d5d96888eb
Created September 19, 2014 04:49
docker enter container
docker ps
ID=291d97be9ac9
PID=`docker inspect --format "{{ .State.Pid }}" $ID`
nsenter --target $PID --mount --uts --ipc --net --pid
ref:
https://blog.codecentric.de/en/2014/07/enter-docker-container/
@feczo
feczo / bq_retry.py
Created August 27, 2014 03:49
Execute BigQuery request with retry
def ExecuteRequestWithRetries(request, num_tries=5):
"""Executes a request and retries certain failures.
Failures are retried if they are in the list of errors to retry.
List includes system errors(500, 503) and an
authentication error(401)
Args:
request: The request to issue to big query. It must be an object with
an execute method.
@feczo
feczo / migrate-gce-disks.md
Last active August 29, 2015 14:05
Migrate disk between zones
SRC_ZONE="us-central1-a"
DST_ZONE="asia-east1-a"
DISK_NAME="disk1"
SNAP_NAME="snap1"
gcloud compute disks snapshot $DISK_NAME --snapshot-names $SNAP_NAME --zone $SRC_ZONE &&
gcloud compute disks delete $DISK_NAME --zone $SRC_ZONE --quiet &&
gcloud compute disks create $DISK_NAME --zone $DST_ZONE --source-snapshot $SNAP_NAME &&

gcloud compute snapshots delete $SNAP_NAME --quiet

--- move_cmds.py 2014-08-06 17:41:47.000000000 +1000
+++ /Users/sub/cloud/platform/gcutil/lib/google_compute_engine/gcutil_lib/move_cmds.py 2014-08-06 18:21:24.000000000 +1000
@@ -291,7 +291,7 @@
self._VerifyOperations(self.MakeListResult(results, 'operationList'))
- def _DeleteInstances(self, instances, zone):
+ def _DeleteInstances(self, instances, zone, raisefatal=True):
"""Deletes the given instances.
@feczo
feczo / store-recreate-instance.md
Last active October 29, 2015 05:37
Retrieve and re-create a TERMINATED compute engine instance from command line
PROJECT='named-enigma-222'
INSTANCE_NAME='test1'

STORE

gcloud compute instances describe --format=json ${INSTANCE_NAME} | egrep -v 'TERMINATED|"id":|"creationTimestamp":|"natIP":' >instance-config.json
apt-get -y update
apt-get -y install nginx; 
echo "<center><h2>Welcome to $HOSTNAME</h2></center>" | sudo tee /usr/share/nginx/www/index.html;
service nginx start

eg.:

@feczo
feczo / gcutil_delete.md
Last active August 29, 2015 14:04
Delete ALL your compute engine instances from the default project

delete EVERY instance

gcutil --format=names listinstances | while read inst; do gcutil deleteinstance $inst -f --delete_boot_pd&; done;

Only Unused ones

gcutil listinstances --filter="status ne RUNNING" --format=names | while read inst; do gcutil deleteinstance $inst -f --delete_boot_pd&; done;

@feczo
feczo / service-account-dev-prod.py
Created July 3, 2014 13:26
Example of using a service account in both production and development with a .pem key converted from devconsole
import webapp2
from apiclient.discovery import build
from apiclient.errors import HttpError
import httplib2
from oauth2client.client import SignedJwtAssertionCredentials
# Enter your Google Developer Project number
PROJECT_NUMBER = "XXXXXXXXXX"
@feczo
feczo / putty-gce.md
Last active July 23, 2020 13:14
How to use Compute Engine - GCE with putty