-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathrebootVPC.py
executable file
·161 lines (135 loc) · 4.84 KB
/
rebootVPC.py
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
#!/usr/bin/python
import sys
import getopt
from cloudstackops import cloudstackops
import os.path
# Function to handle our arguments
def handleArguments(argv):
global DEBUG
DEBUG = 0
global DRYRUN
DRYRUN = 1
global vpcname
vpcname = ''
global vpcuuid
vpcuuid = ''
global networkuuid
networkuuid = ''
global toCluster
toCluster = ''
global configProfileName
configProfileName = ''
global isProjectVm
isProjectVm = 0
# Usage message
help = "Usage: ./" + os.path.basename(__file__) + ' [options] ' + \
'\n --config-profile -c <profilename>\tSpecify the CloudMonkey profile name to get the credentials from (or specify in ./config file)' + \
'\n --vpc-name -v <name>\t\t\tWork with this VPC (r-12345-VM)' + \
'\n --uuid -u <name>\t\t\tWork with this VPC (UUID)' + \
'\n --network-uuid -t <name>\t\tWork with this VPC tier (UUID)' + \
'\n --is-projectrouter\t\t\tThe specified router belongs to a project' + \
'\n --debug\t\t\t\tEnable debug mode' + \
'\n --exec\t\t\t\tExecute for real'
try:
opts, args = getopt.getopt(
argv, "hc:v:u:pt:", [
"config-profile=", "vpc-name=", "uuid=", "network-uuid=", "debug", "exec", "is-projectrouter"])
except getopt.GetoptError:
print help
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print help
sys.exit()
elif opt in ("-c", "--config-profile"):
configProfileName = arg
elif opt in ("-v", "--vpc-name"):
vpcname = arg
elif opt in ("-u", "--uuid"):
vpcuuid = arg
elif opt in ("-t", "--network-uuid"):
networkuuid = arg
elif opt in ("--debug"):
DEBUG = 1
elif opt in ("--exec"):
DRYRUN = 0
elif opt in ("--is-projectrouter"):
isProjectVm = 1
# Default to cloudmonkey default config file
if len(configProfileName) == 0:
configProfileName = "config"
# We need at least these vars
if len(vpcname) == 0 and len(vpcuuid) == 0 and len(networkuuid) == 0:
print vpcuuid
print vpcname
print networkuuid
print help
sys.exit()
# Parse arguments
if __name__ == "__main__":
handleArguments(sys.argv[1:])
# Init CloudStack class
c = cloudstackops.CloudStackOps(DEBUG, DRYRUN)
c.instance_name = "N/A"
if DEBUG == 1:
print "Warning: Debug mode is enabled!"
if DRYRUN == 1:
print "Warning: dry-run mode is enabled, not running any commands!"
# make credentials file known to our class
c.configProfileName = configProfileName
# Init the CloudStack API
c.initCloudStackAPI()
if DEBUG == 1:
print "DEBUG: API address: " + c.apiurl
print "DEBUG: ApiKey: " + c.apikey
print "DEBUG: SecretKey: " + c.secretkey
# Check cloudstack IDs
if DEBUG == 1:
print "DEBUG: Checking CloudStack IDs of provided input.."
if isProjectVm == 1:
projectParam = "true"
else:
projectParam = "false"
# check routerID
if len(vpcuuid) == 0:
vpcuuid = c.checkCloudStackName({'csname': vpcname,
'csApiCall': 'listVPCs',
'listAll': 'true',
'isProjectVm': projectParam})
if len(networkuuid) > 0:
print "Note: Getting VPC id from network uuid %s" % networkuuid
network = c.listNetworks(networkuuid)[0]
vpcuuid = network.vpcid
if vpcuuid == 1 or vpcuuid == "":
print "Error: VPC cannot be found!"
exit(1)
vpc = c.listVPCs(vpcuuid)[0]
print "Note: Found VPC " + vpcname
# Pretty Slack messages
c.instance_name = vpcname
c.slack_custom_title = "Domain"
c.slack_custom_value = vpc.domain
c.zone_name = vpc.zonename
print "Note: Let's reboot the VPC.."
if DRYRUN == 1:
print "Note: Would have rebooted vpc " + vpc.name + " (" + vpcuuid + ")"
else:
# If the network is a VPC
c.task = "Restart VPC with clean up"
message = "Restarting VPC " + vpc.name + " with clean up (" + vpcuuid + ")"
c.print_message(message=message, message_type="Note", to_slack=True)
result = c.restartVPC(vpcuuid)
if result == 1:
print "Restarting failed, will try again!"
result = c.restartVPC(vpcuuid)
if result == 1:
message = "Restarting VPC " + vpc.name + "(" + vpcuuid + ") with clean up failed.\nError: investigate manually!"
c.print_message(message=message, message_type="Error", to_slack=True)
sys.exit(1)
else:
message = "Successfully restarted VPC " + vpc.name + " (" + vpcuuid + ")"
c.print_message(message=message, message_type="Note", to_slack=True)
else:
message = "Successfully restarted VPC " + vpc.name + " (" + vpcuuid + ")"
c.print_message(message=message, message_type="Note", to_slack=True)
print "Note: We're done!"