Exalogic Virtual Tea Break Snippets - Cloning an existing vServer

Following on from my Blog entry "Scripted Template Generation from an existing vServer" I have built a wrapper script that can be used to execute the Template Generation script or Clone a specific vServer. This script was not incorporated into the original script because it must be executed on a Compute Node with access to the /OVS/Repositories directory and the Compute Node are a minimal install and hence do not have all the required software available. As part of the Cloning process this new script will create an Assets input file that can be used with the CreateAssets.sh describe in the blog "Scripting Asset Creation" and optionally execute the result to create the clone.

To successfully run the script you will need the following 3 scripts located in the same directory on a machine with the EMOC cli/api rms installed.

  1. CloneOrTemplateVServer.sh.
  2. CreateTemplateFromVServer.sh.
  3. CreateAssets.sh.

During execute, of the CloneOrTemplateVServer.sh, the script will connect to EMOC and stop the specified vServer before scping the CreateTemplateFromVServer.sh to the specified Compute Node and executing it. Once the vServer has been converted to a Template the script will restart the specified vServer and then retrieve the following information for the vServers and build the Clone Asset file.

  1. Connected Networks
  2. VServer Type

In addition the template that was built, and assumed to be located on the ZFS below /export/common/images/<Sub Dir based on Name>, will have an upload entry, in the Asset file, and then used within the vServer entry. Because during execute we do not know the number of networks associated with the vServer the Clone Asset file will be created with "*" in the IP Address section indicating that the IP should be allocated Automatically. A future enhancement will provide the "-b" flag to indicate a Backup Asset file must be created and this will contain the IP allocated to the current server.

If the opt to execute has been specified, "-e" the CreateAsset.sh script will be executed passing in the Clone Asset file thus importing the template and creating the server.

Usage

usage: ./CloneOrTemplateVServer.sh -a  -n  -u  -p  -t|-b|-c [-e] -i  [-r ] [-w ] [-d ] [-f ]
-a  Name of the Account that contains the Specified vServer.
-u  User that is allowed to access the specified account the default is root
-p  password of the specified user
-n  vServer to be templatised or cloned.
-f  (Default is CreateAssets.in) Defines the file that will be built during the clode operation.
-t|-b|-c Indicates if only a template should be created (-t) or if the cloning file should be generated as well (-c). Specifying -b will cause the current IP Addresses to be used in the Asset File
-e If the action is to clone then the presence of this flag indicates that the resulting AssetsFile should be executed
-r  Location of the repository the default is /OVS/Repositories/*
-w  Working directory where intermediate files will be copied.
-d  Directory where the template tgz will be created. Default is current directory
-i  Address of Compute Node to run the Template functionality from.

Script

Download

 1 #!/bin/bash
2
3 ################################################################################
4 #
5 # Exalogic EL X2-2 2.0.0.4 (Linux x86-64) Configuration Script.
6 #
7 # HEADER START
8 # 
9 # THIS SCRIPT IS PROVIDED ON AN ?AS IS? BASIS, WITHOUT WARRANTY OF ANY KIND, 
10 # EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT 
11 # THE COVERED SCRIPT IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR 
12 # PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE 
13 # OF THE COVERED SOFTWARE IS WITH YOU. SHOULD ANY COVERED SOFTWARE PROVE 
14 # DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER 
15 # CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION.
16 # NO USE OF ANY COVERED SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS 
17 # DISCLAIMER.
18 #
19 # When distributing this Code, include this HEADER in each file.
20 # If applicable, add the following below this this HEADER, with the fields
21 # enclosed by brackets "[]" replaced with your own identifying information:
22 # Portions Copyright [yyyy] [name of copyright owner]
23 # 
24 # HEADER END
25 # 
26 # 
27 # Copyright 2011 Andrew Hopkinson, Oracle Corporation UK Ltd.
28 #
29 ################################################################################
30
31 export OCCLI=/opt/sun/occli/bin
32 export IAAS_HOME=/opt/oracle/iaas/cli
33 export IAAS_BASE_URL=https://localhost
34 export BASE_IAAS_ACCESS_KEY_FILE=iaas_access.key
35 export BASE_KEY_NAME=cli.asset.create
36 export BASE_KEY_FILE=iaas_access.pub
37 export RUN_DATE=`date +"%Y%m%d-%H%M"`
38 #CloudUser used to create vServers & Volumes
39 export IAAS_USER=root
40 export IAAS_PASSWORD_FILE=root.pwd
41 export INPUT_FILE=CreateAssets.in
42
43 export ACCOUNTS_FILE=accounts.out
44 export VOLUMES_FILE=volumes.out
45 export DISTGRPS_FILE=distgrp.out
46 export VNETS_FILE=vnets.out
47 export VSERVER_TYPES_FILE=vstype.out
48 export VSERVER_FILE=vserver.out
49 export VSERVER_TEMPLATES=template.out
50 export NETWORK_STATIC_IPS=staticips.out
51 export KEY_PAIRS=keypairs.out
52
53
54 ## **********************************************************
55
56 PROCESSING_ACCOUNT=""
57
58 function connectToAccount() {
59 if [[ "$ACCOUNT" != "$PROCESSING_ACCOUNT" ]]
60 then
61 if [[ "" != "$PROCESSING_ACCOUNT" ]]
62 then
63 $IAAS_HOME/bin/iaas-delete-key-pair --key-name $KEY_NAME --access-key-file $IAAS_ACCESS_KEY_FILE
64 $IAAS_HOME/bin/akm-delete-access-key $AK
65 fi
66 # Set run specific key information
67 export IAAS_ACCESS_KEY_FILE=$ACCOUNT"."$RUN_DATE"."$BASE_IAAS_ACCESS_KEY_FILE
68 export KEY_NAME=$ACCOUNT"."$RUN_DATE"."$BASE_KEY_NAME
69 export KEY_FILE=$ACCOUNT"."$RUN_DATE"."$BASE_KEY_FILE
70 #echo "IAAS_ACCESS_KEY_FILE=$IAAS_ACCESS_KEY_FILE"
71 #echo "KEY_NAME=$KEY_NAME"
72 echo "KEY_FILE=$KEY_FILE"
73 # Save current processing account
74 PROCESSING_ACCOUNT=$ACCOUNT
75 IAAS_USER=$ACCOUNT_USER
76 echo "$ACCOUNT_PASSWORD" > $IAAS_PASSWORD_FILE
77 $IAAS_HOME/bin/akm-describe-accounts --sep "|" > $ACCOUNTS_FILE
78 while read line
79 do
80 ACCOUNT_ID=${line%%|*}
81 line=${line#*|}
82 ACCOUNT_NAME=${line%%|*}
83 # echo "Id = $ACCOUNT_ID"
84 # echo "Name = $ACCOUNT_NAME"
85 if [[ "$ACCOUNT_NAME" == "$ACCOUNT" ]]
86 then
87 #echo "Found Account $line"
88 AK=`$IAAS_HOME/bin/akm-create-access-key --account $ACCOUNT_ID --access-key-file $IAAS_ACCESS_KEY_FILE`
89 KEYPAIR=`$IAAS_HOME/bin/iaas-create-key-pair --key-name $KEY_NAME --key-file $KEY_FILE`
90 echo "Connected to $ACCOUNT_NAME"
91
92 #cp $IAAS_ACCESS_KEY_FILE $ACCOUNT_NAME$IAAS_ACCESS_KEY_FILE
93 #cp $KEY_FILE $ACCOUNT_NAME$KEY_FILE
94 break
95 fi
96 done < $ACCOUNTS_FILE
97 fi
98 }
99
100 function disconnectFromAccount() {
101 $IAAS_HOME/bin/iaas-delete-key-pair --key-name $KEY_NAME --access-key-file $IAAS_ACCESS_KEY_FILE
102 $IAAS_HOME/bin/akm-delete-access-key $AK
103 PROCESSING_ACCOUNT=""
104 }
105
106 function getDistributionGroups() {
107 $IAAS_HOME/bin/iaas-describe-distribution-groups --sep "|" > $DISTGRPS_FILE
108 }
109
110 function getNetworks() {
111 $IAAS_HOME/bin/iaas-describe-vnets --sep "|" > $VNETS_FILE
112 }
113
114 function getVSTypes() {
115 $IAAS_HOME/bin/iaas-describe-vserver-types --sep "|" > $VSERVER_TYPES_FILE
116 }
117
118 function getTemplates() {
119 $IAAS_HOME/bin/iaas-describe-server-templates --sep "|" > $VSERVER_TEMPLATES
120 }
121
122 function getVolumes() {
123 $IAAS_HOME/bin/iaas-describe-volumes --sep "|" > $VOLUMES_FILE
124 }
125
126 function getVServers() {
127 $IAAS_HOME/bin/iaas-describe-vservers --sep "|" > $VSERVER_FILE
128 }
129
130 function getVServerId() {
131 while read line
132 do
133 VSERVER_ID=${line%%|*}
134 line=${line#*|}
135 NAME=${line%%|*}
136 if [[ "$VSERVER_NAME" == "$NAME" ]]
137 then
138 break;
139 fi
140 VSERVER_ID=""
141 done < $VSERVER_FILE
142 }
143
144
145 function getVServerState() {
146 getVServers
147 while read line
148 do
149 VSERVER_ID=${line%%|*}
150 line=${line#*|}
151 NAME=${line%%|*}
152 line=${line#*|}
153 line=${line#*|}
154 VSERVER_STATE=${line%%|*}
155 if [[ "$VSERVER_NAME" == "$NAME" ]]
156 then
157 break;
158 fi
159 done < $VSERVER_FILE
160 }
161
162 function pauseUntilVServerRunning() {
163 # Wait until the Server is running before creating the next
164 echo "Pausing until vServer is Running"
165 getVServerState
166 while [[ "$VSERVER_STATE" != "RUNNING" ]]
167 do
168 echo "$NAME $VSERVER_STATE"
169 if [[ "$VSERVER_STATE" != "RUNNING" ]]
170 then
171 echo "Sleeping......."
172 sleep 30
173 fi
174 getVServerState
175 done
176 echo "$NAME $VSERVER_STATE"
177 # Lets pause for a minute or two
178 echo "Just Chilling......"
179 sleep 30
180 }
181
182 ## **********************************************************
183
184
185
186
187 function pauseUntilVServerShutdown() {
188 # Wait until the Server is running before creating the next
189 echo "Pausing until vServer has Shutdown"
190 getVServerState
191 while [[ "$VSERVER_STATE" != "SHUTDOWNDETACHED" ]]
192 do
193 echo "$NAME $VSERVER_STATE"
194 if [[ "$VSERVER_STATE" != "SHUTDOWNDETACHED" ]]
195 then
196 echo "Sleeping......."
197 sleep 30
198 fi
199 getVServerState
200 done
201 echo "$NAME $VSERVER_STATE"
202 # Lets pause for a minute or two
203 echo "Just Chilling......"
204 sleep 30
205 }
206
207 #############################################################
208 ##
209 ## stopVServer
210 ## ===========
211 ##
212 ## Find and stop a vServer.
213 ##
214 #############################################################
215
216 function stopVServer() {
217 echo "Stopping vServer $VSERVER_NAME"
218 getVServers
219 getVServerId
220 $IAAS_HOME/bin/iaas-stop-vservers --vserver-ids $VSERVER_ID --force
221 pauseUntilVServerShutdown
222 }
223
224 #############################################################
225 ##
226 ## startVServer
227 ## ============
228 ##
229 ## Find and stop a vServer.
230 ##
231 #############################################################
232
233 function startVServer() {
234 echo "Starting vServer $VSERVER_NAME"
235 getVServers
236 getVServerId
237 $IAAS_HOME/bin/iaas-start-vservers --vserver-ids $VSERVER_ID
238 pauseUntilVServerRunning
239 }
240
241 #############################################################
242 ##
243 ## executeRemoteCreateTemplate
244 ## ===========================
245 ##
246 ## Execute the CreateTemplateFromVServer.sh on the specified 
247 ## Compute Node. This is required because the Repository is
248 ## not accessible from the EC VM. It's assumed we will not 
249 ## need to enter a password.
250 ##
251 #############################################################
252
253 function executeRemoteCreateTemplate() {
254 echo "Executing Remote Functions"
255 if [[ "$CN_IP_ADDRESS" != "" ]]
256 then
257 #ssh root@$CN_IP_ADDRESS "bash -s" < CreateTemplateFromVServer.sh -n $VSERVER_NAME -r $REPOSITORY_DIR -w $WORKING_DIR -d $DESTINATION_DIR
258 #ssh root@$CN_IP_ADDRESS "bash -s -n $VSERVER_NAME -r $REPOSITORY_DIR -w $WORKING_DIR -d $DESTINATION_DIR" < CreateTemplateFromVServer.sh
259 #cat CreateTemplateFromVServer.sh | ssh root@$CN_IP_ADDRESS "bash -s -n $VSERVER_NAME -r $REPOSITORY_DIR -w $WORKING_DIR -d $DESTINATION_DIR"
260 echo "Copying script to $CN_IP_ADDRESS"
261 scp CreateTemplateFromVServer.sh root@$CN_IP_ADDRESS:/tmp
262 ssh root@$CN_IP_ADDRESS "chmod a+x /tmp/CreateTemplateFromVServer.sh"
263 ssh root@$CN_IP_ADDRESS "/tmp/CreateTemplateFromVServer.sh -n $VSERVER_NAME -r $REPOSITORY_DIR -w $WORKING_DIR -d $DESTINATION_DIR"
264 ssh root@$CN_IP_ADDRESS "rm -f /tmp/CreateTemplateFromVServer.sh"
265
266 TEMPLATE_TGZ=$DESTINATION_DIR/el_template_$VSERVER_NAME.tgz
267 fi
268 }
269
270 #############################################################
271 ##
272 ## createTemplate
273 ## ==============
274 ##
275 ## High level template creation function that will call the 
276 ## required processing function in the necessary sequence.
277 ##
278 #############################################################
279
280 function createTemplate() {
281 stopVServer
282 executeRemoteCreateTemplate
283 startVServer
284 echo ""
285 echo ""
286 echo "****************************************************"
287 echo "**"
288 echo "** $TEMPLATE_TGZ has been created from"
289 echo "** vServer $VSERVER_NAME "
290 echo "**"
291 echo "****************************************************"
292 echo ""
293 echo ""
294 }
295
296 #############################################################
297 ##
298 ## getNetworkName
299 ## ==============
300 ##
301 ## Gets the name of a network based on the Id.
302 ##
303 #############################################################
304
305 function getNetworkName() {
306 NETWORK_NAME=""
307 while read line
308 do
309 ID=${line%%|*}
310 line=${line#*|}
311 NAME=${line%%|*}
312 if [[ "$ID" == "$NETWORK_ID" ]]
313 then
314 NETWORK_NAME=$NAME
315 break
316 fi
317 done < $VNETS_FILE
318 }
319
320 #############################################################
321 ##
322 ## getVSTypeName
323 ## =============
324 ##
325 ## Gets the name of a vServer Type based on the Id.
326 ##
327 #############################################################
328
329 function getVSTypeName() {
330 VSTYPE_NAME=""
331 while read line
332 do
333 ID=${line%%|*}
334 line=${line#*|}
335 NAME=${line%%|*}
336 if [[ "$ID" == "$VSTYPE_ID" ]]
337 then
338 VSTYPE_NAME=$NAME
339 break
340 fi
341 done < $VSERVER_TYPES_FILE
342 }
343
344 #############################################################
345 ##
346 ## generateAssetFile
347 ## =================
348 ##
349 ## Generates a Input script that can be used with the 
350 ## CreateAssets.sh to create a server based on the new 
351 ## template.
352 ##
353 #############################################################
354
355 function generateAssetFile() {
356 echo "Generating Clones CreateAsset input file"
357 if [[ "$ASSET_FILE" == "" ]]
358 then
359 ASSET_FILE=$VSERVER_NAME"CreateCloneAsset.in"
360 fi
361 echo "$ACCOUNT:Connect|$ACCOUNT_USER|$ACCOUNT_PASSWORD" > $ASSET_FILE
362 # Here we are assuming that we have used the recommended mount
363 SN_TEMPLATE_FILE=${TEMPLATE_TGZ/u01/export}
364 echo "$ACCOUNT:Upload|ServerTemplate|$VSERVER_NAME-Template|http://$SN_IP_ADDRESS/shares$SN_TEMPLATE_FILE" >> $ASSET_FILE
365 # Get VServer specific information
366 getVServers
367 getNetworks
368 getVSTypes
369
370 while read line
371 do
372 VSERVER_ID=${line%%|*}
373 line=${line#*|}
374 NAME=${line%%|*}
375 line=${line#*|}
376 if [[ "$NAME" == "$VSERVER_NAME" ]]
377 then
378 URL=${line%%|*}
379 line=${line#*|}
380 STATE=${line%%|*}
381 line=${line#*|}
382 NETWORK_IDS=${line%%|*}
383 line=${line#*|}
384 NETWORK_IPS=${line%%|*}
385 line=${line#*|}
386 TEMPLATE_ID=${line%%|*}
387 line=${line#*|}
388 SSH_KEY=${line%%|*}
389 line=${line#*|}
390 VSTYPE_ID=${line%%|*}
391 line=${line#*|}
392
393 NETWORK_NAMES=""
394 NETWORK_IPS=""
395 for NETWORK_ID in ${NETWORK_IDS//,/ }
396 do
397 getNetworkName
398 if [[ "$NETWORK_NAMES" == "" ]]
399 then
400 NETWORK_NAMES=$NETWORK_NAME
401 NETWORK_IPS="*"
402 else
403 NETWORK_NAMES=$NETWORK_NAMES","$NETWORK_NAME
404 NETWORK_IPS=$NETWORK_IPS",*"
405 fi
406 done
407
408 # Get VServer Type Name
409 getVSTypeName
410
411 echo "$ACCOUNT:Create|vServer|$VSERVER_NAME-Clone|$VSTYPE_NAME|$VSERVER_NAME-Template|$NETWORK_NAMES|$NETWORK_IPS" >> $ASSET_FILE
412
413 break
414 fi
415 done < $VSERVER_FILE
416
417 echo "$ACCOUNT:Connect|$ACCOUNT_USER|$ACCOUNT_PASSWORD" >> $ASSET_FILE
418
419 echo "Generated Asset File $ASSET_FILE"
420
421 }
422
423 #############################################################
424 ##
425 ## executeAssetFile
426 ## =================
427 ##
428 ## Execute the CreateAssets.sh script using the generated 
429 ## Asset file. We assume it is in the same directory.
430 ##
431 #############################################################
432
433 function executeAssetFile() {
434 echo "Executing the Generated $ASSET_FILE file"
435 echo ""
436 ./CreateAssets.sh -f $ASSET_FILE -r
437 }
438
439 #############################################################
440 ##
441 ## usage
442 ## =====
443 ##
444 ## Show usage.
445 ##
446 #############################################################
447
448 function usage() {
449 echo ""
450 echo >&2 "usage: $0 -a <Account Name> -n <vServer Name> -u <Account User> -p <Account Password> -t|-c [-e] -i <Compute Node IP> [-r <Repository Directory>] [-w <Working Directory>] [-d <Destination Directory>] [-f <Asset Definition File>]"
451 echo >&2 " -a <Account Name> Name of the Account that contains the Specified vServer."
452 echo >&2 " -u <Account User> User that is allowed to access the specified account the default is root"
453 echo >&2 " -p <Account Password> password of the specified user"
454 echo >&2 " -n <vServer Name> vServer to be templatised or cloned."
455 echo >&2 " -f <Asset Definition File> (Default is CreateAssets.in) Defines the file that will be built during the clode operation."
456 echo >&2 " -t|-c Indicates if only a template should be created (-t) or if the cloning file should be generated as well (-c)"
457 echo >&2 " -e If the action is to clone then the presence of this flag indicates that the resulting AssetsFile should be executed"
458 echo >&2 " -r <Repository Directory> Location of the repository the default is /OVS/Repositories/*"
459 echo >&2 " -w <Working Directory> Working directory where intermediate files will be copied."
460 echo >&2 " -d <Destination Directory> Directory where the template tgz will be created. Default is current directory"
461 echo >&2 " -i <Compute Node IP> Address of Compute Node to run theTemplate functionality from."
462 echo""
463 exit 1
464 }
465
466 ###############################################################
467 ##
468 ## Simple start for the script that will extract the parameters
469 ## and call the appriate start function.
470 ##
471 ###############################################################
472
473 export CLONE=""
474 export TEMPLATE=""
475 export EXECUTE=""
476 export WORKING_DIR="/u01/common/images/vServerTemplateWIP"
477 export DESTINATION_DIR="/u01/common/images/vServerTemplates"
478 export REPOSITORY_DIR="/OVS/Repositories/*"
479 export ACCOUNT_USER="root"
480 export ACCOUNT_PASSWORD="welcome1"
481 export SYSTEMIMGDIR=/mnt/elsystem
482 export CN_IP_ADDRESS=""
483 export SN_IP_ADDRESS="172.17.0.9"
484
485 while [ $# -gt 0 ]
486 do
487 case "$1" in
488 -f) ASSET_FILE="$2"; shift;;
489 -a) ACCOUNT="$2"; shift;;
490 -n) VSERVER_NAME="$2"; shift;;
491 -r) REPOSITORY_DIR="$2"; shift;;
492 -d) DESTINATION_DIR="$2"; shift;;
493 -u) ACCOUNT_USER="$2"; shift;;
494 -p) ACCOUNT_PASSWORD="$2"; shift;;
495 -w) WORKING_DIR="$2"; shift;;
496 -i) CN_IP_ADDRESS="$2"; shift;;
497 -s) SN_IP_ADDRESS="$2"; shift;;
498 -c) CLONE=true;;
499 -t) TEMPLATE=true;;
500 -e) EXECUTE=true;;
501 *) usage;;
502 *) break;;
503 esac
504 shift
505 done
506
507
508 # Check if the JAVA_HOME is set
509 if [[ "$JAVA_HOME" == "" ]]
510 then
511 export JAVA_HOME=/usr/java/latest
512 echo "JAVA_HOME is not defined using $JAVA_HOME"
513 fi
514
515 # Processing function call
516 if [[ "$VSERVER_NAME" == "" || "$REPOSITORY_DIR" == "" || "$CN_IP_ADDRESS" == "" ]]
517 then
518 usage
519 elif [[ "$TEMPLATE" == "true" || "$CLONE" == "true" ]]
520 then
521 connectToAccount
522 createTemplate
523 if [[ "$CLONE" == "true" ]]
524 then
525 generateAssetFile
526 fi
527 disconnectFromAccount
528 if [[ "$EXECUTE" == "true" ]]
529 then
530 executeAssetFile
531 fi
532 fi
533
534
535
536 echo "****************************************"
537 echo "***** Finished Processing Assets *****"
538 echo "****************************************"
539

Originally posted on The Old Toxophilist blog.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

The primary contributors to this blog are comprised of the Exalogic and Cloud Application Foundation contingent of Oracle's Fusion Middleware Architecture Team, fondly known as the A-Team. As part of the Oracle development organization, The A-Team supports some of Oracle's largest and most strategic customers worldwide. Our mission is to provide deep technical expertise to support various Oracle field organizations and customers deploying Oracle Fusion Middleware related products. And to collect real world feedback to continuously improve the products we support. In this blog, our experts and guest experts will focus on Exalogic, WebLogic, Coherence, Tuxedo/mainframe migration, Enterprise Manager and JDK/JRockIT performance tuning. It is our way to share some of our experiences with Oracle community. We hope our followers took away something of value from our experiences. Thank you for visiting and please come back soon.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
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
   
       
Today