Project

General

Profile

Download (47.1 KB) Statistics
| Branch: | Revision:
1
define([
2
    'dojo/io/script',
3
    'dojo/cookie',
4
    "steam2/user",
5
    "steam2/models/Server",
6
//    "stabile/menu",
7
    "stabile/upload",
8
    "stabile/images",
9
    "stabile/servers",
10
    "stabile/networks",
11
    "steam2/stores",
12
    "dijit/form/ComboBox",
13
    "dijit/form/Form",
14
    "dijit/form/Select",
15
    "dijit/layout/StackContainer",
16
    "dijit/layout/StackController",
17
    "dijit/layout/ContentPane"
18
], function(ioScript, cookie, user, Server, /*menu,*/ upload, images, servers, networks, newStores){
19
    var systembuilder = {};
20
    var hostname = location.hostname;
21
    var subdom = hostname;
22
    if (hostname.substring( hostname.indexOf(".")+1 ).indexOf(".")>0) // check if hostname has more than one "."
23
        subdom = hostname.substring( hostname.indexOf(".")+1 );
24
    subdom = "." + subdom;
25
    var dialog;
26

    
27
    systembuilder.system = {
28
        managementlink: null,
29
        upgradelink: null,
30
        done: false,
31
        reshowing: false,
32
        cancelled: false,
33
        cur_sys_tpl: null,
34
        showInputs: false,
35
        homeExcerpt: null,
36
        create: function(){
37
            if(dijit.byId('createSystemDialog') !== undefined){
38
                // destroy the existing and its children
39
                //dijit.byId('createSystemDialog').destroyRecursive();
40
                dialog = dijit.byId('createSystemDialog');
41
                dialog.set("title", "Install Stack <a href=\"https://www.origo.io/info/stabiledocs/web/dashboard/install-stack/\" rel=\"help\" target=\"_blank\" class=\"irigo-tooltip\">help</a>");
42
                systembuilder.system.done = false;
43
                systembuilder.system.reshowing = true;
44
            } else {
45
                dialog = new dijit.Dialog({
46
                    title: "Install Stack <a href=\"https://www.origo.io/info/stabiledocs/web/dashboard/install-stack/\" rel=\"help\" target=\"_blank\" class=\"irigo-tooltip\">help</a>",
47
                    id: 'createSystemDialog',
48
                    style: "width: 90%; left:5%; overflow: auto;"
49
                });
50
                dialog.connect(dialog, "hide", function(e){
51
                    if (dojo.byId("manageSystemIframe")) dojo.byId("manageSystemIframe").src = '';
52
                });
53
            }
54

    
55
// Clear the cookies
56
            dojo.cookie("installsystem", '', {path: '/', expires: 'Sat, 01-Jan-2000 00:00:00 GMT'});
57
            dojo.cookie("installsystem", '', {path: '/', expires: 'Sat, 01-Jan-2000 00:00:00 GMT'});
58
            dojo.cookie("installsystem", '', {path: '/', domain: subdom, expires: 'Sat, 01-Jan-2000 00:00:00 GMT'});
59
            dojo.cookie("installsystem", '', {path: '/', domain: location.hostname, expires: 'Sat, 01-Jan-2000 00:00:00 GMT'});
60
            dojo.cookie("installaccount", '', {path: '/', domain: subdom, expires: 'Sat, 01-Jan-2000 00:00:00 GMT'});
61
            dojo.cookie("installaccount", '', {path: '/', domain: location.hostname, expires: 'Sat, 01-Jan-2000 00:00:00 GMT'});
62

    
63
            function step_one(){
64
                var one = [
65
                    '<form onsubmit="systembuilder.system.build();return false;" class="wizardForm" id="wizardForm" dojoType="dijit.form.Form">',
66
                    '<table border="0" style="margin:0 8px 20px 8px;">',
67
                    '<tr id="tr_master"><td class="wizardLabel">',
68
                        '<label>Select&nbsp;Stack to&nbsp;install:</label>',
69
                        '</td><td class="wizardLabel">',
70
                        '<select id="wizard_master" />',
71
                        '</td></tr>',
72
                    '</table>',
73
                    '<div dojoType="dijit.layout.StackContainer" style="overflow:auto; padding:0 10px 10px 10px;" id="wizardContainer">',
74

    
75
                    '<div dojoType="dijit.layout.ContentPane" id="wizardSubForm0">',
76
                    '<div><h3>',
77
                    '<img src="/stabile/static/img/loader.gif" style="vertical-align:middle; margin-right:20px;" alt="loading..." />',
78
                    '</h3></div>',
79
                    '</div>',
80

    
81
                    '<div dojoType="dijit.layout.ContentPane" id="wizardSubForm1">',
82
                    '<div nostyle="margin-left:auto; font-family:sans-serif; margin-right:auto; text-align:center; padding-top:15%; border: 0px solid;"><h3>',
83
                    '<img src="/stabile/static/img/loader.gif" style="vertical-align:middle; margin-right:20px;" alt="loading..." />',
84
                    'Loading stack settings...',
85
                    '</h3></div>',
86
                    '</div>',
87
                    '<div dojoType="dijit.layout.ContentPane" nostyle="left:0;" id="wizardSubForm2">',
88
                    '<p id="wizardTitle"></p>',
89
                    '<p class="well well-sm" id="wizardHelp" style="max-height:50vh; overflow:auto; width: 100%;">',
90
                    '<table border="0">',
91
                    '<tr id="tr_name" style="display:inherit;"><td class="wizardLabel">',
92
                        '<label>Name:</label>',
93
                        '</td><td class="wizardLabel">',
94
                        '<input id="wizard_name" />',
95
                        '</td></tr>',
96
                    '<tr id="tr_account"><td class="wizardLabel">',
97
                        '<label>Install to account:</label>',
98
                        '</td><td class="wizardLabel">',
99
                        '<select id="wizard_account" name="wizard_account">',
100
                        '</select>',
101
                        '</td></tr>',
102
                    '<tr id="tr_diskbus" class="wizardRow"><td class="wizardLabel">',
103
                        '<label>Disk bus:</label>',
104
                        '</td><td class="wizardLabel">',
105
                        '<select id="wizard_diskbus" name="wizard_diskbus">',
106
                        '<option value="virtio">Paravirtualized Network</option>',
107
                        '</select>',
108
                        '</td></tr>',
109
                    '<tr id="tr_memory" class="wizardRow"><td class="wizardLabel">',
110
                        '<label>Memory:</label>',
111
                        '</td><td class="wizardLabel">',
112
                        '<select id="wizard_memory" name="wizard_memory">',
113
                        '<option>256</option>',
114
                        '<option>512</option>',
115
                        '<option selected >1024</option>',
116
                        '<option>2048</option>',
117
                        '<option>4096</option>',
118
                        '<option>8192</option>',
119
                        '<option>16384</option>',
120
                        '</select>MB',
121
                        '</td></tr>',
122
                    '<tr id="tr_vcpu" class="wizardRow"><td class="wizardLabel">',
123
                        '<label>VCPUs: </label>',
124
                        '</td><td class="wizardLabel">',
125
                        '<input id="wizard_vcpu" name="wizard_vcpu" style="width:16px" />',
126
                        '</td></tr>',
127
                    '<tr id="tr_networktype1" class="wizardRow"><td class="wizardLabel">',
128
                        '<label>Connection:</label>',
129
                        '</td><td class="wizardLabel">',
130
                        '<select id="wizard_networktype1" name="wizard_networktype1">',
131
                        '<option value="ipmapping">IP mapping</option>',
132
                        '<option value="internalip">Internal ip address</option>',
133
                        '<option value="externalip">External ip address</option>',
134
                        '</select>',
135
                        '</td></tr>',
136
                    '<tr id="tr_ports" class="wizardRow"><td class="wizardLabel">',
137
                        '<label>Ports:</label>',
138
                        '</td><td class="wizardLabel">',
139
                        '<input id="wizard_ports" /> <span class="small">E.g. "80, 443". Leave empty to allow traffic to all tcp/udp ports</span>',
140
                        '</td></tr>',
141
                    '<tr id="tr_nicmodel1" class="wizardRow"><td class="wizardLabel">',
142
                        '<label>NIC model:</label>',
143
                        '</td><td class="wizardLabel">',
144
                        '<select id="wizard_nicmodel1" name="wizard_nicmodel1">',
145
                        '<option value="virtio">Paravirtualized Network</option>',
146
                        '</select>',
147
                        '</td></tr>',
148
                    '<tr id="tr_storagepool" class="wizardRow"><td class="wizardLabel">',
149
                        '<label>Storage:</label>',
150
                        '</td><td class="wizardLabel">',
151
                        '<select id="wizard_storagepool" name="wizard_storagepool">',
152
                        '<option value="-1">On node</option>',
153
                        '<option value="0">Shared</option>',
154
                        '</select>',
155
                        '</td></tr>',
156
                    '<tr id="tr_storagepool2" class="wizardRow"><td class="wizardLabel">',
157
                        '<label>Data storage:</label>',
158
                        '</td><td class="wizardLabel">',
159
                        '<select id="wizard_storagepool2" name="wizard_storagepool2">',
160
                        '<option value="0">Shared</option>',
161
                        '<option value="-1">On node</option>',
162
                        '</select>',
163
                        '</td></tr>',
164
                    '<tr id="tr_image2" class="wizardRow"><td class="wizardLabel">',
165
                        '<label>Data image master:</label>',
166
                        '</td><td class="wizardLabel">',
167
                        '<input id="wizard_image2" /> <span class="small">secondary image for data storage</span>',
168
                        '</td></tr>',
169
                    '<tr id="tr_cdrom" class="wizardRow"><td class="wizardLabel">',
170
                        '<label>CD-rom:</label>',
171
                        '</td><td class="wizardLabel">',
172
                        '<select id="wizard_cdrom" name="wizard_cdrom">',
173
                        '<option value="--">--</option>',
174
                        '</select>',
175
                        '</td></tr>',
176
                    '<tr id="tr_boot" class="wizardRow"><td class="wizardLabel">',
177
                        '<label>Boot device:</label>',
178
                        '</td><td class="wizardLabel">',
179
                        '<select id="wizard_boot" name="wizard_boot">',
180
                        '<option value="--">hd</option>',
181
                        '</select>',
182
                        '</td></tr>',
183
                    '<tr id="tr_bschedule" class="wizardRow"><td class="wizardLabel">',
184
                        '<label>Backup:</label>',
185
                        '</td><td class="wizardLabel">',
186
                        '<select id="wizard_bschedule" name="wizard_bschedule">',
187
                        '<option selected value="">None</option>',
188
                        '<option value="daily7">Daily, 7 days</option>',
189
                        '<option value="daily14">Daily, 14 days</option>',
190
                        '</select>',
191
                        '</td></tr>',
192
                    '<tr id="tr_monitors" class="wizardRow"><td class="wizardLabel">',
193
                        '<label>Monitors:</label>',
194
                        '</td><td class="wizardLabel">',
195
                        '<select id="wizard_monitors" name="wizard_monitors">',
196
                        '<option selected value="">None</option>',
197
                        '<option value="ping,diskspace">Ping and diskspace</option>',
198
                        '<option value="ping">Ping</option>',
199
                        '<option value="diskspace">Diskspace</option>',
200
                        '</select>',
201
                        '</td></tr>',
202
                    '<tr id="tr_instances" class="wizardRow"><td class="wizardLabel">',
203
                        '<label>Server instances:</label>',
204
                        '</td><td class="wizardLabel">',
205
                        '<input id="wizard_instances" name="wizard_instances" />',
206
                        '</td></tr>',
207
                    '<tr id="tr_start" class="wizardRow"><td class="wizardLabel">',
208
                        '<label>Start server(s):</label>',
209
                        '</td><td class="wizardLabel">',
210
                        '<span id="wizard_start" name="wizard_start" />',
211
                        '</td></tr>',
212
                    '<tr id="tr_managementlink" style="display:none"><td class="wizardLabel">',
213
                        '<label>Management link:</label>',
214
                        '</td><td class="wizardLabel">',
215
                        '<input id="wizard_managementlink" />',
216
                        '</td></tr>',
217
                    '<tr id="tr_appid" style="display:none"><td class="wizardLabel">',
218
                        '<label>App ID:</label>',
219
                        '</td><td class="wizardLabel">',
220
                        '<input id="wizard_appid" />',
221
                        '</td></tr>',
222
                    '</table>',
223
                    '</div>',
224
                    '</div>',
225
                    '<!-- p class="wizardInfoPane" id="finish_notes" style="display:none;" -->',
226
                    '<!-- /p -->',
227
                    '<div style="padding: 10px 13px 40px 10px; border-top: 1px solid #e5e5e5;">',
228
                    '<span class="pull-left">Hide preconfigured settings: ',
229
                    '<input id="wizard_recomended_settings" name="wizard_recomended_settings" />',
230
                    '</span>',
231
                    '<button id="buildsystembutton" class="btn btn-sm btn-primary pull-right" type="submit">',
232
                    'Install',
233
                    '</button>',
234
                    '</div>',
235
                    '</form>'].join('');
236
                return one;
237
            }
238

    
239
            function set_default_name(servername) {
240
                dijit.byId("wizard_name").setValue(servername);
241
                dijit.byId("wizard_name").focus();
242
            }
243

    
244
            function init_step_one(){
245
                var wizard_name = new dijit.form.ValidationTextBox(
246
                {
247
                    name: 'wizard_name',
248
                    type: 'text',
249
                    required: true,
250
                    style: "width:250px",
251
                    tabindex: 1
252
                }, 'wizard_name');
253
                var wizard_account = new dijit.form.Select(
254
                {
255
                    name: 'wizard_account',
256
                    type: 'text',
257
                    required: false,
258
                    store: stores.accounts,
259
                    value: user.username,
260
                    query: {privileges: /n|a|u|^$/},
261
                    onChange: systembuilder.system.updateWizardAccounts,
262
                    style: "width:250px"
263
                }, 'wizard_account');
264
                var wizard_master = new dijit.form.Select(
265
                {
266
                    name: 'wizard_master',
267
                    type: 'text',
268
                    required: false,
269
                    store: stores.masterimages,
270
                    query: {installable: 'true', status: '*used'},
271
                    searchAttr: 'name',
272
                    onChange: systembuilder.system.on_change_master,
273
                    style: "width:250px"
274
                }, 'wizard_master');
275
                dijit.form.ComboBox({style: "width:64px"}, 'wizard_memory');
276
                dijit.form.NumberSpinner(
277
                {
278
                    value: 1,
279
                    smallDelta: 1,
280
                    constraints: {
281
                        min: 1,
282
                        max: 4,
283
                        places: 0
284
                    },
285
                    style: "width: 32px;"
286
                }, 'wizard_vcpu');
287

    
288
                dijit.form.NumberSpinner(
289
                {
290
                    value: 1,
291
                    smallDelta: 1,
292
                    constraints: {
293
                        min: 1,
294
                        max: 100,
295
                        places: 0
296
                    },
297
                    style: "width: 40px;"
298
                }, 'wizard_instances');
299
                dijit.form.Select({}, 'wizard_storagepool');
300
                dijit.form.Select({}, 'wizard_storagepool2');
301
                dijit.form.Select({store: stores.diskbus, value: 'virtio'}, 'wizard_diskbus');
302
                dijit.form.Select({store: stores.networkInterfaces, value: 'virtio'}, 'wizard_nicmodel1');
303
                dijit.form.Select({store: stores.cdroms, value: '--'}, 'wizard_cdrom');
304
                dijit.form.Select({store: stores.bootDevices, value: 'hd'}, 'wizard_boot');
305
                dijit.form.Select({}, 'wizard_networktype1');
306
                dijit.form.TextBox({disabled: false}, 'wizard_ports');
307
                dijit.form.Select({}, 'wizard_bschedule');
308
                dijit.form.Select({}, 'wizard_monitors');
309
                dijit.form.TextBox({disabled: false, style: {width:"400px"}}, 'wizard_image2');
310
                dijit.form.CheckBox({checked: false}, 'wizard_start');
311
                dijit.form.TextBox({disabled: true}, 'wizard_managementlink');
312
                dijit.form.TextBox({style: "width:30px"}, 'wizard_appid');
313
                dijit.form.CheckBox({checked: !systembuilder.system.showInputs, onChange: systembuilder.system.showRecomendedInputs}, 'wizard_recomended_settings');
314
                $("#buildsystembutton").prop("disabled", true);
315
            }
316

    
317
            dialog.set('content',step_one());
318
            init_step_one();
319
            q = dojo.query('.irigo-tooltip'); q.irigoTooltip && q.irigoTooltip();
320
            stores.masterimages.close();
321
            if (systembuilder.system.reshowing) {
322
                dijit.byId('wizard_master').setStore(stores.masterimages, '--', {query: {installable: 'true', status: '*used'}});
323
                dijit.byId("wizard_account").setStore(stores.accounts, user.username);
324
                dijit.byId("wizard_diskbus").setStore(stores.diskbus, 'virtio');
325
                dijit.byId("wizard_nicmodel1").setStore(stores.networkInterfaces, 'virtio');
326
                dijit.byId("wizard_cdrom").setStore(stores.cdroms, '--');
327
                dijit.byId("wizard_boot").setStore(stores.bootDevices, 'hd');
328
            }
329

    
330
            if (user.privileges.indexOf("n")==-1 && !user.is_admin) dijit.byId("wizard_storagepool").set('disabled', true);
331
            if (user.privileges.indexOf("n")==-1 && !user.is_admin) dijit.byId("wizard_storagepool2").set('disabled', true);
332

    
333
            dialog.show();
334

    
335
            function testName(i) {
336
                testname = 'New Stack ' + Math.uuid().toLowerCase().substr(0,4);
337
                testpath = "/mnt/stabile/images/" + user.username  + "/" + testname + " image.qcow2";
338
                var mast = dijit.byId('wizard_master').value;
339
                if (mast != '') {
340
                    testpath = mast.replace(/(\/.+\/)(\w+)(\/.+)/,"$1" + user.username + "/" + testname + " image.qcow2");
341
                }
342
                stores.images.fetch({query: {path: testpath, installable: "true", status: "*used"}, onComplete: function(items2) {
343
                    if (items2 && items2.length>0) {
344
                        i = i + 1;
345
                        testName(i);
346
                    }
347
                    else (set_default_name(testname));
348
                }});
349
            }
350
        },
351

    
352
        updateWizardAccounts: function() {
353
            document.getElementById("tr_account").style.display = "none";
354
            if (dijit.byId("wizard_account").options.length<=1) {
355
                document.getElementById("tr_account").style.display = "none";
356
            }
357
            else if (home.install_account) {
358
                home.install_account = '';
359
                dijit.byId("wizard_account").set('disabled', true);
360
            } else {
361
                dijit.byId("wizard_account").set('disabled', false);
362
            }
363
        },
364

    
365
        on_change_master: function(path) {
366
            console.log("master changed", path, home.install_sytem);
367
            $("#wizard_ports").val('');
368
            if (home.install_system) { // Don't fire onchange if we are loading a system from the app store
369
                console.log("installing app from app store", home.install_system);
370
                stores.masterimages.fetch( {query: {appid: home.install_system, installable: "true"}, onComplete:
371
                    function(items) {
372
                        if (items.length && items.length>0) {
373
                            dijit.byId("wizard_master").setValue(items[0].path[0]);
374
                            home.install_system = '';
375
                            location.hash = 'home';
376
                        } else {
377
                            IRIGO.toast("The master image was not found! Please make sure your engine is configured to downlod master images from Stabile Registry.");
378
                            home.install_system = '';
379
                            location.hash = 'home';
380
                            systembuilder.system.on_change_master('--');
381
                        }
382
                    }
383
                })
384
            } else if (path == '--') {
385
                $("#buildsystembutton").prop("disabled", true);
386
                if (systembuilder.system.homeExcerpt) {
387
                    document.getElementById("wizardSubForm0").innerHTML = systembuilder.system.homeExcerpt;
388
                    dijit.byId("wizardContainer").selectChild(dijit.byId("wizardSubForm0"), true);
389
                } else {
390
                    document.getElementById("wizardSubForm0").innerHTML =
391
                            '<div style="margin-left:auto; font-family:sans-serif; margin-right:auto; text-align:center; padding-top:15%; border: 0px solid;"><h3><img src="/stabile/static/img/loader.gif" style="vertical-align:middle; margin-right:20px;" alt="loading..." /></h3></div></div>';
392

    
393
                    dijit.byId("wizardContainer").selectChild(dijit.byId("wizardSubForm0"), true);
394
                    var topic_url = "https://www.origo.io/appstore.cgi?action=engineappstore";
395
                    var topicargs = {
396
                        url: topic_url,
397
                        callbackParamName: 'callback'
398
                    };
399
                    var dfd = ioScript.get(topicargs);
400

    
401
                    dfd.then(function(js){
402
                        if (js && js.app) {
403
                            systembuilder.system.homeExcerpt = js.app.summary;
404
                            document.getElementById("wizardSubForm0").innerHTML = systembuilder.system.homeExcerpt;
405
                        }
406
                    });
407
                }
408

    
409
            } else {
410
                dijit.byId("wizardContainer").selectChild(dijit.byId("wizardSubForm1"), true);
411
                $("#buildsystembutton").prop("disabled", true);
412
                stores.masterimages.fetch({
413
                    query: {path: path, installable: "true", status: "*used"},
414
                    onComplete: systembuilder.system.load_changed_master,
415
                    onError: systembuilder.system.master_not_found
416
                });
417
            }
418
        },
419
        load_changed_master: function(item) {
420
            console.log("master changed", item);
421
            if (item[0]) {
422
                var appid = item[0].appid[0];
423
                var version = item[0].version[0];
424
                if (appid && appid!='--') { // && IRIGO.user.enginelinked) {
425
                    console.log("loading app", appid);
426
                    systembuilder.currentManagementlink = item[0].managementlink;
427
                    systembuilder.system.prepareSystem(appid, version);
428
                } else {
429
                    console.log("loading unknown app", item[0].managementlink[0]);
430
                    if (item[0].managementlink[0] && item[0].managementlink[0]!='') {
431
                        var usys = [];
432
                        usys.managementlink = item[0].managementlink[0];
433
                        usys.upgradelink = item[0].upgradelink[0];
434
                        if (item[0].terminallink) usys.terminallink = item[0].terminallink[0];
435
                        if (item[0].image2) usys.image2 = item[0].image2[0];
436
                        systembuilder.currentManagementlink = item[0].managementlink[0];
437
                        systembuilder.system.loadSystem(usys);
438
                    } else {
439
                        systembuilder.currentManagementlink = '';
440
                        systembuilder.system.loadSystem();
441
                    }
442
                }
443
            } else {
444
                systembuilder.currentManagementlink = '';
445
                console.log("no master selected");
446
            }
447
        },
448
        lookup_master: function(str) {
449
        //    stores.masterimages.close();
450
            stores.masterimages.fetch({
451
                query: {path: str, installable: "true", status: "*used"},
452
                onComplete: systembuilder.system.set_master,
453
                onError: systembuilder.system.master_not_found
454
            });
455
        },
456
        set_master: function(item) {
457
            if (!item.length || item.length==0)
458
                systembuilder.system.master_not_found();
459
            else
460
                dijit.byId("wizard_master").setValue(item[0].path[0]);
461
        },
462
        master_not_found: function() {
463
            console.log("Master not found");
464
            home.install_system = '';
465
            $("#buildsystembutton").hide();
466
            document.getElementById("wizardHelp").innerHTML =
467
                    'The master image was not found <a id="wizardHelpDefault" rel="help" target="_blank" class="irigo-tooltip" href="https://www.origo.io/info/stabiledocs/web/dashboard/install-stack/">help</a>';
468
            q = dojo.query('.irigo-tooltip'); q.irigoTooltip && q.irigoTooltip();
469

    
470
            var prop;
471
            var wform = dijit.byId('wizardForm');
472
            dojo.forEach(wform.getChildren(), function(input) {
473
                prop = input.id.substring(7);
474
                var element = document.getElementById("tr_"+prop);
475
                if (element) element.style.display="none";
476
            });
477

    
478
        },
479

    
480
        lookup_cdrom: function(str) {
481
            stores.cdroms.fetch({
482
                query: {path: str},
483
                onComplete: systembuilder.system.set_cdrom,
484
                onError: systembuilder.system.cdrom_not_found
485
            });
486
        },
487
        set_cdrom: function(item) {
488
            if (!item.length || item.length==0)
489
                systembuilder.system.cdrom_not_found();
490
            else
491
                dijit.byId("wizard_cdrom").setValue(item[0].path[0]);
492
        },
493
        cdrom_not_found: function() {
494
            console.log("CDrom not found");
495
        },
496

    
497
        prepareSystem: function(appid, version){
498
            if (appid) {
499
                var topic_url = "/stabile/systems?action=appstore&appid=" + appid + "&version=" + version;
500
                var topicargs = {
501
                    url: topic_url,
502
                    callbackParamName: 'callback'
503
                };
504
                var dfd = ioScript.get(topicargs);
505

    
506
                dfd.then(function(js){
507
                    if (js && js.app) {
508
                        var sys = js.app;
509
                        systembuilder.system.loadSystem(sys);
510
                    } else {
511
                        systembuilder.system.loadSystem();
512
                    }
513
                });
514
            } else {
515
                systembuilder.system.loadSystem();
516
            }
517
        },
518

    
519
        showRecomendedInputs: function(hide) {
520
            var prop;
521
            systembuilder.system.showInputs = !hide;
522
            if (hide) {
523
                console.log("hiding inputs", hide, cur_sys_tpl);
524
                dojo.query(".wizardRow").forEach(function(node){
525
                    prop = node.id.substring(3);
526
                    if ((cur_sys_tpl && cur_sys_tpl[prop] && cur_sys_tpl[prop]!='')) {
527
                        //node.style = "display:none;";
528
                        node.style.display = "";
529
                        console.log(node.style.display, prop, cur_sys_tpl[prop]);
530
                    }
531
                })
532
            } else {
533
                console.log("showing inputs", hide);
534
                dojo.query(".wizardRow").forEach(function(node){
535
                    node.style.display="inherit";
536
                })
537
            }
538
        },
539

    
540
        loadSystem: function(sys_tpl) {
541
            console.log("Loading system", sys_tpl);
542
            //for (prop in sys_tpl) {
543
            //    console.log(prop, sys_tpl[prop]);
544
            //}
545
            var prop;
546
            var wform = dijit.byId('wizardSubForm2');
547
            if (sys_tpl) cur_sys_tpl = sys_tpl;
548
            else cur_sys_tpl = null;
549
            dojo.forEach(wform.getDescendants(), function(input) {
550
                prop = input.id.substring(7);
551
                if (sys_tpl && sys_tpl[prop]) {
552
                    if (prop=="start" && sys_tpl[prop] == "false") {// Tick or untick start checkbox
553
                        input.setValue(false);
554
                    } else if (prop=="master") {
555
                        if (input.value.indexOf(sys_tpl[prop])==-1) {// Incomplete path - look up complete path
556
                            console.log("got incomplete path, looking up", sys_tpl[prop]);
557
                            systembuilder.system.lookup_master("*/" + sys_tpl[prop]);
558
                        }
559
                    } else if (prop=="image2") {
560
                        if (sys_tpl[prop] == '--') input.setValue('')
561
                        else input.setValue(sys_tpl[prop]);
562
                    } else if (prop=="cdrom") {
563
                        if (input.value.indexOf(sys_tpl[prop]) == -1 && sys_tpl[prop] != '--') {// Incomplete path - look up complete path
564
                            console.log("got cdrom incomplete path, looking up", sys_tpl[prop]);
565
                            systembuilder.system.lookup_cdrom("*/" + sys_tpl[prop]);
566
                        }
567

    
568
                    } else if (prop=="name") {
569
                        if (home.install_name) {
570
                            input.setValue(decodeURIComponent(home.install_name));
571
                            home.install_name = '';
572
                        } else {
573
                            input.setValue(sys_tpl[prop]);
574
                        }
575
                    } else if (sys_tpl[prop]) {
576
                        input.setValue(sys_tpl[prop]);
577
                    }
578
                    if (prop!="name" && prop!="master" && prop!="appid" && prop!="managementlink" && prop!="upgradelink" && prop!="version") {
579
                        if (sys_tpl[prop] && !systembuilder.system.showInputs) {
580
                            document.getElementById("tr_"+prop).style.display="none";
581
                        } else {
582
                            document.getElementById("tr_"+prop).style.display="inherit";
583
                        }
584
                    }
585
                } else {
586
                    if (prop=="name") {
587
                        //var testname = 'New App ' + Math.uuid().toLowerCase().substr(0,4);
588
                        var testname = dijit.byId("wizard_master").attr("displayedValue");
589
                        if (home.install_name) {
590
                            testname = home.install_name;
591
                            home.install_name = '';
592
                        }
593
                        input.set("value", testname);
594
                    } else if (prop!="master" && prop!="appid" && prop!="managementlink" && prop!="upgradelink" && prop!="version") {
595
                        if (document.getElementById("tr_"+prop)) document.getElementById("tr_"+prop).style.display="inherit";
596
                    }
597
                }
598
            });
599
            if (sys_tpl && sys_tpl.name) {
600
                document.getElementById("wizardTitle").innerHTML = "<span title=\"Version: " + sys_tpl.version + "\">" + sys_tpl.name + "</span>";
601
            } else {
602
                document.getElementById("wizardTitle").innerHTML = "";
603
            }
604
            if (sys_tpl && sys_tpl.appid) {
605
                document.getElementById("wizardTitle").innerHTML += " <span class=\"dimlink\">(version: " + sys_tpl.version + ", <a class=\"dimlink\" target=\"_blank\" href=\"https://www.stabile.io/registry#app-" + sys_tpl.appid + "\">view in Stabile Registry)</a></span>";
606
            }
607
            if (sys_tpl && sys_tpl.managementlink && sys_tpl.managementlink!='' && sys_tpl.upgradelink && sys_tpl.upgradelink!='') {
608
                dijit.byId('wizard_managementlink').set("value", sys_tpl.managementlink);
609
                document.getElementById("wizardTitle").innerHTML += " <span class=\"alert alert-info small teaser\" title=\"This stack may be managed by clicking the 'manage' button after installing\" style=\"float:right; font-size:13px; padding:8px\">This stack is manageable and upgradable</span>";
610
            } else if (sys_tpl && sys_tpl.managementlink && sys_tpl.managementlink!='') {
611
                dijit.byId('wizard_managementlink').set("value", sys_tpl.managementlink);
612
                document.getElementById("wizardTitle").innerHTML += " <span class=\"alert alert-info small teaser\" title=\"This stack may be managed by clicking the 'manage' button after installing\" style=\"float:right; font-size:13px; padding:8px\">This stack is manageable</span>";
613
            } else if (systembuilder.currentManagementlink) {
614
                dijit.byId('wizard_managementlink').set("value", systembuilder.currentManagementlink);
615
                document.getElementById("wizardTitle").innerHTML += " <span class=\"alert alert-info small teaser\" title=\"This stack may be managed by clicking the 'manage' button after installing\" style=\"float:right; font-size:13px; padding:8px\">This stack is manageable</span>";
616
            }
617
            document.getElementById("wizardTitle").innerHTML = "<h3>" + document.getElementById("wizardTitle").innerHTML + "</h3>";
618
            if (sys_tpl && sys_tpl.name) {
619
                var desc = '';
620
                if (sys_tpl.thumbnail) {
621
                    var thumb = sys_tpl.thumbnail;
622
                    if (thumb.indexOf("http") != 0) thumb = "https://www.origo.io" + thumb;
623
                    desc += '<div style="text-align:center; margin-bottom:20px;"><img style="max-height:120px;" src="' + thumb + '"></div>';
624
                }
625
                desc += sys_tpl.description || 'This stack is waiting for a nice description from its owner...';
626
                document.getElementById("wizardHelp").innerHTML = desc;
627
            } else {
628
                document.getElementById("wizardHelp").innerHTML =
629
                    'You are about to install a new stack from a custom master image.';
630
                q = dojo.query('.irigo-tooltip'); q.irigoTooltip && q.irigoTooltip();
631
            }
632
            dialog.show();
633
            document.getElementById("wizardHelp").style.display="block";
634
            dijit.byId("wizardContainer").selectChild(dijit.byId("wizardSubForm2"), true);
635
            $("#buildsystembutton").prop("disabled", false);
636
            $("#wizardHelp").scrollTop(0);
637
            systembuilder.system.updateWizardAccounts();
638
        },
639

    
640
        manage_in_tab: function(force) {
641
            systembuilder.system.cancelled = false;
642
            var manloc = home.currentManagementlink;
643
            if (home.currentItem && home.currentItem.status == 'upgrading' && !force) {
644
                manloc = '/stabile/static/html/upgrading.html';
645
            }
646
            window.open(manloc,'_blank');
647
        },
648
        terminal_in_tab: function() {
649
            var manloc = home.currentTerminallink;
650
            window.open(manloc,'_blank');
651
        },
652

    
653
        manage: function(managementlink, force, systemname, sysuuid) {
654
            console.log("Managing", systemname, sysuuid);
655
            if (!managementlink) managementlink = home.currentManagementlink;
656
            if (!managementlink) {
657
                IRIGO.toast("No management interface specified for this system");
658
                return;
659
            }
660
            console.log("Managing system:", managementlink);
661
            systembuilder.system.cancelled = false;
662
            if (!systemname && home.currentItem) systemname = home.currentItem.name;
663

    
664
        // Loading external URL's in iframe is no longer supported by most browsers
665
            if (managementlink.indexOf("http")==0) {
666
                systembuilder.system.manage_in_tab(force);
667
                return;
668
            }
669

    
670
            var dialog;
671
            if(dijit.byId('createSystemDialog') !== undefined){
672
                dialog = dijit.byId('createSystemDialog');
673
                if (systemname) dialog.set("title", (systemname.indexOf(":")==-1)?"Manage: " + systemname:systemname);
674
            } else {
675
                dialog = new dijit.Dialog({
676
                    title: ((systemname.indexOf(":")==-1)?"Manage: " + systemname:systemname),
677
                    id: 'createSystemDialog',
678
                    resizable: true,
679
                    style: "width: 90%; overflow: auto;"
680
                });
681
                dialog.connect(dialog, "hide", function(e){
682
                    if (dojo.byId("manageSystemIframe")) dojo.byId("manageSystemIframe").src = '';
683
                });
684
            }
685
            if (sysuuid) dialog.set("sysuuid", sysuuid);
686
            else if (home.currentItem) dialog.set("sysuuid", home.currentItem.uuid);
687

    
688
            dojo.connect(dialog,"hide",function(o){
689
                systembuilder.system.cancelled = true;
690
            });
691

    
692
            if (home.currentItem && home.currentItem.status == 'upgrading' && !force) {
693
                var content = '<iframe src="/stabile/static/html/upgrading.html" id="manageSystemIframe" style="height:75vh;"></iframe>';
694
                dialog.set('content', content);
695
            } else {
696
                var content = '<iframe src="/stabile/static/html/loading.html" id="manageSystemIframe" style="height:75vh;"></iframe>';
697
                dialog.set('content', content);
698
                systembuilder.system.loadIframe(0, dialog, managementlink);
699
            }
700
            dialog.show();
701

    
702
        },
703
        terminal: function() {
704
            systembuilder.system.manage(home.currentTerminallink, false, "Terminal: " + home.currentItem.name);
705
        },
706

    
707
        close: function() {
708
            if(dijit.byId('createSystemDialog') !== undefined){
709
                dijit.byId('createSystemDialog').hide();
710
            }
711
        },
712

    
713
        upgrade: function(internalip) {
714
            IRIGO.toaster([{
715
                message: "Hang on, upgrading your app...",
716
                type: "message",
717
                duration: 2000
718
            }]);
719
            dojo.byId("manageSystemIframe").src = '/stabile/static/html/upgrading.html';
720

    
721
            dojo.xhrGet({
722
                url: "/stabile/systems?action=upgradesystem&internalip=" + internalip,
723
                failOK: true,
724
                timeout: 10000,
725
                handleAs : "json",
726
                load: function(response) {
727
                    if (response.message) {
728
                        IRIGO.toaster([{
729
                            message: response.message,
730
                            type: "message",
731
                            duration: 2000
732
                        }]);
733
                    }
734
                },
735
                error: function(response) {
736
                    console.log("got an error upgrading");
737
                }
738
            });
739

    
740
            dojo.subscribe("upgrade:update", function(task){
741
                if (task.managementlink && task.progress && task.progress==100) {
742
                    grid.alertDialog("Data export done", task.status + "<br><br>", "systembuilder.system.manage", task.managementlink, true);
743
                }
744
            });
745
        },
746

    
747
        loadIframe: function(i, d, link) {
748
            dojo.xhrGet({
749
                url: link,
750
                failOK: true,
751
                timeout: 30000,
752
                load: function(response) { // even after first request succeeds, subsequent may fail, so we wait a little
753
                    if (i>0)
754
                        setTimeout(function() {dojo.byId("manageSystemIframe").src = link}, 8000);
755
                    else
756
                        dojo.byId("manageSystemIframe").src = link;
757
                },
758
                error: function(response) {
759
                    console.log("got an error loading iframe", i);
760
                    if (systembuilder.system.cancelled==false) {
761
                        if (i<12) {
762
                            console.log("Loading in 1 sec...", link, i);
763
                            setTimeout(systembuilder.system.loadIframe, 1000,i+1, d, link);
764
                        } else {
765
                            console.log("giving up loading iframe");
766
                            IRIGO.toast("Management interface for this stack is not ready...");
767
                            dojo.byId("manageSystemIframe").src = "/stabile/static/html/notready.html";
768
                        }
769
                    }
770
                }
771
            });
772
        },
773

    
774
        build: function(){
775
            if (!systembuilder.system.done) {
776
                var appid = dijit.byId('wizard_appid').get('value');
777
                if (dijit.byId("wizard_account").options.length>1 && dijit.byId("wizard_account").value!=user.username) {
778
                    console.log("Keeping app", appid, subdom);
779
                    dojo.cookie("installsystem", appid, {path: '/', domain: subdom});
780
                    home.changeAccount(dijit.byId("wizard_account").value);
781
                    return;
782
                }
783
                $("#buildsystembutton").html('Installing&hellip; <i class="fa fa-cog fa-spin"></i>').prop("disabled", true);
784

    
785
                dojo.forEach(dijit.byId('wizardForm').getChildren(), function(input) {
786
                    input.set('disabled', true);
787
                });
788

    
789
                var name = dijit.byId('wizard_name').value;
790
                var master = dijit.byId('wizard_master').value + "";
791
                var memory = dijit.byId('wizard_memory').value;
792
                var vcpu = dijit.byId('wizard_vcpu').get('value');
793
                var network = dijit.byId('wizard_networktype1').value;
794
                var ports = dijit.byId('wizard_ports').value;
795
                var nicmodel = dijit.byId('wizard_nicmodel1').value;
796
                var diskbus = dijit.byId('wizard_diskbus').value;
797
                var storagepool = dijit.byId('wizard_storagepool').value;
798
                var storagepool2 = dijit.byId('wizard_storagepool2').value;
799
                var cdrom = dijit.byId('wizard_cdrom').value;
800
                var boot = dijit.byId('wizard_boot').value;
801
                var bschedule = dijit.byId('wizard_bschedule').value;
802
                var monitors = dijit.byId('wizard_monitors').value;
803
                var image2 = dijit.byId('wizard_image2').value;
804
                var instances = dijit.byId('wizard_instances').get('value');
805
                var start = dijit.byId('wizard_start').checked;
806
                var managementlink = dijit.byId('wizard_managementlink').get('value');
807

    
808
                var postData = '{"items":[{' +
809
                        '"action": "buildsystem", '+
810
                        '"name": "' + encodeURIComponent(name) + '",' +
811
                        '"master": "' + master + '",' +
812
                        '"memory": ' + memory + ',' +
813
                        '"vcpu": ' + vcpu + ',' +
814
                        '"networktype1": "' + network + '",' +
815
                        '"ports": "' + ports + '",' +
816
                        '"nicmodel1": "' + nicmodel + '",' +
817
                        '"diskbus": "' + diskbus + '",' +
818
                        '"storagepool": "' + storagepool + '",' +
819
                        '"storagepool2": "' + storagepool2 + '",' +
820
                        '"cdrom": "' + cdrom + '",' +
821
                        '"boot": "' + boot + '",' +
822
                        '"bschedule": "' + bschedule + '",' +
823
                        '"monitors": "' + monitors + '",' +
824
                        '"image2": "' + image2 + '",' +
825
                        '"instances": ' + instances + ',' +
826
                        '"managementlink": "' + managementlink + '",' +
827
                        '"appid": "' + appid + '",' +
828
                        '"start": ' + (start?1:0) +
829
                        '}]}';
830
                var xhrArgs = {
831
                    url: "/stabile/systems",
832
                    postData: postData,
833
                    load: function(data){
834
                        home.grid.refresh();
835
                        if (networks.grid && networks.grid.refresh) networks.grid.refresh();
836
                        if (images.grid && images.grid.refresh) images.grid.refresh();
837
                        if (servers.grid && servers.grid.refresh) servers.grid.refresh();
838
                        systembuilder.system.done = true;
839
                        $("#buildsystembutton").text("done").prop("disabled", false).hide();
840
                        var nid = /Status=OK sysuuid: (\S+)/.exec(data);
841
                        var sysuuid;
842
                        if (nid && nid.length>1) sysuuid = nid[1];
843
                        home.updateVitals("update");
844
                        home.monitoringGrid.refresh();
845
                        home.updateUsage();
846

    
847
                        var oq = /\S+=ERROR (Over quota .+)/i.exec(data);
848
                        var overquota;
849
                        if (oq && oq.length>1) overquota = oq[1];
850

    
851
                        if (overquota) {
852
                            IRIGO.toast("You have exceeded one or more of your ressource qoutas: " + overquota + "<br>Please contact your local administrator or Origo Systems if you want to raise your quota");
853
                            console.log("Over quota", overquota);
854
                            dialog.hide();
855
                        } else if (managementlink && start) {
856
                            var n = /Status=OK Network .* saved: (\S+)/i.exec(data);
857
                            var errors = /\S+=ERROR (.+)/i.exec(data);
858
                            if (errors && errors.length>1) {
859
                                IRIGO.toast("Error: " + errors[1]);
860
                                dialog.hide();
861
                            } else if (n && n.length>1) {
862
                                console.log("managing", data, n);
863
                                var l = managementlink.replace(/\{uuid\}/, n[1]);
864
                                if (!$('#createSystemDialog').is(":hidden")) systembuilder.system.manage(l, null, name, sysuuid);
865
                            } else {
866
                                console.log("not managing", data, n);
867
                                dialog.hide();
868
                            }
869
                        } else {
870
                            console.log("not managing", managementlink, start);
871
                            dialog.hide();
872
                        }
873

    
874
                        //var n=data.match(/external IP\: (.+)/g);
875
                        //if (n && managementlink && start) {
876
                        //    var link = "http://" + n[0].substring(12) + managementlink;
877
                        //    dijit.byId('wizard_managementlink').set('value', link);
878
                        //    dialog.set('href',link);
879
                        //} else {
880
                        //    dialog.hide();
881
                        //}
882
                    },
883
                    error: function(error){
884
                        ;
885
                    }
886
                }
887
                var deferred = dojo.xhrPost(xhrArgs);
888
                //console.log("Building system", master, name, memory, vcpus, instances, network);
889
            } else {
890
                console.log("systembulder done");
891
                dialog.hide();
892
            }
893

    
894
            /*function finish(){
895
                console.log("Updating all tabs...");
896
                //newStores.systems.reset();
897
                stores.networks.fetch({query: {user: user.username}});
898
                stores.networks.close();
899
                stores.images.fetch({query: {user: user.username}});
900
                stores.images.close();
901
                if (user.is_admin) ui_update.onUpdate({id: "nodes", force: "true"});
902
                home.grid.refresh();
903
            }*/
904
        }
905
    };
906
    window.systembuilder = systembuilder;
907
})
908

    
909

    
910

    
(19-19/23)