-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.functions
93 lines (82 loc) · 3.71 KB
/
.functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
function awssh() {
ssh -F ~/.ssh/awsconfig `aws ec2 describe-instances --filters Name=tag:Name,Values=$1 Name=instance-state-name,Values=running --query 'Reservations[].Instances[].[PublicIpAddress]' --output text --region ap-southeast-1 --profile carepayaws | head -1`
}
function awsshi() {
ssh -F ~/.ssh/awsconfig `aws ec2 describe-instances --instance-ids $1 --query 'Reservations[].Instances[].[PublicIpAddress]' --output text --region ap-southeast-1 --profile carepayaws | head -1`
}
function k8cfg() {
export AWS_SDK_LOAD_CONFIG=1
export AWS_PROFILE=$1
aws --no-cli-pager sts get-caller-identity || aws sso login
aws eks update-kubeconfig --name $2 --alias $2 }
function awsip() {
aws ec2 describe-instances --filters Name=tag:Name,Values=$1 Name=instance-state-name,Values=running --query 'Reservations[].Instances[].[PublicIpAddress]' --output text --region ap-southeast-1 --profile carepayaws
}
function k8l() {
kubectl logs "$(kubectl get pods -l app=$1 --field-selector=status.phase=Running -o jsonpath='{.items[0].metadata.name}')"
}
function k8r() {
kubectl delete pod "$(kubectl get pods -l app=$1 --field-selector=status.phase=Running -o jsonpath='{.items[0].metadata.name}')"
}
function iamysql() {
mysql --ssl-mode=REQUIRED -u $2 -h $1 -p$(aws rds generate-db-auth-token --hostname $1 --port 3306 --user $2) --enable-cleartext-plugin --protocol=tcp $3
}
function iamysqldump() {
mysqldump --column-statistics=0 --ssl-mode=REQUIRED -u $2 -h $1 -p$(aws rds generate-db-auth-token --hostname $1 --port 3306 --user $2 --profile carepayaws --region ap-southeast-1) --enable-cleartext-plugin --protocol=tcp --databases $3 $4 $5 $6 $7 $8 $9
}
function iamrds() {
aws rds generate-db-auth-token --hostname $1 --port 3306 --user $2 --profile carepayaws --region ap-southeast-1
}
function git-crypt-unlock {
if [[ ! -d .terraform/modules ]]; then
echo "No modules found, please run terraform init"
else
for module in .terraform/modules/*; do
echo "Unlocking ${module}"
(cd ${module} && git-crypt unlock $GIT_CRYPT_KEY)
done
fi
}
function listPublicIps {
for profile in cp-int cp-aws ke-test ke-acc ke-prod ae-test ae-acc ae-uat ae-prod ken-test ken-acc ken-prod; do
echo "\"$profile\": {"
for region in eu-west-1 ap-southeast-1; do
echo "\"$region\": {"
echo -n '"load-balancers": '
aws --profile $profile --region $region elbv2 describe-load-balancers --output json | jq '[.LoadBalancers[].AvailabilityZones[].LoadBalancerAddresses[].IpAddress]'
echo -n '"ec2-instances": '
aws --profile $profile --region $region ec2 describe-instances --output json | jq '[.Reservations[].Instances[] | select(.PublicIpAddress) | .PublicIpAddress]'
echo -n '"rds": '
aws --profile $profile --region $region rds describe-db-instances --output json | jq '[.DBInstances[].Endpoint.Address]'
echo -n '"network-interfaces": '
aws --profile $profile --region $region ec2 describe-network-interfaces --output json | jq '[.NetworkInterfaces[] | select(.Association.PublicIp) | .Association.PublicIp]'
done
echo "}"
done
echo "}"
}
function k8tunnel () {
set -e
host=$1
sport=$(echo $2 | cut -d ":" -f1)
dport=$(echo $2 | cut -d ":" -f2)
suffix=$(openssl rand -hex 3)
trap "kubectl delete pod forwarder-$suffix" SIGINT SIGTERM EXIT
kubectl create -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: forwarder-$suffix
labels:
run : forwarder-$suffix
spec:
containers:
- name: forwarder
image: alpine/socat:latest
args:
- TCP4-LISTEN:${dport},fork,reuseaddr
- TCP:${host}:${dport}
EOF
kubectl wait --for=condition=Ready pod/forwarder-$suffix 2>&1 >/dev/null
kubectl port-forward pod/forwarder-$suffix $sport:$dport
}