-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge6.py
67 lines (58 loc) · 1.69 KB
/
challenge6.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
import os
import pyrax
import pyrax.exceptions as e
import sys, getopt
import time
from time import sleep
def main():
"""Takes cloud files container as argument and creates
a CDN enabled container
"""
# Get container name from command line.
container = ''
try:
opts, args = getopt.getopt(sys.argv[1:],"h:c:",["container="])
except getopt.GetoptError as err:
print 'Usage: challenge3.py -c <container>'
sys.exit(2)
if len(sys.argv) < 3:
print 'Usage: challenge3.py -c <container>'
sys.exit()
for opt, arg in opts:
if opt == '-h':
print 'Usage: challenge3.py -c <container>'
sys.exit()
elif opt == "":
print 'Usage: challenge3.py -c <container>'
sys.exit()
elif opt in ("-c", "--container"):
container = arg
# Path to credential file.
credential_file = os.path.expanduser("~/.rackspace_cloud_credentials")
print "Authenticating"
try:
pyrax.set_credential_file(credential_file)
except e.AuthenticationFailed:
print "Authentication Failed: The file does not contain valid credendials"
sys.exit()
except e.FileNotFound:
print "Authentication file %s not found" % credential_file
sys.exit()
print "Authenticated Successfully as %s" % pyrax.identity.username
cf = pyrax.cloudfiles
# Do the upload
try:
print "Creating container %s" % container
cont = cf.create_container(container)
except:
print "ERROR: Problem creating container %s" % container
sys.exit()
try:
print "Enabling container %s on the CDN" % container
cf.make_container_public(container, ttl=900)
except:
print "ERROR: Problem enabling container %s on the CDN" % container
sys.exit()
print "Container creation completed"
if __name__ == "__main__":
main()