#!/bin/bash # A script to enroll cluster nodes in cobbler based on a cluster # definition in cluster.txt : # # - If a node is provided, that node will be addded or removed depending # on the first parameter # # - if no node is provided, the first parameter will be used to choose # an action to apply to all nodes # # - if a special nodename is provided that matches a role, then all # hosts with that role will have the specified cobbler action (add # or remove) applied to them. [Technically cobbler doesn't know # anything about roles, however it can be convenient to register, # say, all head nodes first, get them up and running without any # risk of work nodes accidentally coming up before they have a head # node to talk to, and then register all work nodes later.] # # If any cobbler registrations were changed, cobbler sync is called # if [[ -z "$1" ]] { echo "Usage: $0 add|remove (hostname)" exit } if [[ ! -z "$2" ]] { global EXACTHOST := $2 } global TRANSCRIPT := '"cobbler-transcript.txt'" if [[ -f $TRANSCRIPT ]] { touch $TRANSCRIPT } if [[ -f cluster.txt ]] { echo "Using cluster definition from cluster.txt" while read HOSTNAME MACADDR IPADDR ILOIPADDR DOMAIN ROLE { if [[ $HOSTNAME = "end" ]] { continue } if [[ "$ROLE" = "bootstrap" ]] { # let's not try to cobbler boot ourselves continue } if [[ $1 = add ]]{ if [[ -z "$EXACTHOST" || "$EXACTHOST" = "$HOSTNAME" || "$EXACTHOST" = "$ROLE" ]] { global MATCH := $HOSTNAME global ACTIONSTRING := ""adding $HOSTNAME.$DOMAIN ($IPADDR,$MACADDR) to cobbler..."" echo $ACTIONSTRING | tee -a $TRANSCRIPT sudo cobbler system add --name=$HOSTNAME --hostname=$HOSTNAME.$DOMAIN --profile=bcpc_host --ip-address=$IPADDR --mac=$MACADDR --interface=eth0 } } elif [[ $1 == remove ]] { if [[ -z "$EXACTHOST" || "$EXACTHOST" = "$HOSTNAME" || "$EXACTHOST" = "$ROLE" ]] { global MATCH := $HOSTNAME global ACTIONSTRING := ""removing $HOSTNAME from cobbler..."" echo $ACTIONSTRING | tee -a $TRANSCRIPT sudo cobbler system remove --name=$HOSTNAME } } else { echo "Usage: \"$1\" unrecognized" exit } } < cluster.txt } else { echo "Error: No cluster definition (cluster.txt) available" exit } if [[ ! -z "$MATCH" ]] { # made at least one change to cobbler config echo "Cobbler sync..." sudo cobbler sync } else { if [[ -z "$EXACTHOST" ]] { echo No hosts defined, no action taken. } else { echo Error : Host "'$2'" unrecognized, no action taken. } } (CommandList children: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr:(BoolUnary op_id:BoolUnary_z child:{(DQ ($ VSub_Number "$1"))})) terminator: ) ] action: [ (C {(echo)} {(DQ ("Usage: ") ($ VSub_Number "$0") (" add|remove (hostname)"))}) (C {(exit)}) ] spids: [-1 73] ) ] spids: [-1 87] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalNot child: (BoolUnary op_id:BoolUnary_z child:{(DQ ($ VSub_Number "$2"))}) ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:EXACTHOST) op: Equal rhs: {($ VSub_Number "$2")} spids: [108] ) ] spids: [108] ) ] spids: [-1 105] ) ] spids: [-1 111] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:TRANSCRIPT) op: Equal rhs: {(DQ (cobbler-transcript.txt))} spids: [114] ) ] spids: [114] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr:(BoolUnary op_id:BoolUnary_f child:{($ VSub_Name "$TRANSCRIPT")})) terminator: ) ] action: [(C {(touch)} {($ VSub_Name "$TRANSCRIPT")})] spids: [-1 130] ) ] spids: [-1 137] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr:(BoolUnary op_id:BoolUnary_f child:{(cluster.txt)})) terminator: ) ] action: [ (C {(echo)} {(DQ ("Using cluster definition from cluster.txt"))}) (While cond: [ (Sentence child: (C {(read)} {(HOSTNAME)} {(MACADDR)} {(IPADDR)} {(ILOIPADDR)} {(DOMAIN)} {(ROLE)}) terminator: ) ] body: (DoGroup children: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobEqual left: {($ VSub_Name "$HOSTNAME")} right: {(DQ (end))} ) ) terminator: ) ] action: [(ControlFlow token:)] spids: [-1 196] ) ] spids: [-1 202] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobEqual left: {(DQ ($ VSub_Name "$ROLE"))} right: {(DQ (bootstrap))} ) ) terminator: ) ] action: [(ControlFlow token:)] spids: [-1 223] ) ] spids: [-1 233] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobEqual left: {($ VSub_Number "$1")} right: {(add)} ) ) terminator: ) ] action: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalOr left: (BoolUnary op_id: BoolUnary_z child: {(DQ ($ VSub_Name "$EXACTHOST"))} ) right: (LogicalOr left: (BoolBinary op_id: BoolBinary_GlobEqual left: {(DQ ($ VSub_Name "$EXACTHOST"))} right: {(DQ ($ VSub_Name "$HOSTNAME"))} ) right: (BoolBinary op_id: BoolBinary_GlobEqual left: {(DQ ($ VSub_Name "$EXACTHOST"))} right: {(DQ ($ VSub_Name "$ROLE"))} ) ) ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:MATCH) op: Equal rhs: {(DQ ($ VSub_Name "$HOSTNAME"))} spids: [293] ) ] spids: [293] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ACTIONSTRING) op: Equal rhs: { (DQ ("adding ") ($ VSub_Name "$HOSTNAME") (.) ($ VSub_Name "$DOMAIN") (" (") ($ VSub_Name "$IPADDR") (",") ($ VSub_Name "$MACADDR") (") to cobbler...") ) } spids: [299] ) ] spids: [299] ) (Pipeline children: [ (C {(echo)} {($ VSub_Name "$ACTIONSTRING")}) (C {(tee)} {(-a)} {($ VSub_Name "$TRANSCRIPT")}) ] negated: False ) (C {(sudo)} {(cobbler)} {(system)} {(add)} {(--name) (Lit_Other "=") ($ VSub_Name "$HOSTNAME")} {(--hostname) (Lit_Other "=") ($ VSub_Name "$HOSTNAME") (.) ($ VSub_Name "$DOMAIN")} {(--profile) (Lit_Other "=") (bcpc_host)} {(--ip-address) (Lit_Other "=") ($ VSub_Name "$IPADDR")} {(--mac) (Lit_Other "=") ($ VSub_Name "$MACADDR")} {(--interface) (Lit_Other "=") (eth0)} ) ] spids: [-1 290] ) ] spids: [-1 361] ) ] spids: [-1 250] ) (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {($ VSub_Number "$1")} right: {(remove)} ) ) terminator: ) ] action: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalOr left: (BoolUnary op_id: BoolUnary_z child: {(DQ ($ VSub_Name "$EXACTHOST"))} ) right: (LogicalOr left: (BoolBinary op_id: BoolBinary_GlobEqual left: {(DQ ($ VSub_Name "$EXACTHOST"))} right: {(DQ ($ VSub_Name "$HOSTNAME"))} ) right: (BoolBinary op_id: BoolBinary_GlobEqual left: {(DQ ($ VSub_Name "$EXACTHOST"))} right: {(DQ ($ VSub_Name "$ROLE"))} ) ) ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:MATCH) op: Equal rhs: {(DQ ($ VSub_Name "$HOSTNAME"))} spids: [420] ) ] spids: [420] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ACTIONSTRING) op: Equal rhs: { (DQ ("removing ") ($ VSub_Name "$HOSTNAME") (" from cobbler...") ) } spids: [426] ) ] spids: [426] ) (Pipeline children: [ (C {(echo)} {($ VSub_Name "$ACTIONSTRING")}) (C {(tee)} {(-a)} {($ VSub_Name "$TRANSCRIPT")}) ] negated: False ) (C {(sudo)} {(cobbler)} {(system)} {(remove)} {(--name) (Lit_Other "=") ($ VSub_Name "$HOSTNAME")} ) ] spids: [-1 417] ) ] spids: [-1 460] ) ] spids: [364 377] ) ] else_action: [ (C {(echo)} { (DQ ("Usage: ") (EscapedLiteralPart token:) ($ VSub_Number "$1") (EscapedLiteralPart token:) (" unrecognized") ) } ) (C {(exit)}) ] spids: [463 480] ) ] spids: [178 483] ) redirects: [(Redir op_id:Redir_Less fd:-1 arg_word:{(cluster.txt)} spids:[485])] ) ] spids: [-1 151] ) ] else_action: [ (C {(echo)} {(DQ ("Error: No cluster definition (cluster.txt) available"))}) (C {(exit)}) ] spids: [489 501] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalNot child: (BoolUnary op_id:BoolUnary_z child:{(DQ ($ VSub_Name "$MATCH"))}) ) ) terminator: ) ] action: [(C {(echo)} {(DQ ("Cobbler sync..."))}) (C {(sudo)} {(cobbler)} {(sync)})] spids: [-1 519] ) ] else_action: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolUnary op_id:BoolUnary_z child:{(DQ ($ VSub_Name "$EXACTHOST"))}) ) terminator: ) ] action: [ (C {(echo)} {(No)} {(hosts)} {(defined) (Lit_Comma ",")} {(no)} {(action)} {(taken.)}) ] spids: [-1 555] ) ] else_action: [ (C {(echo)} {(Error)} {(Lit_Other ":")} {(Host)} {(DQ ("'") ($ VSub_Number "$2") ("'"))} {(unrecognized) (Lit_Comma ",")} {(no)} {(action)} {(taken.)} ) ] spids: [574 601] ) ] spids: [539 603] ) ] )