Skip to content

Commit 99a9a28

Browse files
committed
Major changes
1 parent 4bfed77 commit 99a9a28

File tree

10 files changed

+92
-244
lines changed

10 files changed

+92
-244
lines changed

Vagrantfile

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,36 @@ end
8787

8888
# Setup ssh
8989
def setup_ssh(config, ssh_config)
90-
config.ssh.insert_key = false
91-
config.ssh.private_key_path = [ssh_config['key']['private-key'], "#{Dir.home}/.vagrant.d/insecure_private_key"]
92-
config.vm.provision "file", source: ssh_config['key']['public-key'], destination: "~/.ssh/authorized_keys"
90+
#config.ssh.insert_key = false
91+
#config.ssh.private_key_path = [
92+
# "#{Dir.home}/.vagrant.d/insecure_private_key"
93+
#]
94+
config.vm.provision :shell, privileged: false do |provisioner|
95+
ssh_private_key = File.read(ssh_config['key']['private-key'])
96+
ssh_pub_key = File.readlines(ssh_config['key']['public-key']).first.strip
97+
provisioner.inline = <<-SHELL
98+
if grep -sq "#{ssh_pub_key}" /home/$USER/.ssh/authorized_keys; then
99+
echo "SSH: keys already provisioned."
100+
else
101+
echo "SSH: starting key provisioning"
102+
mkdir -p /home/$USER/.ssh/
103+
touch /home/$USER/.ssh/known_hosts
104+
touch /home/$USER/.ssh/authorized_keys
105+
echo #{ssh_pub_key} >> /home/$USER/.ssh/authorized_keys
106+
sudo bash -c "echo #{ssh_pub_key} >> /root/.ssh/authorized_keys"
107+
fi
108+
109+
if [ #{ssh_config['key']['copy']} == "true" ]; then
110+
echo "SSH: copy is set to true, copying key"
111+
echo "#{ssh_private_key}" > /home/$USER/.ssh/id_rsa
112+
echo #{ssh_pub_key} > /home/$USER/.ssh/id_rsa.pub
113+
chmod 600 /home/$USER/.ssh/id_rsa
114+
chmod 644 /home/$USER/.ssh/id_rsa.pub
115+
fi
116+
chown -R $USER:$USER /home/$USER
117+
exit 0
118+
SHELL
119+
end
93120
end
94121

95122
# Setup sync folder

config.yaml.example

Lines changed: 26 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,58 @@
11
api_version: 2
22
vm:
3-
base_box: 'ubuntu/bionic64'
4-
base_box_version: '20200507.0.0' # Use latest to use latest version
5-
name: 'nvUbuntuDev01'
6-
cpus: 2
7-
memory: 4096
8-
disk_size: '40GB'
3+
base_box: 'sajalshres/ubuntu-dev-box'
4+
base_box_version: '1.0.0' # Use latest to use latest version
5+
name: 'ubuntubox'
6+
cpus: 1
7+
memory: 1024
8+
disk_size: '12GB'
99
providers:
1010
virtualbox:
11-
gui: false
11+
gui: true
1212
network:
1313
ports:
14-
- host: 8000
15-
guest: 8000
16-
- host: 3000
17-
guest: 3000
14+
- host: 9999
15+
guest: 9999
1816
ssh:
1917
key:
2018
# Generate ssh key: ssh-keygen -t rsa -b 4096 -C "[email protected]"
2119
# And place the private and public full path below
22-
private-key: c:/users/sajalshres/.ssh/id_rsa
23-
public-key: c:/users/sajalshres/.ssh/id_rsa.pub
24-
user:
25-
username: developer
26-
uid: 9090
27-
gid: 9090
20+
private-key: c:/users/<your-username>/.ssh/id_rsa
21+
public-key: c:/users/<your-username>/.ssh/id_rsa.pub
22+
# Set copy to true if you want to copy the local keys to box
23+
# Otherwise, provision will generate new keys
24+
copy: true
2825
sync-folder:
2926
enable: false
3027
type: synced_folder
31-
source: d:/example
32-
destination: /home/vagrant/example
28+
source: d:/
29+
destination: /d
3330
provisioners:
34-
- base:
35-
enable: true
36-
before: :all
37-
type: shell
38-
path: provision/base.sh
39-
privileged: true
40-
- python:
41-
enable: true
42-
type: shell
43-
path: provision/python.sh
44-
privileged: true
45-
- docker:
46-
enable: true
47-
type: shell
48-
path: provision/docker.sh
49-
privileged: true
50-
# TODO: Check for following npm error:
51-
## Command: npm install -g eslint
52-
## Error: 429 Too Many Requests - GET https://registry.npmjs.org/strip-ansi
53-
- web-development:
54-
enable: true
55-
type: shell
56-
path: provision/web-development.sh
57-
privileged: true
58-
- desktop:
59-
enable: true
60-
type: shell
61-
path: provision/desktop.sh
62-
privileged: true
63-
# TODO: Not proud of this implementation, change later.
64-
- user:
65-
enable: true
66-
type: shell
67-
path: provision/user.sh
68-
privileged: true
69-
arguments:
70-
- value: user=developer
71-
- value: uid=9090
72-
- value: gid=9090
7331
- git:
74-
enable: false
32+
# This step will check if ssh keys exists from previous step,
33+
# It will generate new keys and add it to github
34+
# Make sure the key title doesnot exist in github
35+
enable: enable
7536
type: shell
7637
path: provision/git.sh
7738
privileged: false
7839
arguments:
7940
# Github user name
80-
- value: username=YOUR-GIT-USERNAME
41+
- value: username=<your-username>
8142
# Github email
82-
- value: email=YOUR-GIT-EMAIL
43+
- value: email=<[email protected]>
8344
# Personal access api token
8445
# Personal access token can be used in place of password
8546
# More info: https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
8647
# Get your token: https://github.com/settings/tokens
87-
- value: token=YOUR-TOKEN-ID
48+
- value: token=<your-token>
49+
# Key title must be unique, and should not exist already in github keys
50+
- value: title=<your-ssh-key-title>
8851
- repositories:
89-
enable: false
52+
enable: enable
9053
type: shell
9154
path: provision/repositories.sh
9255
privileged: false
9356
arguments:
9457
# Your git repositories
9558
- value: [email protected]:sajalshres/linux-dev-box.git
96-
- clean-up:
97-
enable: true
98-
after: :all
99-
type: shell
100-
path: provision/clean-up.sh
101-
privileged: true

provision/desktop.sh

Lines changed: 0 additions & 22 deletions
This file was deleted.

provision/docker.sh

Lines changed: 0 additions & 22 deletions
This file was deleted.

provision/git.sh

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#!/bin/bash
2-
set -ex
2+
set +ex
33

4-
echo "$@"
54
for ARGUMENT in "$@"
65
do
7-
echo "Argument is ${ARGUMENT}"
86
KEY=$(echo $ARGUMENT | cut -f1 -d=)
97
VALUE=$(echo $ARGUMENT | cut -f2 -d=)
108

@@ -21,6 +19,10 @@ do
2119
# Setup token varibale
2220
TOKEN=$VALUE
2321
;;
22+
title)
23+
# Setup token varibale
24+
TITLE=$VALUE
25+
;;
2426
*)
2527
echo "Invalid argument ${KEY}"
2628
;;
@@ -31,6 +33,8 @@ done
3133
PRIVATE_KEY_FILE="${HOME}/.ssh/id_rsa"
3234
PUBLIC_KEY_FILE="${HOME}/.ssh/id_rsa.pub"
3335

36+
37+
3438
function github-authenticated() {
3539
# Attempt to ssh to GitHub
3640
ssh -T [email protected] &>/dev/null
@@ -42,7 +46,7 @@ function github-authenticated() {
4246
# user is not authenticated
4347
return 1
4448
else
45-
echo "unknown exit code in attempt to ssh into [email protected]"
49+
echo "unknown exit code $RET in attempt to ssh into [email protected]"
4650
fi
4751
return 2
4852
}
@@ -54,32 +58,46 @@ function generate-ssh-keys() {
5458
# Add the ssh key to ssh-agent
5559
# ssh-add ~/.ssh/id_rsa
5660

57-
# Add GitHub to known host
58-
ssh-keyscan -H github.com >> ~/.ssh/known_hosts
59-
6061
# Get the content of public key
6162
PUBLIC_KEY=`cat ~/.ssh/id_rsa.pub`
6263

63-
curl -s -u "$USER_NAME:$TOKEN" -X POST -d "{\"title\":\"`hostname`\",\"key\":\"$PUBLIC_KEY\"}" \
64+
curl -s -u "$USER_NAME:$TOKEN" -X POST -d "{\"title\":\"$TITLE\",\"key\":\"$PUBLIC_KEY\"}" \
6465
https://api.github.com/user/keys > /dev/null
6566
}
6667

68+
echo "GIT: Checking if github.com is a known hosts"
69+
ssh-keygen -H -F github.com
70+
RET=$?
71+
if [ $RET == 1 ]; then
72+
# Add GitHub to known host
73+
ssh-keyscan -H github.com >> ~/.ssh/known_hosts
74+
else
75+
echo "GIT: github.com is already a known host"
76+
fi
77+
78+
6779
if [ ! -f "${PRIVATE_KEY_FILE}" ] && [ ! -f "${PUBLIC_KEY_FILE}" ]; then
6880
# SSH key pair doesnot exists
81+
echo "GIT: ${PRIVATE_KEY_FILE} and ${PUBLIC_KEY_FILE} does not exists"
82+
echo "GIT: Generating new ssh key pair"
6983
generate-ssh-keys
7084
else
7185
# SSH Key pair exists
7286
# Check if the key works
87+
echo "GIT: ${PRIVATE_KEY_FILE} and ${PUBLIC_KEY_FILE} already exists"
88+
echo "GIT: Checking is github can be authenticated"
7389
if ! github-authenticated; then
7490
# Existing key is not working, generate again.
91+
echo "GIT: Couldn't authenticate with existing, generating new one"
7592
generate-ssh-keys
7693
fi
7794
fi
7895

7996
# Check if ssh key pair works
8097
if github-authenticated; then
81-
echo "SSH key pair generated and validated successfully"
98+
echo "GIT: SSH key pair setup successfully"
99+
exit 0
82100
else
83-
echo "Some problem in SSH key pair generation, please check logs"
101+
echo "GIT: Some problem in SSH key pair generation, please check logs"
84102
exit 1
85103
fi

provision/python.sh

Lines changed: 0 additions & 14 deletions
This file was deleted.

provision/repositories.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
set -ex
2+
set +ex
33

44
LOCATION="${HOME}/Git"
55
REPOSITORIES=("$@")
@@ -15,7 +15,7 @@ function github-authenticated() {
1515
# user is not authenticated
1616
return 1
1717
else
18-
echo "unknown exit code in attempt to ssh into [email protected]"
18+
echo "REPOSITORY: unknown exit code in attempt to ssh into [email protected]"
1919
fi
2020
return 2
2121
}
@@ -28,16 +28,21 @@ if [ ! -d $LOCATION ]; then
2828
fi
2929

3030
# check if git is authenticated.
31+
echo "REPOSITORY: Checking GitHub access..."
3132
if github-authenticated; then
33+
echo "REPOSITORY: GitHub access successful"
3234
for repository in "${REPOSITORIES[@]}"
3335
do
36+
echo "REPOSITORY: Cloning $repository"
3437
repo_name=`basename "${repository%.*}"`
3538
repo_location="${LOCATION}/${repo_name}"
3639
if [ ! -d $repo_location ]; then
3740
cd $LOCATION
3841
git clone $repository
3942
else
40-
echo "Repo ${repo_name} already exist's in ${LOCATION}"
43+
echo "REPOSITORY: Repo ${repo_name} already exist's in ${LOCATION}, skipping..."
4144
fi
4245
done
46+
else
47+
echo "REPOSITORY: Couldn't authenticate with Github, Please check your keys manually."
4348
fi

0 commit comments

Comments
 (0)