-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge8.py
146 lines (109 loc) · 4.09 KB
/
challenge8.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
#! python -u
import os
import sys
import time
import getopt
import json
import re
import datetime
import socket
from pprint import pprint
from pprint import pformat
import pyrax
import pyrax.exceptions as exc
import base
from base import ChallengeBase
from base import WaitingForTask
from base import CloudServers
from base import log, debug
import utils
DEBUG = 0
PROGRAM_NAME = "challenge8.py"
class Challenge8(ChallengeBase):
"""
Challenge 8: Write a script that will create a static webpage served out of Cloud Files. The script must
create a new container, cdn enable it, enable it to serve an index file, create an index file object,
upload the object to the container, and create a CNAME record pointing to the CDN URL of the container
"""
def __init__ (self, debug_level=0, args=[], optlist=[]) :
ChallengeBase.__init__(self, debug_level)
self.message ="""
usage: %s [-h] [-v]
-h - usage help
-v - verbose / debug output
""" % (PROGRAM_NAME)
self.optlist = optlist
self.args = args
self.container_name = "challenge8"
self.container = None
self.index_name = "index.html"
self.index_file_data = "Hello challenge8, it works!\n"
self.domain_name = "rado-challenge.org"
self.domain = None
self.recs = None
self.rec_CNAME = None
self.rec_CNAME_data = None
def check(self):
debug("check start")
ret = True
containers=self.cf.list_containers()
if self.container_name in containers :
log("container %s exists, please remove it and rerun the scrip again" % self.container_name)
ret = False
try:
self.dns.find(name=self.domain_name)
log("Found domain %s, please delete it and rerun the script" % self.domain_name)
ret = False
except exc.NotFound as e:
pass
return ret
def show(self):
debug("show start")
log("Static site details: (make sure you point your DNS to Rackspace Cloud DNS servers):")
log("ping %s" % self.rec_CNAME)
log("curl -v http://%s/%s" % (self.rec_CNAME, self.index_name) )
log("curl -v http://%s" % (self.rec_CNAME_data) )
def set_container(self):
debug("set_container start")
fname = self.index_name
c=self.cf.create_container(self.container_name)
c.store_object(fname, self.index_file_data)
c.make_public(ttl=300)
c.set_web_index_page(fname)
self.container=c
def set_dns(self):
debug("set_dns start")
log("Creating DNS domain %s and setting CNAME records" % self.domain_name)
self.rec_CNAME = self.container_name + "." + self.domain_name
self.rec_CNAME_data = self.container.cdn_uri.partition("//")[2]
domain=self.dns.create(name=self.domain_name, emailAddress="sample@example.edu", ttl=300, comment="challenge8 domain")
cname_rec = {"type": "CNAME",
"name": self.rec_CNAME,
"data": self.rec_CNAME_data,
"ttl": 300,
"comment": "cloud files CDN enabled container"}
self.recs = domain.add_record(cname_rec)
self.domain = domain
def run(self):
debug("run start")
ChallengeBase.run(self)
if self.check() is False:
self.usage()
sys.exit(-1)
log("Creating container %s and uploading small %s file." % (self.container_name, self.index_name) )
self.set_container()
self.set_dns()
self.show()
if __name__ == '__main__':
optlist, args = getopt.getopt(sys.argv[1:], 'vhid')
for o, val in optlist:
if o == "-v":
DEBUG = 1
base.DEBUG = 1
debug("options: " + ', '.join( map(str,optlist) ) )
debug("arguments: " + ", ".join(args) )
elif o == "-h":
Challenge8(debug_level=DEBUG).usage()
sys.exit()
challenge = Challenge8(debug_level=DEBUG, args=args, optlist=optlist)
challenge.run()