summaryrefslogtreecommitdiff
path: root/adapt-ks-template
blob: 6514745295fb75e6082bf1d5601f1f4c9c027e94 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/bash
#
# proof of concept script to pull a kickstart template and adapt it to 
# a config for a specific host

Self=$(basename $0)
Template="supermicro/SYS-5018D-FN8T/dtn-10g.ks"

function print_usage {
  echo "usage: $Self <options>"
}

function print_help {
cat <<EOF
$Self <options>
This script pulls a kickstart template from somewhere and creates a 
specific kickstart config for a specific host based on the options 
passed to it.

The kickstart config will then be placed somewhere, where it can be pulled
by the installer, which is probably the floppy image borne ipxe booted
CentOS7 install system.

Options:
  -D, --domain          domain, to complete FQDN
  -G, --gateway         Gateway of target system
  -H, --host            hostname of the target system
  -I, --ip              IP address of target system
  -M, --netmask         Netmask of target system
  -N, --nameserver      Nameserver of target system
  -P, --publish-path    Path where results will be stored
  --cosmos-tag          Tag in ndn-sysconf to reference
  --template            Template to use. This is a relative path, plus the 
                        name of the template file. Default:
                        ${Template}
  -h, --help            this
  --krnl-cmdline        add stuff to destination system kernel cmdline
  --sec-ip		IP of secondary interface
  --sec-nm		Netmask of secondary interface

To avoid shell interpretation of whitespace in --krnl-cmdline use escaped
semicolon:
  To achieve this:
    console=tty1 console=ttyS0,115200n8
  write this:
    console=tty1\;console=ttyS0,115200n8
EOF
}

# Needed 'macro/substitutions:
# INSTALLSERVER
# PRIGATEWAY
# PRIIP
# PRINETMASK
# PRINAMESERVERS
# SECIP
# SECNETMASK
# HOSTNAME
# DOMAIN

function parse_commadline {
  while [ "$#" -gt 0 ] ; do
    case "$1" in
    -h|--help)
      print_help
      exit 0
      ;;
    -H|--host)
      Host="$2"
      shift
      ;;
    -D|--domain)
      Domain="$2"
      shift
      ;;
    -I|--ip)
      IP="$2"
      shift
      ;;
    -M|--netmask)
      NM="$2"
      shift
      ;;
    -G|--gateway)
      GW="$2"
      shift
      ;;
    -N|--nameserver)
      NS="$2"
      shift
      ;;
    -P|--publish-path)
      PublishPath="$2"
      shift
      ;;
    -K|--kserver)
      Kserver="$2"
      shift
      ;;
    --cosmos-tag)
      CosmosTag="${2}"
      shift
      ;;
    --template)
      Template="${2}"
      shift
      ;;
    --sec-ip)
      SecIP="${2}"
      shift
      ;;
    --sec-nm)
      SecNM="${2}"
      shift
      ;;
    -T|--tmp-dir)
      TmpDir="${2}"
      shift
      ;;
    --krnl-cmdline)
      KrnlCmdLineOpt="$(echo ${2} | sed -e 's/;/ /g')"
      shift
      ;;
    --gen-cmd)
      GeneratorCommand="$(echo ${2} | sed -e 's/;:;/ /g')"
      shift
      ;;
    *)
      echo "what do you mean \"$1\"?"
      exit 1
      ;;
    esac
    shift
  done
}

function check_options {
  if [ "x${Host}" = "x" ]
  then
    echo "${Self}: --host is mandatory"
    print_usage
    exit 1
  fi
  if [ "x${Domain}" = "x" ]
  then
    echo "${Self}: --domain is mandatory"
    print_usage
    exit 1
  fi
  if [ "x${IP}" = "x" ]
  then
    echo "${Self}: --ip is mandatory"
    print_usage
    exit 1
  fi
  if [ "x${NM}" = "x" ]
  then
    echo "${Self}: --netmask is mandatory"
    print_usage
    exit 1
  fi
  if [ "x${GW}" = "x" ]
  then
    echo "${Self}: --gateway is mandatory"
    print_usage
    exit 1
  fi
  if [ "x${SecIP}" = "x" ]
  then
    echo "${Self}: --sec-ip is mandatory"
    print_usage
    exit 1
  fi
  if [ "x${SecNM}" = "x" ]
  then
    echo "${Self}: --sec-nm is mandatory"
    print_usage
    exit 1
  fi
  if [ "x${Kserver}" = "x" ]
  then
    Kserver="109.105.122.84"
  fi
  if [ "x${NS}" = "x" ]
  then
    NS="109.105.96.141"
  fi
}

parse_commadline ${@}
check_options

if [ "x${TmpDir}" = "x" ]
then
  TmpDir=$(mktemp -d)
else
  mkdir -p "${TmpDir}"
fi

curl -o "${TmpDir}/ks-template" "https://git.nordu.net/?p=ndn-boot-img-stuffs.git;a=blob_plain;f=ks-template/hw/${Template}"
if [ $? -ne 0 ]
then
  echo "failed to download template. This is bad."
  exit 1
fi
if $(file "${TmpDir}/ks-template" | grep -q XML)
then
  echo "downloaded file (${TmpDir}/ks-template) looks wrong"
  exit 1
fi

# boilerplate:
sed -ie '1 a #' "${TmpDir}/ks-template"
sed -ie '2 a # This file generated with this command:' "${TmpDir}/ks-template"
sed -ie "3 a # ${GeneratorCommand}" "${TmpDir}/ks-template"
sed -ie '4 a #' "${TmpDir}/ks-template"

# substitutions:
sed -ie "s/HOSTNAME/${Host}/g" "${TmpDir}/ks-template"
sed -ie "s/DOMAIN/${Domain}/g" "${TmpDir}/ks-template"
sed -ie "s/COSMOSTAG/${CosmosTag}/g" "${TmpDir}/ks-template"
sed -ie "s/INSTALLSERVER/${Kserver}/g" "${TmpDir}/ks-template"
sed -ie "s/PRIIP/${IP}/g" "${TmpDir}/ks-template"
sed -ie "s/PRINETMASK/${NM}/g" "${TmpDir}/ks-template"
sed -ie "s/PRIGATEWAY/${GW}/g" "${TmpDir}/ks-template"
sed -ie "s/PRINAMESERVERS/${NS}/g" "${TmpDir}/ks-template"
sed -ie "s/SECIP/${SecIP}/g" "${TmpDir}/ks-template"
sed -ie "s/SECNETMASK/${SecNM}/g" "${TmpDir}/ks-template"
sed -ie "s/KRNLCMDLINE/${KrnlCmdLineOpt}/g" "${TmpDir}/ks-template"

if [ "x${PublishPath}" = "x" ]
then
  echo "PublishPath no set, leaving everything in ${TmpDir}"
else
  mkdir -p "${PublishPath}/ks"
  mv "${TmpDir}/ks-template" "${PublishPath}/ks/${Host}.ks"
  rm -rf "${TmpDir}/*"
fi