-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge5
executable file
·127 lines (97 loc) · 3.46 KB
/
challenge5
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
#!/usr/bin/env python
import os
import time
import pyrax
import argparse
# Main function
def main():
# Globals(usually want to get this through config or user input)
db_flavor = 'm1.liny'
db_volume_size = 1
db_instance_name = 'challenge5_instance'
db_database_name = 'challenge5_db'
db_database_user = 'challenge5_user'
db_database_pass = 'challenge5_pass'
# Authenticate using a credentials file: "~/.rackspace_cloud_credentials"
cred_file = "%s/.rackspace_cloud_credentials" % (os.environ['HOME'])
print "Setting authentication file to %s" % (cred_file)
pyrax.set_credential_file(cred_file)
# Instantiate a clouddns object
print "Instantiating cloud_dns object"
cdbobj = pyrax.cloud_databases
# Check that the instance doesn't already exist and use it if it does
instances = cdbobj.list()
inst_exists = 0
instobj = 0
for instance in instances:
if instance.name == db_instance_name:
print "Instance " + instance.name + " already esists. "
instobj = instance
inst_exists = 1
# Create a new instance if needed.
if inst_exists == 1:
print "Using existing instance"
else:
# Create the instance
print "Creating instance by the name of " + db_instance_name
instobj = cdbobj.create(db_instance_name, db_flavor, volume=db_volume_size)
# Looks like we are going to have to wait until its in a proper state.
is_building = 1
while is_building:
instances = cdbobj.list()
for instance in instances:
if instance.status != 'ACTIVE':
print "Current instance state is: " + instance.status + ". Waiting 60 seconds"
time.sleep(60)
else:
is_building = 0
# Fetch the updated instance object
instances = cdbobj.list()
for instance in instances:
if instance.name == db_instance_name:
print "Fetching updated instance: " + instance.name
instobj = instance
# Check the database doesn't exist and use it if it does
databases = instobj.list_databases()
db_exists = 0
dbobj = 0
for database in databases:
if database.name == db_database_name:
print "Database " + database.name + " already exists. "
dbobj = database
db_exists = 1
# Create a new database if needed
if db_exists == 1:
print "Using existing database"
else:
# Create the database
print "Creating database with the name of " + db_database_name
dbobj = instobj.create_database(db_database_name)
# Check the user doesn't exist and use it if it does
users = instobj.list_users()
user_exists = 0
userobj = 0
if len(users) != 0:
for user in users:
if user.name == db_database_user:
print "User " + user.name + " already exists. "
userobj = user
user_exists = 1
# Create a new user if needed
if user_exists != 1:
# Create the user
print "Creating user with the name of " + db_database_user
userobj = instobj.create_user(name=db_database_user, password=db_database_pass, database_names=[dbobj.name])
# Print the information
print "\n## New Instance and DB Info ##"
print "Instance Name: " + instobj.name
print "Instance Host: " + instobj.hostname
print "Database Name: " + dbobj.name
print "Database User: " + userobj.name
print "Database Pass: " + db_database_pass
print "\n"
print "Example Conn String: \n"
print "\tmysql -u " + userobj.name + " -p -h " + instobj.hostname + " " + dbobj.name
# Called on execution. We are going to call the main() function
if __name__ == "__main__":
main()