Debian Codegraph @

case_studies/micro_fractal/fractal.jsonnet

raw
1 local cmd = import 'mmlib/v0.1.2/cmd/cmd.libsonnet';
2 local cassandra = import 'mmlib/v0.1.2/db/cassandra.libsonnet';
3 local service_google = import 'mmlib/v0.1.2/service/google.libsonnet';
4 local web_solutions = import 'mmlib/v0.1.2/web/solutions.libsonnet';
5 local web = import 'mmlib/v0.1.2/web/web.libsonnet';
6
7 function(parent, name) service_google.Service(parent, name) {
8 local app = self,
9
10 cassandraUser:: 'fractal',
11 dnsSuffix:: error 'Must override dnsSuffix',
12 cassandraUserPass:: error 'Must override cassandraUserPass',
13 cassandraRootPass:: error 'Must override cassandraRootPass',
14 cassandraKeyspace:: 'fractal',
15 cassandraNodes:: [self.prefixName(n) for n in ['db-n1', 'db-n2', 'db-n3', 'db-n4', 'db-n5']],
16
17 // Configuration shared by the application server and tile generation nodes.
18 ApplicationConf:: {
19 width: 256,
20 height: 256,
21 thumb_width: 64,
22 thumb_height: 64,
23 },
24
25 DebugPackagesMixin:: {
26 StandardRootImage+: {
27 local network_debug = ['traceroute', 'lsof', 'iptraf', 'tcpdump', 'host', 'dnsutils'],
28 aptPackages+: ['vim', 'git', 'psmisc', 'screen', 'strace'] + network_debug,
29 },
30 },
31
32
33 zone: service_google.DnsZone(app, 'zone') {
34 dnsName: app.dnsSuffix,
35 },
36
37 www: service_google.DnsRecordWww(app, 'www') {
38 zone: app.zone,
39 target: app.appserv.fullName,
40 },
41
42 network: service_google.Network(app, 'network'),
43
44 appserv: service_google.Cluster3(app, 'appserv') + web.HttpService3 + web_solutions.DebianFlaskHttpService {
45
46 local service = self,
47 dnsZone: app.zone,
48
49 networkName: app.network.nameRef,
50
51 httpsPort: 443,
52
53 Instance+: app.DebugPackagesMixin {
54 machine_type: 'n1-standard-1',
55 StandardRootImage+: {
56 pipPackages+: ['httplib2', 'cassandra-driver', 'blist'],
57 },
58 local version = self,
59 conf:: app.ApplicationConf {
60 database: app.cassandraKeyspace,
61 db_endpoints: app.cassandraNodes,
62 database_name: app.cassandraKeyspace,
63 database_user: app.cassandraUser,
64 database_pass: app.cassandraUserPass,
65 tilegen_endpoint: app.tilegen.endpoint,
66 },
67 module: 'main', // Entrypoint in the Python code.
68 uwsgiConf+: { lazy: 'true' }, // cassandra-driver does not survive fork()
69 // Copy website content and code.
70 httpContentCmds+: [
71 "echo '%s tilegen' >> /etc/hosts" % app.tilegen.addressRef,
72 cmd.CopyFile { from: std.resolvePath(std.thisFile, 'appserv/*'), to: '/var/www' },
73 cmd.LiteralFile { content: std.toString(version.conf), to: '/var/www/conf.json' },
74 ],
75 },
76 },
77
78 tilegen: service_google.Cluster3(app, 'tilegen') + web.HttpService3 + web_solutions.DebianFlaskHttpService {
79 local service = self,
80 dnsZone: app.zone,
81 endpoint:: self.httpsEndpoint,
82
83 networkName: app.network.nameRef,
84
85 httpsPort: 443,
86 Instance+: app.DebugPackagesMixin {
87 machine_type: 'n1-standard-1',
88 StandardRootImage+: {
89 aptPackages+: ['g++', 'libpng-dev'],
90 },
91 local version = self,
92 srcDir:: 'tilegen3',
93 conf:: app.ApplicationConf {
94 iters: 200,
95 },
96 module: 'mandelbrot_service',
97 httpContentCmds+: [
98 cmd.CopyFile { from: std.resolvePath(std.thisFile, 'tilegen/*'), to: '/var/www' },
99 'g++ -Wall -Wextra -ansi -pedantic -O3 -ffast-math -g /var/www/mandelbrot.cpp -lpng -o /var/www/mandelbrot',
100 cmd.LiteralFile { content: std.toString(version.conf), to: '/var/www/conf.json' },
101 ],
102 },
103 },
104
105 db: cassandra.GcpDebianCassandra(app, 'db') {
106 local db = self,
107 dnsZone: app.zone,
108
109 networkName: app.network.nameRef,
110
111 rootPassword: app.cassandraRootPass,
112
113 cassandraConf+: {
114 rpc_address:: null, // Unset by making it hidden (::).
115 listen_address:: null, // Unset by making it hidden (::).
116 authenticator: 'PasswordAuthenticator',
117 seed_provider: [
118 {
119 class_name: 'org.apache.cassandra.locator.SimpleSeedProvider',
120 parameters: [{ seeds: std.join(', ', app.cassandraNodes) }],
121 },
122 ],
123 },
124
125 Instance+: app.DebugPackagesMixin {
126 machine_type: 'n1-standard-4',
127 },
128
129 // Put some initial data in a fresh database.
130 StarterNode+: {
131 local cql_insert(uuid, x, y, l, n) =
132 'INSERT INTO discoveries (Date, TimeId, X, Y, L, Text) '
133 + ("VALUES ('FIXED', %s, %s, %s, %s, '%s');" % [uuid, x, y, l, n]),
134
135 initCql: [
136 "CREATE USER %s WITH PASSWORD '%s';" % [app.cassandraUser, app.cassandraUserPass],
137 'CREATE KEYSPACE %s WITH REPLICATION = %s;' % [app.cassandraKeyspace, self.initReplication],
138 'USE %s;' % app.cassandraKeyspace,
139 'CREATE TABLE discoveries('
140 + 'Date TEXT, TimeId TIMEUUID, Text TEXT, X FLOAT, Y FLOAT, L INT, '
141 + 'PRIMARY KEY(Date, TimeId));',
142 cql_insert('18063880-5a4d-11e4-ada4-247703d0f194', '0', '0', '0', 'Zoomed Out'),
143 cql_insert('66b6d100-5a53-11e4-aa05-247703d0f194',
144 '-1.21142578125',
145 '0.3212890625',
146 '4',
147 'Lightning'),
148 cql_insert('77ffdd80-5a53-11e4-8ccf-247703d0f194',
149 '-1.7568359375',
150 '-0.0009765625',
151 '5',
152 'Self-similarity'),
153 cql_insert('7fbf8200-5a53-11e4-804a-247703d0f194',
154 '0.342529296875',
155 '0.419189453125',
156 '5',
157 'Windmills'),
158 cql_insert('9ae7bd00-5a66-11e4-9c66-247703d0f194',
159 '-1.48309979046093',
160 '0.00310595797955671',
161 '39',
162 'Star'),
163 cql_insert('75fe4480-5a7c-11e4-a747-247703d0f194',
164 '-0.244976043701172',
165 '0.716987609863281',
166 '10',
167 'Baroque'),
168 cql_insert('abf70380-5b24-11e4-8a46-247703d0f194',
169 '-1.74749755859375',
170 '0.009002685546875',
171 '9',
172 'Hairy windmills'),
173 ],
174 },
175
176 nodes: {
177 n1: db.StarterNode {
178 initReplicationFactor: 1,
179 },
180 },
181 },
182
183 prober: service_google.SingleInstance(app, 'prober') {
184 zone: 'us-central1-b',
185 dnsZone: app.zone,
186 networkName: app.network.nameRef,
187 perServiceFirewalls:: false,
188 Instance+: app.DebugPackagesMixin {
189 local version = self,
190 script:: |||
191 from selenium import webdriver
192 import sched
193 import time
194 # Option 1 - with ChromeOptions
195 chrome_options = webdriver.ChromeOptions()
196 chrome_options.add_argument('--headless')
197 chrome_options.add_argument('--no-sandbox')
198
199 s = sched.scheduler(time.time, time.sleep)
200 def do_probe(sc):
201 driver = webdriver.Chrome(
202 chrome_options=chrome_options,
203 service_args=['--verbose', '--log-path=/var/log/chromedriver.log'])
204 driver.get('%(endpoint)s')
205 time.sleep(5)
206 print(driver.title)
207 print(len(driver.find_element_by_id('remembered_list').find_elements_by_tag_name('img')))
208 s.enter(60, 1, do_probe, (sc,))
209 s.enter(0, 1, do_probe, (s,))
210 s.run()
211 ||| % { endpoint: app.appserv.httpsEndpoint },
212 StandardRootImage+: {
213 aptPackages+: ['chromium', 'unzip'],
214 pipPackages+: ['selenium'],
215 cmds+: [
216 'wget https://chromedriver.storage.googleapis.com/2.35/chromedriver_linux64.zip',
217 'unzip chromedriver_linux64.zip',
218 'mv chromedriver /usr/local/bin',
219 ],
220 },
221 cmds+: [
222 cmd.EnsureDir { dir: '/var/prober', owner: 'root' },
223 cmd.LiteralFile { content: std.toString(version.script), to: '/var/prober/script.py' },
224 ],
225 bootCmds+: [
226 'python /var/prober/script.py',
227 ],
228 },
229 },
230 }