Loading...
Searching...
No Matches
makedata.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief self destructing setup service
4 @details
5 Will create the database and perform config activities.
6
7 It creates the configuration tables.
8
9 The deploy screen's two filesystem questions - list the folders below a
10 path, can this folder be written - are answered by their own services
11 (services/admin/dirlist.sas and services/admin/writecheck.sas). They carry
12 no gate of their own: the platform's permissions on the admin folder decide
13 who may call them, and they keep working while this program is gone.
14
15 DCPATH, ADMIN and SAMPLEDATA are read from the input table with
16 call symputx, so each reads the same whether the caller sent it as a column
17 (the deploy screen, the CLI seeds) or as a URL parameter (the configurator,
18 the run-in-new-window route).
19
20
21 <h4> SAS Macros </h4>
22 @li mf_getapploc.sas
23 @li mf_getuniquefileref.sas
24 @li mf_mkdir.sas
25 @li mf_trimstr.sas
26 @li mfv_existfile.sas
27 @li mp_abort.sas
28 @li mpe_makedata.sas
29 @li mpe_makedatamodel.sas
30 @li mpe_makesampledata.sas
31 @li mv_createfile.sas
32 @li mv_deletejes.sas
33
34 @version 3.5
35 @author 4GL Apps Ltd
36 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
37 and may not be re-distributed or re-sold without the express permission of
38 4GL Apps Ltd.
39
40 @sasjslint {"maxLineLength": 84}
41
42**/
43
44
45%global dcpath ADMIN _contextname keepme sampledata;
46
47%webout(FETCH)
48
49/* enable vars to be passed as url params */
50%let exist=%sysfunc(exist(work.fromjs));
51%let inds=%sysfunc(ifc(&exist=1,fromjs,_null_));
52
53/*
54 Every value the service reads, from the input table into a macro variable of
55 the same name. The LENGTH statement declares them, so a request that sends
56 no table - or a table without that column - leaves its macro variable as it
57 was, a URL parameter or the default above, instead of creating a missing
58 numeric. Every read is therefore gated on the column being present.
59*/
60data _null_;
61 length dcpath $256 admin $64 _contextname $256
62 keepme $1 sampledata $1;
63 set &inds;
64 if not missing(dcpath) then call symputx('dcpath',dcpath);
65 if not missing(admin) then call symputx('ADMIN',admin);
66 if not missing(_contextname)
67 then call symputx('_contextname',_contextname);
68 if not missing(keepme) then call symputx('keepme',keepme);
69 if not missing(sampledata) then call symputx('sampledata',sampledata);
70run;
71
72/*
73 Both flags as Y or N. This reads the macro variable rather than the column,
74 so it covers either route: a column of the table has just been written to it,
75 and on the parameter route (the configurator link, the run-in-new-window
76 route) the URL parameter is already there. A value is never defaulted before
77 this - that is what discarded the parameter - and anything outside the list,
78 absent included, is N.
79*/
80data _null_;
81 call symputx('keepme'
82 ,ifc(upcase(strip(symget('keepme'))) in ('1','Y','YES','TRUE'),'Y','N'));
83 call symputx('sampledata'
84 ,ifc(upcase(strip(symget('sampledata'))) in ('1','Y','YES','TRUE'),'Y','N'));
85run;
86
87%macro self_destruct();
88%if "&keepme" ne "Y" %then %do;
89 %mv_deletejes(path=&apploc/services/admin, name=makedata);
90%end;
91%else %do;
92 %put NOTE: &_program retained, as KEEPME was requested;
93%end;
94%mend self_destruct;
95
96%macro makedata_body(iftrue=1);
97%if not (&iftrue) %then %return;
98
99/* the request's values, as macro variables - a column of the input table reads
100 exactly like the URL parameter of the same name */
101
102
103options noquotelenmax;
104
105%let dclib=%upcase(VIYA%substr(%sysevalf(%sysfunc(datetime())/60),3,4));
106%let dclibname=Data Controller (&dclib);
107%let DC_LIBREF=&dclib;
108%let work=%sysfunc(pathname(work));
109
110
111%let dcpath=%mf_trimstr(&dcpath,/)/&dclib;
112%put &=sysuserid;
113%put &=dcpath;
114%put &=admin;
115
116%mf_mkdir(&dcpath)
117%mp_abort(iftrue= (&syscc ne 0)
118 ,mac=&_program
119 ,msg=%str(Unable to create &dcpath using &sysuserid)
120)
121%mf_mkdir(&dcpath/secret)
122%mf_mkdir(&dcpath/dc_staging)
123
124/* check we have physical permissions to the DCLIB folder */
125data _null_;
126 putlog "dcpath=&dcpath/permTest.txt";
127 putlog "sysuserid=&sysuserid";
128data _null_;
129 file "&dcpath/permTest.txt";
130run;
131%mp_abort(iftrue= (&syscc ne 0)
132 ,mac=&_program
133 ,msg=%str(User &sysuserid does not have WRITE permissions to: &dcpath )
134)
135filename delfile "&dcpath/permTest.txt";
136data _null_;
137 rc=fdelete('delfile');
138run;
139%mp_abort(iftrue= (&syscc ne 0)
140 ,mac=&_program..sas
141 ,msg=%str(User &sysuserid could create (but not delete) &dcpath/permTest.txt )
142)
143
144
145libname &dclib "&dcpath";
146%global admin;
147%let admin=%sysfunc(coalescec(&admin,All Users));
148%mpe_makedatamodel(lib=&dclib)
149%mpe_makedata(lib=&dclib,mpeadmins=&admin,path=%str(&dcpath))
150
151%mp_abort(iftrue=(&syscc ne 0)
152 ,mac=&sysmacroname
153 ,msg=%str(Err during &dclib build)
154)
155
156
157/* sample data library - only when the deployment asked for it */
158%mpe_makesampledata(lib=&dclib,path=%str(&dcpath),iftrue=&sampledata)
159
160
161%mp_abort(iftrue=(&syscc ne 0)
162 ,mac=&sysmacroname
163 ,msg=%str(Err during demo data build)
164)
165
166
167/* the DC precode is stored in the root of the project */
168%let apploc=%mf_getapploc(&_program);
169%put &=apploc;
170
171/* The streamed entry page is DC.html at the appLoc, beside the services folder
172 that serves it. */
173
174filename jobout filesrvc folderpath="&apploc/services";
175data _null_;
176 file jobout('settings.sas');
177 put '/* these values are ignored if DC_LIBREF was declared in autoexec */';
178 put ' ';
179 put '%global DC_LIBREF dc_admin_group dc_staging_area ;';
180 put '/* This library (libref) contains the control datasets for DC */';
181 put '/* If a different libref must be used, configure it below */';
182 put '%let DC_LIBREF=' "&dclib;";
183 put ' ';
184 put "libname &dclib '&dcpath' ;";
185 put ' ';
186 put '/* This group has unrestricted access to Data Controller */';
187 put '%let dc_admin_group=' "&admin;";
188 put ' ';
189 put '/* This physical location is used for staging data and audit history */';
190 put '%let dc_staging_area=' "&dcpath/dc_staging;";
191 put ' ';
192 put 'cas dcsession sessopts=(caslib=casuser);';
193 put 'caslib _all_ assign;';
194 put ' ';
195%if "&sampledata" eq "Y" %then %do;
196 put '/* sample data library */';
197 put "libname DC_DEMO '&dcpath/dcdemo';";
198 put ' ';
199%end;
200run;
201
202
203/* create demo data
204
205cas dcsession;
206caslib _all_ assign;
207caslib casmusic path='/opt/sas/viya/cascache/tracks' libref=casmusic GLOBAL;
208
209proc casutil;
210 LOAD DATA=dcdemo.cars
211 CASOUT="cars"
212 OUTCASLIB="casmusic" PROMOTE ;
213run;
214*/
215
216/*
217cas mysess;
218caslib _all_ assign;
219data casmusic.artists(promote=yes);
220 length name varchar(30);
221 do tracks=1 to 100;
222 name='Phil Collins'!!cats(tracks);
223 output;
224 end;
225run;
226*/
227/*
228%let url=http://millionsongdataset.com/sites/default/files/AdditionalFiles%trim(
229 )/unique_tracks.txt;
230filename test url "&url" lrecl=3000 ;
231
232proc sql;
233drop table casmusic.tunes;
234
235data tracks;
236 infile test dlmstr='<SEP>' dsd end=lastobs;
237 input track_id:$32. song_id:$32. artist_nm:$128. title:$256.;
238 output;
239 if lastobs then do;
240 track_id='dummyrecords';
241 title='none';
242 artist_nm='none';
243 do x=1 to 4000000;
244 drop x;
245 song_id=cats(x);
246 output;
247 end;
248 stop;
249 end;
250run;
251
252proc casutil;
253 LOAD DATA=tracks
254 CASOUT="tunes"
255 OUTCASLIB="casmusic" PROMOTE ;
256run;
257/*
258data append;
259 if 0 then set &dclib..MPE_TABLES;
260 libref="CASMUSIC";
261 dsn='TUNES';
262 num_of_approvals_required=1;
263 loadtype='UPDATE';
264 buskey='TRACK_ID SONG_ID';
265 tx_from=0;
266 tx_to='31DEC9999:23:59:59'dt;
267 output;
268 dsn='ARTISTS';
269 buskey='NAME';
270 output;
271run;
272proc append base=&dclib..MPE_tABLES data=append;
273run;
274*/
275%mp_abort(iftrue=(&syscc ne 0)
276 ,mac=&sysmacroname
277 ,msg=%str(Err during settings job creation)
278)
279
280/* Update the compute context in the streamed entry page so that Data
281 Controller runs under the same context that was selected when this service
282 was invoked. The page is DC.html at the appLoc. The file must be re-created
283 using %mv_createfile (the same macro used to stream the frontend at deploy
284 time) rather than updated in place with a filesrvc fileref - only the Files
285 service POST
286 assigns the correct mime type (text/html / file_html), without which the
287 frontend will not stream.
288
289 A deploy that carried no client leaves no DC.html at the appLoc, and the
290 seed has no business failing over a file it was never given: a page that is
291 not there is reported in the log and the rewrite skipped, so the settings
292 this service exists to write are still written.
293
294 The name arrives as _contextname on the URL - that is what selects the
295 compute context, and every URL variable is a macro variable in the program -
296 and the deploy screen also sends it as the _contextname column, so a job
297 type that does not surface the URL variable still carries it. The column
298 wins when it is there; the URL variable is the value otherwise. With
299 neither, DC.html is left as the deploy wrote it rather than rewritten with an
300 empty context, which would stop the app booting. */
301%global _contextname;
302
303/* Escape regex metacharacters (\ and $) in the context name so they are
304 treated as literals when embedded in the prxchange replacement string.
305 In a Perl regex replacement, \ introduces a backreference and $ introduces
306 variable interpolation; prefixing each with \ quotes them as literals. */
307data _null_;
308 length _ctxname $256 _ctxesc $512;
309 _ctxname=symget('_contextname');
310 _ctxesc=tranwrd(tranwrd(_ctxname,'\','\\'),'$','\$');
311 call symputx('_contextname_escaped',_ctxesc);
312 call symputx('_ctxname_given',missing(_ctxname)=0);
313run;
314
315%if &_ctxname_given=0 %then %do;
316 %put NOTE: no context name reached makedata - DC.html keeps the context ;
317 %put NOTE- the deploy wrote;
318%end;
319%else %if %mfv_existfile(&apploc/DC.html)=0 %then %do;
320 %put NOTE: DC.html is not at &apploc - its context stays as the deploy ;
321 %put NOTE- wrote it;
322%end;
323%else %do;
324 %let dchtml_in=%mf_getuniquefileref();
325 %let dchtml_out=%mf_getuniquefileref();
326 filename &dchtml_in filesrvc folderpath="&apploc" filename="DC.html"
327 lrecl=32767;
328 data _null_;
329 file &dchtml_out lrecl=32767;
330 infile &dchtml_in lrecl=32767 truncover;
331 input;
332 length line $32767;
333 line=prxchange(cats('s|contextname="[^"]*"|contextname="'
334 ,symget('_contextname_escaped')
335 ,'"|'
336 ),-1,_infile_);
337 put line;
338 run;
339 filename &dchtml_in clear;
340
341 %mp_abort(iftrue= (&syscc ne 0)
342 ,mac=&sysmacroname
343 ,msg=%str(Unable to update compute context in DC.html)
344 )
345
346 /* re-create DC.html via the Files service to retain the html mime type */
347 %mv_createfile(path=&apploc, name=DC.html, inref=&dchtml_out)
348 filename &dchtml_out clear;
349
350%end;
351
352%webout(OPEN)
353data result;
354 dclib="&dclib";
355 admingroup="&admin";
356 dcpath="&dcpath";
357run;
358%webout(OBJ,result)
359%webout(CLOSE)
360
361/* keep the program when KEEPME was requested, so a reseed does not need a
362 * redeploy first */
363%self_destruct()
364
365%mend makedata_body;
366
367%makedata_body()
368