-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch.sh
executable file
·125 lines (112 loc) · 3.45 KB
/
launch.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env bash
cd "$(dirname "$0")"
# Get customized variables
source .env
#variables extracted from incus config file
project=$(jq ".project" $file | sed 's/"//g')
image=$(jq '.box' $file | sed 's/"//g')
hostnum=$(jq '.hosts | length' $file | sed 's/"//g')
domain=$(jq '.domain' $file | sed 's/"//g')
declare -A groups;
# check project exists
if ! incus project list | grep $project ; then
incus project create $project
fi
incus project switch $project
# check lxdbr0 network exists and is a bridge
if ! incus network list | grep $network | grep bridge ; then
incus network create $network "ipv4.address=${iprange}" dns.domain=$domain ipv6.nat=false ipv6.address=none ipv4.nat=true
fi
mkdir -p $datadir
yes | ssh-keygen -t rsa -b 4096 -f $privatekey -N ''
echo '#Generated by tdp-incus/launch.sh' > ${hostfile}.tmp
echo "" >> ${hostfile}.tmp
# unless tdp storage exists
if ! incus storage list | grep $storagepool; then
#create local dir storage
mkdir -p $storagedir
incus storage create $storagepool dir source=$storagedir
fi
#foreach hostnum
for f in $(seq 0 $((hostnum - 1))); do
# parse json file to get host config
name=$(jq ".hosts[$f].hostname" $file | sed 's/"//g')
memory=$(jq ".hosts[$f].memory" $file)
cpu=$(jq ".hosts[$f].cpus" $file)
ip=$(jq ".hosts[$f].ip" $file | sed 's/"//g')
for i in $(jq ".hosts[$f].groups[]" $file | sed 's/"//g'); do
#Append host to group multiline string array
groups[$i]="${groups[$i]}\n$name"
done;
# add host to inventory
echo "$name ansible_ssh_host=$ip ansible_ssh_port=22 ansible_ssh_user='$user' ansible_ssh_private_key_file='$privatekey' ip=$ip domain=$domain" >> ${hostfile}.tmp
# launch host with following configuration
incus launch images:$image $name --vm <<-EOF
config:
limits.memory: ${memory}MB
limits.cpu: ${cpu}
user.tdp-groups: $(jq -r ".hosts[$f].groups | @csv" $file | tr -d '"')
user.user-data: |
#cloud-config
fqdn: ${name}.${domain}
manage_etc_hosts: true
package_update: true
package_upgrade: true
package_reboot_if_required: true
packages:
- chrony
- firewalld
- openssh-server
- vim
write_files:
- path: /etc/cloud/templates/hosts.redhat.tmpl
content: |
127.0.0.1 localhost.localdomain localhost
127.0.0.1 localhost4.localdomain4 localhost4
::1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
- path: /etc/ssh/sshd_config
content: |
# Set UsePAM to ssh on passwd-less account
UsePAM yes
# SFTP not working without sftp-server path
Subsystem sftp /usr/libexec/openssh/sftp-server
append: true
runcmd:
- [ systemctl, enable, sshd, --now ]
users:
- name: ${user}
sudo: ALL=(ALL) NOPASSWD:ALL
ssh-authorized-keys:
- $(cat $privatekey.pub)
shell: /bin/bash
devices:
root:
path: /
pool: tdp
size: 50GB
type: disk
cloud-init:
type: disk
source: cloud-init:config
agent:
type: disk
source: agent:config
enp5s0:
ipv4.address: ${ip}
nictype: bridged
parent: $network
type: nic
name: $name
architecture: x86_64
profiles:
- default
EOF
done;
echo >> ${hostfile}.tmp
for key in "${!groups[@]}"; do
# generate ini section of current group (key)
echo -e "[$key]${groups[$key]}\n" >> ${hostfile}.tmp
done
#when finished, replace host file by tmp version
mv ${hostfile}.tmp ${hostfile}