-
Notifications
You must be signed in to change notification settings - Fork 1
/
infrastructure-deployment.sh
109 lines (80 loc) · 2.87 KB
/
infrastructure-deployment.sh
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
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
clear
read -p "Enter Full Name: " FULLNAME
read -p "Enter E-mail: " EMAIL
read -p "GitHub Repo: " GITHUB
read -p "Enter AWS Access Key: " aws_access_key
read -p "Enter AWS Secret Key: " aws_secret_key
read -p "Enter AWS Token: " aws_token
read -p "Enter AWS Region: " aws_region
REPOS="/usr/repos"
GIT_PATH="sphinx_doc"
GIT_PATH_FULL=$REPOS"/"$GIT_PATH
mkdir -p /etc/apt/keyrings
wget -qO - terraform.gpg https://apt.releases.hashicorp.com/gpg | sudo gpg --batch --yes --dearmor -o /usr/share/keyrings/terraform-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/terraform-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" > /etc/apt/sources.list.d/terraform.list
apt-get update
apt-get install -y software-properties-common gnupg2 curl nano awscli ansible terraform git expect
mkdir -p $REPOS
cd $REPOS
if [ ! -d $GIT_PATH_FULL ]
then
echo "Directory $GIT_PATH_FULL DOES NOT exist."
git clone $GITHUB
else
echo "GIT Repo Already Cloned"
fi
#git clone https://github.com/coolrazor007/sphinx_doc.git --quiet
cd $GIT_PATH_FULL
git config --local user.name "$FULLNAME"
git config --local user.email "$EMAIL"
git reset --hard
git clean -fd
git pull
mkdir -p ~/.ssh
###Create SSH key###
# -N "" means no passphrase
# no '' means no overwrite (it answers "n" to a prompt about it. spams it actually)
no '' | ssh-keygen -t ed25519 -C "$EMAIL" -f ~/.ssh/project -N ""
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/project
if grep "ssh-agent" /root/.bashrc
then
echo "bashrc has ssh-agent line"
# found
else
echo "adding ssh-agent line"
echo 'eval "$(ssh-agent -s)"' >> ~/.bashrc
fi
if grep "ssh-add" /root/.bashrc
then
echo "bashrc has ssh-add line"
# found
else
echo "adding ssh-add line"
echo ssh-add ~/.ssh/project >> ~/.bashrc
fi
cp ~/.ssh/project .
cp ~/.ssh/project.pub .
#echo $(cat ~/.ssh/project.pub)
PUBLIC_KEY=$(cat ~/.ssh/project.pub)
TF_SSH_KEY=$GIT_PATH_FULL"/ssh_key.tf"
TF_PROVIDER=$GIT_PATH_FULL"/provider.tf"
PIPELINE_CONFIG=$GIT_PATH_FULL"/roles/jenkins/tasks/pipeline_config.xml"
echo "here's public key var: "
echo $PUBLIC_KEY
sed -i 's','sshpublickey',"$PUBLIC_KEY",'g' $TF_SSH_KEY
cat $TF_SSH_KEY | grep public_key
sed -i 's','user_access_key',"$aws_access_key",'g' $TF_PROVIDER
sed -i 's','user_secret_key',"$aws_secret_key",'g' $TF_PROVIDER
sed -i 's','user_token',"$aws_token",'g' $TF_PROVIDER
sed -i 's','us-west-1',"$aws_region",'g' $TF_PROVIDER
sed -i 's','GIT-REPO',"$GITHUB",'g' $PIPELINE_CONFIG
terraform init
terraform apply --auto-approve
# Found that you need to wait a tiny bit for EC2 instances to boot up or Ansible may fail
echo "pausing for 10 seconds before continuing with Ansible"
sleep 10
ansible-playbook -i inventory.cfg main.yml --key-file "project"
cat $GIT_PATH_FULL/weblinks
echo "all done - see info immediately above for link information"